protected override FurtherValidationResult Validate(ContentModifierForm form, ContentFieldValidatorConfiguration config, ContentType contentType, ContentFieldDefinition fieldDefinition) { var valCfg = config.DirectCastTo <TextFieldLengthValidatorConfiguration>(); var f = form.DirectCastTo <TextFieldModifierForm>(); if (f.Val == null) { return(null); } var result = new FurtherValidationResult(); var dispName = fieldDefinition.Config.Label ?? fieldDefinition.FieldName; if (valCfg.MinLength.HasValue) { var ml = valCfg.MinLength.Value; if (f.Val.Length < ml) { result.AddError(nameof(f.Val), string.Format(valCfg.MinLengthErrorMessage, dispName, ml)); } } if (valCfg.MaxLength.HasValue) { var ml = valCfg.MaxLength.Value; if (f.Val.Length > ml) { result.AddError(nameof(f.Val), string.Format(valCfg.MaxLengthErrorMessage, dispName, ml)); } } return(result); }
protected override FurtherValidationResult Validate(ContentModifierForm form, ContentFieldValidatorConfiguration config, ContentType contentType, ContentFieldDefinition fieldDefinition) { var f = form.DirectCastTo <NumberFieldModifierForm>(); var valCfg = config.DirectCastTo <NumberFieldRangeValidatorConfiguration>(); if (!f.Val.HasValue) { return(null); } var result = new FurtherValidationResult(); var dispName = fieldDefinition.Config.Label ?? fieldDefinition.FieldName; if (valCfg.MinValue.HasValue) { var mv = valCfg.MinValue.Value; if (valCfg.MinValueIsInclusive) { if (f.Val.Value < mv) { result.AddError( nameof(f.Val), string.Format(valCfg.MinValueInclusiveErrorMessage, dispName, mv)); } } else { if (f.Val.Value <= mv) { result.AddError( nameof(f.Val), string.Format(valCfg.MinValueErrorMessage, dispName, mv)); } } } if (valCfg.MaxValue.HasValue) { var mv = valCfg.MaxValue.Value; if (valCfg.MaxValueIsInclusive) { if (f.Val.Value > mv) { result.AddError( nameof(f.Val), string.Format(valCfg.MaxValueInclusiveErrorMessage, dispName, mv)); } } else { if (f.Val.Value >= mv) { result.AddError( nameof(f.Val), string.Format(valCfg.MaxValueErrorMessage, dispName, mv)); } } } return(result); }
public override FurtherValidationResult ValidateModifierForm(IDictionary <string, ContentModifierForm> modifierForm, ApplicationRole datum, DatumModifyOperation operation, Type datumType) { var result = new FurtherValidationResult(); if (operation.IsDeleteOperation()) { if (datum.Name == RequiredRoleInfo.SuperAdmin.Name || datum.Name == RequiredRoleInfo.Authenticated.Name || datum.Name == RequiredRoleInfo.Guest.Name) { result.AddError($"{FORM_KEY}.{nameof(RoleDatumModifierForm.Name)}", $"{RequiredRoleInfo.SuperAdmin.Name}, {RequiredRoleInfo.Authenticated.Name}, " + $"and {RequiredRoleInfo.Guest.Name} are reserved by the system and should not " + $"be deleted."); } } else { var fm = modifierForm[FORM_KEY].DirectCastTo <RoleDatumModifierForm>(); if (operation.IsCreatingNewDatum) { if (fm.Name == RequiredRoleInfo.SuperAdmin.Name || fm.Name == RequiredRoleInfo.Authenticated.Name || fm.Name == RequiredRoleInfo.Guest.Name) { result.AddError($"{FORM_KEY}.{nameof(RoleDatumModifierForm.Name)}", $"{RequiredRoleInfo.SuperAdmin.Name}, {RequiredRoleInfo.Authenticated.Name}, " + $"and {RequiredRoleInfo.Guest.Name} are reserved by the system and no new " + $"role should use their name."); } } if ((datum.Name == RequiredRoleInfo.SuperAdmin.Name || datum.Name == RequiredRoleInfo.Authenticated.Name || datum.Name == RequiredRoleInfo.Guest.Name) && fm.Name != datum.Name) { result.AddError($"{FORM_KEY}.{nameof(RoleDatumModifierForm.Name)}", $"{RequiredRoleInfo.SuperAdmin.Name}, {RequiredRoleInfo.Authenticated.Name}, " + $"and {RequiredRoleInfo.Guest.Name} are reserved by the system and should not " + $"have their name be modified."); } } return(result); }
protected override FurtherValidationResult Validate(ContentModifierForm form, ContentFieldValidatorConfiguration config, ContentType contentType, ContentFieldDefinition fieldDefinition) { var cfg = config.DirectCastTo <TextFieldRegexValidatorConfiguration>(); var fm = form.DirectCastTo <TextFieldModifierForm>(); if (fm.Val == null) { return(null); } if (!cfg.Regex.IsMatch(fm.Val)) { var dispName = fieldDefinition.Config.Label ?? fieldDefinition.FieldName; var res = new FurtherValidationResult(); res.AddError(nameof(fm.Val), string.Format(cfg.ErrorMessage, dispName, cfg.Pattern)); return(res); } return(null); }
public FurtherValidationResult ValidateModifierForm(IDictionary <string, ContentModifierForm> modifierForm, string contentId, string operationName) { var operation = FindModifyOperation(operationName); var content = FindProtoContent(contentId, operationName); var result = new FurtherValidationResult(); foreach (var h in Handlers) { var r = h.ValidateModifierForm(modifierForm, content, operation, ContentType); if (r != null && r.HasError) { foreach (var kv in r.Errors) { foreach (var m in kv.Value) { result.AddError(kv.Key, m); } } } } return(result); }
public FurtherValidationResult ValidateModifierForm(IDictionary <string, ContentModifierForm> modifierForm, ProtoContent content, ContentModifyOperation operation, ContentType contentType) { var result = new FurtherValidationResult(); foreach (var fd in contentType.Fields) { if (!operation.IsAny(fd.Config.HandledModifyOperationNames)) { continue; } var fdr = fd.FieldFinder(); var mod = fd.FieldModifier(); var field = operation.Is(CommonFieldModifyOperationsProvider.CREATE_OPERATION_NAME) ? Activator.CreateInstance(fd.ModelType).DirectCastTo <ContentField>() : fdr.GetModel(content, fd); modifierForm.TryGetValue(fd.FieldName, out var form); if (form == null) { var testForm = mod.BuildModifierForm(field, operation, content, fd); if (testForm != null) { throw new HttpException(400, $"ProtoCMS: content modifier form for field '{fd.FieldName}' " + $"is required and must be an instance of " + $"'{testForm.GetType().FullName}'."); } continue; } foreach (var fvkv in fd.Config.Validators) { var hasValidator = false; foreach (var dv in ContentFieldValidator.DefinedValidators) { if (dv.Name != fvkv.Key || !dv.HandledFormTypes.Contains(form.GetType())) { continue; } hasValidator = true; var valCfg = fvkv.Value ?? Activator.CreateInstance(dv.ConfigType) .DirectCastTo <ContentFieldValidatorConfiguration>(); if (operation.IsAny(valCfg.ModifyOperationNamesThatIgnoreValidation)) { continue; } if (valCfg.GetType() != dv.ConfigType) { throw new InvalidOperationException( $"ProtoCMS: content field validator '{dv.Name}' (for validating form " + $"'{form.GetType().FullName}') can only accept config instance of type " + $"'{dv.ConfigType.FullName}'."); } var valRes = dv.ValidateForm(form, valCfg, contentType, fd); if (valRes != null) { foreach (var kv in valRes.Errors) { if (kv.Value == null || kv.Value.Length == 0) { continue; } foreach (var err in kv.Value) { result.AddError($"{fd.FieldName}.{kv.Key}", err); } } } } if (!hasValidator) { throw new InvalidOperationException($"ProtoCMS: there's no content field validator " + $"named '{fvkv.Key}' that can handle form " + $"'{form.GetType().FullName}' validation."); } } } return(result); }
public override FurtherValidationResult ValidateModifierForm( IDictionary <string, ContentModifierForm> modifierForm, ApplicationUser datum, DatumModifyOperation operation, Type datumType) { var result = new FurtherValidationResult(); if (operation.IsCreateOrUpdateOperation()) { var fm = modifierForm[FORM_KEY].DirectCastTo <UserDatumModifierForm>(); if (operation.IsCreateOperation()) { var existingUser = _userMgr.Users.FirstOrDefault(x => x.UserName == fm.UserName); if (existingUser != null) { result.AddError($"{FORM_KEY}.{nameof(fm.UserName)}", $"Username '{fm.UserName}' is already taken."); } } var checkPassword = operation.IsCreateOperation() || operation.IsUpdateOperation() && fm.ChangePassword; if (checkPassword) { var validator = _userMgr.PasswordValidator; if (fm.Password == null) { result.AddError($"{FORM_KEY}.{nameof(fm.Password)}", "Password is required."); } else { var ir = validator.ValidateAsync(fm.Password).Result; if (!ir.Succeeded) { foreach (var err in ir.Errors) { result.AddError($"{FORM_KEY}.{nameof(fm.Password)}", err); } } if (fm.Password != fm.PasswordConfirmation) { result.AddError($"{FORM_KEY}.{nameof(fm.PasswordConfirmation)}", "Password and password confirmation must match."); } } } } if (operation.IsDeleteOperation()) { var rctx = ProtoCmsRuntimeContext.Current; if (rctx.CurrentUser != null && rctx.CurrentUser.Id == datum.Id) { result.AddError($"{FORM_KEY}.{nameof(UserDatumModifierForm.UserName)}", "You may not delete yourself."); } var superAdminRoleName = RequiredRoleInfo.SuperAdmin.Name; var superadmins = (from u in _dbContext.Users from ur in u.Roles join r in _dbContext.Roles on ur.RoleId equals r.Id where r.Name == superAdminRoleName select u).ToArray(); if (superadmins.Length == 1 && superadmins.FirstOrDefault(x => x.Id == datum.Id) != null) { result.AddError($"{FORM_KEY}.{nameof(UserDatumModifierForm.UserName)}", "You may not delete the only superadmin left in the system."); } } return(result); }
protected override FurtherValidationResult Validate(ContentModifierForm form, ContentFieldValidatorConfiguration config, ContentType type, ContentFieldDefinition definition) { var genericSimpleFormType = FindGenericSimpleFormType(form.GetType()); var valType = genericSimpleFormType.GenericTypeArguments[0]; if (valType.IsValueType && !(valType.IsGenericType && valType.GetGenericTypeDefinition() == typeof(Nullable <>))) { return(null); } var simpleFieldFormType = typeof(SimpleContentFieldModifierForm <>).MakeGenericType(valType); var valProp = simpleFieldFormType.GetProperty(nameof(SimpleContentFieldModifierForm <string> .Val)); var val = valProp?.GetValue(form); var fcfg = definition.Config; var cfg = config.DirectCastTo <SimpleFieldRequiredValidatorConfiguration>(); var dispName = fcfg.Label ?? definition.FieldName; var errMsg = cfg.RequiredNullableErrorMessage; if (val is string) { var valStr = val.DirectCastTo <string>(); if (!string.IsNullOrWhiteSpace(valStr)) { return(null); } errMsg = cfg.RequiredStringErrorMessage; } else if (val != null) { if (typeof(IEnumerable).IsAssignableFrom(valType)) { //https://stackoverflow.com/questions/5604168/calculating-count-for-ienumerable-non-generic var source = (IEnumerable)val; var count = 0; if (source is ICollection sourceAsCollection) { count = sourceAsCollection.Count; } else { var enumerator = source.GetEnumerator(); try { while (enumerator.MoveNext()) { count++; } } finally { if (enumerator is IDisposable disposableObj) { disposableObj.Dispose(); } } } if (count > 0) { return(null); } } else { return(null); } } var vr = new FurtherValidationResult(); vr.AddError(nameof(SimpleContentFieldModifierForm <string> .Val), string.Format(errMsg, new object[] { dispName })); return(vr); }