Esempio n. 1
0
        private static void ValidateFieldNames(IUpsertCommand command, FieldNames?fields, string path, AddValidation e, Func <string, bool> isAllowed)
        {
            if (fields != null)
            {
                fields.Foreach((fieldName, fieldIndex) =>
                {
                    var fieldPrefix = $"{path}[{fieldIndex}]";

                    var field = command?.Fields?.FirstOrDefault(x => x.Name == fieldName);

                    if (string.IsNullOrWhiteSpace(fieldName))
                    {
                        e(Not.Defined("Field"), fieldPrefix);
                    }
                    else if (field == null && !isAllowed(fieldName))
                    {
                        e(T.Get("schemas.fieldNotInSchema"), fieldPrefix);
                    }
                    else if (field?.Properties?.IsUIProperty() == true)
                    {
                        e(T.Get("schemas.fieldCannotBeUIField"), fieldPrefix);
                    }
                });

                foreach (var duplicate in fields.Duplicates())
                {
                    if (!string.IsNullOrWhiteSpace(duplicate))
                    {
                        e(T.Get("schemas.duplicateFieldName", new { field = duplicate }), path);
                    }
                }
            }
        }
Esempio n. 2
0
        public static void CanReorder(ReorderFields command, Schema schema)
        {
            Guard.NotNull(command, nameof(command));

            IArrayField?arrayField = null;

            if (command.ParentFieldId != null)
            {
                arrayField = GuardHelper.GetArrayFieldOrThrow(schema, command.ParentFieldId.Value, false);
            }

            Validate.It(e =>
            {
                if (command.FieldIds == null)
                {
                    e(Not.Defined(nameof(command.FieldIds)), nameof(command.FieldIds));
                }

                if (arrayField == null)
                {
                    ValidateFieldIds(command, schema.FieldsById, e);
                }
                else
                {
                    ValidateFieldIds(command, arrayField.FieldsById, e);
                }
            });
        }
Esempio n. 3
0
        private static void ValidateFieldNames(Schema schema, FieldNames?fields, string path, AddValidation e, Func <string, bool> isAllowed)
        {
            if (fields != null)
            {
                fields.Foreach((fieldName, fieldIndex) =>
                {
                    var fieldPrefix = $"{path}[{fieldIndex}]";

                    var field = schema.FieldsByName.GetOrDefault(fieldName ?? string.Empty);

                    if (string.IsNullOrWhiteSpace(fieldName))
                    {
                        e(Not.Defined("Field"), fieldPrefix);
                    }
                    else if (field == null && !isAllowed(fieldName))
                    {
                        e(T.Get("schemas.fieldNotInSchema"), fieldPrefix);
                    }
                    else if (field?.IsUI() == true)
                    {
                        e(T.Get("schemas.fieldCannotBeUIField"), fieldPrefix);
                    }
                });

                foreach (var duplicate in fields.Duplicates())
                {
                    if (!string.IsNullOrWhiteSpace(duplicate))
                    {
                        e(T.Get("schemas.duplicateFieldName", new { field = duplicate }), path);
                    }
                }
            }
        }
Esempio n. 4
0
        private static void ValidateField(UpsertSchemaFieldBase field, string prefix, AddValidation e)
        {
            if (!field.Name.IsPropertyName())
            {
                e(Not.ValidJavascriptName(nameof(field.Name)), $"{prefix}.{nameof(field.Name)}");
            }

            if (field.Properties == null)
            {
                e(Not.Defined(nameof(field.Properties)), $"{prefix}.{nameof(field.Properties)}");
            }
            else
            {
                if (field.Properties.IsUIProperty())
                {
                    if (field.IsHidden)
                    {
                        e(T.Get("schemas.uiFieldCannotBeHidden"), $"{prefix}.{nameof(field.IsHidden)}");
                    }

                    if (field.IsDisabled)
                    {
                        e(T.Get("schemas.uiFieldCannotBeDisabled"), $"{prefix}.{nameof(field.IsDisabled)}");
                    }
                }

                var errors = FieldPropertiesValidator.Validate(field.Properties);

                errors.Foreach((x, _) => x.WithPrefix($"{prefix}.{nameof(field.Properties)}").AddTo(e));
            }
        }
Esempio n. 5
0
        public static void CanChangePlan(ChangePlan command, AppPlan plan, IAppPlansProvider appPlans)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot change plan.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.PlanId))
                {
                    e(Not.Defined("Plan id"), nameof(command.PlanId));
                    return;
                }

                if (appPlans.GetPlan(command.PlanId) == null)
                {
                    e("A plan with this id does not exist.", nameof(command.PlanId));
                }

                if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor))
                {
                    e("Plan can only changed from the user who configured the plan initially.");
                }

                if (string.Equals(command.PlanId, plan?.PlanId, StringComparison.OrdinalIgnoreCase))
                {
                    e("App has already this plan.");
                }
            });
        }
Esempio n. 6
0
        public static void CanChangePlan(ChangePlan command, IAppEntity app, IAppPlansProvider appPlans)
        {
            Guard.NotNull(command);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.PlanId))
                {
                    e(Not.Defined(nameof(command.PlanId)), nameof(command.PlanId));
                    return;
                }

                if (appPlans.GetPlan(command.PlanId) == null)
                {
                    e(T.Get("apps.plans.notFound"), nameof(command.PlanId));
                }

                var plan = app.Plan;

                if (!string.IsNullOrWhiteSpace(command.PlanId) && plan != null && !plan.Owner.Equals(command.Actor))
                {
                    e(T.Get("apps.plans.notPlanOwner"));
                }
            });
        }
Esempio n. 7
0
        public static void CanUpdate(Schema schema, UpdateField command)
        {
            Guard.NotNull(command, nameof(command));

            var field = GuardHelper.GetFieldOrThrow(schema, command.FieldId, command.ParentFieldId);

            if (field.IsLocked)
            {
                throw new DomainException("Schema field is already locked.");
            }

            Validate.It(() => "Cannot update field.", e =>
            {
                if (command.Properties == null)
                {
                    e(Not.Defined("Properties"), nameof(command.Properties));
                }
                else
                {
                    var errors = FieldPropertiesValidator.Validate(command.Properties);

                    errors.Foreach(x => x.WithPrefix(nameof(command.Properties)).AddTo(e));
                }
            });
        }
Esempio n. 8
0
        public static Task CanCreate(CreateRule command, IAppProvider appProvider)
        {
            Guard.NotNull(command);

            return(Validate.It(() => "Cannot create rule.", async e =>
            {
                if (command.Trigger == null)
                {
                    e(Not.Defined("Trigger"), nameof(command.Trigger));
                }
                else
                {
                    var errors = await RuleTriggerValidator.ValidateAsync(command.AppId.Id, command.Trigger, appProvider);

                    errors.Foreach(x => x.AddTo(e));
                }

                if (command.Action == null)
                {
                    e(Not.Defined("Action"), nameof(command.Action));
                }
                else
                {
                    var errors = command.Action.Validate();

                    errors.Foreach(x => x.AddTo(e));
                }
            }));
        }
Esempio n. 9
0
        public static Task CanUpdate(UpdateRule command, Guid appId, IAppProvider appProvider)
        {
            Guard.NotNull(command);

            return(Validate.It(() => "Cannot update rule.", async e =>
            {
                if (command.Trigger == null && command.Action == null && command.Name == null)
                {
                    e(Not.Defined("Either trigger, action or name"), nameof(command.Trigger), nameof(command.Action));
                }

                if (command.Trigger != null)
                {
                    var errors = await RuleTriggerValidator.ValidateAsync(appId, command.Trigger, appProvider);

                    errors.Foreach(x => x.AddTo(e));
                }

                if (command.Action != null)
                {
                    var errors = command.Action.Validate();

                    errors.Foreach(x => x.AddTo(e));
                }
            }));
        }
Esempio n. 10
0
        public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
        {
            Guard.NotNull(command, nameof(command));

            var config = GetConfigOrThrow(languages, command.Language);

            Validate.It(() => "Cannot update language.", e =>
            {
                if (command.Language == null)
                {
                    e(Not.Defined("Language code"), nameof(command.Language));
                }

                if ((languages.Master == config || command.IsMaster) && command.IsOptional)
                {
                    e("Master language cannot be made optional.", nameof(command.IsMaster));
                }

                if (command.Fallback == null)
                {
                    return;
                }

                foreach (var fallback in command.Fallback)
                {
                    if (!languages.Contains(fallback))
                    {
                        e($"App does not have fallback language '{fallback}'.", nameof(command.Fallback));
                    }
                }
            });
        }
Esempio n. 11
0
        public static void CanRemove(RemoveContributor command, IAppEntity app)
        {
            Guard.NotNull(command, nameof(command));

            var contributors = app.Contributors;

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined(nameof(command.ContributorId)), nameof(command.ContributorId));
                }

                var ownerIds = contributors.Where(x => x.Value == Role.Owner).Select(x => x.Key).ToList();

                if (ownerIds.Count == 1 && ownerIds.Contains(command.ContributorId))
                {
                    e(T.Get("apps.contributors.onlyOneOwner"));
                }
            });

            if (!contributors.ContainsKey(command.ContributorId))
            {
                throw new DomainObjectNotFoundException(command.ContributorId);
            }
        }
Esempio n. 12
0
        public static void CanUpdate(UpdateRole command, IAppEntity app)
        {
            Guard.NotNull(command);

            var roles = app.Roles;

            CheckRoleExists(roles, command.Name);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined(nameof(command.Name)), nameof(command.Name));
                }
                else if (Roles.IsDefault(command.Name))
                {
                    e(T.Get("apps.roles.defaultRoleNotUpdateable"));
                }

                if (command.Permissions == null)
                {
                    e(Not.Defined(nameof(command.Permissions)), nameof(command.Permissions));
                }
            });
        }
Esempio n. 13
0
        public static void CanDelete(DeleteRole command, IAppEntity app)
        {
            Guard.NotNull(command);

            var roles = app.Roles;

            CheckRoleExists(roles, command.Name);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined(nameof(command.Name)), nameof(command.Name));
                }
                else if (Roles.IsDefault(command.Name))
                {
                    e(T.Get("apps.roles.defaultRoleNotRemovable"));
                }

                if (app.Clients.Values.Any(x => string.Equals(x.Role, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e(T.Get("apps.roles.usedRoleByClientsNotRemovable"));
                }

                if (app.Contributors.Values.Any(x => string.Equals(x, command.Name, StringComparison.OrdinalIgnoreCase)))
                {
                    e(T.Get("apps.roles.usedRoleByContributorsNotRemovable"));
                }
            });
        }
Esempio n. 14
0
        public async Task <IEnumerable <ValidationError> > Visit(ContentChangedTriggerV2 trigger)
        {
            var errors = new List <ValidationError>();

            if (trigger.Schemas != null)
            {
                var tasks = new List <Task <ValidationError?> >();

                foreach (var schema in trigger.Schemas)
                {
                    if (schema.SchemaId == DomainId.Empty)
                    {
                        errors.Add(new ValidationError(Not.Defined("SchemaId"), nameof(trigger.Schemas)));
                    }
                    else
                    {
                        tasks.Add(CheckSchemaAsync(schema));
                    }
                }

                var checkErrors = await Task.WhenAll(tasks);

                errors.AddRange(checkErrors.NotNull());
            }

            return(errors);
        }
Esempio n. 15
0
        private static void ValidateField(UpsertSchemaFieldBase field, string prefix, AddValidation e)
        {
            if (!field.Name.IsPropertyName())
            {
                e("Field name must be a valid javascript property name.", $"{prefix}.{nameof(field.Name)}");
            }

            if (field.Properties == null)
            {
                e(Not.Defined("Field properties"), $"{prefix}.{nameof(field.Properties)}");
            }
            else
            {
                if (!field.Properties.IsForApi())
                {
                    if (field.IsHidden)
                    {
                        e("UI field cannot be hidden.", $"{prefix}.{nameof(field.IsHidden)}");
                    }

                    if (field.IsDisabled)
                    {
                        e("UI field cannot be disabled.", $"{prefix}.{nameof(field.IsDisabled)}");
                    }
                }

                var errors = FieldPropertiesValidator.Validate(field.Properties);

                errors.Foreach(x => x.WithPrefix($"{prefix}.{nameof(field.Properties)}").AddTo(e));
            }
        }
Esempio n. 16
0
 private static void ValidateData(ContentDataCommand command, AddValidation e)
 {
     if (command.Data == null)
     {
         e(Not.Defined("Data"), nameof(command.Data));
     }
 }
Esempio n. 17
0
        public static void CanUpdate(Workflows workflows, UpdateWorkflow command)
        {
            Guard.NotNull(command, nameof(command));

            CheckWorkflowExists(workflows, command.WorkflowId);

            Validate.It(() => "Cannot update workflow.", e =>
            {
                if (command.Workflow == null)
                {
                    e(Not.Defined("Workflow"), nameof(command.Workflow));
                    return;
                }

                var workflow = command.Workflow;

                if (!workflow.Steps.ContainsKey(workflow.Initial))
                {
                    e(Not.Defined("Initial step"), $"{nameof(command.Workflow)}.{nameof(workflow.Initial)}");
                }

                if (workflow.Initial == Status.Published)
                {
                    e("Initial step cannot be published step.", $"{nameof(command.Workflow)}.{nameof(workflow.Initial)}");
                }

                var stepsPrefix = $"{nameof(command.Workflow)}.{nameof(workflow.Steps)}";

                if (!workflow.Steps.ContainsKey(Status.Published))
                {
                    e("Workflow must have a published step.", stepsPrefix);
                }

                foreach (var step in workflow.Steps)
                {
                    var stepPrefix = $"{stepsPrefix}.{step.Key}";

                    if (step.Value == null)
                    {
                        e(Not.Defined("Step"), stepPrefix);
                    }
                    else
                    {
                        foreach (var(status, transition) in step.Value.Transitions)
                        {
                            var transitionPrefix = $"{stepPrefix}.{nameof(step.Value.Transitions)}.{status}";

                            if (!workflow.Steps.ContainsKey(status))
                            {
                                e("Transition has an invalid target.", transitionPrefix);
                            }

                            if (transition == null)
                            {
                                e(Not.Defined("Transition"), transitionPrefix);
                            }
                        }
                    }
Esempio n. 18
0
        public static void MustHaveName(this AssetFolderOperation operation, string folderName)
        {
            if (string.IsNullOrWhiteSpace(folderName))
            {
                operation.AddError(Not.Defined(nameof(folderName)), "FolderName");
            }

            operation.ThrowOnErrors();
        }
Esempio n. 19
0
        public static void MustHaveData(this ContentOperation operation, ContentData?data)
        {
            if (data == null)
            {
                operation.AddError(Not.Defined(nameof(data)), nameof(data));
            }

            operation.ThrowOnErrors();
        }
Esempio n. 20
0
        public static void CanAdd(AddWorkflow command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot add workflow.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined("Name"), nameof(command.Name));
                }
            });
        }
Esempio n. 21
0
        public static void CanConfigurePreviewUrls(ConfigurePreviewUrls command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot configure preview urls.", e =>
            {
                if (command.PreviewUrls == null)
                {
                    e(Not.Defined("Preview Urls"), nameof(command.PreviewUrls));
                }
            });
        }
Esempio n. 22
0
        public static void CanCreate(CreateComment command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(() => "Cannot create comment.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.Text))
                {
                    e(Not.Defined("Text"), nameof(command.Text));
                }
            });
        }
Esempio n. 23
0
        public static void CanUploadImage(UploadAppImage command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                if (command.File == null)
                {
                    e(Not.Defined(nameof(command.File)), nameof(command.File));
                }
            });
        }
Esempio n. 24
0
        public static void CanRename(RenameAssetFolder command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.FolderName))
                {
                    e(Not.Defined(nameof(command.FolderName)), nameof(command.FolderName));
                }
            });
        }
Esempio n. 25
0
        public static void CanUploadImage(UploadAppImage command)
        {
            Guard.NotNull(command);

            Validate.It(() => "Cannot upload image.", e =>
            {
                if (command.File == null)
                {
                    e(Not.Defined("File"), nameof(command.File));
                }
            });
        }
Esempio n. 26
0
        public static void CanCreate(CreateComment command)
        {
            Guard.NotNull(command);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.Text))
                {
                    e(Not.Defined(nameof(command.Text)), nameof(command.Text));
                }
            });
        }
Esempio n. 27
0
        public static void CanConfigurePreviewUrls(ConfigurePreviewUrls command)
        {
            Guard.NotNull(command, nameof(command));

            Validate.It(e =>
            {
                if (command.PreviewUrls == null)
                {
                    e(Not.Defined(nameof(command.PreviewUrls)), nameof(command.PreviewUrls));
                }
            });
        }
Esempio n. 28
0
        public static void CanRename(RenameAssetFolder command)
        {
            Guard.NotNull(command);

            Validate.It(() => "Cannot rename asset.", e =>
            {
                if (string.IsNullOrWhiteSpace(command.FolderName))
                {
                    e(Not.Defined("Folder name"), nameof(command.FolderName));
                }
            });
        }
Esempio n. 29
0
        public static void CanAdd(AddWorkflow command)
        {
            Guard.NotNull(command);

            Validate.It(e =>
            {
                if (string.IsNullOrWhiteSpace(command.Name))
                {
                    e(Not.Defined(nameof(command.Name)), nameof(command.Name));
                }
            });
        }
Esempio n. 30
0
        public static Task CanAssign(AppContributors contributors, Roles roles, AssignContributor command, IUserResolver users, IAppLimitsPlan plan)
        {
            Guard.NotNull(command, nameof(command));

            return(Validate.It(() => "Cannot assign contributor.", async e =>
            {
                if (!roles.ContainsKey(command.Role))
                {
                    e(Not.Valid("role"), nameof(command.Role));
                }

                if (string.IsNullOrWhiteSpace(command.ContributorId))
                {
                    e(Not.Defined("Contributor id"), nameof(command.ContributorId));
                }
                else
                {
                    var user = await users.FindByIdOrEmailAsync(command.ContributorId);

                    if (user == null)
                    {
                        throw new DomainObjectNotFoundException(command.ContributorId, "Contributors", typeof(IAppEntity));
                    }

                    command.ContributorId = user.Id;

                    if (!command.IsRestore)
                    {
                        if (string.Equals(command.ContributorId, command.Actor?.Identifier, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new DomainForbiddenException("You cannot change your own role.");
                        }

                        if (contributors.TryGetValue(command.ContributorId, out var role))
                        {
                            if (role == command.Role)
                            {
                                e(Not.New("Contributor", "role"), nameof(command.Role));
                            }
                        }
                        else
                        {
                            if (plan.MaxContributors > 0 && contributors.Count >= plan.MaxContributors)
                            {
                                e("You have reached the maximum number of contributors for your plan.");
                            }
                        }
                    }
                }
            }));
        }