0

我正在尝试确定特定组是否具有对特定网站集的读取权限。

我已经尝试了一天半,但感觉好像找到了三个不同的解决方案!

我到目前为止的代码片段是:

using (SPSite site = new SPSite(this.GenerateAbsoluteUri(moduleCode, academicYear)))
{
    using (SPWeb web = site.OpenWeb())
    {
        for (int i = web.SiteGroups.Count - 1; i >= 0; i--)
        {
            SPGroup group = web.SiteGroups[i];

            if (Regex.IsMatch(group.Name, theGroupImLookingFor))
            {

但是然后呢?!

我的大部分 Google 结果都告诉我有关角色的信息,但我不知道如何将角色与组联系起来。

请帮忙!

4

2 回答 2

2

要将权限分配给用户(帐户)或 SharePoint 组,我们需要按特定顺序查看一些对象。我们需要做的第一件事是获取要分配角色的安全主体(SPUser 或 SPGroup)。接下来我们需要做的是获得我们想要分配的实际权限(角色)(例如:读取、完全控制等……)。然后我们需要创建一个 SPRoleAssignment 对象,并在构造函数中将它传递给我们想要分配权限的 SPUser 或 SPGroup(安全主体)。现在我们需要将角色定义添加到角色分配对象的 RoleDefinitionBindings 集合中。然后我们需要将实际的角色分配添加到 web(站点)并更新 web。下面是完整的代码列表。

// Create the site that contains our list
using(SPSite oSite = new SPSite("<<my site url>>"))
{
    // Open the web object
  using(SPWeb oWeb = oSite.OpenWeb())
  {

    // Get the group that we want to add the user to
    SPGroup oGroup = oWeb.Groups["<<group name>>"];

    // Get the role definition we want to assign ex: Full Control
    SPRoleDefinition oRole = oWeb.RoleDefinitions["<< role name>>"];

    // Create the role assignment object
    SPRoleAssignment oRoleAssignment = new SPRoleAssignment(oGroup);

    // Add the role definition to the role assignemnt. 
    // This will assign the specific permission to the security principal for this role          assignemnt.
    oRoleAssignment.RoleDefinitionBindings.Add(oRole);

     // Now we need to add the role assignment to the web
     oWeb.RoleAssignments.Add(oRoleAssignment);

    // Now update the web
    oWeb.Update();
   }
}
于 2010-12-22T22:55:30.453 回答
0

这是我自己的代码(Sharepoint 2010)的片段。创建角色:

SPRoleDefinition network_role = new SPRoleDefinition();
                network_role.BasePermissions = SPBasePermissions.AddListItems |
                    SPBasePermissions.BrowseDirectories |
                    SPBasePermissions.EditListItems |
                    SPBasePermissions.DeleteListItems;
                network_role.Name = "Network - Project Member";
                network_role.Description = "Provides permissions required for a member of a project.";

                web.RoleDefinitions.Add(network_role);

将角色添加到组:

var assign = new SPRoleAssignment(oweb.SiteGroups["Network Project - " + item.Code]);
assign.RoleDefinitionBindings.Add(network_role);
于 2010-12-21T11:18:59.770 回答