Get Role for Particular User

Topics: Administration, General
Feb 18, 2014 at 12:48 PM
I am building module where i need to get role for particular user. please can some put code to achieve this goal .
Feb 18, 2014 at 12:56 PM
See UserRolesPartHandler.cs in Roles module.


Feb 20, 2014 at 6:08 AM
I have looked into it but i am not able to use it inside AdminMenu of my module. Please eleborate me how can i use it inside GetNavigation. here is the code, i have _userId and want to check which role is assigned to current logged in user.
        public void GetNavigation(NavigationBuilder builder)
        {

            var roles = _roleService.GetRoles();

            var _userId = _requestContextAccessor.Value.HttpContext.User.Identity.Name; 


            // "Webshop"
            builder.Add(item => item

                .Caption(T("Courses"))
                .Position("2")
                .Action("Index", "CourseAdmin", new { area = "Orchard.Club" })
                .Permission(StandardPermissions.SiteOwner)
            );

            // "Webshop"
            builder.Add(item => item
                .Caption(T("Members"))
                .Position("3")
                .Action("Index", "CustomerAdmin", new { area = "Orchard.Club" })
                .Permission(StandardPermissions.SiteOwner)
            );
            // "Webshop"
            builder.Add(item => item

                .Caption(T("My Courses"))
                .Position("2")
                .Action("Index", "Member", new { area = "Orchard.Club" })
            );
        }
Feb 20, 2014 at 7:31 AM
Edited Feb 20, 2014 at 7:33 AM
I tried using the following code but this line of cde

_userRolesRepository.Table;

has only one record for User. I know its because i have some users not in "Orchard_Roles_UserRolesPartRecord" table but registered from the website. I think Orchard should assign the registered user as authenticated role but its not there.
  public void GetNavigation(NavigationBuilder builder)
        {
            
            var _userId = _requestContextAccessor.Value.HttpContext.User.Identity.Name;

            var _user = _membershipService.GetUser(_userId);

            var rolerep = _userRolesRepository.Fetch(x => x.UserId == _user.Id);
            
            foreach (var _item in rolerep)
            {
                if (_item.Role.Name == "Authenticated")
                {
                    // "Webshop"
                    builder.Add(item => item

                        .Caption(T("My Courses"))
                        .Position("2")
                        .Action("Index", "Member", new { area = "Orchard.Club" })
                    );               
                }

                else if (_item.Role.Name == "Moderator")
                {
                    // "Webshop"
                    builder.Add(item => item

                        .Caption(T("Courses"))
                        .Position("2")
                        .Action("Index", "CourseAdmin", new { area = "Orchard.Club" })
                        .Permission(StandardPermissions.SiteOwner)
                    );

                    // "Webshop"
                    builder.Add(item => item
                        .Caption(T("Members"))
                        .Position("3")
                        .Action("Index", "CustomerAdmin", new { area = "Orchard.Club" })
                        .Permission(StandardPermissions.SiteOwner)
                    ); 
                }                    
            }
Feb 20, 2014 at 7:53 AM
I think you can get the user ID of the current user with _contextAccessor.Value.CurrentUser.Id, you don't need to get the http context explicitly.

I don't know what the problem is with the repository. If you can reproduce it, you'd better submit a bug.

There is something off with your scheme of admin emnu with permissions and roles, however. When building the admin menu, using .Permission should be enough. Consider losing dependency on roles here, you can change role names, or add new roles with permissions remaining intact.


Marked as answer by mrhammad on 2/20/2014 at 1:26 AM