Beispiel #1
0
        public async Task <UserDto> Update(UserPut model)
        {
            User dbUser = await userRepository.GetAsync(s => s.Id == model.Id);

            if (!string.IsNullOrEmpty(model.Password))
            {
                dbUser.HashPassword = hashService.HashPassword(model.Password);
            }
            if (!string.IsNullOrEmpty(model.Name))
            {
                dbUser.Name = model.Name;
            }
            if (!string.IsNullOrEmpty(model.Surname))
            {
                dbUser.Surname = model.Surname;
            }
            if (!string.IsNullOrEmpty(model.Username))
            {
                dbUser.Username = model.Username;
            }
            dbUser.CreateDate = DateTime.Now;
            User result = await userRepository.UpdateAsync(dbUser);

            await cacheService.Remove(CacheConsts.UserList);

            return(mapper.Map <UserDto>(result));
        }
Beispiel #2
0
        public static User AsEntity(this UserPut model)
        {
            var entity = new User();

            model.ApplyTo(entity);
            return(entity);
        }
        public async Task <IHttpActionResult> PutUser(string id, UserPut userPut)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            User user = await db.UserDB.Include(u => u.Role).SingleOrDefaultAsync(u => u.ID == id);

            if (user == null)
            {
                return(NotFound());
            }
            else
            {
                user.PassWord    = userPut.PassWord;
                user.FirstName   = userPut.FirstName;
                user.LastName    = userPut.LastName;
                user.Number      = userPut.Number;
                user.PostBox     = userPut.PostBox;
                user.ZipCode     = userPut.ZipCode;
                user.Street      = userPut.Street;
                user.City        = userPut.City;
                user.Country     = userPut.Country;
                user.PhoneNumber = userPut.PhoneNumber;
                user.BirthDate   = userPut.BirthDate;
                user.Picture     = userPut.Picture;
            }

            try
            {
                using (var dbTransaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Entry(user).State = EntityState.Modified;
                        await db.SaveChangesAsync();

                        dbTransaction.Commit();
                    }
                    catch (DbUpdateException ex)
                    {
                        dbTransaction.Rollback();
                    }
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public static UserPut GetApiUser(this ClaimsPrincipal user)
        {
            var model = new UserPut();

            model.UserId      = user.Claims.FirstOrDefault(x => x.Type.Equals(AppSettings.Instance.OidClaim))?.Value;
            model.DisplayName = user.Claims.FirstOrDefault(x => x.Type.Equals(AppSettings.Instance.NameClaim))?.Value ?? "Anonymous";
            return(model);
        }
Beispiel #5
0
        private async Task <IActionResult> CreateUser(UserPut model)
        {
            var entity = model.AsEntity();
            await Services.Context.Users.AddAsync(entity);

            await Services.Context.SaveChangesAsync();

            return(CreatedAtAction("GetUser", new { id = entity.UserId }, entity.AsModel()));
        }
Beispiel #6
0
        public async Task <IActionResult> PutUser(UserPut model)
        {
            var entity = await Services.Context.Users.FirstOrDefaultAsync(x => x.UserId.Equals(model.UserId));

            if (entity == null)
            {
                return(await CreateUser(model));
            }

            model.ApplyTo(entity);
            await Services.Context.SaveChangesAsync();

            return(Ok(entity.AsModel()));
        }
Beispiel #7
0
        public async Task <LoggedInUser> Update(Guid id, UserPut submittedUser)
        {
            var user = await _context.Users.Include(p => p.Person)
                       .FirstOrDefaultAsync(u => u.Id == submittedUser.UserId);

            if (!string.IsNullOrEmpty(submittedUser.Password))
            {
                var saltedPassword = Auth.GeneratePassword(submittedUser.Password);
                user.HashedPassword = saltedPassword.HashedPassword;
                user.Salt           = saltedPassword.Salt;
            }

            if (submittedUser.Person != null)
            {
                await _personService.Update(submittedUser.UserId, submittedUser.Person);
            }
            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (await UserExists(id) == false)
                {
                    throw new ConflictException("User", id.ToString());
                }
                else
                {
                    throw;
                }
            }

            return(await Login(new UserCredentials()
            {
                Username = user.Username, Password = submittedUser.Password
            }));
        }
        public async Task <IActionResult> EditUser(Guid id, UserPut submittedUser)
        {
            var authorization = await AuthorizationService.AuthorizeAsync(User, submittedUser.UserId, "IsCurrentUser");

            if (!authorization.Succeeded)
            {
                return(Unauthorized());
            }
            try
            {
                var loggedInUser = await UserService.Update(id, submittedUser);

                return(Ok(_mapper.Map <LoggedInUser, DTO.LoggedInUser>(loggedInUser)));
            }
            catch (BadRequestException)
            {
                return(BadRequest());
            }
            catch (ConflictException)
            {
                return(Conflict());
            }
        }
Beispiel #9
0
 public async Task <Result <UserGet> > PutUser(UserPut model)
 {
     return(await Put <UserGet, UserPut>(model, $"api/v1/users/{model.UserId}"));
 }
Beispiel #10
0
 public async Task <IActionResult> Put([FromBody] UserPut userPut)
 {
     return(Ok(await userService.Update(userPut)));
 }
Beispiel #11
0
 public static void ApplyTo(this UserPut model, User entity)
 {
     entity.UserId      = model.UserId;
     entity.DisplayName = model.DisplayName;
 }