public void DeleteEmployee(int EmployeeId) { using (HeadSpringDb db = new HeadSpringDb()) { Employee employee = db.Employees.SingleOrDefault(x => x.EmployeeId == EmployeeId); if (employee != null) { if (string.IsNullOrEmpty(employee.UserId)) { ApplicationDbContext context = new ApplicationDbContext(); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); ApplicationUser user = UserManager.FindById(employee.UserId); var roles = UserManager.GetRoles(user.Id); if (roles.Any()) { UserManager.RemoveFromRolesAsync(user.Id, roles.ToArray()); } UserManager.Delete(user); } db.Employees.Remove(employee); db.SaveChanges(); } } }
public void CreateIdentityUser(EmployeeDto employeeDto) { using (var tScope = TransactionFactory.GetScope(90)) { using (HeadSpringDb db = new HeadSpringDb()) { ApplicationDbContext context = new ApplicationDbContext(); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); ApplicationUser user = new ApplicationUser(); if (!Utilities.IsValidEmail(employeeDto.Email)) { throw new BusinessException(Global.Email_Invalid_Error); } if (employeeDto.EmployeeId <= 0 || employeeDto.EmployeeId == null) { Employee employee = new Employee { Name = employeeDto.Name, LastName = employeeDto.LastName, MotherLastName = employeeDto.MotherLastName, Email = employeeDto.Email, Phone = employeeDto.Phone, JobTitle = employeeDto.JobTitle, Location = employeeDto.Location, Active = employeeDto.Active }; if (employeeDto.RequiresUser) { if (db.AspNetUsers.Any(x => x.UserName == employeeDto.Email)) { throw new BusinessException(Global.User_Exists_Error); } if (string.IsNullOrEmpty(employeeDto.RoleName)) { throw new BusinessException(Global.Role_Error_Required); } user.UserName = employeeDto.Email; user.Email = employeeDto.Email; var chkUser = UserManager.Create(user, ConfigurationManager.AppSettings[ConfigKey.EmailPassword].ToString()); if (chkUser.Succeeded) { employee.UserId = user.Id; var result1 = UserManager.AddToRole(user.Id, employeeDto.RoleName); } else { throw new BusinessException(Global.User_Invalid_Error); } } db.Employees.Add(employee); } else { Employee employee = db.Employees.SingleOrDefault(x => x.EmployeeId == employeeDto.EmployeeId); string roleId = string.Empty; AspNetRole role; employee.Name = employeeDto.Name; employee.LastName = employeeDto.LastName; employee.MotherLastName = employeeDto.MotherLastName; employee.Phone = employeeDto.Phone; employee.Location = employeeDto.Location; employee.JobTitle = employeeDto.JobTitle; employee.Active = employeeDto.Active; //Updating Role if (!string.IsNullOrEmpty(employee.UserId) && employeeDto.RequiresUser) { user = UserManager.FindById(employeeDto.UserId); if (user.Roles.Any()) { roleId = user.Roles.SingleOrDefault().RoleId; role = db.AspNetRoles.SingleOrDefault(x => x.Id == roleId); if (role.Name != employeeDto.RoleName) { UserManager.RemoveFromRole(user.Id, role.Name); UserManager.AddToRole(user.Id, employeeDto.RoleName); } } } //Creating user for existing employee if (employeeDto.RequiresUser && string.IsNullOrEmpty(employee.UserId)) { if (db.AspNetUsers.Any(x => x.UserName == employeeDto.Email)) { throw new BusinessException(Global.User_Exists_Error); } if (string.IsNullOrEmpty(employeeDto.RoleName)) { throw new BusinessException(Global.Role_Error_Required); } user.UserName = employeeDto.Email; user.Email = employeeDto.Email; var chkUser = UserManager.Create(user, ConfigurationManager.AppSettings[ConfigKey.EmailPassword].ToString()); if (chkUser.Succeeded) { employee.UserId = user.Id; var result1 = UserManager.AddToRole(user.Id, employeeDto.RoleName); } else { throw new BusinessException(Global.User_Invalid_Error); } } //Deleting user for existing employee else if (!employeeDto.RequiresUser && !string.IsNullOrEmpty(employee.UserId)) { employee.UserId = string.Empty; user = UserManager.FindById(employeeDto.UserId); if (user.Roles.Any()) { roleId = user.Roles.SingleOrDefault().RoleId; role = db.AspNetRoles.SingleOrDefault(x => x.Id == roleId); UserManager.AddToRole(user.Id, role.Name); } UserManager.Delete(user); } } db.SaveChanges(); } tScope.Complete(); } }
public void CreateRolesAndDefaultAdminUser() { using (TransactionScope tscope = new TransactionScope()) { using (HeadSpringDb db = new HeadSpringDb()) { ApplicationDbContext context = new ApplicationDbContext(); var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); if (!roleManager.RoleExists(ConfigurationManager.AppSettings[ConfigKey.AdminRole])) { var role = new IdentityRole(); role.Name = ConfigurationManager.AppSettings[ConfigKey.AdminRole]; roleManager.Create(role); var user = new ApplicationUser(); user.UserName = ConfigurationManager.AppSettings[ConfigKey.EmailUsername].ToString(); user.Email = ConfigurationManager.AppSettings[ConfigKey.EmailUsername].ToString(); var chkUser = UserManager.Create(user, ConfigurationManager.AppSettings[ConfigKey.EmailPassword].ToString()); if (chkUser.Succeeded) { var result1 = UserManager.AddToRole(user.Id, ConfigurationManager.AppSettings[ConfigKey.AdminRole].ToString()); Employee employee = new Employee { UserId = user.Id, Name = "Administrator", LastName = "Admin", MotherLastName = ".", Email = ConfigurationManager.AppSettings[ConfigKey.EmailUsername].ToString(), JobTitle = "Administrator", Location = "Monterrey", Phone = "88888888", Active = true }; db.Employees.Add(employee); } } if (!roleManager.RoleExists(ConfigurationManager.AppSettings[ConfigKey.HRRole].ToString())) { var role = new IdentityRole(); role.Name = ConfigurationManager.AppSettings[ConfigKey.HRRole]; roleManager.Create(role); } if (!roleManager.RoleExists(ConfigurationManager.AppSettings[ConfigKey.InfoRole].ToString())) { var role = new IdentityRole(); role.Name = ConfigurationManager.AppSettings[ConfigKey.InfoRole]; roleManager.Create(role); } db.SaveChanges(); } tscope.Complete(); } }