Ejemplo n.º 1
0
        public async Task <ObjectResult> DeleteUser(string username)
        {
            var accountsServerResult = await accountsService.DeleteUser(username);

            if (accountsServerResult == null)
            {
                return(StatusCode(503, "Accounts service unavailable"));
            }
            else if (string.IsNullOrWhiteSpace(accountsServerResult.Username))
            {
                return(StatusCode(500, "User not found"));
            }

            var newsServerResult = await newsService.DeleteNewsWithAuthor(username);

            if (newsServerResult == null)
            {
                var result = (await accountsService.Register(accountsServerResult))?.IsSuccessStatusCode == true;
                return(StatusCode(503, $"News service unavailable, rollback status: Acc - {(result ? "ok" : "failed")}"));
            }

            var subscriptionsServerResult = await subscriptionsService.RemoveAllAssociatedSubscriptions(username);

            if (subscriptionsServerResult == null)
            {
                var resultNews = true;
                foreach (var news in newsServerResult)
                {
                    resultNews &= (await newsService.AddNews(news))?.IsSuccessStatusCode == true;
                }
                var resultAccounts = (await accountsService.Register(accountsServerResult))?.IsSuccessStatusCode == true;
                return(StatusCode(503, $"Subscriptions service unavailable, rollback status: Acc - { (resultAccounts ? "ok" : "failed")}, News - {(resultNews ? "ok" : "failed")}"));
            }
            eventBus.Publish(new DeleteUserEvent
            {
                Username = username
            });
            return(Ok(""));
        }
Ejemplo n.º 2
0
        //Delete  /api/auth/deleteuser
        public async Task <IActionResult> DeleteUser([FromBody] DeleteUserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _accountsService.GetUser(model.Id);

            if (user == null)
            {
                return(new JsonResult(await Errors
                                      .GetGenericErrorResponse(
                                          new DeleteUserResponse()
                {
                    Id = model.Id,
                    Email = "no_email",
                    StatusCode = 401,
                    Error = "User not found",
                    Description = String.Format(Constants.APIMessages.NotFoundMessage, model.Id),
                    Code = "user_not_found",
                })));
            }


            RolesForUser rolesForUser = await _accountsService.GetUserRoles(user);

            if (rolesForUser.IsNull)
            {
                return(new JsonResult(await Errors
                                      .GetGenericErrorResponse(
                                          new DeleteUserResponse()
                {
                    Id = "no_id",
                    Email = "no_email",
                    StatusCode = 422,
                    Error = "Get roles for user error",
                    Description = "Unable to get roles for user.",
                    Code = "get_roles_for_user_error",
                })));
            }

            if (rolesForUser.Roles.Any())
            {
                IdentityResult removeRolesFromUserResult = await _accountsService.RemoveRolesFromUser(user, rolesForUser.Roles);

                if (!removeRolesFromUserResult.Succeeded)
                {
                    return(new JsonResult(await Errors
                                          .GetGenericErrorResponse(
                                              new DeleteUserResponse()
                    {
                        Id = user.Id,
                        Email = user.Email,
                        StatusCode = 422,
                        Error = "Unable to complete delete opretaion of user related roles.",
                        Description = "Roles realted to the current user could not be removed at this time.",
                        Code = "unable_to_complete_delete_operation_of_user_related_roles",
                    })));
                }
            }

            IdentityResult removeUserResult = await _accountsService.DeleteUser(user);

            if (!removeUserResult.Succeeded)
            {
                IdentityResult reAddRolesToUserResult = await _accountsService.AddRolesToUser(user, rolesForUser.Roles);

                if (reAddRolesToUserResult.Succeeded)
                {
                    return(new JsonResult(await Errors.GetGenericErrorResponse(
                                              new DeleteUserResponse()
                    {
                        Id = user.Id,
                        Email = user.Email,
                        Code = "unable_to_complete_delete_operation",
                        StatusCode = 422,
                        Description = "User was not deleted. The delete task could not be completed at this time.",
                        Error = "Unable to complete delete opretaion."
                    })));
                }

                return(new JsonResult(await Errors.GetGenericErrorResponse(
                                          new DeleteUserResponse()
                {
                    Id = user.Id,
                    Email = user.Email,
                    Code = "unable_to_complete_delete_operation_USER_DOES_NOT_HAVE_ANY_ROLES",
                    StatusCode = 422,
                    Description = "User was not deleted. The delete task could not be completed at this time. User has no roles assign. Pleas add roles to user for access.",
                    Error = "Unable to complete delete opretaion. User does not have any roles"
                })));
            }

            await _context.SaveChangesAsync();

            return(new OkObjectResult(Wrappyfier.WrapDeleteUserResponse(user.Id, user.Email)));
        }