C# - 如何为子网站而非根级别下的用户设置 SharePoint 权限

发布于 2024-12-02 06:15:41 字数 1003 浏览 0 评论 0原文

我正在尝试为 SharePoint 中子网站下的用户添加权限。我能够在根级别成功设置用户的权限,但我不确定如何解决较低级别子网站的问题。下面是我目前所拥有的,但它崩溃了,尽管它显示 VS 中构建没有错误。任何想法都会很棒

        foreach (SPWeb subSite in spSite2.RootWeb.GetSubwebsForCurrentUser())
        {
            if (subSite.Name == "templates")
            {
                Console.WriteLine("\nTEMPLATES SITE");
                Console.Write("\nApplying 'Read' permission to App_user Account");
                spRoleAssignment = new SPRoleAssignment(SPContext.Current.Web.Users[appUserAccount]);
                spRoleAssignment.RoleDefinitionBindings.Add(SPContext.Current.Web.RoleDefinitions["Read"]);
                SPContext.Current.Web.RoleAssignments.Add(spRoleAssignment);
                SPContext.Current.Web.Update();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.CursorLeft = Console.BufferWidth - 16; Console.WriteLine("Applied");
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }

I'm trying to add permissions for a user that is under a subsite in SharePoint. I was able to successfully set the permissions for the user on the root level, but I am not sure how to approach the problem for a lower level subsite. Below is what I have at the moment but it crashes although it shows no error building in VS. Any ideas would be great

        foreach (SPWeb subSite in spSite2.RootWeb.GetSubwebsForCurrentUser())
        {
            if (subSite.Name == "templates")
            {
                Console.WriteLine("\nTEMPLATES SITE");
                Console.Write("\nApplying 'Read' permission to App_user Account");
                spRoleAssignment = new SPRoleAssignment(SPContext.Current.Web.Users[appUserAccount]);
                spRoleAssignment.RoleDefinitionBindings.Add(SPContext.Current.Web.RoleDefinitions["Read"]);
                SPContext.Current.Web.RoleAssignments.Add(spRoleAssignment);
                SPContext.Current.Web.Update();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.CursorLeft = Console.BufferWidth - 16; Console.WriteLine("Applied");
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2024-12-09 06:15:41

我不完全确定您想要实现的目标,也不完全确定您的代码应该在哪里执行(控制台应用程序或计时器作业或在 Web 部件中?)。

我看到以下问题:

  1. 您登录到控制台(我假设您的代码在控制台应用程序中运行),但您访问了 SPContext.Current,这仅在您的代码时可用在 HTTP 请求中运行。

  2. 您正在迭代网络集合。但在您的 for-each 主体中,SPContext.Current.Web 已更新。

  3. 您确实通过web.GetSubwebsForCurrentUser()检索您的网络集合,但随后更改了这些对象的权限。这有点奇怪,因为更新权限是一项“管理任务”,并且方法 GetSubwebsForCurrentUser 更有可能用于低级别用户上下文,以避免访问被拒绝异常。例如,安全地显示网站列表。

  4. 您确实更新了网络上的权限/角色,但缺少检查网络是否具有唯一的角色分配。

  5. 您检查名为“templates”的网站。由于 GetSubwebsForCurrentUser 不是递归的,因此该集合中只能有一个名为“templates”的网站。该网页可以直接打开=>无需通过打开每个子网站来浪费资源。

如果您的任务是在给定的网站“模板”(这是根网站的第一级子网站)上设置角色权限,您可以使用以下代码:

// Open the web directly since it is a direct child of the site collection.
// Use a using to properly release the resources
using (SPWeb web = spSite2.Open("templates"))
{
  SPUser user = web.SiteUsers[appUserAccount];
  SPRoleDefinition roleDef = web.RoleDefinitions.GetByType(SPRoleType.Reader);

  if (!web.HasUniqueRoleAssignments)
  {
    web.BreakRoleInheritance(true);
  }

  spRoleAssignment = new SPRoleAssignment(user);
  spRoleAssignment.RoleDefinitionBindings.Add(roleDef);
  web.RoleAssignments.Add(spRoleAssignment);

  // No need to update the web when changing the permissions
}

I'm not completely sure what you are trying to achieve nor where your code is supposed to be executed (console application or timer job or in a web part?).

I see the following problems:

  1. You log to the console (which let's me assume your code runs in a console application), but you access the SPContext.Current, which is only available if your code runs in a HTTP request.

  2. You are iterating over a collection of webs. But in your for-each body the SPContext.Current.Web is updated.

  3. You do retrieve your web collection via the web.GetSubwebsForCurrentUser(), but are then changing permissions on these objects. This smells a little since updating permissions is an "admin task" and the the method GetSubwebsForCurrentUser is more likely to be used for a low level user context to avoid access denied exception. For instance to safely display a list of webs.

  4. You do update permissions/roles on a web, but a check if the web has unique role assignments is missing.

  5. You check for a web with the name "templates". Since GetSubwebsForCurrentUser is not recursive there can only be one web named "templates" in this collection. This web can be opened direclty => no need to waste resource by opening every sub web.

If your task is to set role permissions on a given web "templates" (which is a 1st level sub web of your root web) you can use the follwing code:

// Open the web directly since it is a direct child of the site collection.
// Use a using to properly release the resources
using (SPWeb web = spSite2.Open("templates"))
{
  SPUser user = web.SiteUsers[appUserAccount];
  SPRoleDefinition roleDef = web.RoleDefinitions.GetByType(SPRoleType.Reader);

  if (!web.HasUniqueRoleAssignments)
  {
    web.BreakRoleInheritance(true);
  }

  spRoleAssignment = new SPRoleAssignment(user);
  spRoleAssignment.RoleDefinitionBindings.Add(roleDef);
  web.RoleAssignments.Add(spRoleAssignment);

  // No need to update the web when changing the permissions
}
怪我入戏太深 2024-12-09 06:15:41

尝试从 SPContext 更新子站点中的角色,而不是当前 Web 中的角色?

AFAIK 每个 SPWeb 对象都有它自己的角色分配。

Try to update roles in the subSite, not in the current web from SPContext?

AFAIK each SPWeb object have it's own roles assignments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文