kamagra how much to take

SharePoint Security Helper Class

In order to circumvent recoding SharePoint security operations several times over, you might find the below class effective.

Some things to note. There are a lot of literal calls in the code, and it’s on purpose. For instance, instead of passing a specific SPWeb object as a parameter, a string value to represent the URL is instead used to hydrate the primary SPWeb.

This code can easily be adjusted, you could create a global SPWeb object (which would aid in trimming the method), you could switch the entire class so that it was tailored towards context, etc. Keeping it in this manner just keeps better medium independence since it makes it easier for client development.

  1. #region Imports
  2. using System.Collections.Generic;
  3. using Microsoft.SharePoint;
  4. #endregion
  5. namespace Example
  6. {
  7. public class SharePointSecurityHelpers
  8. {
  9. #region Helper Methods
  10. public static SPWeb ReturnSPWeb(string url)
  11. {
  12. using (var site = new SPSite(url))
  13. {
  14. using (SPWeb web = site.OpenWeb())
  15. {
  16. return web;
  17. }
  18. }
  19. }
  20. public static SPList GetList(string url, string list)
  21. {
  22. try
  23. {
  24. return ReturnSPWeb(url).Lists[list];
  25. }
  26. catch
  27. {
  28. // Exception Handling Goes Here
  29. return null;
  30. }
  31. }
  32. #endregion
  33. #region Groups And Roles
  34. public static void AddSharePointGroup(string url, string groupName, string groupDescription, string roleName)
  35. {
  36. SPWeb web = ReturnSPWeb(url);
  37.  
  38. web.SiteGroups.Add(groupName, web.AssociatedOwnerGroup, null, groupDescription);
  39. web.AssociatedGroups.Add(web.SiteGroups[groupName]);
  40. web.Update();
  41. SPRoleAssignment assignment = new SPRoleAssignment(web.SiteGroups[groupName]);
  42. SPRoleDefinition roleApp = web.RoleDefinitions[roleName];
  43.  
  44. assignment.RoleDefinitionBindings.Add(roleApp);
  45. }
  46.  
  47. public static List GetAllSharePointGroups(SPWeb web)
  48. {
  49. var tempCollection = new List();
  50. foreach (SPGroup group in web.SiteGroups)
  51. {
  52. tempCollection.Add(group);
  53. }
  54. return tempCollection;
  55. }
  56. public static List GetAllRoleDefinitions(SPWeb web)
  57. {
  58. var tempCollection = new List();
  59. foreach (SPRoleDefinition definition in web.RoleDefinitions)
  60. {
  61. tempCollection.Add(definition);
  62. }
  63. return tempCollection;
  64. }
  65. #endregion
  66. #region Anonymous Methods
  67. public static void SetAnonPermissionsOnSite(string url)
  68. {
  69. SPWeb web = ReturnSPWeb(url);
  70.  
  71. bool ura = web.HasUniqueRoleAssignments;
  72. if (!ura)
  73. {
  74. web.BreakRoleInheritance(true);
  75. }
  76. web.AnonymousPermMask64 = SPBasePermissions.FullMask;
  77. web.BreakRoleInheritance(ura);
  78. }
  79.  
  80. public static void SetAnonPermissionsOnList(string url, string listName)
  81. {
  82. SPList list = GetList(url, listName);
  83. bool ura = list.HasUniqueRoleAssignments;
  84. if (!ura)
  85. {
  86. list.BreakRoleInheritance(true);
  87. }
  88. list.AnonymousPermMask64 = SPBasePermissions.ViewListItems;
  89. list.BreakRoleInheritance(ura);
  90. }
  91. #endregion
  92. #region Inheritance Methods
  93. public static void BreakListInheritance(string url, string listName)
  94. {
  95. SPList list = GetList(url, listName);
  96. list.BreakRoleInheritance(true);
  97. list.Update();
  98. }
  99.  
  100. public static void ResetListInheritance(string url, string listName)
  101. {
  102. SPList list = GetList(url, listName);
  103. list.ResetRoleInheritance();
  104. list.Update();
  105. }
  106.  
  107. public static void BreakSiteInheritance(string url)
  108. {
  109. SPWeb web = ReturnSPWeb(url);
  110. web.BreakRoleInheritance(true);
  111. web.Update();
  112. }
  113.  
  114. public static void ResetSiteInheritance(string url)
  115. {
  116. SPWeb web = ReturnSPWeb(url);
  117. web.ResetRoleInheritance();
  118. web.Update();
  119. }
  120.  
  121. public static string CheckIfUniqueAssign(ISecurableObject testableObject)
  122. {
  123. if (testableObject.HasUniqueRoleAssignments)
  124. {
  125. return "This Object Is Unique From Parent!";
  126. }
  127. return "This Object Inherits Permission From Parent!";
  128. }
  129. #endregion
  130. #region User Methods
  131. public static void AddUserToSharePointGroup(string url, string userName, string groupName)
  132. {
  133. SPWeb web = ReturnSPWeb(url);
  134. SPUser user = web.EnsureUser(userName);
  135. SPGroup group = web.SiteGroups[groupName];
  136. group.AddUser(user.LoginName, user.Email, user.Name, user.Notes);
  137. group.Update();
  138. }
  139.  
  140. public static void AddUserToSharePoint(string url, string userName, string role)
  141. {
  142. SPWeb web = ReturnSPWeb(url)
  143. web.AllowUnsafeUpdates = true;
  144. SPUser user = web.EnsureUser(userName);
  145. SPRoleDefinitionCollection roleDef = web.RoleDefinitions;
  146. SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
  147. var roleAssignment = new SPRoleAssignment(user);
  148. SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
  149. roleDefBindings.Add(roleDef[role]);
  150. roleAssignments.Add(roleAssignment);
  151. web.AllowUnsafeUpdates = false;
  152. }
  153. #endregion
  154. }
  155. }

Just throw your namespace at the top, and you should be ready to go!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>