Esempio n. 1
0
        public IActionResult ChangeRole(string id, string roleName)
        {
            if (String.IsNullOrEmpty(id))
            {
                return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND));
            }

            string parsedRole = Enum.Parse(typeof(UserRole), roleName.ToString()).ToString();
            var    role       = _roleManager.FindByNameAsync(parsedRole)
                                .GetAwaiter().GetResult();

            if (String.IsNullOrEmpty(role.Name))
            {
                return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND));
            }

            var user = _userManager.GetUserAsync(User).GetAwaiter().GetResult();

            if (user.Id == id)
            {
                return(RedirectToAction(nameof(Index)).WithWarning(LOCALIZATION_WARNING_SELF_CHANGEROLE));
            }

            var account = _userManager.FindByIdAsync(id).GetAwaiter().GetResult();

            if (account == null)
            {
                return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND));
            }

            var roles = _userManager.GetRolesAsync(account).GetAwaiter().GetResult();

            if (roles.Count > 0)
            {
                _userManager.RemoveFromRolesAsync(account, roles.ToArray())
                .GetAwaiter().GetResult();
                _userRoleCountryService.Delete(account.Id, CountryId);
            }

            var addingresult = _userManager.AddToRoleAsync(account, parsedRole).GetAwaiter().GetResult();

            _userRoleCountryService.Add(new UserRoleCountryCreateModel
            {
                RoleId            = role.Id,
                CountryId         = CountryId,
                ApplicationUserId = account.Id
            });


            if (!addingresult.Succeeded)
            {
                return(RedirectToAction(nameof(Index)).WithError(LOCALIZATION_ERROR_DEFAULT));
            }


            return(RedirectToAction(nameof(Index)).WithSuccess(LOCALIZATION_SUCCESS_DEFAULT));
        }
        public async Task <IActionResult> Delete(string id)
        {
            var account = _userManager.FindByIdAsync(id).GetAwaiter().GetResult();

            if (account == null)
            {
                return(BadRequest("Account doesnt exist"));
            }

            if (_userRoleCountryService.Delete(id))
            {
                if (!_userManager.DeleteAsync(account).GetAwaiter().GetResult().Succeeded)
                {
                    return(BadRequest("Cannot delete account"));
                }
                return(Ok());
            }
            return(BadRequest("Delete action failed"));
        }