public async Task <IActionResult> UpdateUser(UserUpdateDto model)
        {
            if (!ModelState.IsValid || model.Id == Guid.Empty)
            {
                return(BadRequest(ModelState));
            }

            // Ensure the user is either in the admin role or is the user itself
            if (HttpContext.User.HasClaim(
                    claim => claim.Type == Helper.RoleClaimKey && claim.Value == Helper.ClaimAdmin) ||
                HttpContext.User.Claims.First(x => x.Type == ClaimTypes.PrimarySid).Value == model.Id.ToString())
            {
                // Find user
                var found = await _administrationManager.Get(model.Id);

                if (found != null)
                {
                    var item = Mapper.Map <UserUpdateDto, User>(model, found);
                    // NOTE: Removing claims since validation will complain that there are duplicates.
                    item.Claims = new HashSet <UserClaim>();
                    var validation = ValidationCatalog.Validate(item);
                    if (validation.IsValid)
                    {
                        var result = await _administrationManager.Update(item);

                        return(new JsonResult(result));
                    }

                    // Add the errors
                    foreach (var error in validation.Errors)
                    {
                        foreach (var allErrorMessage in error.AllErrorMessages())
                        {
                            ModelState.AddModelError("Error(s): ", allErrorMessage);
                        }
                    }

                    return(BadRequest(ModelState));
                }
            }

            return(Unauthorized());
        }
Пример #2
0
        public async void CanUpdateUser()
        {
            var random = DateTime.Now.ToString("MMddyyhhmmssfff");
            // Create
            var user = new User(Guid.Empty
                                , $"{DataGenerator.GenerateRandomName(1).FirstOrDefault()?.Item1}-{random}"
                                , $"{DataGenerator.GenerateRandomName(1).FirstOrDefault().Item1}-{random}@testing.com"
                                , true
                                , _passwordStorage.HashPassword(new User(), "testdb99!!")
                                , Guid.NewGuid().ToString()
                                , true
                                , true
                                , false
                                , false
                                , 0
                                , DateTime.UtcNow
                                , DateTime.UtcNow);

            user.Profile = new UserProfile(user.Id
                                           , DataGenerator.GenerateRandomName(1).FirstOrDefault().Item1
                                           , DataGenerator.GenerateRandomName(1).FirstOrDefault().Item2
                                           , Guid.Parse("5ebf5cca-df92-49c6-ae5f-f3c9670bf9d3")
                                           , Guid.Parse("2af6ff6c-8bb8-46f0-b27e-81def1b76b64")
                                           , Guid.Parse("8a29a4ab-62a7-4a06-b2fa-46a40f449a84"));
            user.PhoneNumber = "123-456-7890";
            var id = await _administrationManager.Create(user);

            Assert.True(id != Guid.Empty, "Failed to create user");
            // Find
            var found = await _administrationManager.Get(id);

            Assert.True(found != null, "Failed to find user");
            // Update
            found.PhoneNumber = "999-999-9999";
            var update = await _administrationManager.Update(found);

            Assert.True(update, "Failed to update user");
        }