using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Linq; using CMS.CMSHelper; using CMS.FormControls; using CMS.GlobalHelper; using CMS.UIControls; using CMS.WorkflowEngine; using CMS.SettingsProvider; using CMS.ExtendedControls; using CMS.WorkflowEngine.Definitions; public partial class CMSModules_Workflows_Controls_UI_WorkflowStep_Security : CMSAdminEditControl { #region "Variables" private int siteId = 0; private WorkflowStepInfo mWorkflowStep = null; private WorkflowInfo mWorkflow = null; private SourcePoint mCurrentSourcePoint = null; protected string currentRoles = string.Empty; protected string currentUsers = string.Empty; #endregion #region "Properties" /// /// Workflow step ID /// public int WorkflowStepID { get; set; } /// /// With source point GUID set, this control manages security for source point. With null manages security for workflow step. /// public Guid? SourcePointGuid { get; set; } /// /// Site ID /// public int SiteID { get; set; } /// /// Workflow step /// public WorkflowStepInfo WorkflowStep { get { if (mWorkflowStep == null) { mWorkflowStep = WorkflowStepInfoProvider.GetWorkflowStepInfo(WorkflowStepID); } return mWorkflowStep; } } /// /// Workflow /// private WorkflowInfo Workflow { get { if ((mWorkflow == null) && (WorkflowStep != null)) { mWorkflow = WorkflowInfoProvider.GetWorkflowInfo(WorkflowStep.StepWorkflowID); } return mWorkflow; } } /// /// Source point on workflow step /// public SourcePoint CurrentSourcePoint { get { if ((mCurrentSourcePoint == null) && (SourcePointGuid != null)) { mCurrentSourcePoint = WorkflowStep.StepDefinition.SourcePoints.FirstOrDefault(i => i.Guid == SourcePointGuid); } return mCurrentSourcePoint; } } /// /// Indicates if the control should perform the operations. /// public override bool StopProcessing { get { return base.StopProcessing; } set { base.StopProcessing = value; siteSelector.StopProcessing = value; usRoles.StopProcessing = value; usUsers.StopProcessing = value; } } /// /// Indicates if the control is used on the live site. /// public override bool IsLiveSite { get { return base.IsLiveSite; } set { base.IsLiveSite = value; siteSelector.IsLiveSite = value; usRoles.IsLiveSite = value; usUsers.IsLiveSite = value; } } /// /// Roles security of workflow step or its source point /// private WorkflowStepSecurityEnum RolesSecurity { get { if (SourcePointGuid == null) { return WorkflowStep.StepRolesSecurity; } else { return (CurrentSourcePoint != null) ? CurrentSourcePoint.StepRolesSecurity : WorkflowStepSecurityEnum.Default; } } set { if (SourcePointGuid == null) { WorkflowStep.StepRolesSecurity = value; } else { if (CurrentSourcePoint != null) { CurrentSourcePoint.StepRolesSecurity = value; } } } } /// /// Users security of workflow step or its source point /// private WorkflowStepSecurityEnum UsersSecurity { get { if (SourcePointGuid == null) { return WorkflowStep.StepUsersSecurity; } else { return CurrentSourcePoint != null ? CurrentSourcePoint.StepUsersSecurity : WorkflowStepSecurityEnum.Default; } } set { if (SourcePointGuid == null) { WorkflowStep.StepUsersSecurity = value; } else { if (CurrentSourcePoint != null) { CurrentSourcePoint.StepUsersSecurity = value; } } } } #endregion #region "Page events" protected void Page_Load(object sender, EventArgs e) { if (WorkflowStepID <= 0) { StopProcessing = true; return; } EditedObject = WorkflowStep; if (SourcePointGuid != null) { ShowInformation(GetString("workflowsteppoint.securityInfo")); } else if (WorkflowStep.StepAllowBranch) { ShowInformation(GetString("workflowstep.securityInfo")); } if (Workflow.IsAutomation) { pnlRoles.GroupingText = GetString("processstep.rolessecurity"); pnlUsers.GroupingText = GetString("processstep.userssecurity"); } else { pnlRoles.GroupingText = (SourcePointGuid == null) ? GetString("workflowstep.rolessecurity") : GetString("workflowsteppoint.rolessecurity"); pnlUsers.GroupingText = (SourcePointGuid == null) ? GetString("workflowstep.userssecurity") : GetString("workflowsteppoint.userssecurity"); } // Set site selector siteSelector.AllowGlobal = true; siteSelector.DropDownSingleSelect.AutoPostBack = true; siteSelector.AllowAll = false; siteSelector.OnlyRunningSites = false; siteSelector.UniSelector.OnSelectionChanged += UniSelector_OnSelectionChanged; usRoles.OnSelectionChanged += usRoles_OnSelectionChanged; usUsers.OnSelectionChanged += usUsers_OnSelectionChanged; usRoles.ObjectType = PredefinedObjectType.ROLE; usUsers.ObjectType = PredefinedObjectType.USER; rbRoleType.SelectedIndexChanged += rbRoleType_SelectedIndexChanged; rbUserType.SelectedIndexChanged += rbUserType_SelectedIndexChanged; if (!RequestHelper.IsPostBack()) { siteId = SiteID; siteSelector.Value = siteId; string resPrefix = (SourcePointGuid == null) ? "workflowstep" : "workflowsteppoint"; DataHelper.FillWithEnum(rbRoleType, resPrefix + ".security", null); DataHelper.FillWithEnum(rbUserType, resPrefix + ".usersecurity", null); rbRoleType.SelectedValue = ((int)RolesSecurity).ToString(); rbUserType.SelectedValue = ((int)UsersSecurity).ToString(); } else { // Make sure the current site is always selected int selectedId = ValidationHelper.GetInteger(siteSelector.Value, 0); if (selectedId == 0) { selectedId = SiteID; siteSelector.Value = SiteID; } siteId = selectedId; } // If global role selected - set siteID to 0 if (siteSelector.GlobalRecordValue == siteId.ToString()) { siteId = 0; } string siteIDWhere = (siteId == 0) ? "SiteID IS NULL" : "SiteID = " + siteId; usRoles.WhereCondition = siteIDWhere + " AND RoleGroupID IS NULL"; usUsers.WhereCondition = "(UserIsHidden = 0 OR UserIsHidden IS NULL)"; // Get the active roles for this site string where = String.Format("[StepID] = {0} AND [StepSourcePointGuid] {1}", WorkflowStepID, (SourcePointGuid == null ? "IS NULL" : String.Format("= '{0}'", SourcePointGuid.ToString()))); var roles = WorkflowStepRoleInfoProvider.GetWorkflowStepRoles(where, null, 0, "RoleID").Select(r => r.RoleID.ToString()); currentRoles = string.Join(";", roles.ToArray()); // Get the active users for this site var users = WorkflowStepUserInfoProvider.GetWorkflowStepUsers(where, null, 0, "UserID").Select(u => u.UserID.ToString()); currentUsers = string.Join(";", users.ToArray()); // Init lists when security type changes if (!RequestHelper.IsPostBack() || RequestHelper.GetPostBackControlID(Page).StartsWithCSafe(rbRoleType.UniqueID) || RequestHelper.GetPostBackControlID(Page).StartsWithCSafe(rbUserType.UniqueID)) { usRoles.Value = currentRoles; usUsers.Value = currentUsers; } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (WorkflowStepID > 0) { usRoles.Visible = (RolesSecurity != WorkflowStepSecurityEnum.Default); plcRolesBox.Visible = usRoles.Visible; usUsers.Visible = (UsersSecurity != WorkflowStepSecurityEnum.Default); } } void rbRoleType_SelectedIndexChanged(object sender, EventArgs e) { RolesSecurity = (WorkflowStepSecurityEnum)ValidationHelper.GetInteger(rbRoleType.SelectedValue, 0); WorkflowStep.Update(); ShowChangesSaved(); } void rbUserType_SelectedIndexChanged(object sender, EventArgs e) { UsersSecurity = (WorkflowStepSecurityEnum)ValidationHelper.GetInteger(rbUserType.SelectedValue, 0); WorkflowStep.Update(); ShowChangesSaved(); } protected void usRoles_OnSelectionChanged(object sender, EventArgs e) { SaveRolesData(); pnlUpdateRoles.Update(); } protected void usUsers_OnSelectionChanged(object sender, EventArgs e) { SaveUsersData(); pnlUpdateUsers.Update(); } #endregion #region "Control handling" /// /// Handles site selection change event. /// protected void UniSelector_OnSelectionChanged(object sender, EventArgs e) { pnlUpdateRoles.Update(); } /// /// Saves roles data /// private void SaveRolesData() { Guid stepSourcePointGuid = (SourcePointGuid == null) ? Guid.Empty : SourcePointGuid.Value; // Remove old items string newValues = ValidationHelper.GetString(usRoles.Value, null); string items = DataHelper.GetNewItemsInList(newValues, currentRoles); if (!String.IsNullOrEmpty(items)) { string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { // Add all new items to site foreach (string item in newItems) { int roleId = ValidationHelper.GetInteger(item, 0); // If role is authorized, remove it WorkflowStepRoleInfo wsr = WorkflowStepRoleInfoProvider.GetWorkflowStepRoleInfo(WorkflowStepID, roleId, stepSourcePointGuid); if (wsr != null) { wsr.Delete(); } } } } // Add new items items = DataHelper.GetNewItemsInList(currentRoles, newValues); if (!String.IsNullOrEmpty(items)) { string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { // Add all new items to site foreach (string item in newItems) { int roleId = ValidationHelper.GetInteger(item, 0); // If role is not authorized, authorize it if (WorkflowStepRoleInfoProvider.GetWorkflowStepRoleInfo(WorkflowStepID, roleId, stepSourcePointGuid) == null) { WorkflowStepRoleInfoProvider.AddRoleToWorkflowStep(WorkflowStepID, roleId, stepSourcePointGuid); } } } } ShowChangesSaved(); } /// /// Saves users data /// private void SaveUsersData() { Guid stepSourcePointGuid = (SourcePointGuid == null) ? Guid.Empty : SourcePointGuid.Value; // Remove old items string newValues = ValidationHelper.GetString(usUsers.Value, null); string items = DataHelper.GetNewItemsInList(newValues, currentUsers); if (!String.IsNullOrEmpty(items)) { string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { // Add all new items to site foreach (string item in newItems) { int userId = ValidationHelper.GetInteger(item, 0); // If user is authorized, remove it WorkflowStepUserInfo wsu = WorkflowStepUserInfoProvider.GetWorkflowStepUserInfo(WorkflowStepID, userId, stepSourcePointGuid); if (wsu != null) { wsu.Delete(); } } } } // Add new items items = DataHelper.GetNewItemsInList(currentUsers, newValues); if (!String.IsNullOrEmpty(items)) { string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); if (newItems != null) { // Add all new items to site foreach (string item in newItems) { int userId = ValidationHelper.GetInteger(item, 0); // If user is not authorized, authorize it if (WorkflowStepUserInfoProvider.GetWorkflowStepUserInfo(WorkflowStepID, userId, stepSourcePointGuid) == null) { WorkflowStepUserInfoProvider.AddUserToWorkflowStep(WorkflowStepID, userId, stepSourcePointGuid); } } } } ShowChangesSaved(); } #endregion }