void LoadAuthorizationState()
	{
		OperationsEdit.State.Clear();
		BXFileAuthorizationState state = BXFileAuthorizationManager.BuildAuthorizationState(delegate(string path)
		{
			foreach (Entry entry in validItems)
				if (BXPathComparer.IsSubDir(path, entry.Path))
					return true;
			return false;
		});
		//List<string> changedRoles = new List<string>();

		Dictionary<Entry, BXOperationsEditOperationState> fileState = new Dictionary<Entry, BXOperationsEditOperationState>(); //moved out of section to save memory
		foreach (KeyValuePair<string, BXOperationsEditRoleInfo> role in OperationsEdit.Roles)
		{
			int roleId = int.Parse(role.Key);
			bool changed = false;
			foreach (string op in OperationsEdit.Operations.Keys)
			{
				BXOperationsEditInheritedOperationState inheritedOperationState = BXOperationsEditInheritedOperationState.Denied;
				BXOperationsEditOperationState currentOperationState = BXOperationsEditOperationState.Inherited;

				//Calculate inherited state
				if (!string.IsNullOrEmpty(curDir))
					foreach (KeyValuePair<string, BXParamsBag<Dictionary<int, bool>>> pathState in state)
						if (BXPathComparer.IsSubDir(pathState.Key, curDir))
						{
							Dictionary<int, bool> operationState;
							bool allowed;
							if (pathState.Value.TryGetValue(op, out operationState) && operationState.TryGetValue(roleId, out allowed))
							{
								changed = true;
								inheritedOperationState = allowed ? BXOperationsEditInheritedOperationState.Allowed : BXOperationsEditInheritedOperationState.Denied;
							}
						}


				List<string> allowedEntries = new List<string>();
				List<string> deniedEntries = new List<string>();
				List<string> inheritedEntries = new List<string>();

				foreach (Entry entry in validItems)
				{
					BXParamsBag<Dictionary<int, bool>> pathState;
					Dictionary<int, bool> operationState;
					bool allowed;
					if (state.TryGetValue(entry.Path, out pathState)
						&& pathState.TryGetValue(op, out operationState)
						&& operationState.TryGetValue(roleId, out allowed))
					{
						changed = true;
						(allowed ? allowedEntries : deniedEntries).Add(entry.Name);
					}
					else
						inheritedEntries.Add(entry.Name);
				}

				//only one list should contain elements
				int containers =
					(inheritedEntries.Count == 0 ? 0 : 1)
					+ (allowedEntries.Count == 0 ? 0 : 1)
					+ (deniedEntries.Count == 0 ? 0 : 1);
				if (containers > 1)
					currentOperationState = BXOperationsEditOperationState.DontModify;
				else if (allowedEntries.Count > 0)
					currentOperationState = BXOperationsEditOperationState.Allowed;
				else if (deniedEntries.Count > 0)
					currentOperationState = BXOperationsEditOperationState.Denied;

				if (currentOperationState != BXOperationsEditOperationState.Inherited || inheritedOperationState != BXOperationsEditInheritedOperationState.Denied)
				{
					BXOperationsEditOperationInfo info = new BXOperationsEditOperationInfo(currentOperationState, inheritedOperationState);
					if (currentOperationState == BXOperationsEditOperationState.DontModify)
					{
						info.AllowDontModify = true;
						OperationsEdit.ShowLegendDontModify = true;
						StringBuilder note = new StringBuilder();
						if (allowedEntries.Count > 0)
						{
							note.AppendFormat("<font style=\"color: Green; font-weight: bold\" >{0} : </font>", GetMessage("OperationState.Allowed"));
							note.AppendLine(Encode(string.Join(", ", allowedEntries.ToArray())));
							note.AppendLine("<br/>");
						}
						if (deniedEntries.Count > 0)
						{
							note.AppendFormat("<font style=\"color: Red; font-weight: bold\" >{0} : </font>", GetMessage("OperationState.Denied"));
							note.AppendLine(Encode(string.Join(", ", deniedEntries.ToArray())));
							note.AppendLine("<br/>");
						}
						if (inheritedEntries.Count > 0)
						{
							note.AppendFormat(
								"<font style=\"color: Gray; font-weight: bold\" >{0} &quot;{1}&quot;: </font>", 
								GetMessage("OperationState.Inherited"),
								inheritedOperationState == BXOperationsEditInheritedOperationState.Allowed ? GetMessage("OperationState.Allowed") : GetMessage("OperationState.Denied")	
							);
							if (inheritedEntries.Count > Math.Max(allowedEntries.Count, deniedEntries.Count))
							{
								note.AppendFormat("<i>{0}</i>", GetMessage("OtherFilesAndFolder"));
								note.AppendLine();
							}
							else
								note.AppendLine(Encode(string.Join(", ", inheritedEntries.ToArray())));
							note.AppendLine("<br/>");
						}

						info.NoteHtml = note.ToString();
						note.Length = 0;
					}
					role.Value.Operations.Add(op, info);
				}
			}
			if (changed)
				OperationsEdit.State.SetRoleFromData(role.Key);
		}
	}
Esempio n. 2
0
	private void LoadAccessState()
	{
		AccessEdit.State.Clear();

		foreach (BXRole role in UserRoles)
		{
			string stringRoleId = role.RoleId.ToString();

			#region Get Inherited Tasks
			BXRoleTaskCollection inheritedTasks = BXRoleTaskManager.GetList(
				new BXFormFilter(
					new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
					new BXFormFilterItem("Role.ModuleId", string.Empty, BXSqlFilterOperators.Equal)
				),
				new BXOrderBy_old("TaskName", "Asc")
			);
			inheritedTasks.RemoveAll(delegate(BXRoleTask task)
			{
				return !AccessEdit.Operations.ContainsKey("t" + task.TaskId.ToString());
			});
			#endregion

			#region Get Current Tasks
			BXRoleTaskCollection currentTasks =
				forum != null
				? BXRoleTaskManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", "forum", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ExternalId", forum.Id.ToString(), BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("TaskName", "Asc")
				)
				: new BXRoleTaskCollection();
			currentTasks.RemoveAll(delegate(BXRoleTask task)
			{
				return !AccessEdit.Operations.ContainsKey("t" + task.TaskId.ToString());
			});
			#endregion

			#region Get Inherited Operations
			BXRoleOperationCollection inheritedOperations = BXRoleOperationManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", string.Empty, BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("OperationName", "Asc")
				);
			inheritedOperations.RemoveAll(delegate(BXRoleOperation operation)
			{
				return !AccessEdit.Operations.ContainsKey("o" + operation.OperationId.ToString());
			});
			#endregion

			#region Get Current Operations
			BXRoleOperationCollection currentOperations =
				forum != null
				? BXRoleOperationManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", "forum", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ExternalId", forum.Id.ToString(), BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("OperationName", "Asc")
				)
				: new BXRoleOperationCollection();
			currentOperations.RemoveAll(delegate(BXRoleOperation operation)
			{
				return !AccessEdit.Operations.ContainsKey("o" + operation.OperationId.ToString());
			});
			#endregion


			if (inheritedTasks.Count > 0 || currentTasks.Count > 0 || inheritedOperations.Count > 0 || currentOperations.Count > 0)
			{
				BXOperationsEditRoleInfo roleInfo = AccessEdit.Roles[stringRoleId];

				foreach (BXRoleTask task in inheritedTasks)
				{
					string stringTaskId = "t" + task.TaskId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringTaskId, out opInfo))
						roleInfo.Operations.Add(stringTaskId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.InheritedState = BXOperationsEditInheritedOperationState.Allowed;
				}
				foreach (BXRoleTask task in currentTasks)
				{
					string stringTaskId = "t" + task.TaskId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringTaskId, out opInfo))
						roleInfo.Operations.Add(stringTaskId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.State = BXOperationsEditOperationState.Allowed;
				}
				foreach (BXRoleOperation operation in inheritedOperations)
				{
					string stringOperationId = "o" + operation.OperationId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringOperationId, out opInfo))
						roleInfo.Operations.Add(stringOperationId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.InheritedState = BXOperationsEditInheritedOperationState.Allowed;
				}
				foreach (BXRoleOperation operation in currentOperations)
				{
					string stringOperationId = "o" + operation.OperationId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringOperationId, out opInfo))
						roleInfo.Operations.Add(stringOperationId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.State = BXOperationsEditOperationState.Allowed;
				}

				AccessEdit.State.SetRoleFromData(stringRoleId);
			}
		}
	}
Esempio n. 3
0
	void LoadAuthorizationState()
	{
		BXFileAuthorizationState state = BXFileAuthorizationManager.BuildAuthorizationState(curPath);
		List<int> changedRoles = new List<int>();

		//Проходим по всем путям, от которых можно унаследовать права
		foreach (KeyValuePair<string, BXParamsBag<Dictionary<int, bool>>> path in state)
		{
			if (BXPathComparer.IsSubDir(path.Key, curPath))
			{
				bool thisPath = BXPathComparer.Instance.Equals(path.Key, curPath);
				//Проходим по всем операциям и ролям
				foreach (KeyValuePair<string, Dictionary<int, bool>> op in path.Value)
				{
					//Находим операцию в списке операций
					int i = Array.FindIndex(BXFileOperation.Operations, delegate(string value)
					{
						return string.Equals(op.Key, value, StringComparison.InvariantCultureIgnoreCase);
					});
					if (i == -1)
						continue;

					string operation = BXFileOperation.Operations[i];

					foreach (KeyValuePair<int, bool> r in op.Value)
					{
						//Находим роль в списке ролей
						BXOperationsEditRoleInfo roleInfo = null;
						if (!OperationsEdit.Roles.TryGetValue(r.Key.ToString(), out roleInfo))
							continue;

						BXOperationsEditOperationInfo operationInfo = null;
						if (!roleInfo.Operations.TryGetValue(operation, out operationInfo))
							roleInfo.Operations.Add(operation, operationInfo = new BXOperationsEditOperationInfo());

						if (thisPath)
						{
							//если права заданы на уровне самого файла, то отмечаем их отдельно
							operationInfo.State = op.Value[r.Key] ? BXOperationsEditOperationState.Allowed : BXOperationsEditOperationState.Denied;
						}
						else
						{
							//иначе наследуем
							operationInfo.State = BXOperationsEditOperationState.Inherited;
							operationInfo.InheritedState = op.Value[r.Key] ? BXOperationsEditInheritedOperationState.Allowed : BXOperationsEditInheritedOperationState.Denied;
						}

						int pos = changedRoles.BinarySearch(r.Key);
						if (pos < 0)
							changedRoles.Insert(~pos, r.Key);
					}
				}
			}
			//BXOperationsEditRoleInfo role = OperationsEdit.Roles[roleIdString];

			//Устанавливаем стартовое значение - все операции наследуют запрещение
			//foreach (string op in BXFileOperation.Operations)
			//	role.Operations[op] = new BXOperationsEditOperationInfo(BXOperationsEditOperationState.Inherited, BXOperationsEditInheritedOperationState.Denied);
		}

		foreach (int roleId in changedRoles)
			OperationsEdit.State.SetRoleFromData(roleId.ToString());
	}
Esempio n. 4
0
	private void LoadAccessState()
	{
		//SKIP THIS THING IF AJAX
		ScriptManager sm = ScriptManager.GetCurrent(this);
		if (sm != null && sm.IsInAsyncPostBack)
		{ 
			bool skip = true;
			for (Control parent = AccessEdit.Parent; parent != null; parent = parent.Parent)
			{
				UpdatePanel panel = parent as UpdatePanel;
				if (panel != null && panel.IsInPartialRendering)
				{ 
					skip = false; 
					break;
				}
			}
			if (skip)
				return;
		}

		AccessEdit.State.Clear();
		
		foreach (BXRole role in Roles)
		{
			string stringRoleId = role.RoleId.ToString();

			#region Get Inherited Tasks
			BXRoleTaskCollection inheritedTasks = BXRoleTaskManager.GetList(
				new BXFormFilter(
					new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
					new BXFormFilterItem("Role.ModuleId", string.Empty, BXSqlFilterOperators.Equal)
				),
				new BXOrderBy_old("TaskName", "Asc")
			);
			inheritedTasks.RemoveAll(delegate(BXRoleTask task)
			{
				return !AccessEdit.Operations.ContainsKey("t" + task.TaskId.ToString());
			});
			#endregion

			#region Get Current Tasks
			BXRoleTaskCollection currentTasks =
				iblock != null
				? BXRoleTaskManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", "iblock", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ExternalId", iblockId.ToString(), BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("TaskName", "Asc")
				)
				: new BXRoleTaskCollection();
			currentTasks.RemoveAll(delegate(BXRoleTask task)
			{
				return !AccessEdit.Operations.ContainsKey("t" + task.TaskId.ToString());
			});
			#endregion

			#region Get Inherited Operations
			BXRoleOperationCollection inheritedOperations = BXRoleOperationManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", string.Empty, BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("OperationName", "Asc")
				);
			inheritedOperations.RemoveAll(delegate(BXRoleOperation operation)
			{
				return !AccessEdit.Operations.ContainsKey("o" + operation.OperationId.ToString());
			});
			#endregion

			#region Get Current Operations
			BXRoleOperationCollection currentOperations =
				iblock != null
				? BXRoleOperationManager.GetList(
					new BXFormFilter(
						new BXFormFilterItem("Role.RoleName", role.RoleName, BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ModuleId", "iblock", BXSqlFilterOperators.Equal),
						new BXFormFilterItem("Role.ExternalId", iblockId.ToString(), BXSqlFilterOperators.Equal)
					),
					new BXOrderBy_old("OperationName", "Asc")
				)
				: new BXRoleOperationCollection();
			currentOperations.RemoveAll(delegate(BXRoleOperation operation)
			{
				return !AccessEdit.Operations.ContainsKey("o" + operation.OperationId.ToString());
			});
			#endregion


			if (inheritedTasks.Count > 0 || currentTasks.Count > 0 || inheritedOperations.Count > 0 || currentOperations.Count > 0)
			{
				BXOperationsEditRoleInfo roleInfo = AccessEdit.Roles[stringRoleId];

				foreach (BXRoleTask task in inheritedTasks)
				{
					string stringTaskId = "t" + task.TaskId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringTaskId, out opInfo))
						roleInfo.Operations.Add(stringTaskId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.InheritedState = BXOperationsEditInheritedOperationState.Allowed;
				}
				foreach (BXRoleTask task in currentTasks)
				{
					string stringTaskId = "t" + task.TaskId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringTaskId, out opInfo))
						roleInfo.Operations.Add(stringTaskId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.State = BXOperationsEditOperationState.Allowed;
				}
				foreach (BXRoleOperation operation in inheritedOperations)
				{
					string stringOperationId = "o" + operation.OperationId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringOperationId, out opInfo))
						roleInfo.Operations.Add(stringOperationId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.InheritedState = BXOperationsEditInheritedOperationState.Allowed;
				}
				foreach (BXRoleOperation operation in currentOperations)
				{
					string stringOperationId = "o" + operation.OperationId.ToString();
					BXOperationsEditOperationInfo opInfo;
					if (!roleInfo.Operations.TryGetValue(stringOperationId, out opInfo))
						roleInfo.Operations.Add(stringOperationId, opInfo = new BXOperationsEditOperationInfo());
					opInfo.State = BXOperationsEditOperationState.Allowed;
				}

				AccessEdit.State.SetRoleFromData(stringRoleId);
			}
		}
	}