SharePoint 2103 Check user permission on list
一、需求: check user 对SharePoint list 的permission
代码如下:
private static string GetListPermission(SPList list, string loginName)
{
string perStr = string.Empty;
SPSecurity.RunWithElevatedPrivileges(() =>
{
try
{
SPUser user = list.ParentWeb.Users[loginName];
SPRoleAssignment roleAssignment = list.RoleAssignments.GetAssignmentByPrincipal(user);
SPRoleDefinitionBindingCollection defColl = roleAssignment.RoleDefinitionBindings;
foreach (SPRoleDefinition roleDef in defColl)
{
perStr += roleDef.Name + ";";
}
}
catch (Exception)
{
logger.Debug("Get user permission by list.GetUserEffectivePermissionInfo method, list title: {0}, loginName: {1}.", list.Title, loginName);
try
{
SPPermissionInfo permissionInfo = list.GetUserEffectivePermissionInfo(loginName);
var roleAssignments = permissionInfo.RoleAssignments;
foreach (SPRoleAssignment roleAssignment in roleAssignments)
{
SPRoleDefinitionBindingCollection roleDefColl = roleAssignment.RoleDefinitionBindings;
foreach (SPRoleDefinition roleDef in roleDefColl)
{
perStr += roleDef.Name + ";";
}
}
}
catch (Exception ex)
{
logger.Error("An error occurred while getting permission by list.GetUserEffectivePermissionInfo method, list title: {0}, loginName: {1}, exception; {2}.",
list.Title, loginName, ex.ToString());
}
}
});
return perStr;
}
注意: catch中的代码作用是check,当user是AD group中的member,但却不单独存在于web userInformation list中,此时如果直接获取user SPRoleAssignment,则抛‘Index is out of range’, 所以这样的user可以通过
list.GetUserEffectivePermissionInfo(loginName); 来获取SPPermissionInfo,然后再获取user的SPRoleDefinition,有的读者会问,为什么不直接通过catch中的方法获取,这样无论这个user是否只存在于AD group中都不会抛异常
可以正确的获取到SPRoleDefinition,其实是可以的,之所以这样做,原因在于效率问题。
二、需求: set permission to list
代码如下:
private static void SetLibPermission(SPList list, bool isRead)
{
try
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
bool hasUnique = list.HasUniqueRoleAssignments;
list.ParentWeb.AllowUnsafeUpdates = true;
if (!hasUnique)
{
list.BreakRoleInheritance(false);
list.Update();
}
try
{
SPUser user = list.ParentWeb.EnsureUser(userInfo.Key);
SPRoleDefinitionCollection objDefiColl = list.ParentWeb.RoleDefinitions;
SPRoleAssignment objRoleAssign = new SPRoleAssignment(user);
SPRoleDefinition roleDefination = null;
if (isRead)
{
roleDefination = objDefiColl.GetByType(SPRoleType.Reader);
}
else
{
roleDefination = objDefiColl.GetByType(SPRoleType.Contributor);
}
objRoleAssign.RoleDefinitionBindings.Add(roleDefination);
list.RoleAssignments.Add(objRoleAssign);
}
catch (Exception ex)
{ }
list.Update();
list.ParentWeb.AllowUnsafeUpdates = false;
}); }
catch (Exception ex)
{ }
}
注意:给list赋权限,需要打破继承,具体可以根据实际需求
代码中的userInfo.Key即为loginName
list.ParentWeb.EnsureUser(userInfo.Key);即把user保存到user information list中
SharePoint 2103 Check user permission on list的更多相关文章
- SharePoint Security and Permission System Overview
转:http://www.sharepointblues.com/2010/09/01/sharepoint-security-and-permission-system-overview/ Shar ...
- The Managed Metadata Service or Connection is currently not available 分类: Sharepoint 2015-07-09 13:28 5人阅读 评论(0) 收藏
Does the following error message looks familiar to you? (When you go to Site Actions –> Site Sett ...
- 【转】必需知道的 SharePoint 权限 Tips
SharePoint Tips about Permissions: What you need to know I have been writing tips about Shar ...
- Installing FIM 2010 R2 SP1 Portal on SharePoint Foundation 2013
http://www.fimspecialist.com/fim-portal/installing-fim-2010-r2-sp1-portal-on-sharepoint-foundation-2 ...
- How does Web Analytics works under sharePoint 2010
[http://gokanx.wordpress.com/2013/06/15/how-does-web-analytics-works-under-sharepoint-2010/] You nee ...
- SharePoint REST api
http://msdn.microsoft.com/en-us/magazine/dn198245.aspx Understanding and Using the SharePoint 2013 R ...
- permission 文档 翻译 运行时权限
文档位置:API24/guide/topics/security/permissions.html System Permissions 系统权限 Android is a privilege-se ...
- SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
通过PowerShell创建SharePoint Site Collection,代码如下: Add-PSSnapin microsoft.sharepoint.powershell function ...
- Failed to start NodeManager caused by "/var/lib/hadoop-yarn/yarn-nm-recovery/yarn-nm-state/LOCK: Permission denied"
Hadoop 安装步骤: 0. 安装前准备(节点机器,环境设置,yum源设置) 1. 配置并安装Cloudera-Manager 2. 启动 CM 服务 3. 安装CDH,并配置集群 4. 启动 ...
随机推荐
- C语言 · 时间转换
问题描述 给定一个以秒为单位的时间t,要求用"<H>:<M>:<S>"的格式来表示这个时间.<H>表示时间,<M>表示分 ...
- Socket聊天程序——初始设计
写在前面: 可能是临近期末了,各种课程设计接踵而来,最近在csdn上看到2个一样问答(问题A,问题B),那就是编写一个基于socket的聊天程序,正好最近刚用socket做了一些事,出于兴趣,自己抽了 ...
- xpath提取多个标签下的text
title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(70)-微信公众平台开发-成为开发者
系列目录 前言: 一.阅读这段系列之前,你必须花半天时间大致阅读微信公众平台的API文档,我尽量以简短快速的语言与大家分享一个过程 二.借助微信公众平台SDK Senparc.Weixin for C ...
- .NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”
FileProvider构建了一个抽象文件系统,作为它的两个具体实现,PhysicalFileProvider和EmbeddedFileProvider则分别为我们构建了一个物理文件系统和程序集内嵌文 ...
- spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析
1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...
- duang~免费的学习视频来啦:学霸君之全栈测试
学霸君向童鞋们推荐一款 同名学霸学习 视频教程 重点是完全免费收看学习噢!!! 今天 学霸君推荐腾讯课堂的学霸君之全栈测试 复制下方链接至腾讯课堂中报名学习 https://ke.qq.com/cou ...
- MVC还是MVVM?或许VMVC更适合WinForm客户端
最近开始重构一个稍嫌古老的C/S项目,原先采用的技术栈是『WinForm』+『WCF』+『EF』.相对于现在铺天盖地的B/S架构来说,看上去似乎和Win95一样古老,很多新入行的,可能就没有见过经典的 ...
- IteratorPattern(迭代子模式)
/** * 迭代子模式 * @author TMAC-J * 聚合:某一类对象的集合 * 迭代:行为方式,用来处理聚合 * 是一种行为模式,用于将聚合本身和操作聚合的行为分离 * Java中的COLL ...
- 基于Vue2.0的单页面开发方案
2016的最后一天,多多少少都应该总结一下这一年的得失,哪里做的好,哪里需要改进,记一笔,或许将来会用到呢. 毕业差不多半年了,一直是一个人在负责公司项目的前端开发与维护,当时公司希望前后端分离,提高 ...