More in this section

Forums / General Discussions / User Roles in Code

User Roles in Code

5 posts, 0 answered
  1. AFTR
    AFTR avatar
    22 posts
    Registered:
    05 Oct 2010
    07 Mar 2011
    Link to this post
    I am developing a custom user control for my Sitefinity application. It has a Rad Grid on it which is bound to data that will be dynamically retrieved in code.

    The data needs to be retrieved based on what the user's roles are.

    How can I get the current user's roles in code so I can retrieve the correct data.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    07 Mar 2011
    Link to this post
    Hello ,

    There is a method of RoleManager - GetRolesForUser that you can use. You can use SecurityManager.GetCurrentUserId() to get the id of the current user.

    Regards,
    Ivan Dimitrov
    the Telerik team
  3. Phil
    Phil avatar
    89 posts
    Registered:
    19 Oct 2010
    08 Mar 2011
    Link to this post
    We just upgraded from build 1098 to build 1210

    With respect to this code:

    var roleMgr = new RoleManager();
    var rolesForUser = roleMgr.GetRolesForUser(SecurityManager.GetCurrentUserId()).ToList();

    Before the upgrade GetRolesForUser(), as used above, returned all the roles for the user.
    Now (build 1210) GetRolesForUser(), as used above, does not return Administrators or BackendUsers.

    Can you please verify this.

    If what I am observing is correct, what method can I use to get all the user's roles?
  4. Developer
    Developer avatar
    12 posts
    Registered:
    23 Oct 2008
    08 Mar 2011
    Link to this post
    Hi,

    I see the same issue.

    Besides, there are some other predefined roles like Authors, Editors, Designers that do not come back. So, when I check for existing roles and try to create a role if it does not exist, it creates additional role e.g. Editors, and I can assign users only to "MY" role but not to the role that came with Sitefinity.
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 Mar 2011
    Link to this post
    Hello,

    The method woks properly. Most probably you are not passing the correct provider to it

                var userID = SecurityManager.GetCurrentUserId();
                var manager = RoleManager.GetManager("AppRoles");
                var roles = manager.GetRolesForUser(userID).ToList();

    AppRoles is the provider used for the default roles like "Administrators"

    Regards,
    Ivan Dimitrov
    the Telerik team
5 posts, 0 answered