/// <summary>
        /// Generates a list of `role` claims for the provided identity roles.
        /// </summary>
        /// <param name="roles">Roles to generate claims for.</param>
        /// <returns>Returns the newly generated claims.</returns>
        /// <exception cref="EntityNotFoundException">Thrown if any of the referenced domains could be found.</exception>
        private async Task <IEnumerable <Claim> > GenerateRoleClaims(IEnumerable <Role> roles)
        {
            Dictionary <Guid, Domain> domains = new Dictionary <Guid, Domain>();
            List <Claim> claims = new List <Claim>();

            // For each role, retrieve domain if neccessary, and set claim with role name `[<domainName>.]<roleName>`.
            foreach (Role role in roles)
            {
                string roleName = "";
                if (role.DomainId.HasValue)
                {
                    Guid domainId = role.DomainId.Value;
                    if (!domains.ContainsKey(domainId))
                    {
                        domains.Add(domainId, await DomainService.GetDomain(domainId));
                    }
                    roleName += domains[domainId].Name + ".";
                }
                roleName += role.Name;
                claims.Add(new Claim(MisConstants.JWT_ROLES, roleName));
            }

            // Done, return role claims
            return(claims);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets roles, optionally filtered to the roles that belong to a given domain.
        /// </summary>
        /// <param name="domainId">ID of the domain to get identity roles for.</param>
        /// <returns>Returns a list of roles.</returns>
        /// <exception cref="EntityNotFoundException">Thrown if a domain ID was provided but no matching domain could be found.</exception>
        public async Task <IEnumerable <Role> > GetRoles(string filter = null, Guid?domainId = null)
        {
            if (filter == null && domainId == null)
            {
                return(await RoleRepository.GetRoles());
            }
            else if (filter == null && domainId != null)
            {
                await DomainService.GetDomain(domainId.Value); // Throws an EntityNotFoundException if domain doesn't exist

                return(await RoleRepository.GetDomainRoles(domainId.Value));
            }
            else if (filter != null && domainId == null)
            {
                return(await RoleRepository.SearchRolesByName(filter));
            }
            else
            {
                await DomainService.GetDomain(domainId.Value); // Throws an EntityNotFoundException if domain doesn't exist

                return(await RoleRepository.SearchDomainRolesByName(domainId.Value, filter));
            }
        }