1

我已经建立了自定义成员资格和角色提供程序。用户是属于公司的一些客户,我使用公司作为角色。

我想创建 SharePoint Group 并向其中添加更多公司(例如行业类型),然后由 SPGroup 进行重定向和安全性。

如何检索当前登录用户的 SPGroup?
我想在我的自定义登录页面中这样做,所以另一个问题是如何在知道登录名的情况下检索 SPUser 或 SPGroup ?

这就是我现在所拥有的:


private List GetGroupsForUser(List roleAccounts)
{
    List groups = new List();
    SPSecurity.RunWithElevatedPrivileges(
     delegate()
     {
         using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID))
         {
             SPUserCollection users = site.RootWeb.SiteUsers;
             foreach (string account in roleAccounts)
             {
                 SPGroupCollection accGroups = users[account].Groups;
                 foreach (SPGroup spg in groups)
                 {
                     groups.Add(spg);
                 }
             }
         }
     }
     );

    return groups;

}

private string GetRoleManagerName()
{
    foreach (KeyValuePair setting in SPContext.Current.Site.WebApplication.IisSettings)
    {
        if (string.IsNullOrEmpty(setting.Value.RoleManager) == false)
            return setting.Value.RoleManager.ToLower();
    }
    return null;
}

private List GetSpAccounts()
{
    List roleAccounts = new List();
    string roleProviderName = GetRoleManagerName();
    foreach (string role in Roles.GetRolesForUser(login.UserName))
    {
        roleAccounts.Add(roleProviderName + ":" + role.ToLower());
    }

    return roleAccounts;
}


// and now I can use it
List roleAccounts = GetSpAccounts();
List groups = GetGroupsForUser(roleAccounts);

但是我有一种感觉,我不应该像这样手动执行此操作。如果只将角色添加到组中,目标受众将如何工作?

4

1 回答 1

2

Use the OwnedGroups property of the SPUser class to return the collection of groups owned by a user.

Update misunderstood the question:

  1. Get the currently logged in user: SPContext.Current.Web.CurrentUser
  2. Add that use to a group: SPGroup.Users.Add([username],[email],[name],[notes]);

Update, third times the charm. So you want to find out what group the user is in based on the roles they have? It's a bit of a combination of the above two attempts at answering it:

var matched = from r in SPContext.Current.Web.CurrentUser.Roles
              where r.Name = [Role]
              select r.Groups;

Important note that the Roles property won't work in the next version of SharePoint! Personally I think SPContext.Current.Web.CurrentUser.Groups would be an easier way to figure out what groups the user is in, but it ignores your role requirementment.

于 2009-03-20T12:48:25.480 回答