namespace Microsoft.SharePoint
{
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint; public static class PermissionManager
{ //SPGroup group = web.Groups[0];
//SPUser user = web.Users[0];
//SPUser user2 = web.EnsureUser("mangaldas.mano");
//SPUser user3 = web.EnsureUser("Domain Users");
//SPPrincipal[] principals = { group, user, user2, user3 }; #region Adding Permissions to an item /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, SPUser user, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, SPUser user, string rolename)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[rolename];
if (roleDefinition != null)
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, string groupName, string rolename)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[rolename];
SPGroup group = item.Web.Groups[groupName];
if (roleDefinition != null && group != null)
SetPermissions(item, (SPPrincipal)group, roleDefinition);
}
}
public static void SetPermissions(SPListItem item, string groupName, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SPGroup group = item.Web.Groups[groupName];
if (group != null)
SetPermissions(item, (SPPrincipal)group, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principals">IEnumerable</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, IEnumerable<SPPrincipal> principals, SPRoleType roleType)
{
if (item != null)
{
foreach (SPPrincipal principal in principals)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, principal, roleDefinition);
}
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
SetPermissions(item, principal, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void SetPermissions(SPListItem item, SPUser user, SPRoleDefinition roleDefinition)
{
if (item != null)
{
SetPermissions(item, (SPPrincipal)user, roleDefinition);
}
} /// <summary>
/// Set Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void SetPermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
if (item != null)
{
BreakRoleSPListItem(item);
SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}
} #endregion #region Deleting all user Permissions from an item /// <summary>
/// remove permisions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="user">SPUser</param>
public static void RemovePermissions(SPListItem item, SPUser user)
{
if (item != null)
{
RemovePermissions(item, user as SPPrincipal);
}
} public static void RemovePermissions(SPListItem item, string groupName)
{
if (item != null)
{
SPGroup group = item.Web.Groups[groupName];
if (group != null)
RemovePermissions(item, group as SPPrincipal);
}
} /// <summary>
/// remove permisions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
public static void RemovePermissions(SPListItem item, SPPrincipal principal)
{
if (item != null)
{
BreakRoleSPListItem(item);
item.RoleAssignments.Remove(principal);
// item.SystemUpdate();
}
} #endregion #region Removing specific roles from an item /// <summary>
/// remove permission
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
if (item != null)
{
SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal);
if (roleAssignment != null)
{
if (roleAssignment.RoleDefinitionBindings.Contains(roleDefinition))
{
roleAssignment.RoleDefinitionBindings.Remove(roleDefinition);
roleAssignment.Update();
}
}
}
} /// <summary>
/// remove permission
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void RemovePermissionsSpecificRole(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
RemovePermissionsSpecificRole(item, principal, roleDefinition);
}
}
public static void RemovePermissionsSpecificRole(SPListItem item, SPUser user, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
if (roleDefinition != null)
RemovePermissionsSpecificRole(item, user as SPPrincipal, roleDefinition);
}
}
public static void RemovePermissionsSpecificRole(SPListItem item, string groupName, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
SPGroup group = item.Web.Groups[groupName];
if (group != null && roleDefinition != null)
RemovePermissionsSpecificRole(item, group as SPPrincipal, roleDefinition);
}
}
#endregion #region Updating or Modifying Permissions on an item /// <summary>
/// modify Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleType">SPRoleType</param>
public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleType roleType)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
ChangePermissions(item, principal, roleDefinition);
}
}
public static void ChangePermissions(SPListItem item, SPUser user, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
if (roleDefinition != null)
ChangePermissions(item, user as SPPrincipal, roleDefinition);
}
} public static void ChangePermissions(SPListItem item, string groupName, string roleName)
{
if (item != null)
{
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions[roleName];
SPGroup group = item.Web.Groups[groupName];
if (roleDefinition != null)
ChangePermissions(item, group as SPPrincipal, roleDefinition);
}
}
/// <summary>
/// modify Permissions
/// </summary>
/// <param name="item">SPListItem</param>
/// <param name="principal">SPPrincipal</param>
/// <param name="roleDefinition">SPRoleDefinition</param>
public static void ChangePermissions(SPListItem item, SPPrincipal principal, SPRoleDefinition roleDefinition)
{
//SPRoleAssignment roleAssignment = item.RoleAssignments.GetAssignmentByPrincipal(principal); //if (roleAssignment != null)
//{
// roleAssignment.RoleDefinitionBindings.RemoveAll();
// roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
// roleAssignment.Update();
//}
RemovePermissions(item, principal);
SetPermissions(item, principal, roleDefinition);
} #endregion public static void BreakRoleSPListItem(SPListItem item)
{
if (item.HasUniqueRoleAssignments)
{
item.ResetRoleInheritance();
}
item.BreakRoleInheritance(false);
} public static SPUser GetSPUserFromSPListItemByFieldName(SPListItem spItem, string fieldName)
{
SPFieldUser field = spItem.Fields[fieldName] as SPFieldUser;
if (field != null && spItem[fieldName] != null)
{
SPFieldUserValue fieldValue = field.GetFieldValue(spItem[fieldName].ToString()) as SPFieldUserValue;
if (fieldValue != null)
{
return fieldValue.User;
}
}
return null;
}
class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
public DisabledItemEventsScope()
{
this.EventFiringEnabled = false;
}
#region IDisposable Members
public void Dispose()
{
this.EventFiringEnabled = true;
}
#endregion
}
public static void SaveListItem(SPListItem item)
{
using (DisabledItemEventsScope scope = new DisabledItemEventsScope())
{
item.SystemUpdate();
}
}
}
}

http://blog.csdn.net/minglelui/article/details/6731824

SharePoint SPListItem 权限设置的更多相关文章

  1. sharepoint 调查问卷权限设置

    参考网址:http://www.cnblogs.com/mybi/archive/2011/04/18/2019935.html 按文章设置后发现访问时提示没有权限. 于是把新权限(问卷回复)的权限组 ...

  2. SharePoint 列表权限控制

    一提到sharepoint 我相信权限控制一定会是一个很重要的话题,尤其是对列表的权限控制,创建.修改.查看权限.其实网上这方面的资料已经很多了,尤其是以下2篇文章,后来索性把CodeArt_Perm ...

  3. Atitit  godaddy 文件权限 root权限设置

    Atitit  godaddy 文件权限 root权限设置 1. ubuntu需要先登录,再su切换到root1 2. sudo 授权许可使用的su,也是受限制的su1 3. ubuntu默认吗roo ...

  4. Linux 权限设置chmod

    Linux中设置权限,一般用chmod命令 1.介绍 权限设置chmod 功能:改变权限命令.常用参数: 1=x(执行权execute) 2=w(写权write) 4=r(读权Read) setuid ...

  5. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(23)-设置角色遗留问题和为权限设置角色以及EasyUI Tabs的使用

    ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框架搭建    (2):数据库访问层的设计Demo    (3):面向接口编程   (4 ):业务逻辑层的封装    ...

  6. MAC OS X的ACL扩展权限设置

    在WEB开发时,网站是以_www的用户运行的,而我在本地是以liuwencan的用户编辑的.这就带来一个问题:如果所有文件属于liuwencan,那么网站运行需要写文件时就因无权限而失败:如果所有文件 ...

  7. C#-WinForm-MDI窗体容器、权限设置

    MDI窗体容器 - 放窗体的容器 窗体时顶级控件,是不允许放到其他的控件或窗体中的 (李献策lxc) 窗体属性中有一个属性:IsMdiContainer - 确定该窗体是否是MDI容器 在窗体中放一个 ...

  8. ACL权限设置命令setfacl和getfacl命令

    ACL权限设置命令setfacl和getfacl命令 setfacl命令是用来在命令行里设置ACL(访问控制列表).在命令行里,一系列的命令跟随以一系列的文件名. [TOC] 选项 |参数|说明|   ...

  9. MDI窗体容器、权限设置

    一.MDI窗体容器: 1.功能: 它可以让其它窗体在它的内部打开,无法超出它的范围 将某个窗体的属性:IsMdiContainer设置为true - 窗口样式 2.问题: (1)如何将其它窗体在它的内 ...

随机推荐

  1. 子类 父类强转 HttpServlet service实现

    相当于 走父类 临时走了一趟 HttpServletRequest ->ServletRequets -> HttpServeltRequest /* */ public void ser ...

  2. progress进度条的样式修改

    由于公司在做的公众号上需要进度条,我就想着用progress标签,可是progress标签很难修改样式,因而在网上查询了一番. 现在总结一下. progress是H5新标签,主要用于显示进度条.由于他 ...

  3. BZOJ.2938.[POI2000]病毒(AC自动机)

    题目链接 \(Description\) 给n个模式串,问是否存在长度无限的主串,使得任何一个模式串都没有在主串中出现. \(Solution\) 先建AC自动机. 假设我们有了一个无限长的安全代码, ...

  4. 【Codechef FRBSUM】【FJOI2016】【BZOJ4299】【BZOJ 4408】 可持久化线段树

    4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 475  Solved: 287[Submit][Status ...

  5. STM32 microcontroller system memory boot mode

    The bootloader is stored in the internal boot ROM memory (system memory) of STM32 devices. It is pro ...

  6. C - 项目收藏

    Web框架 [荐]Kore:开源 C 语言 Web 框架 Raphters:A web framework for C ulfius:Web Framework for REST API in C, ...

  7. android操作sqlite数据库及心得

    写这篇文章主要是网上的对sqlite的操作太多且太杂,非常多时候都不能非常好的运用到自己的项目中,结构不清晰,我自己写了一篇适合刚刚接触的人看的操作方法. 近来用android时要将一些数据保存起来, ...

  8. 华为机试正式版(西安c/c++/java),今天下午去机试的题目,新奇出炉了!

    下面题目都是回顾的.题目都非常easy, 大家有些基础就能够參加!(语言能够是c/c++.也能够是java的) 题目一(60分): 字符串操作. 将小写转换成大写, 将大写转化为小写, 数字的不做转换 ...

  9. redis实现发布(订阅)消息

    redis实现发布(订阅)消息 什么是redis的发布订阅(pub/sub)?   Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能.基于事件的系统中,Pub/S ...

  10. iOS内存管理策略和实践

    转:http://www.cocoachina.com/applenews/devnews/2013/1126/7418.html 内存管理策略(memory Management Policy) N ...