Ejemplo n.º 1
0
        public async Task <IActionResult> GetGlobalRoles([FromQuery] string username)
        {
            var normalizedUsername = UsernameNormalizer.Normalize(username);

            // Authroize
            var loggedInUsername    = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name);
            var authorizationResult = await authorizationModule.AuthorizeAsync(new GetGlobalRolesResourceDescription(normalizedUsername), loggedInUsername);

            if (!authorizationResult.IsAuthorized)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized"));
            }

            if (!await authenticationModule.ExistsAsync(normalizedUsername))
            {
                return(NotFound($"User '{normalizedUsername}' doesn't exist"));
            }

            var roles = await authenticationModule.GetGlobalRolesForUserAsync(normalizedUsername);

            return(new ContentResult
            {
                ContentType = Conventions.JsonContentType,
                Content = JsonConvert.SerializeObject(roles),
                StatusCode = (int)HttpStatusCode.OK
            });
        }