Пример #1
0
        public async Task <ActionResult> Edit(string employee)
        {
            var allJobs = jobService.GetAsync();
            var roles   = await employeeService.GetAllRoles();

            var emp = await employeeService.GetSingleEmployeeAsync(employee);

            var vm = new EditEmployeeViewModel()
            {
                SelectedRole   = emp.Role,
                AvailableRoles = roles,
                Email          = emp.UserName,
                FirstName      = emp.First,
                LastName       = emp.Last,
                IsExempt       = emp.IsExempt
            };


            return(View("Edit", vm));
        }
Пример #2
0
        public async Task <ActionResult> EditEmployee(EditEmployeeViewModel employee)
        {
            if (ModelState.IsValid)
            {
                var res = await updateEmployeeCommand.UpdateAsync(employee);

                if (res.Successful)
                {
                    NotificationsController.AddNotification(this.User.SafeUserName(), $"{employee.Email} has been updated.");
                    return(RedirectToAction(nameof(Edit), new { employee = employee.Email }));
                }
                else
                {
                    foreach (var err in res.Errors)
                    {
                        ModelState.AddModelError(string.Empty, err);
                    }
                }
            }

            employee.AvailableRoles = await employeeService.GetAllRoles();

            return(View("Edit", employee));
        }
Пример #3
0
        public async Task <Result> UpdateAsync(EditEmployeeViewModel employee)
        {
            var allCommandErrors = new List <string>();
            var userRole         = employee.SelectedRole;

            var userToUpdate = await userManager.FindByNameAsync(employee.Email);

            var passwordChange = !string.IsNullOrWhiteSpace(employee.Password) &&
                                 employee.Password.Equals(employee.PasswordConfirm);

            if (passwordChange)
            {
                userToUpdate.PasswordHash = userManager.PasswordHasher.HashPassword(userToUpdate, employee.Password);
                var resulty = await userManager.UpdateAsync(userToUpdate);

                allCommandErrors.AddRange(resulty.Errors.Select(err => $"{err.Code}-{err.Description}"));
            }

            if (!allCommandErrors.Any())
            {
                var existingRoles = await userManager.GetRolesAsync(userToUpdate);

                var hasDifferentRolesThenDesired = existingRoles == null || !existingRoles.Any() || existingRoles.Any(x => x != employee.SelectedRole);

                var hasNewEmail  = !string.IsNullOrWhiteSpace(employee.NewEmail) && employee.Email != employee.NewEmail;
                var updatedEmail = hasNewEmail ? employee.NewEmail : null;
                if (hasNewEmail)
                {
                    userToUpdate.Email    = employee.NewEmail;
                    userToUpdate.UserName = employee.NewEmail;
                    await userManager.UpdateAsync(userToUpdate);
                }

                if (hasDifferentRolesThenDesired)
                {
                    var res1 = await userManager.RemoveFromRolesAsync(userToUpdate, existingRoles);

                    allCommandErrors.AddRange(res1.Errors.Select(err => $"{err.Code}-{err.Description}"));
                    if (res1.Succeeded)
                    {
                        if (employee.SelectedRole == UserRoleName.Disabled)
                        {
                            userToUpdate.LockoutEnabled = true;
                            userToUpdate.LockoutEnd     = DateTime.Now.AddYears(100);
                            var res2 = await userManager.UpdateAsync(userToUpdate);

                            allCommandErrors.AddRange(res2.Errors.Select(err => $"{err.Code}-{err.Description}"));
                            if (res2.Succeeded)
                            {
                                await SaveLocalEmployeeInfo(employee, updatedEmail);
                            }
                        }
                        else
                        {
                            var res2 = await userManager.AddToRoleAsync(userToUpdate, employee.SelectedRole);

                            allCommandErrors.AddRange(res2.Errors.Select(err => $"{err.Code}-{err.Description}"));
                            if (res2.Succeeded)
                            {
                                await SaveLocalEmployeeInfo(employee, updatedEmail);
                            }
                        }
                    }
                }
                else
                {
                    await SaveLocalEmployeeInfo(employee, updatedEmail);
                }
            }

            return(new Result(allCommandErrors.Any(), allCommandErrors.ToArray()));
        }