PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/LiquidSilver/HgRole.cs

http://liquidsilver.codeplex.com
C# | 341 lines | 209 code | 56 blank | 76 comment | 23 complexity | 37bd3178c0556e4264ac112afdfdf446 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Permissions;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.Security;
  6. namespace LiquidSilver
  7. {
  8. /// <summary>
  9. /// Manages an SPRoleAssignmentCollection object.
  10. /// </summary>
  11. public class HgRole
  12. {
  13. #region Properties
  14. /// <summary>
  15. /// Gets the parent SPWeb object of the managed object.
  16. /// </summary>
  17. public SPWeb ParentWeb { get; private set; }
  18. /// <summary>
  19. /// Gets the SPRoleAssignmentCollection object which is being managed.
  20. /// </summary>
  21. public SPRoleAssignmentCollection RoleAssignments { get; private set; }
  22. #endregion Properties
  23. #region Constructors
  24. /// <summary>
  25. /// Instantiates a new SPRoleAssignmentManager object to manage the
  26. /// specified SPRoleAssignmentCollection object.
  27. /// </summary>
  28. /// <param name="roleAssignments">The SPRoleAssignmentCollection object
  29. /// to manage.</param>
  30. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  31. public HgRole(
  32. SPRoleAssignmentCollection roleAssignments)
  33. {
  34. Init(roleAssignments);
  35. }
  36. #endregion Constructors
  37. #region Private Methods
  38. /// <summary>
  39. /// Gets an array of SPRoleDefinition objects specified by the names.
  40. /// </summary>
  41. /// <param name="roleDefinitionNames">The names of the
  42. /// SPRoleDefinition objects.</param>
  43. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  44. private SPRoleDefinition[] GetRoleDefinitions(
  45. params string[] roleDefinitionNames)
  46. {
  47. var rds = new List<SPRoleDefinition>();
  48. foreach (string rd in roleDefinitionNames)
  49. rds.Add(ParentWeb.RoleDefinitions[rd]);
  50. return rds.ToArray();
  51. }
  52. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  53. private void Init(SPRoleAssignmentCollection roleAssignments)
  54. {
  55. RoleAssignments = roleAssignments;
  56. var parent = roleAssignments.Parent;
  57. ParentWeb = parent as SPWeb;
  58. if (ParentWeb != null)
  59. return;
  60. var parentList = parent as SPList;
  61. if (parentList != null)
  62. {
  63. ParentWeb = parentList.ParentWeb;
  64. return;
  65. }
  66. var parentItem = parent as SPListItem;
  67. if (parentItem != null)
  68. {
  69. ParentWeb = parentItem.Web;
  70. return;
  71. }
  72. throw new ArgumentException(
  73. "Only SPRoleAssignmentCollection object which is a member " +
  74. "of SPWeb, SPList, or SPListItem is allowed.");
  75. }
  76. #endregion Private Methods
  77. #region Public Methods
  78. /// <summary>
  79. /// Adds one or more permissions to the specified principal.
  80. /// </summary>
  81. /// <param name="principal">The principal to be given the
  82. /// permissions.</param>
  83. /// <param name="roleDefinitions">The list of role definitions
  84. /// having the permissions.</param>
  85. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  86. public HgRole AddPermissions(SPPrincipal principal,
  87. params SPRoleDefinition[] roleDefinitions)
  88. {
  89. SPRoleAssignment ra = null;
  90. try
  91. {
  92. ra = RoleAssignments.GetAssignmentByPrincipal(principal);
  93. }
  94. catch (ArgumentOutOfRangeException)
  95. {
  96. // Could not find the SPPrincipal object.
  97. }
  98. catch (ArgumentException)
  99. {
  100. /// The SPPrincipal object resides within a group and the
  101. /// ISecurableObject type is SPWeb.
  102. }
  103. if (ra == null)
  104. {
  105. ra = new SPRoleAssignment(principal);
  106. foreach (SPRoleDefinition rd in roleDefinitions)
  107. {
  108. ra.RoleDefinitionBindings.Add(rd);
  109. }
  110. RoleAssignments.Add(ra);
  111. }
  112. else
  113. {
  114. foreach (SPRoleDefinition rd in roleDefinitions)
  115. {
  116. if (!ra.RoleDefinitionBindings.Contains(rd))
  117. ra.RoleDefinitionBindings.Add(rd);
  118. }
  119. ra.Update();
  120. }
  121. return this;
  122. }
  123. /// <summary>
  124. /// Adds one or more permissions to the specified principal.
  125. /// </summary>
  126. /// <param name="principal">The principal to be given the
  127. /// permissions.</param>
  128. /// <param name="roleDefinitionNames">The list of role definitions
  129. /// names having the permissions.</param>
  130. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  131. public HgRole AddPermissions(SPPrincipal principal,
  132. params string[] roleDefinitionNames)
  133. {
  134. return AddPermissions(principal,
  135. GetRoleDefinitions(roleDefinitionNames));
  136. }
  137. /// <summary>
  138. /// Breaks the role inheritance from the parent object.
  139. /// </summary>
  140. /// <param name="copyRoleAssignments">If true, copy the role
  141. /// assignments of the parent object.</param>
  142. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  143. public HgRole BreakRoleInheritance(
  144. bool copyRoleAssignments)
  145. {
  146. var parent = RoleAssignments.Parent;
  147. if (parent.HasUniqueRoleAssignments)
  148. return this;
  149. parent.BreakRoleInheritance(copyRoleAssignments);
  150. Init(parent.RoleAssignments);
  151. return this;
  152. }
  153. /// <summary>
  154. /// Removes all permissions.
  155. /// </summary>
  156. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  157. public HgRole RemovePermissions()
  158. {
  159. var count = RoleAssignments.Count;
  160. for (int i = 0; i < count; i++)
  161. RoleAssignments.Remove(0);
  162. return this;
  163. }
  164. /// <summary>
  165. /// Removes permissions of the specified principal.
  166. /// </summary>
  167. /// <param name="principal">The principal to delete the permissions
  168. /// from.</param>
  169. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  170. public HgRole RemovePermissions(SPPrincipal principal)
  171. {
  172. RoleAssignments.Remove(principal);
  173. return this;
  174. }
  175. /// <summary>
  176. /// Removes a set of permissions of the specified principal.
  177. /// </summary>
  178. /// <param name="principal">The principal to delete the permissions
  179. /// from.</param>
  180. /// <param name="roleDefinitions">The list of role definitions having
  181. /// the permissions to delete.</param>
  182. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  183. public HgRole RemovePermissions(SPPrincipal principal,
  184. params SPRoleDefinition[] roleDefinitions)
  185. {
  186. SPRoleAssignment ra = null;
  187. try
  188. {
  189. ra = RoleAssignments.GetAssignmentByPrincipal(principal);
  190. }
  191. catch (ArgumentOutOfRangeException)
  192. {
  193. // Could not find the SPPrincipal object.
  194. }
  195. catch (ArgumentException)
  196. {
  197. /// The SPPrincipal object resides within a group and the
  198. /// ISecurableObject type is SPWeb.
  199. }
  200. if (ra == null)
  201. return this;
  202. var bindings = ra.RoleDefinitionBindings;
  203. foreach (SPRoleDefinition rd in roleDefinitions)
  204. bindings.Remove(rd);
  205. ra.Update();
  206. return this;
  207. }
  208. /// <summary>
  209. /// Removes a set of permissions of the specified principal.
  210. /// </summary>
  211. /// <param name="principal">The principal to delete the permissions
  212. /// from.</param>
  213. /// <param name="roleDefinitionNames">The list of role definitions
  214. /// names having the permissions to delete.</param>
  215. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  216. public HgRole RemovePermissions(SPPrincipal principal,
  217. params string[] roleDefinitionNames)
  218. {
  219. return RemovePermissions(principal,
  220. GetRoleDefinitions(roleDefinitionNames));
  221. }
  222. /// <summary>
  223. /// Updates all permissions changes.
  224. /// </summary>
  225. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  226. public HgRole Update()
  227. {
  228. var parent = RoleAssignments.Parent;
  229. var web = parent as SPWeb;
  230. if (web != null)
  231. {
  232. web.Update();
  233. return this;
  234. }
  235. var list = parent as SPList;
  236. if (list != null)
  237. {
  238. list.Update();
  239. return this;
  240. }
  241. var item = parent as SPListItem;
  242. if (item != null)
  243. {
  244. item.SystemUpdate(false);
  245. return this;
  246. }
  247. return this;
  248. }
  249. #endregion Public Methods
  250. #region Public Static Methods
  251. /// <summary>
  252. /// Checks if a list of principals contains a specific user.
  253. /// </summary>
  254. /// <param name="principals">The list of principals to check from.</param>
  255. /// <param name="user">The user to search.</param>
  256. /// <returns>True if the list contains the user, false otherwise.</returns>
  257. [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
  258. public static bool DoesPrincipalsContainUser(IEnumerable<SPPrincipal> principals,
  259. SPUser user)
  260. {
  261. foreach (SPPrincipal principal in principals)
  262. {
  263. SPUser pUser = principal as SPUser;
  264. if (pUser != null)
  265. {
  266. if (pUser.LoginName.Equals(user.LoginName,
  267. StringComparison.CurrentCultureIgnoreCase))
  268. return true;
  269. }
  270. else
  271. {
  272. SPGroup pGroup = principal as SPGroup;
  273. string groupName = pGroup.Name;
  274. foreach (SPGroup userGroup in user.Groups)
  275. {
  276. if (userGroup.Name.Equals(groupName))
  277. return true;
  278. }
  279. }
  280. }
  281. return false;
  282. }
  283. #endregion Public Static Methods
  284. }
  285. }