public static InvitationToken Create(DbContext db, String email, User inviter, Organization organization, List<String> roles, List<Region> regions) { var UserStore = new CUserStore<User>(db); var UserManager = new UserManager<User>(UserStore); var RoleManager = new RoleManager<Role>(new CRoleStore<Role>(db)); InvitationToken result = new InvitationToken(); while(result.Token == null) { var token = GenerateToken(); var exists = db.Set<InvitationToken>().Any(i => i.Token == token); if (!exists) result.Token = token; } User user = new User(); user.Email = email; user.UserName = email; user.fkOrganizationId = organization.Id; user.Id = Guid.NewGuid().ToString(); user.Organization = organization; var userRes = UserManager.Create(user); if (!userRes.Succeeded) throw new ApplicationException(userRes.Errors.First()); result.User = user; result.fkInviterId = inviter.Id; result.fkUserId = user.Id; db.Set<InvitationToken>().Add(result); db.SaveChanges(); result.Inviter = inviter; result.User = user; foreach(var role in roles) { var res = UserManager.AddToRole(user.Id, role); Console.WriteLine(res); } foreach(var region in regions) { var scope = new UserScope { fkUserId = user.Id, fkRegionId = region.Id }; db.Set<UserScope>().Add(scope); } db.SaveChanges(); return result; }
public static InvitationToken Create(DbContext db, String email, User inviter, Organization organization, List<String> roles, List<Region> regions) { var UserStore = new CUserStore<User>(db); var UserManager = new UserManager<User>(UserStore); var RoleManager = new RoleManager<Role>(new CRoleStore<Role>(db)); InvitationToken result = new InvitationToken(); result.Token = GenerateToken(); User user = new User(); user.Email = email; user.UserName = email; user.fkOrganizationId = organization.Id; user.Id = Guid.NewGuid().ToString(); user.Organization = organization; UserManager.Create(user); result.User = user; result.fkInviterId = inviter.Id; result.fkUserId = user.Id; db.Set<InvitationToken>().Add(result); db.SaveChanges(); result.Inviter = inviter; result.User = user; foreach(var role in roles) { var res = UserManager.AddToRole(user.Id, role); Console.WriteLine(res); } foreach(var region in regions) { var scope = new UserScope { fkUserId = user.Id, fkRegionId = region.Id }; db.Set<UserScope>().Add(scope); } db.SaveChanges(); return result; }
public static InvitationToken Create(DbContext db, String email, User inviter, Organization organization, List <String> roles, List <Region> regions) { var UserStore = new CUserStore <User>(db); var UserManager = new UserManager <User>(UserStore); var RoleManager = new RoleManager <Role>(new CRoleStore <Role>(db)); InvitationToken result = new InvitationToken(); while (result.Token == null) { var token = GenerateToken(); var exists = db.Set <InvitationToken>().Any(i => i.Token == token); if (!exists) { result.Token = token; } } User user = new User(); user.Email = email; user.UserName = email; user.fkOrganizationId = organization.Id; user.Id = Guid.NewGuid().ToString(); user.Organization = organization; var userRes = UserManager.Create(user); if (!userRes.Succeeded) { throw new ApplicationException(userRes.Errors.First()); } result.User = user; result.fkInviterId = inviter.Id; result.fkUserId = user.Id; db.Set <InvitationToken>().Add(result); db.SaveChanges(); result.Inviter = inviter; result.User = user; foreach (var role in roles) { var res = UserManager.AddToRole(user.Id, role); Console.WriteLine(res); } foreach (var region in regions) { var scope = new UserScope { fkUserId = user.Id, fkRegionId = region.Id }; db.Set <UserScope>().Add(scope); } db.SaveChanges(); return(result); }