Beispiel #1
0
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserToEditDTO userToEditDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userFromRepo = await _userRepo.GetUser(id);

            if (userFromRepo == null)
            {
                return(NotFound($"Usuário com ID {id} não encontrado"));
            }

            var currentUserId      = this.LoggedUser;
            var loggedUserFromRepo = await _userRepo.GetUser(this.LoggedUser);

            if (loggedUserFromRepo.Id != userFromRepo.Id && !loggedUserFromRepo.IsAdmin())
            {
                return(Unauthorized());
            }

            _mapper.Map(userToEditDTO, userFromRepo);
            await this.UpdateUserTeams(userFromRepo, userToEditDTO.TeamIds);

            if (await _userRepo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Falha ao salvar alterações para o usuário com ID {id}");
        }
Beispiel #2
0
        public async Task <IActionResult> AddUser([FromBody] UserToEditDTO userDTO)
        {
            if (!string.IsNullOrEmpty(userDTO.Email))
            {
                userDTO.Email = userDTO.Email.ToLower();
            }

            if (await _authRepo.UserExists(userDTO.Email))
            {
                ModelState.AddModelError("Email", "Email já existente no sistema");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var currentUserId      = this.LoggedUser;
            var loggedUserFromRepo = await _userRepo.GetUser(this.LoggedUser);

            if (!loggedUserFromRepo.IsAdmin())
            {
                return(Unauthorized());
            }

            var userToCreate = _mapper.Map <User>(userDTO);
            await _authRepo.AddMember(userToCreate, userDTO.Password, loggedUserFromRepo.Organization);

            var userToReturn = _mapper.Map <UserToListDTO>(userToCreate);

            return(CreatedAtRoute("GetUser", new { controller = "Users", id = userToCreate.Id }, userToReturn));
        }