mirror of
https://github.com/wekan/wekan.git
synced 2025-12-18 16:30:13 +01:00
The before.insert hooks have the problem, that they are executed in a different order if called from the client or from the server. If called from the client, the before.insert hook is called before validation of the schema, but if called from the server, the validation is called first and fails.
35 lines
801 B
JavaScript
35 lines
801 B
JavaScript
// This collection shouldn't be manipulated directly by instead throw the
|
|
// `UnsavedEdits` API on the client.
|
|
UnsavedEditCollection = new Mongo.Collection('unsaved-edits');
|
|
|
|
UnsavedEditCollection.attachSchema(new SimpleSchema({
|
|
fieldName: {
|
|
type: String,
|
|
},
|
|
docId: {
|
|
type: String,
|
|
},
|
|
value: {
|
|
type: String,
|
|
},
|
|
userId: {
|
|
type: String,
|
|
autoValue() { // eslint-disable-line consistent-return
|
|
if (this.isInsert && !this.isSet) {
|
|
return this.userId;
|
|
}
|
|
},
|
|
},
|
|
}));
|
|
|
|
if (Meteor.isServer) {
|
|
function isAuthor(userId, doc, fieldNames = []) {
|
|
return userId === doc.userId && fieldNames.indexOf('userId') === -1;
|
|
}
|
|
UnsavedEditCollection.allow({
|
|
insert: isAuthor,
|
|
update: isAuthor,
|
|
remove: isAuthor,
|
|
fetch: ['userId'],
|
|
});
|
|
}
|