Esempio n. 1
0
 public IActionResult GetUserProfile()
 {
     try
     {
         var response = UserProfileResponseMapper.MapUserToResponseModel(User);
         return(Ok(response));
     }
     catch (Exception e)
     {
         _logger.LogError(e, "User does not have permission. Error: '{message}'", e.Message);
         return(Unauthorized());
     }
 }
Esempio n. 2
0
        public void Should_refuse_access_with_missing_role()
        {
            var USERNAME = ClaimsPrincipalBuilder.USERNAME;

            var userWithoutRole = new ClaimsPrincipalBuilder()
                                  .WithClaim(ClaimTypes.GivenName, UserData.FIRST_NAME)
                                  .WithClaim(ClaimTypes.Surname, UserData.LAST_NAME)
                                  .WithClaim("name", UserData.DISPLAY_NAME)
                                  .WithUsername(USERNAME)
                                  .Build();

            Assert.Throws <UnauthorizedAccessException>(() => UserProfileResponseMapper.MapUserToResponseModel(userWithoutRole));
        }
Esempio n. 3
0
 public IActionResult GetUserProfile()
 {
     try
     {
         var response = UserProfileResponseMapper.MapUserToResponseModel(User);
         return(Ok(response));
     }
     catch (Exception e)
     {
         const string message = "User does not have permission";
         _logger.LogError(e, message);
         return(Unauthorized(message));
     }
 }
Esempio n. 4
0
        public void Should_map_all_properties()
        {
            const string ROLE     = "VHQA";
            var          USERNAME = ClaimsPrincipalBuilder.USERNAME;

            var user = new ClaimsPrincipalBuilder()
                       .WithClaim(ClaimTypes.GivenName, UserData.FIRST_NAME)
                       .WithClaim(ClaimTypes.Surname, UserData.LAST_NAME)
                       .WithClaim("name", UserData.DISPLAY_NAME)
                       .WithUsername(USERNAME)
                       .WithRole(ROLE).Build();

            var response = UserProfileResponseMapper.MapUserToResponseModel(user);

            response.Username.Should().Be(USERNAME);
            response.Role.Should().Be(ROLE);
        }
Esempio n. 5
0
        public async Task <IActionResult> GetProfileByUsernameAsync([FromQuery] string username)
        {
            var usernameClean = username.ToLower().Trim();

            try
            {
                var userProfile = await _userCache.GetOrAddAsync
                                  (
                    usernameClean, async key => await _userApiClient.GetUserByAdUserNameAsync(usernameClean)
                                  );

                var response = UserProfileResponseMapper.MapToResponseModel(userProfile);
                return(Ok(response));
            }
            catch (UserApiException e)
            {
                _logger.LogError(e, "Unable to get user profile");
                return(StatusCode(e.StatusCode, e.Response));
            }
        }