Esempio n. 1
0
        // Methods //////////////////////////////////////////////////////////////////////
		public override void SetData(object data)
		{
		    _originalData = data as PasswordField.PasswordData;
		    //SetStarsOnTextBoxes();

            #region template

            if ((!UseBrowseTemplate && !UseEditTemplate) && !UseInlineEditTemplate)
                return;

            var title = GetLabelForTitleControl() as Label;
            var desc = GetLabelForDescription() as Label;
            var pwd1 = GetInnerControl() as TextBox;

		    var title2 = GetLabel(String.Concat(TitleControlID, "2")) as Label;
            var desc2 = GetLabel(String.Concat(DescriptionControlID, "2")) as Label;
            var pwd2 = GetPwdControl(InnerControl2ID) as TextBox;

            if (title != null) title.Text = this.Field.DisplayName;
            if (desc != null) desc.Text = this.Field.Description;
            if (pwd1 != null) SetStarsOnTextBox(pwd1);

            var passwordFieldSetting = this.Field.FieldSetting as PasswordFieldSetting;
            if (passwordFieldSetting != null && passwordFieldSetting.ReenterDescription != null && desc2 != null)
                desc2.Text = passwordFieldSetting.ReenterDescription;

            if (passwordFieldSetting != null && passwordFieldSetting.ReenterTitle != null && title2 != null)
                title2.Text = passwordFieldSetting.ReenterTitle;

            if (pwd2 != null) SetStarsOnTextBox(pwd2);

            #endregion
		}
Esempio n. 2
0
        public override FieldValidationResult ValidateData(object value, Field field)
        {
            if (value == null)
            {
                return(new FieldValidationResult(CannotBeNullError));
            }

            var data = value as PasswordField.PasswordData;

            if (data == null)
            {
                var sdata = value as string;
                if (sdata != null)
                {
                    data = new PasswordField.PasswordData {
                        Text = sdata
                    }
                }
                ;
            }
            if (data == null)
            {
                return(new FieldValidationResult(DataMustBePasswordData));
            }

            // check password
            var hash = data.Hash;

            if (!String.IsNullOrEmpty(data.Text))
            {
                hash      = ((PasswordField)field).EncodePassword(data.Text);
                data.Hash = hash;
            }

            var user = field.Content.ContentHandler as User;

            if (user == null)
            {
                var checkResult = this.CheckPassword(hash, null);
                if (!checkResult.Valid)
                {
                    var validationResult = new PasswordFieldValidationResult(checkResult.Message);
                    validationResult.CheckResult = checkResult;
                    return(validationResult);
                }
            }
            else
            {
                var checkResult = this.CheckPassword(hash, user.GetOldPasswords());
                if (!checkResult.Valid)
                {
                    var validationResult = new PasswordFieldValidationResult(checkResult.Message);
                    validationResult.CheckResult = checkResult;
                    return(validationResult);
                }
            }

            var  origValue            = field.OriginalValue as PasswordField.PasswordData;
            bool isValidOriginalValue = !String.IsNullOrEmpty(origValue.Hash) || this.Compulsory != true;

            if (data.Text == null && data.Hash == null && !isValidOriginalValue)
            {
                return(new FieldValidationResult(OriginalIsInvalidError));
            }

            if (data.Text != null)
            {
                return(base.ValidateData(data.Text, field));
            }

            return(FieldValidationResult.Successful);
        }
Esempio n. 3
0
        public override FieldValidationResult ValidateData(object value, Field field)
        {
            if (value == null)
                return new FieldValidationResult(CannotBeNullError);

            var data = value as PasswordField.PasswordData;
            if (data == null)
            {
                var sdata = value as string;
                if (sdata != null)
                    data = new PasswordField.PasswordData { Text = sdata };
            }
            if (data == null)
                return new FieldValidationResult(DataMustBePasswordData);

            // check password
            var user = field.Content.ContentHandler as User;
            if (user == null)
            {
                var checkResult = this.CheckPassword(data.Text, null);
                if (!checkResult.Valid)
                {
                    var validationResult = new PasswordFieldValidationResult(checkResult.Message);
                    validationResult.CheckResult = checkResult;
                    return validationResult;
                }
            }
            else
            {
                var checkResult = this.CheckPassword(data.Text, user.GetOldPasswords());
                if (!checkResult.Valid)
                {
                    var validationResult = new PasswordFieldValidationResult(checkResult.Message);
                    validationResult.CheckResult = checkResult;
                    return validationResult;
                }
            }

            var origValue = field.OriginalValue as PasswordField.PasswordData;
            bool isValidOriginalValue = !String.IsNullOrEmpty(origValue.Hash) || this.Compulsory != true;

            if (data.Text == null && data.Hash == null && !isValidOriginalValue)
                return new FieldValidationResult(OriginalIsInvalidError);

            if (data.Text != null)
                return base.ValidateData(data.Text, field);

            return FieldValidationResult.Successful;
        }
Esempio n. 4
0
        public void FieldValidation_PasswordAndControl()
        {
            string ctd;
            bool isValid;
            FieldValidationResult validationResult;

            PrivateType fieldSettingAccessor = new PrivateType(typeof(PasswordFieldSetting));
            var defaultStars = SenseNet.Portal.UI.Controls.Password.DefaultStars;
            //====

            ctd = @"<?xml version='1.0' encoding='utf-8'?>
				<ContentType name='ValidatedContent' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'>
					<Fields>
						<Field name='PasswordTest' type='Password'>
							<Configuration>
								<Compulsory>true</Compulsory>
								<MinLength>3</MinLength>
								<MaxLength>8</MaxLength>
								<Regex>^[a-zA-Z0-9]*$</Regex>
							</Configuration>
						</Field>
					</Fields>
				</ContentType>";
            ContentTypeInstaller.InstallContentType(ctd);

            SNC.Content content = SNC.Content.CreateNew("ValidatedContent", _testRoot, "abc");
            var pwdField = content.Fields["PasswordTest"];
            var pwdControl = new SenseNet.Portal.UI.Controls.Password();
            var pwdControlAcc = new PasswordAccessor(pwdControl);
            pwdControlAcc.ConnectToField(pwdField);
            pwdControlAcc.SetDataInternal();

            //-- stars are not overridden and original is invalid (null)
            pwdControlAcc.InputTextBox.Text = defaultStars;
            pwdControlAcc.InputTextBox2.Text = defaultStars;
            pwdField.SetData(pwdControl.GetData());

            isValid = content.IsValid;
            validationResult = content.Fields["PasswordTest"].ValidationResult;
            Assert.IsFalse(isValid, "#1");
            Assert.IsTrue(validationResult.Category == PasswordFieldSetting.OriginalIsInvalidError, "#2");

            //-- inputs are not eqals
            pwdControlAcc.InputTextBox.Text = "asdf";
            pwdControlAcc.InputTextBox2.Text = "qwer";
            var expectedExceptionWasThrown = false;
            try
            {
                pwdField.SetData(pwdControl.GetData());
            }
            catch (SenseNet.Portal.UI.Controls.FieldControlDataException)
            {
                expectedExceptionWasThrown = true;
            }
            isValid = content.IsValid;
            validationResult = content.Fields["PasswordTest"].ValidationResult;
            Assert.IsTrue(expectedExceptionWasThrown, "#3");
            Assert.IsFalse(isValid, "#4");

            //-- correct #1
            pwdControlAcc.InputTextBox.Text = "zxcv";
            pwdControlAcc.InputTextBox2.Text = "zxcv";
            pwdField.SetData(pwdControl.GetData());

            isValid = content.IsValid;
            validationResult = content.Fields["PasswordTest"].ValidationResult;
            Assert.IsTrue(isValid, "#6");
            Assert.IsTrue(validationResult == FieldValidationResult.Successful, "#7");

            //-- unchanged control does not change the content

            content.Save();
            var contentId = content.Id;
            var hash = ((PasswordField.PasswordData)content.Fields["PasswordTest"].OriginalValue).Hash;

            SNC.Content content2 = SNC.Content.Load(contentId);
            pwdField = content.Fields["PasswordTest"];
            pwdControl = new SenseNet.Portal.UI.Controls.Password();
            pwdControlAcc = new PasswordAccessor(pwdControl);
            pwdControlAcc.ConnectToField(pwdField);
            pwdControlAcc.SetDataInternal();
            //-- user does not change the values
            pwdField.SetData(pwdControl.GetData());
            isValid = content.IsValid;
            validationResult = content.Fields["PasswordTest"].ValidationResult;
            Assert.IsTrue(isValid, "#8");
            Assert.IsTrue(validationResult == FieldValidationResult.Successful, "#9");
            content.Save();
            SNC.Content content3 = SNC.Content.Load(contentId);
            var hash3 = ((PasswordField.PasswordData)content3["PasswordTest"]).Hash;
            Assert.IsTrue(hash == hash3, "#10");

            //-- setting back original value
            content["PasswordTest"] = new PasswordField.PasswordData { Hash = hash };
            isValid = content.IsValid;
            content.Save();
            SNC.Content content4 = SNC.Content.Load(contentId);
            var hash4 = ((PasswordField.PasswordData)content4["PasswordTest"]).Hash;
            Assert.IsTrue(hash == hash4, "#11");

        }
Esempio n. 5
0
        public void FieldValidation_Password()
        {
            string ctd;
            bool isValid;
            FieldValidationResult validationResult;

            PrivateType fieldSettingAccessor = new PrivateType(typeof(PasswordFieldSetting));
            string minLengthError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("MinLengthName");
            string maxLengthError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("MaxLengthName");
            string regexError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("RegexName");
            //string compulsoryError = (string)fieldSettingAccessor.GetStaticFieldOrProperty("CompulsoryName");

            //====

            ctd = @"<?xml version='1.0' encoding='utf-8'?>
				<ContentType name='ValidatedContent' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'>
					<Fields>
						<Field name='PasswordTest' type='Password'>
							<Configuration>
								<MinLength>3</MinLength>
								<MaxLength>8</MaxLength>
								<Regex>^[a-zA-Z0-9]*$</Regex>
							</Configuration>
						</Field>
					</Fields>
				</ContentType>";
            ContentTypeInstaller.InstallContentType(ctd);

            SNC.Content content = SNC.Content.CreateNew("ValidatedContent", _testRoot, "abc");

            content["PasswordTest"] = new PasswordField.PasswordData { Text = "ab" };
            isValid = content.IsValid;
            validationResult = content.Fields["PasswordTest"].ValidationResult;
            Assert.IsFalse(isValid, "#1");
            Assert.IsTrue(validationResult.Category == ShortTextFieldSetting.MinLengthName, "#2");
            Assert.IsTrue((int)content.Fields["PasswordTest"].ValidationResult.GetParameter(ShortTextFieldSetting.MinLengthName) == 3, "#3");

            content["PasswordTest"] = new PasswordField.PasswordData { Text = "abcdefgh01" };
            isValid = content.IsValid;
            Assert.IsFalse(isValid, "#4");
            Assert.IsTrue(content.Fields["PasswordTest"].ValidationResult.Category == ShortTextFieldSetting.MaxLengthName, "#5");
            Assert.IsTrue((int)content.Fields["PasswordTest"].ValidationResult.GetParameter(ShortTextFieldSetting.MaxLengthName) == 8, "#6");

            content["PasswordTest"] = new PasswordField.PasswordData { Text = "!@#$%^" };
            isValid = content.IsValid;
            Assert.IsFalse(isValid, "#7");
            Assert.IsTrue(content.Fields["PasswordTest"].ValidationResult.Category == ShortTextFieldSetting.RegexName, "#8");

            content["PasswordTest"] = new PasswordField.PasswordData { Text = "Correct" };
            isValid = content.IsValid;
            Assert.IsTrue(isValid, "#9");
        }