private async Task <bool> SetLockout(UserAccountInfoViewModel model, TicketDeskUser user)
        {
            var currentlyLocked   = UserManager.IsLockedOut(model.User.Id);
            var currentlyDisabled = currentlyLocked && (user.LockoutEndDateUtc ?? DateTime.MinValue).ToUniversalTime() == DateTime.MaxValue.ToUniversalTime();

            if (currentlyDisabled != model.IsDisabled || currentlyLocked != model.IsLocked)
            {
                DateTimeOffset newLockoutDate = DateTimeOffset.MinValue;
                if (model.IsDisabled)
                {
                    newLockoutDate = DateTimeOffset.MaxValue;
                }
                else if (model.IsLocked)
                {
                    newLockoutDate = DateTimeOffset.Now.Add(UserManager.DefaultAccountLockoutTimeSpan);
                }

                var result = await UserManager.SetLockoutEndDateAsync(model.User.Id, newLockoutDate);

                if (!result.Succeeded)
                {
                    AddErrors(result);
                }
                return(result.Succeeded);
            }
            return(true);
        }
        public async Task <ActionResult> Edit(UserAccountInfoViewModel model)
        {
            var tdPendingRole = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");

            ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id;
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await UserManager.FindByIdAsync(model.User.Id);

            //deal with locks first
            if (await SetLockout(model, user))
            {
                //do user info updates
                if (await UpdateUserInfo(user, model))
                {
                    //if any roles other than pending are selected, make sure we remove the pending role from the model first
                    if (tdPendingRole != null)
                    {
                        if (model.Roles.Any(r => r != tdPendingRole.Id))
                        {
                            var mRoles = model.Roles.ToList();
                            mRoles.Remove(tdPendingRole.Id);
                            model.Roles = mRoles;
                        }
                    }

                    //get role changes
                    var roleIdsToRemove = user.Roles.Select(ur => ur.RoleId).Except(model.Roles).ToList();
                    var roleIdsToAdd    = model.Roles.Except(user.Roles.Select(ur => ur.RoleId)).ToList();
                    //do role removes
                    if (await RemoveRoles(roleIdsToRemove, user))
                    {
                        //do role adds
                        if (await AddRoles(roleIdsToAdd, user))
                        {
                            //everything worked, return to index (if anything failed, will return to the edit user view)
                            return(RedirectToAction("Index"));
                        }
                    }
                }
            }
            //Since the above operations could be partially committed before a failure,
            //  we will re-get the user so we are sure the screen will accurately show
            //  the current data. Model errors will have been added by the respective
            //  update operations that failed
            user = await UserManager.FindByIdAsync(model.User.Id);

            model = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
            return(View(model));
        }
        private async Task <bool> UpdateUserInfo(TicketDeskUser user, UserAccountInfoViewModel model)
        {
            user.UserName    = model.User.Email;
            user.Email       = model.User.Email;
            user.DisplayName = model.User.DisplayName;
            var result = await UserManager.UpdateAsync(user);

            if (!result.Succeeded)
            {
                AddErrors(result);
            }
            return(result.Succeeded);
        }
        public async Task <ActionResult> Edit(string id)
        {
            //ViewBag.AllRolesList = await RoleManager.Roles.ToListAsync();
            var user = await UserManager.FindByIdAsync(id);

            if (user == null)
            {
                return(RedirectToAction("Index"));
            }
            var model         = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
            var tdPendingRole = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");

            ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id;
            return(View(model));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Edit(string id)
        {
            //ViewBag.AllRolesList = await RoleManager.Roles.ToListAsync();
            var user = await UserManager.FindByIdAsync(id);

            if (user == null)
            {
                return(RedirectToAction("Index"));
            }
            var model            = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
            var tdPendingRole    = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");
            var tdAdministrators = RoleManager.Roles.ToList().Where(w => user.Roles.Select(s => s.RoleId).Contains(w.Id)).Select(s => s.Name).Where(w => w == "TdAdministrators").FirstOrDefault();

            if (tdAdministrators != null)
            {
                ViewBag.tdAdministrators = true;
            }

            ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id;
            return(View(model));
        }
        public IViewComponentResult Invoke()
        {
            var user = _context.ApplicationUsers.FirstOrDefault(x => x.UserName == User.Identity.Name);

            if (user == null)
            {
                throw new Exception("User not found!");
            }

            var userAccountInfo = new UserAccountInfoViewModel
            {
                UserName      = user.UserName,
                UserAvatarURL = user.AvatarURL,
                UserEmail     = user.Email,
                Updates       = 42,
                Messages      = 42,
                Tasks         = 42,
                Comments      = 42,
                Payments      = 42,
                Projects      = 42
            };

            return(View("UserAccountInfo", userAccountInfo));
        }
        public async Task <ActionResult> Edit(UserAccountInfoViewModel model)
        {
            var tdPendingRole = RoleManager.Roles.FirstOrDefault(r => r.Name == "TdPendingUsers");

            ViewBag.TdPendingUsersRoleId = tdPendingRole == null ? "" : tdPendingRole.Id;
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = await UserManager.FindByIdAsync(model.User.Id);

            var demoMode = (ConfigurationManager.AppSettings["ticketdesk:DemoModeEnabled"] ?? "false").Equals("true", StringComparison.InvariantCultureIgnoreCase);

            if (demoMode && model.User.Email.EndsWith("@example.com", StringComparison.InvariantCultureIgnoreCase))
            {
                ModelState.AddModelError("Profile", Strings.UnableToChangeDemoUser);
            }
            else
            {
                //deal with locks first
                if (await SetLockout(model, user))
                {
                    //do user info updates
                    if (await UpdateUserInfo(user, model))
                    {
                        //if any roles other than pending are selected, make sure we remove the pending role from the model first
                        if (tdPendingRole != null)
                        {
                            if (model.Roles.Any(r => r != tdPendingRole.Id))
                            {
                                var mRoles = model.Roles.ToList();
                                mRoles.Remove(tdPendingRole.Id);
                                model.Roles = mRoles;
                            }
                        }

                        //get role changes
                        var roleIdsToRemove = user.Roles.Select(ur => ur.RoleId).Except(model.Roles).ToList();

                        if (User.Identity.GetUserId() == user.Id)
                        {
                            //stop current user from removing their own admin role
                            roleIdsToRemove = roleIdsToRemove.Except(RoleManager.Roles.Where(r => r.Name == "tdAdministrators").Select(r => r.Id)).ToList();
                        }


                        var roleIdsToAdd = model.Roles.Except(user.Roles.Select(ur => ur.RoleId)).ToList();
                        //do role removes
                        if (await RemoveRoles(roleIdsToRemove, user))
                        {
                            //do role adds
                            if (await AddRoles(roleIdsToAdd, user))
                            {
                                //everything worked, return to index (if anything failed, will return to the edit user view)
                                return(RedirectToAction("Index"));
                            }
                        }
                    }
                }
            }
            //Since the above operations could be partially committed before a failure,
            //  we will re-get the user so we are sure the screen will accurately show
            //  the current data. Model errors will have been added by the respective
            //  update operations that failed
            user = await UserManager.FindByIdAsync(model.User.Id);

            model = new UserAccountInfoViewModel(user, user.Roles.Select(r => r.RoleId));
            return(View(model));
        }