From 3ae4fda0c359f81271a7a151f74ad50492c6c239 Mon Sep 17 00:00:00 2001 From: ochicf Date: Wed, 5 Sep 2018 11:24:43 +0200 Subject: [PATCH 1/3] pass collection as property when running new API callbacks --- packages/vulcan-lib/lib/server/mutators.js | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/vulcan-lib/lib/server/mutators.js b/packages/vulcan-lib/lib/server/mutators.js index 629f57852..fcd182164 100644 --- a/packages/vulcan-lib/lib/server/mutators.js +++ b/packages/vulcan-lib/lib/server/mutators.js @@ -58,8 +58,8 @@ export const createMutator = async ({ collection, document, data, currentUser, v const validationErrors = validateDocument(newDocument, collection, context); // run validation callbacks - newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.validate`, iterator: newDocument, properties: { currentUser, validationErrors }}); - newDocument = await runCallbacks({ name: `*.create.validate`, iterator: newDocument, properties: { currentUser, validationErrors }}); + newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.validate`, iterator: newDocument, properties: { currentUser, validationErrors, collection }}); + newDocument = await runCallbacks({ name: `*.create.validate`, iterator: newDocument, properties: { currentUser, validationErrors, collection }}); // OpenCRUD backwards compatibility newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.validate`, newDocument, currentUser, validationErrors); @@ -108,8 +108,8 @@ export const createMutator = async ({ collection, document, data, currentUser, v // } // run sync callbacks - newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.before`, iterator: newDocument, properties: { currentUser }}); - newDocument = await runCallbacks({ name: `*.create.before`, iterator: newDocument, properties: { currentUser }}); + newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.before`, iterator: newDocument, properties: { currentUser, collection }}); + newDocument = await runCallbacks({ name: `*.create.before`, iterator: newDocument, properties: { currentUser, collection }}); // OpenCRUD backwards compatibility newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.before`, newDocument, currentUser); newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.sync`, newDocument, currentUser); @@ -118,8 +118,8 @@ export const createMutator = async ({ collection, document, data, currentUser, v newDocument._id = await Connectors.create(collection, newDocument); // run any post-operation sync callbacks - newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.after`, iterator: newDocument, properties: { currentUser }}); - newDocument = await runCallbacks({ name: `*.create.after`, iterator: newDocument, properties: { currentUser }}); + newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.create.after`, iterator: newDocument, properties: { currentUser, collection }}); + newDocument = await runCallbacks({ name: `*.create.after`, iterator: newDocument, properties: { currentUser, collection }}); // OpenCRUD backwards compatibility newDocument = await runCallbacks(`${collectionName.toLowerCase()}.new.after`, newDocument, currentUser); @@ -176,8 +176,8 @@ export const updateMutator = async ({ collection, documentId, selector, data, se let validationErrors; validationErrors = validateData(data, document, collection, context); - data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.validate`, iterator: data, properties: { document, currentUser, validationErrors }}); - data = await runCallbacks({ name: `*.update.validate`, iterator: data, properties: { document, currentUser, validationErrors }}); + data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.validate`, iterator: data, properties: { document, currentUser, validationErrors, collection }}); + data = await runCallbacks({ name: `*.update.validate`, iterator: data, properties: { document, currentUser, validationErrors, collection }}); // OpenCRUD backwards compatibility data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.validate`, dataToModifier(data), document, currentUser, validationErrors)); @@ -213,8 +213,8 @@ export const updateMutator = async ({ collection, documentId, selector, data, se } // run sync callbacks - data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.before`, iterator: data, properties: { document, currentUser, newDocument }}); - data = await runCallbacks({ name: `*.update.before`, iterator: data, properties: { document, currentUser, newDocument }}); + data = await runCallbacks({ name: `${typeName.toLowerCase()}.update.before`, iterator: data, properties: { document, currentUser, newDocument, collection }}); + data = await runCallbacks({ name: `*.update.before`, iterator: data, properties: { document, currentUser, newDocument, collection }}); // OpenCRUD backwards compatibility data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.before`, dataToModifier(data), document, currentUser, newDocument)); data = modifierToData(await runCallbacks(`${collectionName.toLowerCase()}.edit.sync`, dataToModifier(data), document, currentUser, newDocument)); @@ -246,8 +246,8 @@ export const updateMutator = async ({ collection, documentId, selector, data, se } // run any post-operation sync callbacks - newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.update.after`, iterator: newDocument, properties: { document, currentUser }}); - newDocument = await runCallbacks({ name: `*.update.after`, iterator: newDocument, properties: { document, currentUser }}); + newDocument = await runCallbacks({ name: `${typeName.toLowerCase()}.update.after`, iterator: newDocument, properties: { document, currentUser, collection }}); + newDocument = await runCallbacks({ name: `*.update.after`, iterator: newDocument, properties: { document, currentUser, collection }}); // OpenCRUD backwards compatibility newDocument = await runCallbacks(`${collectionName.toLowerCase()}.edit.after`, newDocument, document, currentUser); @@ -292,8 +292,8 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU // if document is not trusted, run validation callbacks if (validate) { - document = await runCallbacks({ name: `${typeName.toLowerCase()}.delete.validate`, iterator: document, properties: { currentUser }}); - document = await runCallbacks({ name: `*.delete.validate`, iterator: document, properties: { currentUser }}); + document = await runCallbacks({ name: `${typeName.toLowerCase()}.delete.validate`, iterator: document, properties: { currentUser, collection }}); + document = await runCallbacks({ name: `*.delete.validate`, iterator: document, properties: { currentUser, collection }}); // OpenCRUD backwards compatibility document = await runCallbacks(`${collectionName.toLowerCase()}.remove.validate`, document, currentUser); } @@ -310,8 +310,8 @@ export const deleteMutator = async ({ collection, documentId, selector, currentU } } - await runCallbacks({ name: `${typeName.toLowerCase()}.delete.before`, iterator: document, properties: { currentUser }}); - await runCallbacks({ name: `*.delete.before`, iterator: document, properties: { currentUser }}); + await runCallbacks({ name: `${typeName.toLowerCase()}.delete.before`, iterator: document, properties: { currentUser, collection }}); + await runCallbacks({ name: `*.delete.before`, iterator: document, properties: { currentUser, collection }}); // OpenCRUD backwards compatibility await runCallbacks(`${collectionName.toLowerCase()}.remove.before`, document, currentUser); await runCallbacks(`${collectionName.toLowerCase()}.remove.sync`, document, currentUser); From 755d5f6ad0f4a72f8eacbd60100849cdae61fd46 Mon Sep 17 00:00:00 2001 From: Eric Burel Date: Wed, 5 Sep 2018 17:08:29 +0200 Subject: [PATCH 2/3] allow user to return nothing in submitCallback Useful when the callback is meant for side effect and not for modifying the data --- packages/vulcan-forms/lib/components/Form.jsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vulcan-forms/lib/components/Form.jsx b/packages/vulcan-forms/lib/components/Form.jsx index 9ed382271..237327683 100644 --- a/packages/vulcan-forms/lib/components/Form.jsx +++ b/packages/vulcan-forms/lib/components/Form.jsx @@ -227,7 +227,7 @@ class SmartForm extends Component { - Nested array item: 'addresses.1.city' */ - compactParent(data, path); + compactParent(data, path); } }); @@ -572,10 +572,10 @@ class SmartForm extends Component { // default to overwriting old value with new const { mode = 'overwrite' } = options; - + // keep the previous ones and extend (with possible replacement) with new ones this.setState(prevState => { - + // keep only the relevant properties const { currentValues, currentDocument, deletedValues } = cloneDeep(prevState); const newState = { currentValues, currentDocument, deletedValues, foo: {} }; @@ -811,7 +811,7 @@ class SmartForm extends Component { // if there's a submit callback, run it if (this.props.submitCallback) { - data = this.props.submitCallback(data); + data = this.props.submitCallback(data) || data; } if (this.getFormType() === 'new') { From 37294c3ad258b429763c044f99b2250eb0a1fef9 Mon Sep 17 00:00:00 2001 From: Eric Burel Date: Wed, 5 Sep 2018 17:09:23 +0200 Subject: [PATCH 3/3] allow to edit username, and 3rd party name if it is set --- packages/vulcan-users/lib/modules/schema.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/vulcan-users/lib/modules/schema.js b/packages/vulcan-users/lib/modules/schema.js index d222d12f9..e9b63d0c6 100644 --- a/packages/vulcan-users/lib/modules/schema.js +++ b/packages/vulcan-users/lib/modules/schema.js @@ -53,9 +53,10 @@ const schema = { type: String, optional: true, canRead: ['guests'], + canUpdate: ['admins'], canCreate: ['members'], onInsert: user => { - if (user.services && user.services.twitter && user.services.twitter.screenName) { + if ((!user.username) && user.services && user.services.twitter && user.services.twitter.screenName) { return user.services.twitter.screenName; } }, @@ -202,7 +203,7 @@ const schema = { fieldName: 'avatarUrl', type: 'String', resolver: async (user, args, { Users }) => { - + if (_.isEmpty(user)) return null; if (user.avatarUrl) { @@ -213,7 +214,7 @@ const schema = { const fullUser = await Users.loader.load(user._id); return Users.avatar.getUrl(fullUser); } - + } } }, @@ -232,9 +233,9 @@ const schema = { return Utils.getUnusedSlugByCollectionName('Users', basicSlug); } }, - /** - The user's Twitter username - */ + /** + The user's Twitter username +*/ twitterUsername: { type: String, optional: true, @@ -269,7 +270,7 @@ const schema = { form: { options: function () { const groups = _.without(_.keys(getCollection('Users').groups), "guests", "members", "admins"); - return groups.map(group => {return {value: group, label: group};}); + return groups.map(group => { return { value: group, label: group }; }); } }, }, @@ -289,7 +290,7 @@ const schema = { resolver: (user, args, { Users }) => { return Users.getProfileUrl(user, true); }, - } + } }, editUrl: { @@ -301,7 +302,7 @@ const schema = { resolver: (user, args, { Users }) => { return Users.getEditUrl(user, true); }, - } + } } };