private void CreateRoot(UnitOfWork unitOfWork) { var employeesCreator = new EmployeeCreator(unitOfWork, _auth, _repositoriesFactory); var coursesProvider = new CoursesProvider(unitOfWork, _repositoriesFactory); Console.WriteLine("\nTworzenie użytkownika root"); Console.WriteLine("\nPodaj numer kierunku z listy do której przynależy użytkownik root:"); int courseId = int.Parse(Console.ReadLine()); var course = coursesProvider.GetCourse(courseId); Console.WriteLine("\nPodaj nazwę użytkownika do logowania:"); string userName = Console.ReadLine(); Console.WriteLine("\nPodaj hasło do logowania:"); string password = Console.ReadLine(); Console.WriteLine("\nPodaj adres email:"); string email = Console.ReadLine(); Console.WriteLine("\nPodaj imię:"); string firstName = Console.ReadLine(); Console.WriteLine("\nPodaj nazwisko:"); string lastName = Console.ReadLine(); Console.WriteLine("\nPodaj pytanie do przywracania hasła:"); string question = Console.ReadLine(); Console.WriteLine("\nPodaj odpowiedź na pytanie:"); string answer = Console.ReadLine(); EmployeeCreateStatus status = EmployeeCreateStatus.None; employeesCreator.Create(userName, firstName, lastName, password, email, question, answer, true, true, true, course, out status); }
public Employee Create(string userName, string firstName, string lastName, string password, string email, string question, string answer, bool moderator, bool root, bool administrator, Course course, out EmployeeCreateStatus status) { status = EmployeeCreateStatus.None; try { var s = _authenticationService.Register(userName, password, email, question, answer, true); status = ConvertStatus(s); if (status == EmployeeCreateStatus.Success) { _unitOfWork.BeginTransaction(); if (administrator) { _authenticationService.RoleService.AddUserToRole(userName, EmployeesRoles.Administrator); } if (moderator) { _authenticationService.RoleService.AddUserToRole(userName, EmployeesRoles.Moderator); } if (root) { _authenticationService.RoleService.AddUserToRole(userName, EmployeesRoles.Root); } var employeesRepository = _repositoriesFactory.CreateEmployeeRepository(_unitOfWork); var userId = _authenticationService.MembershipService.GetUserProfileByName(userName).Id; var employee = employeesRepository.FindBy(userId); employee.FirstName = firstName; employee.LastName = lastName; employee.Email = email; employee.Moderator = moderator; employee.Root = root; employee.Administrator = administrator; employee.Course = course; _unitOfWork.Commit(); return(employee); } return(null); } catch (Exception e) { if (status == EmployeeCreateStatus.Success) { _unitOfWork.Rollback(); if (_authenticationService.RoleService.IsUserInRole(userName, EmployeesRoles.Administrator)) { _authenticationService.RoleService.RemoveUserFromRole(userName, EmployeesRoles.Administrator); } if (_authenticationService.RoleService.IsUserInRole(userName, EmployeesRoles.Root)) { _authenticationService.RoleService.RemoveUserFromRole(userName, EmployeesRoles.Root); } if (_authenticationService.RoleService.IsUserInRole(userName, EmployeesRoles.Moderator)) { _authenticationService.RoleService.RemoveUserFromRole(userName, EmployeesRoles.Moderator); } _authenticationService.MembershipService.DeleteUser(userName); } throw e; } }