Update doc and generated template

This commit is contained in:
Aurelsicoko 2017-11-29 15:46:28 +01:00
parent c971f9c054
commit 8f8fa6b158
6 changed files with 44 additions and 125 deletions

View File

@ -311,6 +311,10 @@ Callbacks on `fetch`:
- beforeFetch
- afterFetch
Callbacks on `fetchAll`:
- beforeFetchAll
- afterFetchAll
Callbacks on `create`:
- beforeCreate
- afterCreate
@ -334,20 +338,12 @@ module.exports = {
/**
* Triggered before user creation.
*/
beforeCreate: function (next) {
beforeCreate: async (model) => {
// Hash password.
strapi.api.user.services.user.hashPassword(this.password)
.then((passwordHashed) => {
// Set the password.
this.password = passwordHashed;
const passwordHashed = await strapi.api.user.services.user.hashPassword(this.password);
// Execute the callback.
next();
})
.catch((error) => {
next(error);
});
}
// Set the password.
model.password = passwordHashed;
}
}
```
@ -363,21 +359,12 @@ module.exports = {
/**
* Triggered before user creation.
*/
beforeCreate: function (model, attrs, options) {
return new Promise((resolve, reject) => {
beforeCreate: async (model, attrs, options) => {
// Hash password.
strapi.api.user.services.user.hashPassword(model.attributes.password)
.then((passwordHashed) => {
// Set the password.
model.set('password', passwordHashed);
const passwordHashed = await strapi.api.user.services.user.hashPassword(model.attributes.password);
// Execute the callback.
resolve();
})
.catch((error) => {
reject(error);
});
}
// Set the password.
model.set('password', passwordHashed);
});
}
}

View File

@ -5,81 +5,50 @@
*/
module.exports = {
// Before saving a value.
// Fired before an `insert` or `update` query.
// beforeSave: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeSave: async (model) => {},
// After saving a value.
// Fired after an `insert` or `update` query.
// afterSave: function (doc, next) {
// next();
// },
// afterSave: async (model, result) => {},
// Before fetching all values.
// Fired before a `fetchAll` operation.
// beforeFetchAll: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeFetchAll: async (model) => {},
// After fetching all values.
// Fired after a `fetchAll` operation.
// afterFetchAll: function (doc, next) {
// next();
// },
// afterFetchAll: async (model, results) => {},
// Fired before a `fetch` operation.
// beforeFetch: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeFetch: async (model) => {},
// After fetching a value.
// Fired after a `fetch` operation.
// afterFetch: function (doc, next) {
// next();
// },
// afterFetch: async (model, result) => {},
// Before creating a value.
// Fired before `insert` query.
// beforeCreate: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeCreate: async (model) => {},
// After creating a value.
// Fired after `insert` query.
// afterCreate: function (doc, next) {
// next();
// },
// afterCreate: async (model, result) => {},
// Before updating a value.
// Fired before an `update` query.
// beforeUpdate: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeUpdate: async (model) => {},
// After updating a value.
// Fired after an `update` query.
// afterUpdate: function (doc, next) {
// next();
// },
// afterUpdate: async (model, result) => {},
// Before destroying a value.
// Fired before a `delete` query.
// beforeDestroy: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeDestroy: async (model) => {},
// After destroying a value.
// Fired after a `delete` query.
// afterDestroy: function (doc, next) {
// next();
// }
// afterDestroy: async (model, result) => {}
};

View File

@ -113,7 +113,9 @@ module.exports = function (strapi) {
_.forEach(postLifecycle, (fn, key) => {
if (_.isFunction(target[model.toLowerCase()][fn])) {
collection.schema.post(key, target[model.toLowerCase()][fn]);
collection.schema.post(key, function (doc, next) {
target[model.toLowerCase()][fn](this, doc).then(next).catch(err => strapi.log.error(err))
});
}
});

View File

@ -51,10 +51,10 @@ class EditForm extends React.Component {
render() {
const source = getQueryParameters(this.props.location.search, 'source');
const currentSchema = get(this.props.schema, [this.props.currentModelName]) || get(this.props.schema, ['plugins', source, this.props.currentModelName]);
const currentLayout = get(this.props.layout, [this.props.currentModelName]);
const currentLayout = get(this.props.layout, [this.props.currentModelName, 'attributes']);
// Remove `id` field
const displayedFields = merge(currentLayout, omit(currentSchema.fields, 'id'));
const displayedFields = merge(get(currentLayout), omit(currentSchema.fields, 'id'));
// List fields inputs
const fields = Object.keys(displayedFields).map(attr => {
@ -64,7 +64,7 @@ class EditForm extends React.Component {
const validationsIndex = findIndex(this.props.formValidations, ['name', attr]);
const validations = get(this.props.formValidations[validationsIndex], 'validations') || {};
const layout = Object.keys(get(currentLayout, `attributes.${attr}`, {})).reduce((acc, current) => {
const layout = Object.keys(get(currentLayout, attr, {})).reduce((acc, current) => {
acc[current] = isFunction(currentLayout[attr][current]) ?
currentLayout[attr][current](this) :
currentLayout[attr][current];

View File

@ -5,81 +5,50 @@
*/
module.exports = {
// Before saving a value.
// Fired before an `insert` or `update` query.
// beforeSave: (model) => {
// return Promise.resolve();
// },
// beforeSave: async (model) => {},
// After saving a value.
// Fired after an `insert` or `update` query.
// afterSave: function (doc, next) {
// next();
// },
// afterSave: async (model, result) => {},
// Before fetching all values.
// Fired before a `fetchAll` operation.
beforeFetchAll: (model) => {
// Use `this` to get your current object
console.log(model);
return Promise.resolve();
},
// beforeFetchAll: async (model) => {},
// After fetching all values.
// Fired after a `fetchAll` operation.
// afterFetchAll: function (doc, next) {
// next();
// },
// afterFetchAll: async (model, results) => {},
// Fired before a `fetch` operation.
// beforeFetch: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeFetch: async (model) => {},
// After fetching a value.
// Fired after a `fetch` operation.
// afterFetch: function (doc, next) {
// next();
// },
// afterFetch: async (model, result) => {},
// Before creating a value.
// Fired before `insert` query.
// beforeCreate: function (next) {
// Use `this` to get your current object
// next();
// },
// beforeCreate: async (model) => {},
// After creating a value.
// Fired after `insert` query.
// afterCreate: function (doc, next) {
// next();
// },
// afterCreate: async (model, result) => {},
// Before updating a value.
// Fired before an `update` query.
// beforeUpdate: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeUpdate: async (model) => {},
// After updating a value.
// Fired after an `update` query.
// afterUpdate: function (doc, next) {
// next();
// },
// afterUpdate: async (model, result) => {},
// Before destroying a value.
// Fired before a `delete` query.
// beforeDestroy: function (next) {
// // Use `this` to get your current object
// next();
// },
// beforeDestroy: async (model) => {},
// After destroying a value.
// Fired after a `delete` query.
// afterDestroy: function (doc, next) {
// next();
// }
// afterDestroy: async (model, result) => {}
};

View File

@ -39,10 +39,6 @@ module.exports = {
*/
add: async (values) => {
if (values.password) {
values.password = await strapi.plugins['users-permissions'].services.user.hashPassword(values);
}
const data = await strapi.plugins['users-permissions'].models.user.create(_.omit(values, _.keys(_.groupBy(strapi.plugins['users-permissions'].models.user.associations, 'alias'))));
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(data), { values }));
return data;
@ -58,10 +54,6 @@ module.exports = {
// Note: The current method will return the full response of Mongo.
// To get the updated object, you have to execute the `findOne()` method
// or use the `findOneOrUpdate()` method with `{ new:true }` option.
if (values.password) {
values.password = await strapi.plugins['users-permissions'].services.user.hashPassword(values);
}
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(params), { values }));
return strapi.plugins['users-permissions'].models.user.update(params, values, { multi: true });
},
@ -91,9 +83,9 @@ module.exports = {
return data;
},
hashPassword: function (user) {
hashPassword: function (user = {}) {
return new Promise((resolve) => {
if (!user.hasOwnProperty('password') || !user.password || this.isHashed(user.password)) {
if (!user.password || this.isHashed(user.password)) {
resolve(null);
} else {
bcrypt.hash(user.password, 10, (err, hash) => {