+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Unable to find Roles For CurrentUser????

Unable to find Roles For CurrentUser????

2 posts, 0 answered
  1. Dustin
    Dustin avatar
    20 posts
    Registered:
    05 Jan 2012
    07 May 2013
    Link to this post
    I am trying to dynamically allow functionality of inserts/edits/deletes in a user control calender widget based on who is logged in, but I am unable to find the roles for the current user?

    I am trying to allow certain groups certian functionality, but without finding the roles, this is useless...

    ...
    var identity = ClaimsManager.GetCurrentIdentity();
    RoleManager roleManager = RoleManager.GetManager();
    EventsManager manager = EventsManager.GetManager();

    //default radscheduler function 
    Calendar.AllowDelete = false;
    Calendar.AllowEdit = false;
    Calendar.AllowInsert = false;

    if (identity.IsAuthenticated)
    {
    List<Role> roles = roleManager.GetRolesForUser(ClaimsManager.GetCurrentUserId()).ToList();
    labeltext += " Found " + roles.Count().ToString() + " roles for " + ClaimsManager.GetCurrentUserId);
    //labeltext = identity.Name + " is Authenticated as ID " + identity.UserId;
    var isUserInRole = roleManager.IsUserInRole(identity.UserId, "Authors");

    if (isUserInRole)
    {
    labeltext = labeltext + " + is in Authors";
    Calendar.AllowInsert = true;
    }

    isUserInRole = roleManager.IsUserInRole(identity.UserId, "Editors");

    if (isUserInRole)
    {
    labeltext = labeltext + " + is in Editors";
    Calendar.AllowDelete = true;
    Calendar.AllowEdit = true;
    Calendar.AllowInsert = true;
    }

    isUserInRole = roleManager.IsUserInRole(identity.UserId, "Administrators");

    if (isUserInRole)
    {
    labeltext = labeltext + " + is in Administrators";
    Calendar.AllowDelete = true;
    Calendar.AllowEdit = true;
    Calendar.AllowInsert = true;
    }
    }

    but if I log in for an admin account I will get the labeltext... as seen in the attached and below

    Found 0 roles for dc1f1f74-64d1-4fa2-89ff-b0e0f71a00ba

    I can login to the backed in and for this user_id  i see roles for admin...and a test user I see three different roles... I can even log into the sql tables and run a query and find the roles... (see attached)

    am I missing something?  I tried following the documentation for this link...
    http://www.sitefinity.com/documentation/documentationarticles/developers-guide/deep-dive/security/roles/users-and-roles#checkwhetherauserisinarole


  2. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    10 May 2013
    Link to this post
    Hi Dustin,

    You are not able to find the roles for the current user, because you are searching them in the wrong role provider: RoleManager.GetManager() returns manager for the default role provider. However, the roles like Administrators, Authors, Editors, etc belongs to AppRoles provider (They are in the same table sf_roles but with different app_name). You can check the available role providers from Administration > Settings > Advanced > Security > Role Providers.

    In order to get correct results, you have to get the role manager by specifying the AppRoles provider:
    RoleManager roleManager = RoleManager.GetManager("AppRoles");

    However, much better approach is to use the cached collection of roles in the current identity. There is no need to query the database every time, since you already have this information cached. Here is an example:
    var user = ClaimsManager.GetCurrentIdentity();
    var isAuthenticated = user.IsAuthenticated;
    var isBackend = user.IsBackendUser;
    var isUnrestricted = user.IsUnrestricted;
    var isAuthor = user.Roles.Any(r => r.Name == "Authors" && r.Provider == "AppRoles");

    Hope this is helpful.

    Regards,
    Vlad
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered