public void GenerateUserAndContainerSnapshotTest() { TestRoleData roleData = SCObjectGenerator.PrepareTestRoleWithOrgAndGroup(); SCConditionCalculator calculator = new SCConditionCalculator(); ProcessProgress.Clear(); ProcessProgress.Current.RegisterResponser(TestProgressResponser.Instance); calculator.GenerateUserAndContainerSnapshot(new List <ISCUserContainerObject>() { roleData.Group }); calculator.GenerateUserAndContainerSnapshot(new List <ISCUserContainerObject>() { roleData.Role }); SameContainerUserAndContainerSnapshotCollection snapshot = UserAndContainerSnapshotAdapter.Instance.LoadByContainerID(roleData.Role.ID); Console.WriteLine(roleData.ToString()); Console.Error.WriteLine("Error: {0}", ProcessProgress.Current.GetDefaultError()); Console.WriteLine("Output: {0}", ProcessProgress.Current.GetDefaultOutput()); Assert.IsTrue(snapshot.ContainsKey(roleData.UserInGroup.ID)); Assert.IsTrue(snapshot.ContainsKey(roleData.UserInOrg.ID)); Assert.IsTrue(snapshot.ContainsKey(roleData.UserInRole.ID)); Assert.IsTrue(snapshot.ContainsKey(roleData.UserInConditionRole.ID)); Assert.IsTrue(snapshot.ContainsKey(roleData.UserInConditionGroup.ID)); }
/// <summary> /// 合并用户信息,一个事物内更新所有的 /// </summary> /// <param name="users"></param> public void BatchMerge(string containerID, string containerSchemaType, SchemaObjectCollection users) { containerID.CheckStringIsNullOrEmpty("containerID"); containerSchemaType.CheckStringIsNullOrEmpty("containerSchemaType"); users.NullCheck("users"); SameContainerUserAndContainerSnapshotCollection existedData = this.LoadByContainerID(containerID); SameContainerUserAndContainerSnapshotCollection insertData = GetInsertData(containerID, containerSchemaType, existedData, users); SameContainerUserAndContainerSnapshotCollection updateData = GetUpdateData(containerID, containerSchemaType, existedData, users); string sqlInsert = this.PrepareUpdateSql(insertData); string sqlUpdate = this.PrepareUpdateSql(updateData); using (TransactionScope scope = TransactionScopeFactory.Create()) { DateTime dtInsert = (DateTime)DbHelper.RunSqlReturnScalar(sqlInsert, this.GetConnectionName()); SCActionContext.Current.TimePoint.IsMinValue(() => SCActionContext.Current.TimePoint = dtInsert); DateTime dtUpdate = (DateTime)DbHelper.RunSqlReturnScalar(sqlUpdate, this.GetConnectionName()); SCActionContext.Current.TimePoint.IsMinValue(() => SCActionContext.Current.TimePoint = dtUpdate); scope.Complete(); } }
private static SameContainerUserAndContainerSnapshotCollection GetUpdateData(string containerID, string containerSchemaType, SameContainerUserAndContainerSnapshotCollection existedData, SchemaObjectCollection users) { SameContainerUserAndContainerSnapshotCollection updatedInfo = new SameContainerUserAndContainerSnapshotCollection(); foreach (UserAndContainerSnapshot uacs in existedData) { if (users.ContainsKey(uacs.UserID)) { //原来是已删除的,现在改为Normal if (uacs.Status != SchemaObjectStatus.Normal) { uacs.Status = SchemaObjectStatus.Normal; updatedInfo.Add(uacs); } } else { //现在不存在了,状态需要改为已删除 uacs.Status = SchemaObjectStatus.Deleted; updatedInfo.Add(uacs); } } return(updatedInfo); }
public void AddUserToGroupExecutorTest() { SCGroup group = SCObjectGenerator.PrepareGroupObject(); SCObjectOperations.Instance.AddGroup(group, SCOrganization.GetRoot()); SCUser user1 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user1, SCOrganization.GetRoot()); SCMemberRelation mr = SCObjectOperations.Instance.AddUserToGroup(user1, group); Assert.AreEqual(group.SchemaType, mr.ContainerSchemaType); Assert.AreEqual(user1.SchemaType, mr.MemberSchemaType); SCUser user2 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user2, SCOrganization.GetRoot()); SCObjectOperations.Instance.AddUserToGroup(user2, group); Assert.AreEqual(2, group.CurrentUsers.Count); Assert.IsTrue(group.CurrentUsers.ContainsKey(user1.ID)); Assert.IsTrue(group.CurrentUsers.ContainsKey(user2.ID)); Assert.IsTrue(user1.CurrentGroups.ContainsKey(group.ID)); Assert.IsTrue(user2.CurrentGroups.ContainsKey(group.ID)); SameContainerUserAndContainerSnapshotCollection ugSnapshot = UserAndContainerSnapshotAdapter.Instance.LoadByContainerID(group.ID); Assert.IsTrue(ugSnapshot.ContainsKey(user1.ID)); Assert.IsTrue(ugSnapshot.ContainsKey(user2.ID)); }
public void MergeUserInGroupExecutorTest() { // 准备4位用户。User1位状态不变,User2待删除,User3新增加,User4复活 SCGroup group = SCObjectGenerator.PrepareGroupObject(); SCObjectOperations.Instance.AddGroup(group, SCOrganization.GetRoot()); SCUser user1 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user1, SCOrganization.GetRoot()); SCMemberRelation mr = SCObjectOperations.Instance.AddUserToGroup(user1, group); Assert.AreEqual(group.SchemaType, mr.ContainerSchemaType); Assert.AreEqual(user1.SchemaType, mr.MemberSchemaType); SCUser user2 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user2, SCOrganization.GetRoot()); SCObjectOperations.Instance.AddUserToGroup(user2, group); SCUser user3 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user3, SCOrganization.GetRoot()); SCUser user4 = SCObjectGenerator.PrepareUserObject(); SCObjectOperations.Instance.AddUser(user4, SCOrganization.GetRoot()); SCObjectOperations.Instance.AddUserToGroup(user4, group); SCObjectOperations.Instance.RemoveUserFromGroup(user4, group); user4.Status = SchemaObjectStatus.Normal; SchemaObjectCollection needMergeUsers = new SchemaObjectCollection() { user1, user3, user4 }; UserAndContainerSnapshotAdapter.Instance.Merge(group.ID, group.SchemaType, needMergeUsers); Console.WriteLine("Group ID: {0}", group.ID); SameContainerUserAndContainerSnapshotCollection ugSnapshot = UserAndContainerSnapshotAdapter.Instance.LoadByContainerID(group.ID); Assert.IsTrue(ugSnapshot.ContainsKey(user1.ID)); Assert.AreEqual(SchemaObjectStatus.Normal, ugSnapshot[user1.ID].Status); Assert.IsTrue(ugSnapshot.ContainsKey(user2.ID)); Assert.AreEqual(SchemaObjectStatus.Deleted, ugSnapshot[user2.ID].Status); Assert.IsTrue(ugSnapshot.ContainsKey(user3.ID)); Assert.AreEqual(SchemaObjectStatus.Normal, ugSnapshot[user3.ID].Status); Assert.IsTrue(ugSnapshot.ContainsKey(user4.ID)); Assert.AreEqual(SchemaObjectStatus.Normal, ugSnapshot[user4.ID].Status); }
/// <summary> /// 合并用户信息 /// </summary> /// <param name="users"></param> public void Merge(string containerID, string containerSchemaType, SchemaObjectCollection users) { containerID.CheckStringIsNullOrEmpty("containerID"); containerSchemaType.CheckStringIsNullOrEmpty("containerSchemaType"); users.NullCheck("users"); SameContainerUserAndContainerSnapshotCollection existedData = this.LoadByContainerID(containerID); SameContainerUserAndContainerSnapshotCollection insertData = GetInsertData(containerID, containerSchemaType, existedData, users); SameContainerUserAndContainerSnapshotCollection updateData = GetUpdateData(containerID, containerSchemaType, existedData, users); ProcessProgress.Current.MaxStep += insertData.Count; ProcessProgress.Current.MaxStep += updateData.Count; ExecUpdateSeperately(insertData); ExecUpdateSeperately(updateData); }
private void ExecUpdateSeperately(SameContainerUserAndContainerSnapshotCollection data) { foreach (UserAndContainerSnapshot obj in data) { string sql = PrepareOneUpdateSql(obj); using (TransactionScope scope = TransactionScopeFactory.Create()) { DateTime dtUpdate = (DateTime)DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName()); SCActionContext.Current.TimePoint.IsMinValue(() => SCActionContext.Current.TimePoint = dtUpdate); scope.Complete(); } ProcessProgress.Current.Increment(); ProcessProgress.Current.Response(); } }
private void ExecUpdateSeperately(SameContainerUserAndContainerSnapshotCollection data) { foreach (UserAndContainerSnapshot obj in data) { string sql = PrepareOneUpdateSql(obj); using (TransactionScope scope = TransactionScopeFactory.Create()) { DateTime dtUpdate = (DateTime)DbHelper.RunSqlReturnScalar(sql, this.GetConnectionName()); SCActionContext.Current.TimePoint.IsMinValue(() => SCActionContext.Current.TimePoint = dtUpdate); scope.Complete(); } ProcessProgress.Current.Increment(); ProcessProgress.Current.Response(); } }
public void DeleteRoleTest() { SCApplication application = SCObjectGenerator.PrepareApplicationObject(); SCObjectOperations.Instance.AddApplication(application); SCRole role = SCObjectGenerator.PrepareRoleObject(); SCObjectOperations.Instance.AddRole(role, application); Console.WriteLine("RoleID: {0}", role.ID); SCUser user1 = SCObjectGenerator.PrepareUserObject("RU1", "User1", "RoleUser1"); SCUser user2 = SCObjectGenerator.PrepareUserObject("RU2", "User2", "RoleUser2"); SCObjectOperations.Instance.AddUser(user1, SCOrganization.GetRoot()); SCObjectOperations.Instance.AddUser(user2, SCOrganization.GetRoot()); SCObjectOperations.Instance.AddMemberToRole(user1, role); SCObjectOperations.Instance.AddMemberToRole(user2, role); SCObjectAndRelationCollection relations = SCSnapshotAdapter.Instance.QueryRolesContainsMembers(new string[] { "Roles" }, new string[] { role.ID }, false, DateTime.MinValue); Assert.AreEqual(role.CurrentMembers.Count, relations.Count); SCObjectOperations.Instance.DeleteRole(role); Assert.AreEqual(0, application.CurrentRoles.Count); Assert.AreEqual(0, user1.CurrentRoles.Count); Assert.AreEqual(0, user2.CurrentRoles.Count); SCObjectAndRelationCollection relationDeleted = SCSnapshotAdapter.Instance.QueryRolesContainsMembers(new string[] { "Roles" }, new string[] { role.ID }, false, DateTime.MinValue); Assert.AreEqual(role.CurrentMembers.Count, relationDeleted.Count); SameContainerUserAndContainerSnapshotCollection containsUsers = UserAndContainerSnapshotAdapter.Instance.LoadByContainerID(role.ID); containsUsers.ForEach(u => Assert.AreEqual(SchemaObjectStatus.Deleted, u.Status)); }
private static SameContainerUserAndContainerSnapshotCollection GetInsertData(string containerID, string containerSchemaType, SameContainerUserAndContainerSnapshotCollection existedData, SchemaObjectCollection users) { SameContainerUserAndContainerSnapshotCollection newInfo = new SameContainerUserAndContainerSnapshotCollection(); foreach (SchemaObjectBase user in users) { if (existedData.ContainsKey(user.ID) == false && newInfo.ContainsKey(user.ID) == false) { UserAndContainerSnapshot uacs = new UserAndContainerSnapshot(); uacs.ContainerID = containerID; uacs.ContainerSchemaType = containerSchemaType; uacs.UserID = user.ID; uacs.UserSchemaType = "Users"; uacs.Status = SchemaObjectStatus.Normal; newInfo.Add(uacs); } } return(newInfo); }
/// <summary> /// 根据Container来加载UserAndContainerSnapshot的信息 /// </summary> /// <param name="containerID"></param> /// <param name="timePoint"></param> /// <returns></returns> public SameContainerUserAndContainerSnapshotCollection LoadByContainerID(string containerID, DateTime timePoint) { containerID.CheckStringIsNullOrEmpty("containerID"); ConnectiveSqlClauseCollection timePointBuilder = VersionStrategyQuerySqlBuilder.Instance.TimePointToBuilder(timePoint); WhereSqlClauseBuilder whereBuilder = new WhereSqlClauseBuilder(); whereBuilder.AppendItem("ContainerID", containerID); ConnectiveSqlClauseCollection connectiveBuilder = new ConnectiveSqlClauseCollection(whereBuilder, timePointBuilder); string sql = string.Format("SELECT * FROM {0} WHERE {1}", this.GetLoadingTableName(timePoint), connectiveBuilder.ToSqlString(TSqlBuilder.Instance)); SameContainerUserAndContainerSnapshotCollection result = new SameContainerUserAndContainerSnapshotCollection(); using (DbContext context = DbContext.GetContext(this.GetConnectionName())) { using (IDataReader reader = DbHelper.RunSqlReturnDR(sql, this.GetConnectionName())) { while (reader.Read()) { UserAndContainerSnapshot item = new UserAndContainerSnapshot(); ORMapping.DataReaderToObject(reader, item); if (result.ContainsKey(item.UserID) == false) { result.Add(item); } } } } return(result); }
public void IsChildAllByIDBuiltInFunctionTest() { SchemaObjectAdapter.Instance.ClearAllData(); TestRoleData roleData = SCObjectGenerator.PrepareTestRoleWithOrgAndGroup(data => string.Format("IsChildAll(\"{0}\", \"{1}\")", "Guid", data.SidelineOrganization.ID)); SCConditionCalculator calculator = new SCConditionCalculator(); ProcessProgress.Clear(); ProcessProgress.Current.RegisterResponser(TestProgressResponser.Instance); calculator.GenerateUserAndContainerSnapshot(new List <ISCUserContainerObject>() { roleData.BuiltInFunctionRole }); SameContainerUserAndContainerSnapshotCollection snapshot = UserAndContainerSnapshotAdapter.Instance.LoadByContainerID(roleData.BuiltInFunctionRole.ID); Console.WriteLine(roleData.ToString()); Console.Error.WriteLine("Error: {0}", ProcessProgress.Current.GetDefaultError()); Console.WriteLine("Output: {0}", ProcessProgress.Current.GetDefaultOutput()); Assert.IsFalse(snapshot.ContainsKey(roleData.SidelineUserInOrg.ID)); }
private static SameContainerUserAndContainerSnapshotCollection GetUpdateData(string containerID, string containerSchemaType, SameContainerUserAndContainerSnapshotCollection existedData, SchemaObjectCollection users) { SameContainerUserAndContainerSnapshotCollection updatedInfo = new SameContainerUserAndContainerSnapshotCollection(); foreach (UserAndContainerSnapshot uacs in existedData) { if (users.ContainsKey(uacs.UserID)) { //原来是已删除的,现在改为Normal if (uacs.Status != SchemaObjectStatus.Normal) { uacs.Status = SchemaObjectStatus.Normal; updatedInfo.Add(uacs); } } else { //现在不存在了,状态需要改为已删除 uacs.Status = SchemaObjectStatus.Deleted; updatedInfo.Add(uacs); } } return updatedInfo; }
private static SameContainerUserAndContainerSnapshotCollection GetInsertData(string containerID, string containerSchemaType, SameContainerUserAndContainerSnapshotCollection existedData, SchemaObjectCollection users) { SameContainerUserAndContainerSnapshotCollection newInfo = new SameContainerUserAndContainerSnapshotCollection(); foreach (SchemaObjectBase user in users) { if (existedData.ContainsKey(user.ID) == false && newInfo.ContainsKey(user.ID) == false) { UserAndContainerSnapshot uacs = new UserAndContainerSnapshot(); uacs.ContainerID = containerID; uacs.ContainerSchemaType = containerSchemaType; uacs.UserID = user.ID; uacs.UserSchemaType = "Users"; uacs.Status = SchemaObjectStatus.Normal; newInfo.Add(uacs); } } return newInfo; }
/// <summary> /// 根据Container来加载UserAndContainerSnapshot的信息 /// </summary> /// <param name="containerID"></param> /// <param name="timePoint"></param> /// <returns></returns> public SameContainerUserAndContainerSnapshotCollection LoadByContainerID(string containerID, DateTime timePoint) { containerID.CheckStringIsNullOrEmpty("containerID"); ConnectiveSqlClauseCollection timePointBuilder = VersionStrategyQuerySqlBuilder.Instance.TimePointToBuilder(timePoint); WhereSqlClauseBuilder whereBuilder = new WhereSqlClauseBuilder(); whereBuilder.AppendItem("ContainerID", containerID); ConnectiveSqlClauseCollection connectiveBuilder = new ConnectiveSqlClauseCollection(whereBuilder, timePointBuilder); string sql = string.Format("SELECT * FROM {0} WHERE {1}", this.GetLoadingTableName(timePoint), connectiveBuilder.ToSqlString(TSqlBuilder.Instance)); SameContainerUserAndContainerSnapshotCollection result = new SameContainerUserAndContainerSnapshotCollection(); using (DbContext context = DbContext.GetContext(this.GetConnectionName())) { using (IDataReader reader = DbHelper.RunSqlReturnDR(sql, this.GetConnectionName())) { while (reader.Read()) { UserAndContainerSnapshot item = new UserAndContainerSnapshot(); ORMapping.DataReaderToObject(reader, item); if (result.ContainsKey(item.UserID) == false) result.Add(item); } } } return result; }