Exemplo n.º 1
0
        public async Task <IdentityResult> DeleteAsync(TravelUser user, CancellationToken cancellationToken)
        {
            var result = await _usersWriteService.DeleteUserAsync(user.Id);

            if (result.Status == ResponseStatus.Succeeded)
            {
                return(IdentityResult.Success);
            }
            return(IdentityResult.Failed());
        }
        public async Task <IActionResult> Delete(string id)
        {
            try
            {
                UserViewModel user   = MapToUserToUserVM(_usersReadService.GetUserById(id).Result.User);
                var           editor = await _userManager.FindByEmailAsync(User.Identity.Name);

                if (user.Id == editor.Id)
                {
                    return(BadRequest("You can't delete yourself"));
                }
                var roles = await _userManager.GetRolesAsync(editor);

                if (user.Role == "admin" && !roles.Contains("admin"))
                {
                    return(Unauthorized());
                }

                var result = await _usersWriteService.DeleteUserAsync(id);

                if (result.Status == ResponseStatus.Unauthorized)
                {
                    return(Unauthorized());
                }
                if (result.Status == ResponseStatus.Failed)
                {
                    return(BadRequest());
                }

                return(NoContent());
            }
            catch (Exception)
            {
                return(BadRequest());
            }
        }