{sharepoint} SetPermission

  1     using System;  
  2     using System.Collections.Generic;  
  3     using System.Linq;  
  4     using System.Text;  
  5     using Microsoft.SharePoint;  
  6       
  7     namespace CR.SharePoint.HR.Common  
  8     {  
  9         public static class PermissionManager  
 10         {  
 11       
 12             //SPGroup group = web.Groups[0];   
 13             //SPUser user = web.Users[0];   
 14             //SPUser user2 = web.EnsureUser("mangaldas.mano");   
 15             //SPUser user3 = web.EnsureUser("Domain Users");   
 16             //SPPrincipal[] principals = { group, user, user2, user3 };   
 17      
 18             #region Adding Permissions to an item  
 19       
 20             /// <summary>  
 21             /// Set Permissions  
 22             /// </summary>  
 23             /// <param name="item">SPListItem</param>  
 24             /// <param name="principals">IEnumerable</param>  
 25             /// <param name="roleType">SPRoleType</param>  
 26             public static void SetPermissions(SPListItem item, IEnumerable<SPPrincipal> principals, SPRoleType roleType)   
 27             {   
 28                 if (item != null)   
 29                 {   
 30                     foreach (SPPrincipal principal in principals)   
 31                     {   
 32                         SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);   
 33                         SetPermissions(item, principal, roleDefinition);   
 34                     }   
 35                 }   
 36             }       
 37               
 38             /// <summary>  
 39             /// Set Permissions  
 40             /// </summary>  
 41             /// <param name="item">SPListItem</param>  
 42             /// <param name="user">SPUser</param>  
 43             /// <param name="roleType">SPRoleType</param>  
 44             public static void SetPermissions(SPListItem item, SPUser user, SPRoleType roleType)   
 45             {   
 46                 if (item != null)   
 47                 {   
 48                     SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);   
 49                     SetPermissions(item, (SPPrincipal)user, roleDefinition);   
 50                 }   
 51             }     
 52               
 53             /// <summary>  
 54             /// Set Permissions  
 55             /// </summary>  
 56             /// <param name="item">SPListItem</param>  
 57             /// <param name="principal">SPPrincipal</param>  
 58             /// <param name="roleType">SPRoleType</param>  
 59             public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)   
 60             {   
 61                 if (item != null)   
 62                 {   
 63                     SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);   
 64                     SetPermissions(item, principal, roleDefinition);   
 65                 }   
 66             }     
 67               
 68             /// <summary>  
 69             /// Set Permissions  
 70             /// </summary>  
 71             /// <param name="item">SPListItem</param>  
 72             /// <param name="user">SPUser</param>  
 73             /// <param name="roleDefinition">SPRoleDefinition</param>  
 74             public static void SetPermissions(SPListItem item, SPUser user, SPRoleDefinition roleDefinition)   
 75             {   
 76                 if (item != null)   
 77                 {   
 78                     SetPermissions(item, (SPPrincipal)user, roleDefinition);   
 79                 }   
 80             }     
 81               
 82             /// <summary>  
 83             /// Set Permissions  
 84             /// </summary>  
 85             /// <param name="item">SPListItem</param>  
 86             /// <param name="principal">SPPrincipal</param>  
 87             /// <param name="roleDefinition">SPRoleDefinition</param>  
 88             public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)   
 89             {   
 90                 if (item != null)   
 91                 {   
 92                     SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);   
 93                     roleAssignment.RoleDefinitionBindings.Add(roleDefinition);   
 94                     item.RoleAssignments.Add(roleAssignment);   
 95                 }   
 96             }  
 97      
 98             #endregion  
 99      
100             #region Deleting all user Permissions from an item  
101       
102             /// <summary>  
103             /// remove permisions  
104             /// </summary>  
105             /// <param name="item">SPListItem</param>  
106             /// <param name="user">SPUser</param>  
107             public static void RemovePermissions(SPListItem item, SPUser user)   
108             {   
109                 if (item != null)   
110                 {   
111                     RemovePermissions(item, user as SPPrincipal);   
112                 }   
113             }     
114               
115             /// <summary>  
116             /// remove permisions  
117             /// </summary>  
118             /// <param name="item">SPListItem</param>  
119             /// <param name="principal">SPPrincipal</param>  
120             public static void RemovePermissions(SPListItem item, SPPrincipal principal)   
121             {   
122                 if (item != null)   
123                 {   
124                     item.RoleAssignments.Remove(principal);   
125                     item.SystemUpdate();   
126                 }  
127             }  
128      
129             #endregion  
130      
131             #region Removing specific roles from an item  
132       
133             /// <summary>  
134             /// remove permission  
135             /// </summary>  
136             /// <param name="item">SPListItem</param>  
137             /// <param name="principal">SPPrincipal</param>  
138             /// <param name="roleDefinition">SPRoleDefinition</param>  
139             public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)   
140             {   
141                 if (item != null)   
142                 {   
143                     SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal);   
144                     if (roleAssignment != null)   
145                     {   
146                         if (roleAssignment.RoleDefinitionBindings.Contains(roleDefinition))   
147                         {   
148                             roleAssignment.RoleDefinitionBindings.Remove(roleDefinition);   
149                             roleAssignment.Update();   
150                         }   
151                     }   
152                 }   
153             }  
154       
155             /// <summary>  
156             /// remove permission  
157             /// </summary>  
158             /// <param name="item">SPListItem</param>  
159             /// <param name="principal">SPPrincipal</param>  
160             /// <param name="roleType">SPRoleType</param>  
161             public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleType roleType)   
162             {   
163                 if (item != null)   
164                 {   
165                     SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);   
166                     RemovePermissionsSpecificRole(item, principal, roleDefinition);   
167                 }   
168             }  
169      
170             #endregion  
171      
172             #region Updating or Modifying Permissions on an item  
173       
174             /// <summary>  
175             /// modify Permissions  
176             /// </summary>  
177             /// <param name="item">SPListItem</param>  
178             /// <param name="principal">SPPrincipal</param>  
179             /// <param name="roleType">SPRoleType</param>  
180             public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)   
181             {   
182                 if (item != null)   
183                 {   
184                     SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);   
185                     ChangePermissions(item, principal, roleDefinition);   
186                 }   
187             }  
188       
189             /// <summary>  
190             /// modify Permissions  
191             /// </summary>  
192             /// <param name="item">SPListItem</param>  
193             /// <param name="principal">SPPrincipal</param>  
194             /// <param name="roleDefinition">SPRoleDefinition</param>  
195             public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)   
196             {   
197                 SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal);   
198                   
199                 if (roleAssignment != null)   
200                 {   
201                     roleAssignment.RoleDefinitionBindings.RemoveAll();   
202                     roleAssignment.RoleDefinitionBindings.Add(roleDefinition); roleAssignment.Update();   
203                 }   
204             }  
205      
206             #endregion  
207       
208         }  
209     }  
View Code

 

posted @ 2016-05-17 21:25  Areas  阅读(235)  评论(0编辑  收藏  举报