상속: SenseNet.ContentRepository.Schema.FieldValidationResult
예제 #1
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);
        }
예제 #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 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;
        }