From 5870e7cde96eae18e19f2d77f0f983ecf451485c Mon Sep 17 00:00:00 2001 From: James Date: Thu, 9 Jul 2020 14:17:24 -0400 Subject: [PATCH] WIP - merges base fields with user fields --- demo/collections/Admin.js | 14 +++++++ demo/collections/Media.js | 11 +++++ src/auth/baseAPIKeyFields.js | 2 + src/auth/baseFields.js | 1 + src/collections/sanitize.js | 78 +++++++++++++++++++++++++++++++----- 5 files changed, 96 insertions(+), 10 deletions(-) diff --git a/demo/collections/Admin.js b/demo/collections/Admin.js index 6f88c192dc..ba3b015272 100644 --- a/demo/collections/Admin.js +++ b/demo/collections/Admin.js @@ -36,6 +36,20 @@ module.exports = { saveToJWT: true, hasMany: true, }, + { + name: 'apiKey', + access: { + read: ({ req: { user } }) => { + if (checkRole(['admin'], user)) { + return true; + } + + return { + email: user.email, + }; + }, + }, + }, ], timestamps: true, }; diff --git a/demo/collections/Media.js b/demo/collections/Media.js index bdd41bac0f..2b4d443487 100644 --- a/demo/collections/Media.js +++ b/demo/collections/Media.js @@ -41,6 +41,17 @@ module.exports = { required: true, localized: true, }, + { + name: 'sizes', + fields: [ + { + name: 'icon', + access: { + read: ({ req: { user } }) => Boolean(user), + }, + }, + ], + }, ], timestamps: true, }; diff --git a/src/auth/baseAPIKeyFields.js b/src/auth/baseAPIKeyFields.js index 460e3a970c..408f167d65 100644 --- a/src/auth/baseAPIKeyFields.js +++ b/src/auth/baseAPIKeyFields.js @@ -3,11 +3,13 @@ module.exports = [ name: 'enableAPIKey', type: 'checkbox', defaultValue: false, + hidden: 'admin', }, { name: 'apiKey', type: 'text', minLength: 24, maxLength: 48, + hidden: 'admin', }, ]; diff --git a/src/auth/baseFields.js b/src/auth/baseFields.js index 5a52863d1a..24397e6da7 100644 --- a/src/auth/baseFields.js +++ b/src/auth/baseFields.js @@ -6,6 +6,7 @@ module.exports = [ label: 'Email', type: 'email', validate: validations.email, + hidden: 'admin', }, { name: 'resetPasswordToken', diff --git a/src/collections/sanitize.js b/src/collections/sanitize.js index 3b9454da11..c03f1fbd12 100644 --- a/src/collections/sanitize.js +++ b/src/collections/sanitize.js @@ -1,9 +1,53 @@ +const merge = require('deepmerge'); const { DuplicateCollection, MissingCollectionLabel } = require('../errors'); const sanitizeFields = require('../fields/sanitize'); const toKebabCase = require('../utilities/toKebabCase'); const baseAuthFields = require('../auth/baseFields'); const baseAPIKeyFields = require('../auth/baseAPIKeyFields'); +const mergeBaseFields = (fields, baseFields) => { + const mergedFields = []; + + if (fields) { + baseFields.forEach((baseField) => { + let matchedIndex = null; + + const match = fields.find((field, i) => { + if (field.name === baseField.name) { + matchedIndex = i; + return true; + } + + return false; + }); + + if (match) { + const matchCopy = { ...match }; + fields.splice(matchedIndex, 1); + + let mergedField = { + ...baseField, + ...matchCopy, + }; + + if (baseField.fields && matchCopy.fields) { + mergedField.fields = mergeBaseFields(matchCopy.fields, baseField.fields); + return mergedFields.push(mergedField); + } + + mergedField = merge(mergedField, matchCopy, { arrayMerge: (_, source) => source }); + return mergedFields.push(mergedField); + } + + return mergedFields.push(baseField); + }); + + return mergedFields; + } + + return baseFields; +}; + const sanitizeCollection = (collections, collection) => { // ///////////////////////////////// // Ensure collection is valid @@ -51,16 +95,19 @@ const sanitizeCollection = (collections, collection) => { required: true, unique: true, readOnly: true, + hidden: 'admin', }, { name: 'mimeType', label: 'MIME Type', type: 'text', readOnly: true, + hidden: 'admin', }, { name: 'filesize', label: 'File Size', type: 'number', readOnly: true, + hidden: 'admin', }, ]; @@ -71,16 +118,19 @@ const sanitizeCollection = (collections, collection) => { label: 'Width', type: 'number', readOnly: true, + hidden: 'admin', }, { name: 'height', label: 'Height', type: 'number', readOnly: true, + hidden: 'admin', }, { name: 'sizes', label: 'Sizes', type: 'group', + hidden: 'admin', fields: collection.upload.imageSizes.map((size) => ({ label: size.name, name: size.name, @@ -91,26 +141,31 @@ const sanitizeCollection = (collections, collection) => { label: 'Width', type: 'number', readOnly: true, + hidden: 'admin', }, { name: 'height', label: 'Height', type: 'number', readOnly: true, + hidden: 'admin', }, { name: 'mimeType', label: 'MIME Type', type: 'text', readOnly: true, + hidden: 'admin', }, { name: 'filesize', label: 'File Size', type: 'number', readOnly: true, + hidden: 'admin', }, { name: 'filename', label: 'File Name', type: 'text', readOnly: true, + hidden: 'admin', }, ], })), @@ -118,31 +173,34 @@ const sanitizeCollection = (collections, collection) => { ]); } + uploadFields = mergeBaseFields(sanitizedCollection.fields, uploadFields); + sanitizedCollection.fields = [ - ...sanitizedCollection.fields, ...uploadFields, + ...sanitizedCollection.fields, ]; } if (collection.auth) { - sanitizedCollection.fields = [ - ...baseAuthFields, - ...sanitizedCollection.fields, - ]; + let authFields = baseAuthFields; if (collection.auth.useAPIKey) { - sanitizedCollection.fields = [ - ...sanitizedCollection.fields, - ...baseAPIKeyFields, - ]; + authFields = authFields.concat(baseAPIKeyFields); } + + authFields = mergeBaseFields(sanitizedCollection.fields, authFields); + + sanitizedCollection.fields = [ + ...authFields, + ...sanitizedCollection.fields, + ]; } // ///////////////////////////////// // Sanitize fields // ///////////////////////////////// - sanitizedCollection.fields = sanitizeFields(collection.fields); + sanitizedCollection.fields = sanitizeFields(sanitizedCollection.fields); return sanitizedCollection; };