File: D:/HostingSpaces/RMourik/bassol.nl/CMS/CMSModules/Membership/Pages/Users/User_Edit_General.aspx.cs
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using CMS.ExtendedControls;
using CMS.Helpers;
using CMS.Base;
using CMS.Localization;
using CMS.SiteProvider;
using CMS.Membership;
using CMS.UIControls;
using CMS.ExtendedControls.ActionsConfig;
using CMS.DataEngine;
public partial class CMSModules_Membership_Pages_Users_User_Edit_General : CMSUsersPage
{
#region "Protected variables"
protected int userId = 0;
protected string password;
protected string myCulture = string.Empty;
protected string myUICulture = string.Empty;
private UserInfo ui = null;
#endregion
#region "Page events"
protected void Page_Load(object sender, EventArgs e)
{
userId = QueryHelper.GetInteger("objectid", 0);
// Get user info object and check if UI should be displayed
ui = UserInfoProvider.GetUserInfo(userId);
CheckUserAvaibleOnSite(ui);
EditedObject = ui;
ucUserName.UseDefaultValidationGroup = false;
cultureSelector.DisplayAllCultures = true;
lblResetToken.Text = GetString("mfauthentication.token.reset");
// Register picture delete script
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "PictDelConfirm",
ScriptHelper.GetScript("function DeleteConfirmation(){ return confirm(" + ScriptHelper.GetString(GetString("MyProfile.PictDeleteConfirm")) + ");}"));
// Check that only global administrator can edit global administrator's accounts
if (!CheckGlobalAdminEdit(ui))
{
plcTable.Visible = false;
ShowError(GetString("Administration-User_List.ErrorGlobalAdmin"));
}
if (!RequestHelper.IsPostBack())
{
LoadData();
}
// Set hide action if user extend validity of his own account
if (ui.UserID == CurrentUser.UserID)
{
btnExtendValidity.OnClientClick = "window.top.HideWarning()";
}
// Register help variable for user is external confirmation
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "IsExternal", ScriptHelper.GetScript("var isExternal = " + chkIsExternal.Checked.ToString().ToLowerCSafe() + ";"));
// Javascript code for "Is external user" confirmation
string javascript = ScriptHelper.GetScript(
@"function CheckExternal() {
var checkbox = document.getElementById('" + chkIsExternal.ClientID + @"')
if(checkbox.checked && !isExternal) {
if(!confirm('" + GetString("user.confirmexternal") + @"')) {
checkbox.checked = false ;
}
}}");
// Register script to the page
ScriptHelper.RegisterClientScriptBlock(this, typeof(string), ClientID + "CheckExternal", javascript);
// Assign to ok button
if (!chkIsExternal.Checked)
{
btnOk.OnClientClick = "CheckExternal()";
}
// Display impersonation link if current user is global administrator and edited user is not global admin
if (CurrentUser.IsGlobalAdministrator && (ui.UserID != CurrentUser.UserID) && !ui.IsPublic())
{
string message = GetImpersonalMessage(ui);
HeaderAction action = new HeaderAction();
action.Text = GetString("Membership.Impersonate");
action.Tooltip = GetString("Membership.Impersonate");
action.OnClientClick = "if (!confirm('" + message + "')) { return false; }";
action.CommandName = "impersonate";
CurrentMaster.HeaderActions.AddAction(action);
CurrentMaster.HeaderActions.ActionPerformed += HeaderActions_ActionPerformed;
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (ui != null)
{
// Reset flag
CheckBoxEnabled.Enabled = true;
// Show warning message
if (!ui.Enabled)
{
string description = null;
if (ui.UserSettings.UserWaitingForApproval)
{
description = GetString("Administration-User_List.AccountLocked.WaitingForApproval");
}
else
{
switch (UserAccountLockCode.ToEnum(ui.UserAccountLockReason))
{
case UserAccountLockEnum.MaximumInvalidLogonAttemptsReached:
description = GetString("Administration-User_List.AccountLocked.MaximumInvalidPasswordAttempts");
CheckBoxEnabled.Enabled = false;
break;
case UserAccountLockEnum.PasswordExpired:
description = GetString("Administration-User_List.AccountLocked.PasswordExpired");
CheckBoxEnabled.Enabled = false;
break;
case UserAccountLockEnum.DisabledManually:
description = GetString("Administration-User_List.AccountLocked.Disabledmanually");
break;
}
}
ShowWarning(description);
}
// Check "modify" permission
if (!CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
{
btnExtendValidity.Enabled = btnResetLogonAttempts.Enabled = false;
btnResetToken.Enabled = false;
}
// Display impersonation link if current user is global administrator
if (CurrentMaster.HeaderActions.ActionsList != null)
{
var impersonateAction = CurrentMaster.HeaderActions.ActionsList.Find(a => a.CommandName == "impersonate");
if (impersonateAction != null)
{
if (CurrentUser.IsGlobalAdministrator && (ui != null) && (ui.UserID != CurrentUser.UserID) && !ui.IsPublic() && (!ui.IsGlobalAdministrator))
{
string message = GetImpersonalMessage(ui);
impersonateAction.OnClientClick = "if (!confirm('" + message + "')) { return false; }";
}
else
{
impersonateAction.Visible = false;
}
}
}
}
}
/// <summary>
/// Users actions.
/// </summary>
private void HeaderActions_ActionPerformed(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "impersonate":
// Use user impersonate
UserInfo ui = UserInfoProvider.GetUserInfo(userId);
AuthenticationHelper.ImpersonateUser(ui);
break;
case ComponentEvents.SAVE:
btnOk_Click(sender, EventArgs.Empty);
break;
}
}
/// <summary>
/// Saves data of edited user from TextBoxes into DB.
/// </summary>
protected void btnOk_Click(object sender, EventArgs e)
{
UserPrivilegeLevelEnum privilegeLevel = (UserPrivilegeLevelEnum)drpPrivilege.Value.ToInteger(0);
// Check "modify" permission
if (!CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
{
RedirectToAccessDenied("CMS.Users", "Modify");
}
string result = ValidateGlobalAndDeskAdmin(userId);
// Find whether user name is valid
if (result == String.Empty)
{
if (!ucUserName.IsValid())
{
result = ucUserName.ValidationError;
}
}
String userName = ValidationHelper.GetString(ucUserName.Value, String.Empty);
if (result == String.Empty)
{
// Finds whether required fields are not empty
result = new Validator().NotEmpty(txtFullName.Text, GetString("Administration-User_New.RequiresFullName")).Result;
}
// Store the old display name
var oldDisplayName = ui.Generalized.ObjectDisplayName;
if ((result == String.Empty) && (ui != null))
{
// If site prefixed allowed - ad site prefix
if ((SiteID != 0) && UserInfoProvider.UserNameSitePrefixEnabled(SiteContext.CurrentSiteName))
{
if (!UserInfoProvider.IsSitePrefixedUser(userName))
{
userName = UserInfoProvider.EnsureSitePrefixUserName(userName, SiteContext.CurrentSite);
}
}
// Validation for site prefixed users
if (!UserInfoProvider.IsUserNamePrefixUnique(userName, ui.UserID))
{
ShowError(GetString("Administration-User_New.siteprefixeduserexists"));
return;
}
// Ensure same password
password = ui.GetValue("UserPassword").ToString();
// Test for unique username
UserInfo uiTest = UserInfoProvider.GetUserInfo(userName);
if ((uiTest == null) || (uiTest.UserID == userId))
{
if (ui == null)
{
ui = new UserInfo();
}
bool globAdmin = ui.IsGlobalAdministrator;
bool editor = ui.IsEditorInternal;
// Email format validation
string email = txtEmail.Text.Trim();
if ((email != string.Empty) && (!ValidationHelper.IsEmail(email)))
{
ShowError(GetString("Administration-User_New.WrongEmailFormat"));
return;
}
bool oldGlobal = ui.IsGlobalAdministrator;
bool oldEditor = ui.IsEditorInternal;
// Define domain variable
string domains = null;
// Get all user sites
DataTable ds = UserInfoProvider.GetUserSites(userId, null, null, 0, "SiteDomainName");
if (!DataHelper.DataSourceIsEmpty(ds))
{
foreach (DataRow dr in ds.Rows)
{
domains += ValidationHelper.GetString(dr["SiteDomainName"], string.Empty) + ";";
}
// Remove ";" at the end
if (domains != null)
{
domains = domains.Remove(domains.Length - 1);
}
}
else
{
DataSet siteDs = SiteInfoProvider.GetSites().Columns("SiteDomainName");
if (!DataHelper.DataSourceIsEmpty(siteDs))
{
// Create list of available site domains
domains = TextHelper.Join(";", DataHelper.GetStringValues(siteDs.Tables[0], "SiteDomainName"));
}
}
// Check limitations for Global administrator
if (CurrentUser.IsGlobalAdministrator && ((privilegeLevel == UserPrivilegeLevelEnum.GlobalAdmin) || (privilegeLevel == UserPrivilegeLevelEnum.Admin)) && !oldGlobal)
{
if (!UserInfoProvider.LicenseVersionCheck(domains, FeatureEnum.Administrators, ObjectActionEnum.Insert, globAdmin))
{
ShowError(GetString("License.MaxItemsReachedGlobal"));
return;
}
}
// Check limitations for editors
if ((privilegeLevel == UserPrivilegeLevelEnum.Editor) && !oldEditor)
{
if (!UserInfoProvider.LicenseVersionCheck(domains, FeatureEnum.Editors, ObjectActionEnum.Insert, editor))
{
ShowError(GetString("License.MaxItemsReachedEditor"));
return;
}
}
// Check whether email is unique if it is required
if (!UserInfoProvider.IsEmailUnique(email, ui))
{
ShowError(GetString("UserInfo.EmailAlreadyExist"));
return;
}
// Set properties
ui.Email = email;
ui.FirstName = txtFirstName.Text.Trim();
ui.FullName = txtFullName.Text.Trim();
ui.LastName = txtLastName.Text.Trim();
ui.MiddleName = txtMiddleName.Text.Trim();
ui.UserName = userName;
ui.Enabled = CheckBoxEnabled.Checked;
ui.UserIsHidden = chkIsHidden.Checked;
ui.IsExternal = chkIsExternal.Checked;
ui.UserIsDomain = chkIsDomain.Checked;
ui.SetValue("UserPassword", password);
ui.UserID = userId;
ui.UserStartingAliasPath = txtUserStartingPath.Text.Trim();
ui.UserMFRequired = chkIsMFRequired.Checked;
// Global admin can set anything
if (CurrentUser.CheckPrivilegeLevel(UserPrivilegeLevelEnum.GlobalAdmin)
// Other users can set only editor and non privileges
|| ((privilegeLevel != UserPrivilegeLevelEnum.Admin) && (privilegeLevel != UserPrivilegeLevelEnum.GlobalAdmin))
// Admin can manage his own privilege
|| ((privilegeLevel == UserPrivilegeLevelEnum.Admin) && (CurrentUser.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Admin) && (CurrentUser.UserID == ui.UserID))))
{
ui.SetPrivilegeLevel(privilegeLevel);
}
LoadUserLogon(ui);
// Set values of cultures.
string culture = ValidationHelper.GetString(cultureSelector.Value, "");
ui.PreferredCultureCode = culture;
if (lstUICulture.SelectedValue == "0")
{
ui.PreferredUICultureCode = "";
}
else
{
// Set preferred UI culture
CultureInfo ci = CultureInfoProvider.GetCultureInfo(ValidationHelper.GetInteger(lstUICulture.SelectedValue, 0));
ui.PreferredUICultureCode = ci.CultureCode;
}
// Refresh page breadcrumbs if display name changed
if (ui.Generalized.ObjectDisplayName != oldDisplayName)
{
ScriptHelper.RefreshTabHeader(Page, ui.FullName);
}
using (CMSActionContext context = new CMSActionContext())
{
// Check whether the username of the currently logged user has been changed
if (CurrentUserChangedUserName())
{
// Ensure that an update search task will be created but NOT executed when updating the user
context.EnableSmartSearchIndexer = false;
}
// Update the user
UserInfoProvider.SetUserInfo(ui);
// Check whether the username of the currently logged user has been changed
if (CurrentUserChangedUserName())
{
// Ensure that current user is not logged out if he changes his user name
if (RequestHelper.IsFormsAuthentication())
{
FormsAuthentication.SetAuthCookie(ui.UserName, false);
// Update current user
MembershipContext.AuthenticatedUser = new CurrentUserInfo(ui, true);
// Reset current user
CurrentUser = null;
}
}
}
ShowChangesSaved();
}
else
{
// If user exists
ShowError(GetString("Administration-User_New.UserExists"));
}
}
else
{
ShowError(result);
}
if ((ui.UserInvalidLogOnAttempts == 0) && (ui.UserAccountLockReason != UserAccountLockCode.FromEnum(UserAccountLockEnum.MaximumInvalidLogonAttemptsReached)))
{
btnResetLogonAttempts.Enabled = false;
}
LoadPasswordExpiration(ui);
}
#endregion
#region "Protected methods"
/// <summary>
/// Returns the impersonalization message for current user.
/// </summary>
/// <param name="ui">User info</param>
protected string GetImpersonalMessage(UserInfo ui)
{
string message = String.Empty;
// Editor message
if (ui.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Editor, CurrentSiteName))
{
message = GetString("Membership.ImperConfirmEditor");
}
// Default user message
else
{
message = GetString("Membership.ImperConfirmDefault");
}
return message;
}
/// <summary>
/// Loads data of edited user from DB into TextBoxes.
/// </summary>
protected void LoadData()
{
// Fill lstUICulture (loop over and localize them first)
DataSet uiCultures = CultureInfoProvider.GetUICultures(orderBy: "CultureName ASC");
LocalizeCultureNames(uiCultures);
lstUICulture.DataSource = uiCultures.Tables[0].DefaultView;
lstUICulture.DataTextField = "CultureName";
lstUICulture.DataValueField = "CultureID";
lstUICulture.DataBind();
lstUICulture.Items.Insert(0, GetString("Administration-User_Edit.Default"));
lstUICulture.Items[0].Value = "0";
if (ui != null)
{
// Get user info properties
txtEmail.Text = ui.Email;
txtFirstName.Text = ui.FirstName;
txtFullName.Text = ui.FullName;
txtLastName.Text = ui.LastName;
txtMiddleName.Text = ui.MiddleName;
ucUserName.Value = ui.UserName;
CheckBoxEnabled.Checked = ui.Enabled;
chkIsExternal.Checked = ui.IsExternal;
chkIsDomain.Checked = ui.UserIsDomain;
chkIsHidden.Checked = ui.UserIsHidden;
chkIsMFRequired.Checked = ui.UserMFRequired;
// Privilege drop down check
if (!CurrentUser.CheckPrivilegeLevel(UserPrivilegeLevelEnum.GlobalAdmin))
{
// Disable for global admins
if (ui.CheckPrivilegeLevel(UserPrivilegeLevelEnum.GlobalAdmin))
{
drpPrivilege.Enabled = false;
}
else
// Only global admin can manage other admins.
if (ui.CheckPrivilegeLevel(UserPrivilegeLevelEnum.Admin))
{
// Allow manage only for user himself
if (ui.UserID != CurrentUser.UserID)
{
drpPrivilege.Enabled = false;
}
else
{
drpPrivilege.ExcludedValues = ((int)UserPrivilegeLevelEnum.GlobalAdmin).ToString();
}
}
else
{
drpPrivilege.ExcludedValues = (int)UserPrivilegeLevelEnum.GlobalAdmin + ";" + (int)UserPrivilegeLevelEnum.Admin;
}
}
if (ui.IsGlobalAdministrator)
{
drpPrivilege.Value = ui.UserGlobalAccessDisabled ? (int)UserPrivilegeLevelEnum.Admin : (int)UserPrivilegeLevelEnum.GlobalAdmin;
}
else if (ui.IsEditorInternal)
{
drpPrivilege.Value = (int)UserPrivilegeLevelEnum.Editor;
}
password = ui.GetValue("UserPassword").ToString();
// Disable username textbox for public user
if (ui.IsPublic())
{
ucUserName.Enabled = false;
}
myCulture = ui.PreferredCultureCode;
myUICulture = ui.PreferredUICultureCode;
lblInvalidLogonAttemptsNumber.Text = string.Format(GetString("general.attempts"), ui.UserInvalidLogOnAttempts);
if (ui.UserInvalidLogOnAttempts > 0)
{
lblInvalidLogonAttemptsNumber.Style.Add(HtmlTextWriterStyle.Color, "Red");
}
else
{
btnResetLogonAttempts.Enabled = (ui.UserAccountLockReason == UserAccountLockCode.FromEnum(UserAccountLockEnum.MaximumInvalidLogonAttemptsReached));
}
LoadPasswordExpiration(ui);
txtUserStartingPath.Text = ui.UserStartingAliasPath;
}
// Set content culture
cultureSelector.Value = myCulture;
if (!string.IsNullOrEmpty(myUICulture))
{
// Set UI culture
try
{
CultureInfo ciUI = CultureInfoProvider.GetCultureInfo(myUICulture);
lstUICulture.SelectedIndex = lstUICulture.Items.IndexOf(lstUICulture.Items.FindByValue(ciUI.CultureID.ToString()));
}
catch
{
lstUICulture.SelectedIndex = lstUICulture.Items.IndexOf(lstUICulture.Items.FindByValue("0"));
}
}
else
{
lstUICulture.SelectedIndex = lstUICulture.Items.IndexOf(lstUICulture.Items.FindByValue("0"));
}
if (ui != null)
{
// If new user
lblCreatedInfo.Text = ui.UserCreated.ToString();
lblLastLogonTime.Text = ui.LastLogon.ToString();
LoadUserLogon(ui);
if (ui.UserCreated == DateTimeHelper.ZERO_TIME)
{
lblCreatedInfo.Text = GetString("general.na");
}
if (ui.LastLogon == DateTimeHelper.ZERO_TIME)
{
lblLastLogonTime.Text = GetString("general.na");
}
}
}
/// <summary>
/// Displays user's last logon information.
/// </summary>
/// <param name="ui">User info</param>
protected void LoadUserLogon(UserInfo ui)
{
if ((ui.UserLastLogonInfo != null) && (ui.UserLastLogonInfo.ColumnNames != null) && (ui.UserLastLogonInfo.ColumnNames.Count > 0))
{
foreach (string column in ui.UserLastLogonInfo.ColumnNames)
{
// Create new control to display last logon information
Panel grp = new Panel
{
CssClass = "control-group-inline"
};
plcUserLastLogonInfo.Controls.Add(grp);
Label lbl = new Label();
grp.Controls.Add(lbl);
lbl.CssClass = "form-control-text";
lbl.Text = HTMLHelper.HTMLEncode(TextHelper.LimitLength((string)ui.UserLastLogonInfo[column], 80, "..."));
lbl.ToolTip = HTMLHelper.HTMLEncode(column + " - " + (string)ui.UserLastLogonInfo[column]);
}
}
else
{
plcUserLastLogonInfo.Controls.Add(new LocalizedLabel
{
ResourceString = "general.na",
CssClass = "form-control-text"
});
}
}
/// <summary>
/// Check whether current user is allowed to modify another user. Return "" or error message.
/// </summary>
/// <param name="userId">Modified user</param>
protected string ValidateGlobalAndDeskAdmin(int userId)
{
string result = String.Empty;
if (CurrentUser.IsGlobalAdministrator)
{
// User is global admin
return result;
}
UserInfo userInfo = UserInfoProvider.GetUserInfo(userId);
if (userInfo == null)
{
result = GetString("Administration-User.WrongUserId");
}
else if (userInfo.IsGlobalAdministrator)
{
// Current user has lower permissions than given user
result = GetString("Administration-User.NotAllowedToModify");
}
return result;
}
/// <summary>
/// Localizes culture names and sorts them in ascending order.
/// </summary>
/// <param name="uiCultures">DataSet containing the UI cultures</param>
private void LocalizeCultureNames(DataSet uiCultures)
{
// Localize all available UI cultures
if (!DataHelper.DataSourceIsEmpty(uiCultures))
{
for (int i = 0; i < uiCultures.Tables[0].Rows.Count; i++)
{
uiCultures.Tables[0].Rows[i]["CultureName"] = ResHelper.LocalizeString(uiCultures.Tables[0].Rows[i]["CultureName"].ToString());
}
}
uiCultures.Tables[0].DefaultView.Sort = "CultureName ASC";
}
/// <summary>
/// Load user password expiration
/// </summary>
/// <param name="ui">User info</param>
protected void LoadPasswordExpiration(UserInfo ui)
{
int expDays = 0;
lblExpireIn.Style.Clear();
lblPassExpiration.Text = GetString("Administration-User_Edit_General.PasswordExpireIn");
if (!AuthenticationHelper.IsPasswordExpirationEnabled(SiteContext.CurrentSiteName, out expDays))
{
lblExpireIn.Text = GetString("security.never");
btnExtendValidity.Enabled = (ui.UserAccountLockReason == UserAccountLockCode.FromEnum(UserAccountLockEnum.PasswordExpired));
}
else
{
// Get password expiration, negative number means not expired, positive means expired, DateTime.Min means not expired but never changed password
int dayDiff = (ui.UserPasswordLastChanged == DateTime.MinValue) ? -expDays : ((DateTime.Now - ui.UserPasswordLastChanged).Days - expDays);
if (dayDiff >= 0)
{
lblPassExpiration.Text = GetString("Administration-User_Edit_General.PasswordExpired");
lblExpireIn.Style.Add(HtmlTextWriterStyle.Color, "Red");
}
lblExpireIn.Text = string.Format(GetString("general.validity.days"), Math.Abs(dayDiff));
}
}
/// <summary>
/// Reset user account lock status
/// </summary>
protected void btnResetLogonAttempts_Click(object sender, EventArgs e)
{
// Check "modify" permission
if (!CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
{
RedirectToAccessDenied("CMS.Users", "Modify");
}
bool unlocked = false;
if (ui.UserAccountLockReason == UserAccountLockCode.FromEnum(UserAccountLockEnum.MaximumInvalidLogonAttemptsReached))
{
AuthenticationHelper.UnlockUserAccount(ui);
unlocked = true;
}
else
{
ui.UserInvalidLogOnAttempts = 0;
UserInfoProvider.SetUserInfo(ui);
}
LoadData();
lblInvalidLogonAttemptsNumber.Style.Clear();
ShowConfirmation(unlocked ? GetString("Administration-User.InvalidLogonAttemptsResetUnlock") : GetString("Administration-User.InvalidLogonAttemptsReset"));
}
/// <summary>
/// Reset user account lock status
/// </summary>
protected void btnExtendValidity_Click(object sender, EventArgs e)
{
// Check "modify" permission
if (!CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
{
RedirectToAccessDenied("CMS.Users", "Modify");
}
bool unlocked = false;
ui.UserPasswordLastChanged = DateTime.Now;
if (ui.UserAccountLockReason == UserAccountLockCode.FromEnum(UserAccountLockEnum.PasswordExpired))
{
AuthenticationHelper.UnlockUserAccount(ui);
unlocked = true;
}
else
{
UserInfoProvider.SetUserInfo(ui);
}
LoadData();
if (unlocked)
{
ShowConfirmation(GetString("Administration-User.ExtendPasswordUnlock"));
}
else
{
ShowConfirmation(GetString("Administration-User.ExtendPassword"));
}
}
/// <summary>
/// Reset token to initial state.
/// </summary>
protected void btnResetToken_Click(object sender, EventArgs e)
{
// Check "modify" permission
if (!CurrentUser.IsAuthorizedPerResource("CMS.Users", "Modify"))
{
RedirectToAccessDenied("CMS.Users", "Modify");
}
MFAuthenticationHelper.ResetTokenAndIterationForUser(ui);
LoadData();
ShowConfirmation(GetString("administration-user.token.reset"));
}
#endregion
#region "Private methods"
/// <summary>
/// Checks if currently logged user changes his user name.
/// </summary>
private bool CurrentUserChangedUserName()
{
return (CurrentUser != null) && (CurrentUser.UserID == ui.UserID) && (CurrentUser.UserName != ui.UserName);
}
#endregion
}