示例#1
0
        public IEnumerable <RoleClaims> GetAllRoleClaims()
        {
            var roleClaims = (from role in _context.Roles
                              join claim in _context.RoleClaims on role.Id equals claim.RoleId into RoleClaims
                              from rc in RoleClaims.DefaultIfEmpty()
                              select new { Role = role, RoleClaims = rc }
                              ).ToLookup(x => x.Role)
                             .Select(x => new { Role = x.Key, Claims = x.Select(c => c.RoleClaims).Where(c => c != null).ToList() });

            var convertedRoleClaims = roleClaims.Select(x =>
                                                        new RoleClaims {
                Role = _mapper.Map <Role>(x.Role), Claims = x.Claims != null ? x.Claims.Select(c => c.ClaimValue) : new List <string>()
            });

            return(convertedRoleClaims);
        }
示例#2
0
        public RoleClaims GetRoleClaims(string roleId)
        {
            var roleClaims = (from role in _context.Roles
                              join claim in _context.RoleClaims on role.Id equals claim.RoleId into RoleClaims
                              from rc in RoleClaims.DefaultIfEmpty()
                              select new
            {
                Role = role,
                RoleClaims = rc
            }).ToLookup(x => x.Role)
                             .Select(x => new { Role = x.Key, Claims = x.Select(x => x.RoleClaims).Where(c => c != null).ToList() })
                             .FirstOrDefault(x => x.Role.Id == roleId);

            var model = new RoleClaims();

            model.Role   = _mapper.Map <Role>(roleClaims.Role);
            model.Claims = roleClaims.Claims != null?roleClaims.Claims.Select(x => x.ClaimValue) : new List <string>();

            return(model);
        }
        private UserRolesClaims GetProfile(BlogUser user)
        {
            var profile   = new UserRolesClaims();
            var userRoles = _context.UserRoles.Where(x => x.UserId == user.Id).Select(x => x.RoleId);
            var roles     = (from role in _context.Roles
                             where userRoles.Contains(role.Id)
                             join claim in _context.RoleClaims on role.Id equals claim.RoleId into RoleClaims
                             from rc in RoleClaims.DefaultIfEmpty()
                             select new
            {
                Role = role,
                Claims = rc
            }).ToLookup(x => x.Role)
                            .Select(x => new { Role = x.Key, Claims = x.Select(c => c.Claims).Where(c => c != null).ToList() });

            var userClaims      = _context.UserClaims.Where(x => x.UserId == user.Id);
            var groupUserEmails = _context.Users.Where(x =>
                                                       user.SupervisorId == null ?
                                                       x.SupervisorId == user.Id :
                                                       (x.SupervisorId == user.SupervisorId || x.Id == user.SupervisorId) && x.Id != user.Id).Select(x => x.Email);
            var roleClaims = new List <RoleClaims>();

            foreach (var r in roles)
            {
                var roleClaim = new RoleClaims();
                roleClaim.Role   = _mapper.Map <Role>(r.Role);
                roleClaim.Claims = r.Claims.Select(x => x.ClaimValue);
                roleClaims.Add(roleClaim);
            }

            profile.User        = _mapper.Map <User>(user);
            profile.UserClaims  = _mapper.Map <IEnumerable <UserClaim> >(userClaims);
            profile.Roles       = roleClaims;
            profile.GroupEmails = groupUserEmails;

            return(profile);
        }