using System; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.SiteProvider; using CMS.UIControls; using CMS.ExtendedControls; using CMS.SettingsProvider; public partial class CMSModules_Membership_Controls_Roles_RoleEdit : CMSAdminEditControl { #region "Private Variables" private int mSiteId = 0; private int mGroupId = 0; private int mRoleId = 0; private Guid mGroupGuid = Guid.Empty; private bool mGlobalRole = false; #endregion #region "Public properties" /// /// Gets or sets the group GUID. /// public Guid GroupGUID { get { return mGroupGuid; } set { mGroupGuid = value; } } /// /// Indicates whether the role is global (independent on site). /// public bool GlobalRole { get { return mGlobalRole; } set { mGlobalRole = value; } } /// /// Gets or sets the group ID for which the roles should be displayed (0 means all groups). /// public int GroupID { get { return mGroupId; } set { mGroupId = value; } } /// /// Gets or sets the site ID for which the roles should be displayed. /// public int SiteID { get { return mSiteId; } set { mSiteId = value; } } /// /// Gets the role ID of currently processed role. /// public int RoleID { get { return mRoleId; } set { mRoleId = value; } } /// /// Messages placeholder /// public override MessagesPlaceHolder MessagesPlaceHolder { get { return plcMess; } } /// /// Indicates if control is used on live site. /// public override bool IsLiveSite { get { return base.IsLiveSite; } set { plcMess.IsLiveSite = value; base.IsLiveSite = value; } } #endregion protected void Page_Load(object sender, EventArgs e) { // Do not edit code name in simple mode if (DisplayMode == ControlDisplayModeEnum.Simple) { plcCodeName.Visible = false; } rfvCodeName.ErrorMessage = GetString("general.requirescodename"); rfvDisplayName.ErrorMessage = GetString("general.requiresdisplayname"); txtRoleDisplayName.IsLiveSite = IsLiveSite; txtDescription.IsLiveSite = IsLiveSite; if (GroupID > 0) { plcIsPublic.Visible = true; } ReloadData(false); } /// /// Reloads textboxes with new data. /// public override void ReloadData(bool forceReload) { RoleInfo ri = RoleInfoProvider.GetRoleInfo(ItemID); if (ri != null) { CurrentUserInfo user = CMSContext.CurrentUser; // Security test if (!user.UserSiteManagerAdmin) { if (ri.SiteID == 0) { RedirectToAccessDenied(GetString("general.actiondenied")); } else { SiteInfo si = SiteInfoProvider.GetSiteInfo(ri.SiteID); if (si != null) { if (!user.IsGlobalAdministrator && !CMSContext.CurrentUser.IsInSite(si.SiteName)) { RedirectToAccessDenied(GetString("general.actiondenied")); } } } } } if ((!RequestHelper.IsPostBack()) || (forceReload)) { string roleName = string.Empty; if (ri != null) { LoadData(ri); SiteID = ri.SiteID; roleName = ri.RoleName; } else { txtRoleCodeName.Text = null; txtRoleDisplayName.Text = null; txtDescription.Text = null; chkIsDomain.Checked = false; chkIsAdmin.Checked = false; } bool displayIsDomain = ((roleName != RoleInfoProvider.EVERYONE) && (roleName != RoleInfoProvider.AUTHENTICATED) && (roleName != RoleInfoProvider.NOTAUTHENTICATED) && (GroupID == 0)); plcIsDomain.Visible = displayIsDomain; } } /// /// Loads data of edited role from DB into TextBoxes. /// protected void LoadData(RoleInfo ri) { txtRoleCodeName.Text = ri.RoleName; txtRoleDisplayName.Text = ri.DisplayName; txtDescription.Text = ri.Description; chkIsAdmin.Checked = ri.RoleIsGroupAdministrator; chkIsDomain.Checked = ri.RoleIsDomain; } /// /// Saves data of edited role from TextBoxes into DB. /// protected void btnOK_Click(object sender, EventArgs e) { if (!CheckPermissions("cms.roles", PERMISSION_MODIFY)) { return; } // Generate code name in simple mode string codeName = txtRoleCodeName.Text.Trim(); string displayName = txtRoleDisplayName.Text.Trim(); // Check whether required fields are not empty string errorMessage = new Validator().NotEmpty(displayName, GetString("general.requiresdisplayname")) .NotEmpty(codeName, GetString("general.requirescodename")) .IsCodeName(codeName, GetString("general.invalidcodename")).Result; if (errorMessage == string.Empty) { txtRoleCodeName.Text = codeName; txtRoleDisplayName.Text = displayName; if (GlobalRole && CMSContext.CurrentUser.UserSiteManagerAdmin) { RoleInfo ri = RoleInfoProvider.GetExistingRoleInfo(codeName, 0); if ((ri == null) || (ri.RoleID == ItemID) || (codeName == BaseInfo.CODENAME_AUTOMATIC)) { SaveRole(ri, codeName, displayName); } else { ShowError(GetString("Administration-Role_New.RoleExists")); } } else { SiteInfo si = SiteInfoProvider.GetSiteInfo(SiteID); if (si != null) { // Check unique name RoleInfo ri = RoleInfoProvider.GetExistingRoleInfo(codeName, si.SiteID); if ((ri == null) || (ri.RoleID == ItemID) || (codeName == BaseInfo.CODENAME_AUTOMATIC)) { SaveRole(ri, codeName, displayName); } else { ShowError(GetString("Administration-Role_New.RoleExists")); } } } } else { ShowError(errorMessage); } } private void SaveRole(RoleInfo ri, string codeName, string displayName) { bool newRole = false; // Get object if (ri == null) { ri = RoleInfoProvider.GetRoleInfo(ItemID); if (ri == null) { ri = new RoleInfo(); // indicate this is new role and should be redirected after safe newRole = true; } } if (ri.DisplayName != displayName) { // Refresh a breadcrumb if used in the tabs layout ScriptHelper.RefreshTabHeader(Page, string.Empty); } // Set the fields ri.DisplayName = displayName; ri.RoleName = codeName; ri.RoleID = ItemID; ri.Description = txtDescription.Text; ri.SiteID = mSiteId; ri.RoleIsDomain = chkIsDomain.Checked; // If group id is present then it's group role if (GroupID > 0) { ri.RoleGroupID = mGroupId; ri.RoleIsGroupAdministrator = chkIsAdmin.Checked; } RoleInfoProvider.SetRoleInfo(ri); ItemID = ri.RoleID; ShowChangesSaved(); // if new group was created redirect to edit page if (newRole) { RoleID = ri.RoleID; RaiseOnSaved(); } } }