From f1c8fb7b17e3676203bcf2a5c91fcd1887d5b40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20J=C3=B8rgensen?= Date: Sat, 15 Oct 2016 22:00:03 +0200 Subject: [PATCH] Added validation to the user edit-form in the backoffice --- .../umbraco/users/EditUser.aspx.cs | 77 +++++++++++++++++-- src/umbraco.controls/pane.cs | 16 ++++ 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/users/EditUser.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/users/EditUser.aspx.cs index d9605506b4..a25d62665b 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/users/EditUser.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/users/EditUser.aspx.cs @@ -30,6 +30,8 @@ using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services; using PropertyType = umbraco.cms.businesslogic.propertytype.PropertyType; +using System.Text.RegularExpressions; +using System.Text; namespace umbraco.cms.presentation.user { @@ -43,11 +45,16 @@ namespace umbraco.cms.presentation.user CurrentApp = DefaultApps.users.ToString(); } protected HtmlTable macroProperties; - protected TextBox uname = new TextBox(); - protected TextBox lname = new TextBox(); + protected TextBox uname = new TextBox() { ID = "uname" }; + protected RequiredFieldValidator unameValidator = new RequiredFieldValidator(); + protected TextBox lname = new TextBox() { ID = "lname" }; + protected RequiredFieldValidator lnameValidator = new RequiredFieldValidator(); + protected CustomValidator lnameCustomValidator = new CustomValidator(); protected PlaceHolder passw = new PlaceHolder(); protected CheckBoxList lapps = new CheckBoxList(); - protected TextBox email = new TextBox(); + protected TextBox email = new TextBox() { ID = "email" }; + protected RequiredFieldValidator emailValidator = new RequiredFieldValidator(); + protected CustomValidator emailCustomValidator = new CustomValidator(); protected DropDownList userType = new DropDownList(); protected DropDownList userLanguage = new DropDownList(); protected CheckBox NoConsole = new CheckBox(); @@ -173,10 +180,10 @@ namespace umbraco.cms.presentation.user passw.Controls.Add(passwordChanger); passw.Controls.Add(validatorContainer); - pp.addProperty(ui.Text("user", "username", UmbracoUser), uname); - pp.addProperty(ui.Text("user", "loginname", UmbracoUser), lname); + pp.addProperty(ui.Text("user", "username", UmbracoUser), uname, unameValidator); + pp.addProperty(ui.Text("user", "loginname", UmbracoUser), lname, lnameValidator, lnameCustomValidator); pp.addProperty(ui.Text("user", "password", UmbracoUser), passw); - pp.addProperty(ui.Text("email", UmbracoUser), email); + pp.addProperty(ui.Text("email", UmbracoUser), email, emailValidator, emailCustomValidator); pp.addProperty(ui.Text("user", "usertype", UmbracoUser), userType); pp.addProperty(ui.Text("user", "language", UmbracoUser), userLanguage); @@ -219,6 +226,50 @@ namespace umbraco.cms.presentation.user sectionValidator.CssClass = "error"; sectionValidator.Style.Add("color", "red"); + unameValidator.ControlToValidate = uname.ID; + unameValidator.Display = ValidatorDisplay.Dynamic; + unameValidator.ErrorMessage = ui.Text("defaultdialogs", "requiredField", UmbracoUser); + unameValidator.CssClass = "error"; + unameValidator.Style.Add("color", "red"); + unameValidator.Style.Add("margin-left", "5px"); + unameValidator.Style.Add("line-height", "28px"); + + lnameValidator.ControlToValidate = lname.ID; + lnameValidator.Display = ValidatorDisplay.Dynamic; + lnameValidator.ErrorMessage = ui.Text("defaultdialogs", "requiredField", UmbracoUser); + lnameValidator.CssClass = "error"; + lnameValidator.Style.Add("color", "red"); + lnameValidator.Style.Add("margin-left", "5px"); + lnameValidator.Style.Add("line-height", "28px"); + + lnameCustomValidator.ServerValidate += LnameCustomValidator_ServerValidate; + lnameCustomValidator.Display = ValidatorDisplay.Dynamic; + lnameCustomValidator.ControlToValidate = lname.ID; + var localizedLname = ui.Text("user", "loginname", UmbracoUser); + lnameCustomValidator.ErrorMessage = ui.Text("errorHandling", "errorExistsWithoutTab", localizedLname, UmbracoUser); + lnameCustomValidator.CssClass = "error"; + lnameCustomValidator.Style.Add("color", "red"); + lnameCustomValidator.Style.Add("margin-left", "5px"); + lnameCustomValidator.Style.Add("line-height", "28px"); + + emailValidator.ControlToValidate = email.ID; + emailValidator.Display = ValidatorDisplay.Dynamic; + emailValidator.ErrorMessage = ui.Text("defaultdialogs", "requiredField", UmbracoUser); + emailValidator.CssClass = "error"; + emailValidator.Style.Add("color", "red"); + emailValidator.Style.Add("margin-left", "5px"); + emailValidator.Style.Add("line-height", "28px"); + + emailCustomValidator.ServerValidate += EmailCustomValidator_ServerValidate; + emailCustomValidator.Display = ValidatorDisplay.Dynamic; + emailCustomValidator.ControlToValidate = email.ID; + var localizedEmail = ui.Text("general", "email", UmbracoUser); + emailCustomValidator.ErrorMessage = ui.Text("errorHandling", "errorRegExpWithoutTab", localizedEmail, UmbracoUser); + emailCustomValidator.CssClass = "error"; + emailCustomValidator.Style.Add("color", "red"); + emailCustomValidator.Style.Add("margin-left", "5px"); + emailCustomValidator.Style.Add("line-height", "28px"); + SetupForm(); SetupChannel(); @@ -227,6 +278,16 @@ namespace umbraco.cms.presentation.user .SyncTree(UID.ToString(), IsPostBack); } + private void LnameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args) + { + var usersWithLoginName = ApplicationContext.Services.UserService.GetByUsername(lname.Text); + args.IsValid = usersWithLoginName == null || usersWithLoginName.Id == u.Id; + } + + private void EmailCustomValidator_ServerValidate(object source, ServerValidateEventArgs args) + { + args.IsValid = Regex.IsMatch(email.Text.Trim(), @"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$"); + } void sectionValidator_ServerValidate(object source, ServerValidateEventArgs args) { @@ -531,7 +592,9 @@ namespace umbraco.cms.presentation.user } else { - ClientTools.ShowSpeechBubble(speechBubbleIcon.error, ui.Text("speechBubbles", "editUserError", UmbracoUser), ""); + ClientTools.ShowSpeechBubble(speechBubbleIcon.error, + ui.Text("speechBubbles", "validationFailedHeader", UmbracoUser), + ui.Text("speechBubbles", "validationFailedMessage", UmbracoUser)); } } diff --git a/src/umbraco.controls/pane.cs b/src/umbraco.controls/pane.cs index 0facc946a3..499695debf 100644 --- a/src/umbraco.controls/pane.cs +++ b/src/umbraco.controls/pane.cs @@ -36,6 +36,22 @@ namespace umbraco.uicontrols set { m_title = value; } } + public void addProperty(string Caption, Control C, params BaseValidator[] validators) + { + + PropertyPanel pp = new PropertyPanel(); + pp.Controls.Add(C); + + foreach (var validator in validators) + { + validator.Display = ValidatorDisplay.Dynamic; + pp.Controls.Add(validator); + } + pp.Text = Caption; + + this.Controls.Add(pp); + } + public void addProperty(string Caption, Control C) {