views:

2972

answers:

4

Hi,

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.

A: 

Have you tried using RunWithElevatedPrivileges?

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
             //put your code here to get the group and test for the user
        });
cjuk
No, that doesn't change anything. The indexer still throws an exception.
Flo
+1  A: 

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;

    }
Kusek
Please note that the AllUsers collection may not contain the member unless the user has browsed the site.
AlexanderN
Why not just use the oUser.ID instead of UserToken?
Chaulky
A: 

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.

Robin Meuré
+4  A: 

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);
}
Alex Angas
Thanks for your solution. Although I was not sure what you mean with "SPrac" so I wrote an extension method for the SPGroup class which checks the SPGroup.Users.Xml property for an user's login.
Flo
Sorry, find and replace gone awry. Fixed now.
Alex Angas