public Task MapUserToComplex(long userId, long complexId, long roleId) { var taskResult = Task.Run(() => { using (var context = new DbContext()) { var map = new MapUserToComplex() { UserId = userId, ComplexId = complexId, RoleId = roleId }; context.MapUserToComplexes.Add(map); context.SaveChanges(); } }); return(taskResult); }
public Task <ApplicationUser> CreateComplexUser(ApplicationUser user, long complexId, ApplicationUser currentUser) { var taskResult = Task.Run(async() => { using (var context = new DbContext()) { //check if user exist with username before var existing = context .Users .FirstOrDefault(u => u.PhoneNumber == user.PhoneNumber); if (existing != null) { //Map user to super admin role for the complex. Super Admin = 3 var roleexist = existing.Roles.FirstOrDefault(r => r.RoleId == ProgramCommon.SuperAdmin); if (roleexist == null) { existing.Roles.Add(new ApplicationUserRole() { RoleId = ProgramCommon.SuperAdmin }); } var map = new MapUserToComplex() { UserId = existing.Id, ComplexId = complexId, RoleId = ProgramCommon.SuperAdmin }; context.MapUserToComplexes.Add(map); context.SaveChanges(); return(existing); } else { user.Roles.Add(new ApplicationUserRole() { RoleId = ProgramCommon.Frontend }); //Map user to super admin role for the complex. Super Admin = 3 user.Roles.Add(new ApplicationUserRole() { RoleId = ProgramCommon.SuperAdmin }); var passwordHash = new PasswordHasher(); user.PasswordHash = passwordHash.HashPassword(user.PhoneNumber); user.SecurityStamp = Guid.NewGuid().ToString(); await base.CreateAsync(user); //await base.AddToRoleAsync(user, "Frontend"); var map = new MapUserToComplex() { UserId = user.Id, ComplexId = complexId, RoleId = ProgramCommon.SuperAdmin }; context.MapUserToComplexes.Add(map); context.SaveChanges(); return(user); } } }); return(taskResult); }