Ejemplo n.º 1
0
        public async Task <IActionResult> UpdateProfile(int id, [FromBody] UpdateUserProfileModel model)
        {
            if (id <= 0 || model is null)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = ModelState.Values.SelectMany(x => x.Errors) }));
            }

            try
            {
                var user = await this._userService.Get(id);

                if (user == null)
                {
                    return(NotFound(new { Message = "User not found" }));
                }

                user.Culture   = model.Culture;
                user.Firstname = model.Firstname;
                user.Surname   = model.Surname;
                user.Timezone  = model.Timezone;

                if (!string.IsNullOrEmpty(model.Password) & !string.IsNullOrEmpty(model.NewPassword))
                {
                    if (!PasswordHasher.VerifyHash(model.Password, user.Salt, user.PasswordHash))
                    {
                        return(BadRequest(new { Message = "Wrong current password" }));
                    }

                    var hash = PasswordHasher.ComputeHash(model.NewPassword);
                    user.PasswordHash = hash.Hash;
                    user.Salt         = hash.Salt;
                    await _userService.Update(user);

                    return(Ok(new { Message = "User was updated" }));
                }

                await _userService.Update(user);

                var links = HypermediaHelper.PutUserHypermediaLinks(this, id);

                return(Ok(new
                {
                    Message = "User updated",
                    Links = links
                }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Token([FromBody] LoginModel credentials)
        {
            if (credentials == null || !ModelState.IsValid)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            try
            {
                var user = await this._userService.FindByUserName(credentials.UserName);

                if (user == null)
                {
                    return(NotFound(new { Message = "User not found" }));
                }

                TokenModel token = null;
                if (PasswordHasher.VerifyHash(credentials.Password, user.Salt, user.PasswordHash))
                {
                    token = await this._userManager.GenerateToken(user);
                }
                else
                {
                    return(BadRequest(new { Message = "Wrong current password" }));
                }

                await _userService.SetRefreshToken(user.Id, token.RefreshToken);

                //add hypermedia
                var links = HypermediaHelper.AuthHypermediaLinks(this);

                return(Ok(new { token, links }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Get(int id)
        {
            if (id <= 0)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            try
            {
                var user = await this._userService.FindById(id);

                if (user is null)
                {
                    return(NotFound(new { Message = "User not found" }));
                }

                var roles = await this._userService.GetRoles(user.UserName);

                var links = HypermediaHelper.GetUserHypermediaLinks(this, id);

                var userResult = UserParser.UserDtoToUserModel(user);
                userResult.Roles = roles.ToList();

                return(Ok(new
                {
                    User = userResult,
                    Links = links
                }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] CreateUpdateUserModel model)
        {
            if (model is null)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = ModelState.Values.SelectMany(x => x.Errors) }));
            }

            try
            {
                var user = UserParser.CreateUpdateUserModelToUserDto(model);

                await _userService.Create(user);
                await UpdateRoles(user, model.Roles);

                var links = HypermediaHelper.PostUserHypermediaLinks(this, user.Id);

                return(Ok(new
                {
                    Message = "User was created",
                    Links = links
                }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Register([FromBody] CreateUserModel model)
        {
            if (model is null)
            {
                return(BadRequest(new { Message = "Invalid client request" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(new { Message = ModelState.Values.SelectMany(x => x.Errors) }));
            }

            try
            {
                var user = UserParser.CreateUserModelToUserDto(model);

                await _userService.Create(user);

                var token = await this._userManager.GenerateToken(user);

                await _userService.SetRefreshToken(user.Id, token.RefreshToken);

                //add hypermedia
                var links = HypermediaHelper.AuthHypermediaLinks(this);

                return(Ok(new { token, links }));
            }
            catch (UserException exception)
            {
                return(BadRequest(new { exception.Message }));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }