设为首页 收藏本站
查看: 856|回复: 0

[经验分享] 向SharePoint 2010中添加Permission Level,Group,以及相应的User

[复制链接]

尚未签到

发表于 2015-9-25 13:42:38 | 显示全部楼层 |阅读模式
  在SharePoint Server 2010中权限管理涉及到的几个概念可以描述如下:
  1:SharePoint Server 2010 Permission: SharePoint2010 Server中总共包含 33 种基本的permission(当然是通过二进制的每一位进行控制基本的permission),这些基本的permission分别控制着对各个基本对象的view,create,edit,delete 的基本操作。而且这些permission基本分为三大类:list permissions(包含item permissions), site permissions, 和 personal permissions。 例如:site permissions 可以应用到制定的site上,list permissions可以应用到lists以及相应的items上, 而personal permissions可以应用到personal views 或者 private Web Parts 等。
  2:Permission Level:每个 permission level 都是不同 permission 的一个集合,并且在代码程序中permission level将作为Role的一个属性值,通过Role Assignment的方式添加给对应的Group中,在SharePoint Server 2010中有5种默认的permission level,分别为:Full Control, Design, Contribute, Read, Limited Access. 在这5种permission level中除了Full Control和Limited Access 其他3中都是可以修改的,与此同时我们可以自定义我们自己的permission level。
  3:Group:每个Group可以包含不同的permission level,也就是在这个Group里面的User可以操作具有操作权限的那些对象,与此同时,每个User可以在不同的Group中,那么这里会有一个permission叠加的逻辑,也就是计算User所具有的所有权限(将所有的所属Group的所有Permission Level中所有的Permission叠加在一起)。
  更多关于SharePoint Permission 的概念 请看: http://technet.microsoft.com/en-us/library/cc721640(v=office.14).aspx
  
  接下来我们要用代码的方式实现:创建permission level, 然后创建具有permission level的group,之后将user添加到我们创建的group中。
  在SharePoint Project中添加一个Feature 取名:CustomUserGroupFeature
  在此Feature中添加一个EventHandler并完成功能代码
  CustomUserGroupFeature.EventReceiver.cs



using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Linq;
namespace EricSunSharePointProject.Features.CustomUserGroupFeature
{
/// <summary>
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
/// </summary>
/// <remarks>
/// The GUID attached to this class may be used during packaging and should not be modified.
/// </remarks>

[Guid("7ae2e739-1863-4b34-b3cb-a7fd6fd04fa4")]
public class CustomUserGroupFeatureEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
//public override void FeatureActivated(SPFeatureReceiverProperties properties)
//{
//}

// Uncomment the method below to handle the event raised before a feature is deactivated.
//public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
//{
//}

// Uncomment the method below to handle the event raised after a feature has been installed.
//public override void FeatureInstalled(SPFeatureReceiverProperties properties)
//{
//}

// Uncomment the method below to handle the event raised before a feature is uninstalled.
//public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
//{
//}
// Uncomment the method below to handle the event raised when a feature is upgrading.
//public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
//{
//}
const string Administrators = "EricSun Content Administrators";
const string Approvers = "EricSun Content Approvers";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string groupDescription = "EricSun Content";
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
CreateSubSiteGroup(web, Administrators, GetAdministratorPermission(), groupDescription + " Administrators Group", Administrators, "Can view, add, update, delete, and customize list items and documents.");
CreateSubSiteGroup(web, Approvers, GetApproverPermission(), groupDescription + " Approvers Group", Approvers, "Can view, and approve list items and documents.");
}
}
catch (SPException ex)
{
}
}

// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb web = properties.Feature.Parent as SPWeb)
{
DeleteSubSiteGroup(web, Administrators);
DeleteSubSiteGroup(web, Approvers);
}
}
catch (SPException ex)
{
}
}

/// <SUMMARY>
/// Create group
/// </SUMMARY>
private void CreateSubSiteGroup(SPWeb web, string groupName, SPBasePermissions PermissionLevel, string groupDescription, string roleName, string description)
{
try
{
SPUserCollection users = web.AllUsers;
SPUser owner = web.SiteAdministrators[0];
SPMember member = web.SiteAdministrators[0];
SPGroupCollection groups = web.SiteGroups;
if (!groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName, StringComparison.Ordinal)))
{
//add new group if not found
                    groups.Add(groupName, member, owner, groupDescription);
}
SPGroup newSPGroup = groups[groupName];
SPRoleDefinition role = new SPRoleDefinition();
role.Name = roleName;
role.Description = description;
role.BasePermissions = PermissionLevel;
if (!web.RoleDefinitions.Cast<SPRoleDefinition>().Any(r => r.Name.Equals(roleName, StringComparison.Ordinal)))
{
//add role definition if not found
                    web.RoleDefinitions.Add(role);
}
role = web.RoleDefinitions[roleName];
SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup);
roleAssignment.RoleDefinitionBindings.Add(role);
web.RoleAssignments.Add(roleAssignment);
web.Update();
}
catch (SPException ex)
{
}
}
/// <SUMMARY>
/// Delete group for subsite
/// </SUMMARY>
private void DeleteSubSiteGroup(SPWeb web, string groupName)
{
try
{
SPGroupCollection groups = web.SiteGroups;
groups.Remove(groupName);
web.Update();
}
catch (SPException ex)
{
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private SPBasePermissions GetAdministratorPermission()
{
return SPBasePermissions.EditListItems | SPBasePermissions.ViewListItems | SPBasePermissions.DeleteListItems
| SPBasePermissions.AddListItems | SPBasePermissions.OpenItems;
}
private SPBasePermissions GetApproverPermission()
{
return SPBasePermissions.ApproveItems;
}
}
}
  
。。。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118758-1-1.html 上篇帖子: Sharepoint列表数据导入导出工具(支持查阅项及用户类型) 下篇帖子: SharePoint单点登陆(Single Sign On)实战
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表