Browse Source

Do not check for specific meta fields in the backend.

pull/447/head
Sebastian 6 years ago
parent
commit
bf1fda88b0
  1. 46
      backend/src/Squidex.Domain.Apps.Core.Model/Schemas/MetaFields.cs
  2. 27
      backend/src/Squidex.Domain.Apps.Entities/Schemas/Guards/GuardSchema.cs

46
backend/src/Squidex.Domain.Apps.Core.Model/Schemas/MetaFields.cs

@ -1,46 +0,0 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Reflection;
namespace Squidex.Domain.Apps.Core.Schemas
{
public static class MetaFields
{
private static readonly HashSet<string> AllList = new HashSet<string>();
public static ISet<string> All
{
get { return AllList; }
}
public static readonly string Id = "meta.id";
public static readonly string Created = "meta.created";
public static readonly string CreatedByAvatar = "meta.createdBy.avatar";
public static readonly string CreatedByName = "meta.createdBy.name";
public static readonly string LastModified = "meta.lastModified";
public static readonly string LastModifiedByAvatar = "meta.lastModifiedBy.avatar";
public static readonly string LastModifiedByName = "meta.lastModifiedBy.name";
public static readonly string Status = "meta.status";
public static readonly string StatusColor = "meta.status.color";
public static readonly string Version = "meta.version";
static MetaFields()
{
foreach (var field in typeof(MetaFields).GetFields(BindingFlags.Public | BindingFlags.Static))
{
if (field.FieldType == typeof(string))
{
var value = field.GetValue(null) as string;
AllList.Add(value!);
}
}
}
}
}

27
backend/src/Squidex.Domain.Apps.Entities/Schemas/Guards/GuardSchema.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using Squidex.Domain.Apps.Core;
@ -113,8 +114,8 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
Validate.It(() => "Cannot configure UI fields.", e =>
{
ValidateFieldNames(schema, command.FieldsInLists, nameof(command.FieldsInLists), e, true);
ValidateFieldNames(schema, command.FieldsInReferences, nameof(command.FieldsInReferences), e);
ValidateFieldNames(schema, command.FieldsInLists, nameof(command.FieldsInLists), e, IsMetaField);
ValidateFieldNames(schema, command.FieldsInReferences, nameof(command.FieldsInReferences), e, IsNotAllowed);
});
}
@ -162,8 +163,8 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
}
}
ValidateFieldNames(command, command.FieldsInLists, nameof(command.FieldsInLists), e, true);
ValidateFieldNames(command, command.FieldsInReferences, nameof(command.FieldsInReferences), e);
ValidateFieldNames(command, command.FieldsInLists, nameof(command.FieldsInLists), e, IsMetaField);
ValidateFieldNames(command, command.FieldsInReferences, nameof(command.FieldsInReferences), e, IsNotAllowed);
}
private static void ValidateRootField(UpsertSchemaField field, UpsertCommand command, string prefix, AddValidation e)
@ -261,7 +262,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
}
}
private static void ValidateFieldNames(Schema schema, FieldNames? fields, string path, AddValidation e, bool withMeta = false)
private static void ValidateFieldNames(Schema schema, FieldNames? fields, string path, AddValidation e, Func<string, bool> isAllowed)
{
if (fields != null)
{
@ -279,7 +280,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
{
e(Not.Defined("Field"), fieldPrefix);
}
else if (field == null && (!withMeta || !MetaFields.All.Contains(fieldName)))
else if (field == null && !isAllowed(fieldName))
{
e($"Field is not part of the schema.", fieldPrefix);
}
@ -299,7 +300,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
}
}
private static void ValidateFieldNames(UpsertCommand command, FieldNames? fields, string path, AddValidation e, bool withMeta = false)
private static void ValidateFieldNames(UpsertCommand command, FieldNames? fields, string path, AddValidation e, Func<string, bool> isAllowed)
{
if (fields != null)
{
@ -317,7 +318,7 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
{
e(Not.Defined("Field"), fieldPrefix);
}
else if (field == null && (!withMeta || !MetaFields.All.Contains(fieldName)))
else if (field == null && !isAllowed(fieldName))
{
e($"Field is not part of the schema.", fieldPrefix);
}
@ -337,6 +338,16 @@ namespace Squidex.Domain.Apps.Entities.Schemas.Guards
}
}
private static bool IsMetaField(string field)
{
return field.StartsWith("meta.", StringComparison.Ordinal);
}
private static bool IsNotAllowed(string field)
{
return false;
}
private static void ValidateFieldIds<T>(ReorderFields c, IReadOnlyDictionary<long, T> fields, AddValidation e)
{
if (c.FieldIds != null && (c.FieldIds.Count != fields.Count || c.FieldIds.Any(x => !fields.ContainsKey(x))))

Loading…
Cancel
Save