public SimplUser(Entity e) { if (e != null) { EntityId = e.EntityId; LastName = e.EntityName1; if (e.EntityName2 != null) FirstName = e.EntityName2; if (e.EntityName1 != "Unauthenticated User") { using (var db = DBContextFactory.CreateContext()) { var uniqueId = db.UniqueIds.FirstOrDefault(u => u.EntityId == e.EntityId); if (uniqueId != null) UniqueId = db.UniqueIds.Any(u => u.EntityId == e.EntityId) ? uniqueId.UniqueIdValue : "N/A"; } } } else { EntityId = 0; LastName = "Null"; FirstName = "Null"; UniqueId = "N/A"; } }
/// <summary> /// AddUpdateCurrentUser method /// </summary> /// <param name="asppUser"></param> /// <param name="asppGroups"></param> /// <param name="simplUser"></param> /// <param name="db"></param> public void AddUpdateCurrentUser(ASPP_Users asppUser, List<ASPP_Groups> asppGroups, Entity simplUser, SIMPLEntities db) { // import user information from ASPP db simplUser.EntityName1 = asppUser.Last_Name; simplUser.EntityName2 = asppUser.First_Name; simplUser.EntityTypeId = DetermineEntityType(asppUser, asppGroups, db); if (simplUser.EntityTypeId == 0) throw new ASPPException("No roles defined for user"); simplUser.Cbr = asppUser.Phone != null ? Regex.Replace(asppUser.Phone, "[^0-9]", "") : ""; simplUser.Email = asppUser.Email; }
/// <summary> /// ToggleActiveRoles method /// </summary> /// <param name="simplRoles"></param> /// <param name="currentAsppRoles"></param> /// <param name="simplUser"></param> /// <param name="sdb"></param> public void ToggleActiveRoles(IEnumerable<UserRole> simplRoles, List<int> currentAsppRoles, Entity simplUser, SIMPLEntities sdb) { // for each role in simpl db make active if current in ASPP, otherwise make inactive foreach (var role in simplRoles) { role.IsActive = currentAsppRoles.Contains(role.RoleType); } }
/// <summary> /// SyncUniqueId method /// </summary> /// <param name="simplUser"></param> /// <param name="db"></param> /// <param name="userId"></param> public void SyncUniqueId(Entity simplUser, SIMPLEntities db, string userId) { //see if id exists in unique ids table. if not, add it var id = db.UniqueIds.FirstOrDefault(u => u.UniqueIdValue == userId); if (id != null) return; id = new UniqueId { EntityId = simplUser.EntityId, UniqueIdTypeId = RegexValidator.DetermineUniqueIdType(userId), UniqueIdValue = userId }; db.UniqueIds.AddObject(id); }
/// <summary> /// Updates (via inserting new rows or deactivating current rows) the simpl db roleTypes table with a users current permissions, /// which are based on the ASPP_Users GroupList /// </summary> /// <param name="asppGroups"></param> /// <param name="simplUser"></param> /// <param name="db"></param> public void SyncRoles(IEnumerable<ASPP_Groups> asppGroups, Entity simplUser, SIMPLEntities db) { // get aspp groupd IDs and translate to SIMPL roles in SIMPL db var asppGroupIntegers = asppGroups.Select(g => g.Group_ID).ToList(); // populate list of simpl roles that correlate to CURRENT group assignments in ASPP. var myCurrentRoleTypes = db.ASPPGroupsTranslations.Where(r => asppGroupIntegers .Contains(r.ASPP_Group_Id)).Select(r => r.SIMPL_RoleType_Id).ToList(); // If no roles exist for user, insert all, else do partial insert, trim removed roles, and add new ones if (db.UserRoles.Count(u => u.EntityId == simplUser.EntityId) <= 0) { // no roles exist, so insert all InsertSimplRoles(myCurrentRoleTypes, simplUser, db); } else { // Roles currently exist, so do a partial insert of translated groupIds which don't exist in UserRoles table, activate, then deactivate // First, get list of roles coming from ASPP not currently in SIMPL db var simplRoles = db.UserRoles.Where(r => r.EntityId == simplUser.EntityId).ToList(); var simplRoleIds = simplRoles.Select(r => r.RoleType).ToList(); var roleIdsToInsert = myCurrentRoleTypes.Where(i => !simplRoleIds.Contains(i)).ToList(); // then, perform updates InsertSimplRoles(roleIdsToInsert, simplUser, db); ToggleActiveRoles(simplRoles, myCurrentRoleTypes, simplUser, db); } }
/// <summary> /// Updates the integer LocId value on an Entity entity based on the company_name /// field in the ASPP_Users database via a translation table in the simpl db /// </summary> /// <param name="asppUser"></param> /// <param name="simplUser"></param> /// <param name="db"></param> public void SyncLocation(ASPP_Users asppUser, Entity simplUser, SIMPLEntities db) { var loc = db.ASPPLocTranslations.FirstOrDefault(l => l.ASPP_Loc == asppUser.Company_Name); if (loc == null) // User's aspp_location does not exist in the simpl translation table, so throw error and stop run. throw new ASPPException("User's location does not exist/is out-of-date. Please submit a support ticket to resolve."); // User's aspp_location exists in translation table, so set loc_id to corresponding simpl LocId (int) simplUser.LocId = loc.SIMPL_LocId; }
/// <summary> /// InsertSimplRoles method /// </summary> /// <param name="simplRoleList"></param> /// <param name="simplUser"></param> /// <param name="sdb"></param> public void InsertSimplRoles(IEnumerable<int> simplRoleList, Entity simplUser, SIMPLEntities sdb) { foreach (var roleTypeId in simplRoleList) { var newRole = new UserRole { EntityId = simplUser.EntityId, RoleType = roleTypeId, IsActive = true }; sdb.UserRoles.AddObject(newRole); } }
/// <summary> /// FullUserImport method /// </summary> /// <param name="ASPPuser"></param> /// <param name="asppGroups"></param> /// <param name="userId"></param> public void FullUserImport(ASPP_Users ASPPuser, List<ASPP_Groups> asppGroups, string userId) { using (var db = DBContextFactory.CreateContext()) { var newUser = new Entity(); AddUpdateCurrentUser(ASPPuser, asppGroups, newUser, db); db.Entities.AddObject(newUser); SyncLocation(ASPPuser, newUser, db); SyncRoles(asppGroups, newUser, db); SyncUniqueId(newUser, db, userId); db.SaveChanges(); } }