Ejemplo n.º 1
0
        public async Task <IActionResult> Register(EmployeeWithPassword employeeWithPassword)
        {
            var employee = employeeWithPassword.Employee;

            if (string.IsNullOrWhiteSpace(employee.Name) ||
                _db.Employee.Any(c => c.Name == employee.Name))
            {
                return(View(employeeWithPassword));
            }
            if (await _userManager.FindByEmailAsync(employee.Email) != null)
            {
                ModelState.AddModelError("Email", "An account with that email already exists.");
                return(View(employeeWithPassword));
            }

            if (employee.Name.Contains(' '))
            {
                ModelState.AddModelError("White", "Name can not contain spaces.");
                return(View(employeeWithPassword));
            }

            if (!_db.Employee.Any(e => e.Name == employee.Name ||
                                  e.Email == employee.Email))
            {
                ModelState.AddModelError("Found",
                                         "Employee found with matching name or email, please try again.");
                return(View(employeeWithPassword));
            }

            _db.AddEmployee(employee);

            var user = new AppUser
            {
                UserName = employee.Name,
                Email    = employee.Email
            };
            var result = await _userManager.CreateAsync(user, employeeWithPassword.Password);

            if (result.Succeeded)
            {
                var newUser = await _userManager.FindByEmailAsync(employee.Email);

                await _userManager.AddToRoleAsync(newUser, "User");

                await _userManager.AddToRoleAsync(newUser, "Employee");

                return(RedirectToAction("Account", "Account"));
            }

            //Return to Create page if create failed
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(error.Code, error.Description);
            }

            return(View(employeeWithPassword));
        }
Ejemplo n.º 2
0
        public async System.Threading.Tasks.Task <IActionResult> Register(EmployeeWithPassword employeeWithPassword)
        {
            if (ModelState.IsValid)
            {
                Employee employee = employeeWithPassword.Employee;
                if (repository.Employees.Any(c => c.Email == employee.Email))
                {
                    ModelState.AddModelError("", "Email must be unique");
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        AppUser user = new AppUser
                        {
                            // email and username are synced - this is by choice
                            Email    = employee.Email,
                            UserName = employee.Email
                        };
                        // Add user to Identity DB
                        IdentityResult result = await userManager.CreateAsync(user, employeeWithPassword.Password);

                        if (!result.Succeeded)
                        {
                            AddErrorsFromResult(result);
                        }
                        else
                        {
                            // Assign user to employees Role
                            result = await userManager.AddToRoleAsync(user, "Employee");

                            if (!result.Succeeded)
                            {
                                // Delete User from Identity DB
                                await userManager.DeleteAsync(user);

                                AddErrorsFromResult(result);
                            }
                            else
                            {
                                // Create employee (Northwind)
                                repository.AddEmployee(employee);
                                return(RedirectToAction("Index", "Home"));
                            }
                        }
                    }
                }
            }
            return(View());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Register(EmployeeWithPassword employeeWithPassword, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Employees employee = employeeWithPassword.Employee;
                repository.AddEmployee(employee);
                if (ModelState.IsValid)
                {
                    EmployeeUser user = new EmployeeUser
                    {
                        UserName = employee.EmployeeId.ToString()
                    };
                    // Add user to Identity DB
                    IdentityResult result = await userManager.CreateAsync(user, employeeWithPassword.Password);

                    if (!result.Succeeded)
                    {
                        AddErrorsFromResult(result);
                    }
                    else
                    {
                        // Assign user to employee Role
                        result = await userManager.AddToRoleAsync(user, "Employee");

                        if (!result.Succeeded)
                        {
                            // Delete User from Identity DB
                            await userManager.DeleteAsync(user);

                            repository.DeleteEmployee(employee);
                            AddErrorsFromResult(result);
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
            }
            return(View("Index", new ViewEmployeesModel
            {
                EmployeeUsers = userManager.Users,
                Employees = repository.Employees
            }));
        }