Пример #1
0
        public async Task <ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var user = await UserManager.FindByIdAsync(id);

            if (user == null)
            {
                return(HttpNotFound());
            }
            var result = await UserManager.DeleteAsync(user);

            if (result.Succeeded)
            {
                //Clear cache
                CachingHelpers.ClearUserCache();
                CachingHelpers.ClearUserCache(id);

                return(Json(new { success = true }));
            }
            else
            {
                throw new Exception("Failed to delete the user");
            }
        }
Пример #2
0
        public UsersAdminController(ApplicationUserManager userManager, ApplicationRoleManager roleManager, IIdentityStore identityStore)
        {
            UserManager = userManager;
            RoleManager = roleManager;

            _identityStore = identityStore;
            //Clear cache
            CachingHelpers.ClearUserCache();
        }
Пример #3
0
        public async Task <ActionResult> Edit([Bind(Include = "Email,Id,PhoneNumber,ProviderId")] EditUserViewModel editUser, params string[] selectedRole)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByIdAsync(editUser.Id);

                if (user == null)
                {
                    return(HttpNotFound());
                }

                user.Email       = editUser.Email;
                user.PhoneNumber = editUser.PhoneNumber;
                //user.ProviderId = editUser.ProviderId;

                var userRoles = await UserManager.GetRolesAsync(user.Id);

                await UserManager.UpdateAsync(user);

                selectedRole = selectedRole ?? new string[] { };

                var result = await UserManager.AddUserToRolesAsync(user.Id, selectedRole.Except(userRoles).ToList <string>());

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", result.Errors.First());
                    return(View());
                }
                result = await UserManager.RemoveUserFromRolesAsync(user.Id, userRoles.Except(selectedRole).ToList <string>());

                if (!result.Succeeded)
                {
                    this.AddNotification("Update user profiles faied: " + result.Errors.First(), NotificationType.ERROR);
                    return(View());
                }

                MenuHelper.ClearUserMenuCache(editUser.Id);

                //Clear cache
                CachingHelpers.ClearUserCache();
                CachingHelpers.ClearUserCache(editUser.Id);

                this.AddNotification(ManagerResource.LB_UPDATE_SUCCESS, NotificationType.SUCCESS);
                return(RedirectToAction("Edit/" + editUser.Id));
            }

            this.AddNotificationModelStateErrors(ModelState);
            return(RedirectToAction("Edit/" + editUser.Id));
        }
Пример #4
0
        public async Task <ActionResult> Lock(string id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var user = await UserManager.FindByIdAsync(id);

            if (user == null)
            {
                return(HttpNotFound());
            }

            //var helper = new IdentityModelHelper(UserManager, RoleManager);
            //var model = await helper.GetUserDetailsViewModel(id);
            try
            {
                var result = await UserManager.LockUserAccount(id, 1000 * 1000);

                if (!result.Succeeded)
                {
                    AddErrors(result.Errors);
                }
                else
                {
                    //Clear cache
                    CachingHelpers.ClearUserCache();
                    CachingHelpers.ClearUserCache(id);

                    //model = await helper.GetUserDetailsViewModel(id);
                    return(RedirectToAction("Details/" + id, "UsersAdmin"));
                }
            }
            catch (Exception ex)
            {
                AddError(ex);
            }
            //ViewBag.RoleNames = await UserManager.GetRolesAsync(user.Id);
            return(View());
        }
Пример #5
0
        public async Task <ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                userViewModel.Password = Utility.Md5HashingData(userViewModel.Password);
                var user = new ApplicationUser {
                    UserName = userViewModel.UserName, Email = userViewModel.Email, PhoneNumber = userViewModel.PhoneNumber, EmailConfirmed = true
                };

                var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);

                //Clear cache
                CachingHelpers.ClearUserCache();

                //Add User to the selected Roles
                if (adminresult.Succeeded)
                {
                    if (selectedRoles != null)
                    {
                        var result = await UserManager.AddUserToRolesAsync(user.Id, selectedRoles);

                        if (!result.Succeeded)
                        {
                            ModelState.AddModelError("", result.Errors.First());
                            ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                            return(View());
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", adminresult.Errors.First());
                    ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                    return(View());
                }
                return(RedirectToAction("Edit/" + user.Id));
            }
            //ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
            return(View());
        }
Пример #6
0
 public UsersAdminController(IIdentityStore identityStore)
 {
     _identityStore = identityStore;
     //Clear cache
     CachingHelpers.ClearUserCache();
 }