/// <summary> /// Adds a user to the system, only if they do not exist. /// </summary> private static void AddInitialUser(this IDbAppContext context, User initialUser) { User user = context.GetUserBySmUserId(initialUser.SmUserId); if (user != null) { return; } user = new User(); user.Active = true; user.Email = initialUser.Email; user.GivenName = initialUser.GivenName; user.Initials = initialUser.Initials; user.SmAuthorizationDirectory = initialUser.SmAuthorizationDirectory; user.SmUserId = initialUser.SmUserId; user.Surname = initialUser.Surname; District district = null; if (initialUser.District != null) { district = context.GetDistrictByMinistryDistrictId(initialUser.District.MinistryDistrictID); } user.District = district; context.Users.Add(user); context.SaveChanges(); string[] userRoles = initialUser.UserRoles.Select(x => x.Role.Name).ToArray(); if (user.UserRoles == null) { user.UserRoles = new List <UserRole>(); } foreach (string userRole in userRoles) { Role role = context.GetRole(userRole); if (role != null) { user.UserRoles.Add( new UserRole { EffectiveDate = DateTime.UtcNow.Date, Role = role }); } } context.Users.Update(user); context.SaveChanges(); }
private static void AddInitialUser(this IDbAppContext context, User initialUser) { User user = context.GetUserBySmUserId(initialUser.SmUserId); if (user != null) { return; } user = new User { Active = true, Email = initialUser.Email, GivenName = initialUser.GivenName, Initials = initialUser.Initials, SmAuthorizationDirectory = initialUser.SmAuthorizationDirectory, SmUserId = initialUser.SmUserId, Surname = initialUser.Surname }; District district = null; if (initialUser.District != null) { district = context.GetDistrictByMinistryDistrictId(initialUser.District.MinistryDistrictID); } user.District = district; context.Users.Add(user); context.SaveChanges(); string[] userRoles = initialUser.UserRoles.Select(x => x.Role.Name).ToArray(); if (user.UserRoles == null) { user.UserRoles = new List <UserRole>(); } foreach (string userRole in userRoles) { Role role = context.GetRole(userRole); if (role != null) { user.UserRoles.Add( new UserRole { EffectiveDate = DateTime.UtcNow, Role = role }); } } string[] userGroups = initialUser.GroupMemberships.Select(x => x.Group.Name).ToArray(); if (user.GroupMemberships == null) { user.GroupMemberships = new List <GroupMembership>(); } foreach (string userGroup in userGroups) { Group group = context.GetGroup(userGroup); if (group != null) { user.GroupMemberships.Add( new GroupMembership { Active = true, Group = context.GetGroup("Other") }); } } context.Users.Update(user); context.SaveChanges(); }
private static void AddInitialUser(this IDbAppContext context, User initialUser) { User user = context.GetUserBySmUserId(initialUser.SmUserId); if (user != null) { return; } user = new User { Active = true, Email = initialUser.Email, GivenName = initialUser.GivenName, Initials = initialUser.Initials, SmAuthorizationDirectory = initialUser.SmAuthorizationDirectory, SmUserId = initialUser.SmUserId, Surname = initialUser.Surname, AppCreateUserid = SystemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = SystemId, AppLastUpdateTimestamp = DateTime.UtcNow }; District district = null; if (initialUser.District != null) { district = context.GetDistrictByMinistryDistrictId(initialUser.District.MinistryDistrictID); } user.District = district; string[] userRoles = initialUser.UserRoles.Select(x => x.Role.Name).ToArray(); if (user.UserRoles == null) { user.UserRoles = new List <UserRole>(); } foreach (string userRole in userRoles) { Role role = context.GetRole(userRole); if (role != null) { user.UserRoles.Add( new UserRole { EffectiveDate = DateTime.UtcNow, Role = role, AppCreateUserid = SystemId, AppCreateTimestamp = DateTime.UtcNow, AppLastUpdateUserid = SystemId, AppLastUpdateTimestamp = DateTime.UtcNow }); } } context.Users.Add(user); }