Exemple #1
0
        public IActionResult UpdateCredentials(UserUpdateCredentialsRequest request)
        {
            int?id = HttpContext.Session.GetInt32("userId");

            if (id == null)
            {
                return(StatusCode(440));
            }

            var response = _service.UpdateCredentials((int)id, request);

            return(response.Equals(Error.OK) ? Ok() : BadRequest(response));
        }
        public string UpdateCredentials(int id, UserUpdateCredentialsRequest request)
        {
            var user = _context.UserEntity.FirstOrDefault(x => x.ID == id);

            if (user == null)
            {
                return(string.Format(Error.CANNOT_FIND_USER, id));
            }

            var loginReq = new UserLoginRequest
            {
                Login    = user.Login,
                Password = request.OldPassword
            };

            if (Login(loginReq) == null)
            {
                return(Error.INVALID_OLD_PASSWORD);
            }

            if (request.Login != user.Login)
            {
                if (_context.UserEntity.FirstOrDefault(x => x.Login == request.Login) == null)
                {
                    user.Login = request.Login;
                }
                else
                {
                    return(string.Format(Error.LOGIN_EXIST, request.Login));
                }
            }

            if (request.NewPassword != request.OldPassword)
            {
                int    random = new Random().Next();
                string pw     = request.NewPassword + random.ToString();
                user.PasswordHash = Hash(pw);
                user.Salt         = random;
            }

            _context.SaveChanges();
            return(Error.OK);
        }