Sharepoint:检查用户是否是组的成员

发布于 2024-07-25 18:01:51 字数 221 浏览 4 评论 0原文

如何检查用户(不是当前登录的用户)是否是某个组的成员? 尝试从不属于其成员的组中检索用户会导致 SPException,因此不可能检查 null。

那么你会如何解决这个问题。 目前,我考虑在 SPGroup.Users.XML 字符串中搜索用户名或迭代所有组成员并检查登录名。

更新: 我忘了提及,我想避免使用异常处理来检查用户的成员资格。

how can I check if a user (not the one currently logged in) is member of a certain group? Trying to retrieve a user from a group of which he's not a member leads to an SPException, so checking for null is not possible.

So how would you solve this problem. At the moment I think about searching in the SPGroup.Users.XML string for the user's name or iterating over all the group members and checking the login names.

Update:
I forgot to mention that I want to avoid the usage of exception handling to check the user's membership.

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

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

发布评论

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

评论(6

苦行僧 2024-08-01 18:01:51

为 SPUser 和静态方法创建扩展类< /a>:

public static class SPUserExtension {
   public static bool InGroup(this SPUser user, SPGroup group)
      {
        return user.Groups.Cast<SPGroup>()
          .Any(g => g.ID == group.ID);
      }
   }
}

然后在 SPUser 对象上调用此方法:

SPUser user;
SPGroup group;
//...
bool isMember = user.InGroup(group);

Create an Extension class for SPUser and static method:

public static class SPUserExtension {
   public static bool InGroup(this SPUser user, SPGroup group)
      {
        return user.Groups.Cast<SPGroup>()
          .Any(g => g.ID == group.ID);
      }
   }
}

Then invoke this method on your SPUser object:

SPUser user;
SPGroup group;
//...
bool isMember = user.InGroup(group);
泅人 2024-08-01 18:01:51

我通过使用 LINQ 编写扩展方法来完成此操作。 SPGroup 继承自 SPPrincipal,因此您应该能够将其传递给 principal 参数:

public static bool Contains(this SPRoleAssignmentCollection rac, SPPrincipal principal)
{
    XElement racXml = XElement.Parse(rac.Xml);
    return racXml.Elements("permission").Any(vw => (int)vw.Attribute("memberid") == principal.ID);
}

I have done this by writing an extension method using LINQ. SPGroup inherits from SPPrincipal so you should be able to pass it through to the principal parameter:

public static bool Contains(this SPRoleAssignmentCollection rac, SPPrincipal principal)
{
    XElement racXml = XElement.Parse(rac.Xml);
    return racXml.Elements("permission").Any(vw => (int)vw.Attribute("memberid") == principal.ID);
}
(り薆情海 2024-08-01 18:01:51

有几种方法。 SharePoint 组有一个选项,可以仅允许组所有者查看成员资格详细信息或允许每个人查看成员资格详细信息。 如果允许每一项,您将不会受到安全限制,否则您需要 RunWithElevatedPrivileges,并确保获得 SPSite 和 SPSite 的新实例。 里面要用到SPWeb。

据说以下是选项:

private Boolean isUserInGroup(SPGroup oGroupToTestFor,String sUserLoginName)
    {   
        Boolean bUserIsInGroup = false;
        try
        {
            SPUser x = oGroupToTestFor.Users[sUserLoginName];
            bUserIsInGroup = true;
        }
        catch (SPException)
        {
            bUserIsInGroup = false;
        }
        return bUserIsInGroup;

    }

另一种方法是

private Boolean isUserInGroup(SPGroup oGroupToTestFor, String sUserLoginName)
    {   
        Boolean bUserIsInGroup = false;

            SPUser oUser =null;
            try{
                oUser = SPContext.Current.Web.AllUsers[sUserLoginName];
                }
            catch{}
            if(oUser!=null){
            foreach (SPUser item in oGroupToTestFor.Users)
            {
                if (item.UserToken == oUser.UserToken)
                {
                    bUserIsInGroup = true;
                    break;
                }                    
            }
            }

        return bUserIsInGroup;

    }

Couple of ways. SharePoint group has an Option that can allow only the Group Owner to see the membership details or allow everyone to view the membership details. If every one is allowed you will not get the Security restriction, else you need to RunWithElevatedPrivileges, and be sure to get a New Instance of the SPSite & SPWeb to be used inside that.

Being said that below are the options:

private Boolean isUserInGroup(SPGroup oGroupToTestFor,String sUserLoginName)
    {   
        Boolean bUserIsInGroup = false;
        try
        {
            SPUser x = oGroupToTestFor.Users[sUserLoginName];
            bUserIsInGroup = true;
        }
        catch (SPException)
        {
            bUserIsInGroup = false;
        }
        return bUserIsInGroup;

    }

Another way is to

private Boolean isUserInGroup(SPGroup oGroupToTestFor, String sUserLoginName)
    {   
        Boolean bUserIsInGroup = false;

            SPUser oUser =null;
            try{
                oUser = SPContext.Current.Web.AllUsers[sUserLoginName];
                }
            catch{}
            if(oUser!=null){
            foreach (SPUser item in oGroupToTestFor.Users)
            {
                if (item.UserToken == oUser.UserToken)
                {
                    bUserIsInGroup = true;
                    break;
                }                    
            }
            }

        return bUserIsInGroup;

    }
伊面 2024-08-01 18:01:51

您是否尝试过使用RunWithElevatedPrivileges

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
             //put your code here to get the group and test for the user
        });

Have you tried using RunWithElevatedPrivileges?

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
             //put your code here to get the group and test for the user
        });
淤浪 2024-08-01 18:01:51

为了使 SPSecurity.RunWith.. 工作,您需要有一个 SPSite 和/或 SPWeb 对象的新实例,并且不使用上下文,否则它将无法工作。

您有 SPUser 对象可以使用吗? 如果是这样,您可以只使用 SPUser.Groups 集合。

In order to make the SPSecurity.RunWith.. work, you need to have a new instance of the SPSite and/or SPWeb object and not use the context otherwise it won't work.

Do you have the SPUser object to use? If so, you can just use the SPUser.Groups collection.

灯下孤影 2024-08-01 18:01:51

我实现了一种简单的方法来检查特定 SharePoint 组中是否存在特定用户。 SPUser 对象上使用 linq 的简单语句。

bool userExsists = spUser.Groups.Cast<SPGroup>().Any(g => g.Name.ToLower() == spGroup.Name.ToLower());

查找有关 SharePoint 核心解决方案

I have implemented a simple way to check if specific user exists in a specific SharePoint group. A simple statement with linq on SPUser object.

bool userExsists = spUser.Groups.Cast<SPGroup>().Any(g => g.Name.ToLower() == spGroup.Name.ToLower());

Find the detailed post on SharePoint Core Solutions.

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