如何:打破角色分配继承关系
上次修改时间: 2011年4月30日
适用范围: SharePoint Foundation 2010
本文内容
取消列表的安全继承
取消文档的安全继承并添加用户作为读者
取消文档的安全继承并更改用户的权限
可以通过对象的 BreakRoleInheritance 方法来取消网站、列表或列表项的安全继承,以便父对象上的角色分配不再应用于子对象。例如,使列表上的角色分配不再应用于列表项。对于网站和列表,该方法将传递两个布尔参数:copyRoleAssignments 和 clearSubScopes。第一个参数指定是否保留已从父网站集或网站继承的当前角色分配,第二个参数指定是否清除子对象的唯一权限,以便它们以后能够从父网站或列表继承权限。如果将 copyRoleAssignments 参数设置为 false,则运行代码的当前用户将获得对象的完全控制权。网站、列表或列表项的 ResetRoleInheritance 方法可恢复从父对象到子对象的角色分配继承。
取消列表的安全继承
以下示例演示如何使用 List 类 (JavaScript: List) 的 BreakRoleInheritance(Boolean, Boolean) 方法 (JavaScript: breakRoleInheritance(copyRoleAssignments, clearSubscopes)) 取消列表的安全继承。运行该示例后,在网站级别所做的后续角色分配将不会影响列表中的角色分配。该示例在不取消表内单个项目的唯一角色分配的情况下,取消 Announcements 列表的继承,但保留当前角色分配。
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class BreakSecurityInheritance
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext oContext = new ClientContext(siteUrl);
SP.List oList = oContext.Web.Lists.GetByTitle("Announcements");
oList.BreakRoleInheritance(true, false);
oContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class BreakSecurityInheritance
Shared Sub Main ()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("Announcements")
oList.BreakRoleInheritance(True, False)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function breakSecurityInheritance() {
var clientContext = new SP.ClientContext(siteUrl);
this.oList = clientContext.get_web().get_lists().getByTitle('Announcements');
oList.breakRoleInheritance(true, false);
clientContext.load(oList);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert(this.oList.get_title() + ' role inheritance broken.');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
取消文档的安全继承并添加用户作为读者
ListItem 类 (JavaScript: ListItem) 的已继承 BreakRoleInheritance(Boolean, Boolean) 方法 (JavaScript: breakRoleInheritance(copyRoleAssignments)) 只传递一个布尔参数,该参数指定是否保留父列表的角色分配。以下示例取消列表内单个项目的安全继承,并为该项目添加一下指定的用户作为读者。由于 copyRoleAssignments 参数设置为 false,因此运行该代码的当前用户将被赋予该项目的完全控制权。
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class BreakSecurityInheritanceAddUser
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("MyList");
int itemId = 3;
ListItem oListItem = oList.Items.GetById(itemId);
oListItem.BreakRoleInheritance(false);
User oUser = clientContext.Web.SiteUsers.GetByLoginName(@"DOMAIN\alias");
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
collRoleDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader));
oListItem.RoleAssignments.Add(oUser, collRoleDefinitionBinding);
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class BreakSecurityInheritance
Shared Sub Main ()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("MyList")
Dim itemId As Integer = 3
Dim oListItem As ListItem = oList.Items.GetById(itemId)
oListItem.BreakRoleInheritance(False)
Dim oUser As User = clientContext.Web.SiteUsers.GetByLoginName("DOMAIN\alias")
Dim collRoleDefinitionBinding As New RoleDefinitionBindingCollection(clientContext)
collRoleDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader))
oListItem.RoleAssignments.Add(oUser, collRoleDefinitionBinding)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function breakSecurityInheritanceAddUser() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var itemId = 4;
this.oListItem = oList.get_items().getById(itemId);
oListItem.breakRoleInheritance(false);
this.oUser = clientContext.get_web().get_siteUsers().getByLoginName('DOMAIN\\alias');
var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.reader));
oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);
clientContext.load(oUser);
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert('Role inheritance broken for item ' +
this.oListItem.get_item('Title') +
' and new role assignment for ' +
this.oUser.get_loginName());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
取消文档的安全继承并更改用户的权限
以下示例取消列表内某个项目的安全继承,但保留对该项目的当前角色分配。该示例将 Reader 权限分配给网站集内的指定用户,并使用 GetByLoginName(String) 方法 (JavaScript: getByLoginName(loginName)) 从网站集内的用户集合中检索用户。
using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
namespace Microsoft.SDK.SharePointServices.Samples
{
class BreakSecurityInheritanceChangeUser
{
static void Main()
{
string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("MyList");
int itemId = 2;
ListItem oListItem = oList.Items.GetById(itemId);
oListItem.BreakRoleInheritance(true);
User oUser = clientContext.Web.SiteUsers.GetByLoginName(@"DOMAIN\alias");
oListItem.RoleAssignments.GetByPrincipal(oUser).DeleteObject();
RoleDefinitionBindingCollection collRollDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
collRollDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader));
oListItem.RoleAssignments.Add(oUser, collRollDefinitionBinding);
clientContext.ExecuteQuery();
}
}
}
Imports System
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client
Namespace Microsoft.SDK.SharePointServices.Samples
Class BreakSecurityInheritance
Shared Sub Main ()
Dim siteUrl As String = "http://MyServer/sites/MySiteCollection"
Dim clientContext As New ClientContext(siteUrl)
Dim oList As SP.List = clientContext.Web.Lists.GetByTitle("MyList")
Dim itemId As Integer = 2
Dim oListItem As ListItem = oList.Items.GetById(itemId)
oListItem.BreakRoleInheritance(True)
Dim oUser As User = clientContext.Web.SiteUsers.GetByLoginName("DOMAIN\alias")
oListItem.RoleAssignments.GetByPrincipal(oUser).DeleteObject()
Dim collRollDefinitionBinding As New RoleDefinitionBindingCollection(clientContext)
collRollDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader))
oListItem.RoleAssignments.Add(oUser, collRollDefinitionBinding)
clientContext.ExecuteQuery()
End Sub
End Class
End Namespace
var siteUrl = '/sites/MySiteCollection';
function breakSecurityInheritanceChangeUser() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var itemId = 5;
this.oListItem = oList.get_items().getById(itemId);
oListItem.breakRoleInheritance(true);
this.oUser = clientContext.get_web().get_siteUsers().getByLoginName('DOMAIN\\alias');
oListItem.get_roleAssignments().getByPrincipal(oUser).deleteObject();
var collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(clientContext);
collRoleDefinitionBinding.add(clientContext.get_web().get_roleDefinitions().getByType(SP.RoleType.administrator));
oListItem.get_roleAssignments().add(oUser, collRoleDefinitionBinding);
clientContext.load(oUser);
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args) {
alert('Role inheritance broken for item ' +
this.oListItem.get_item('Title') +
' and new role assignment for ' +
this.oUser.get_loginName());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
有关在 Microsoft SharePoint Foundation 2010 Silverlight 对象模型上下文中使用客户端对象的信息和示例,请参阅使用 Silverlight 对象模型。