mirror of
https://github.com/strapi/strapi.git
synced 2025-11-05 12:24:35 +00:00
Update doc and generated template
This commit is contained in:
parent
c971f9c054
commit
8f8fa6b158
@ -311,6 +311,10 @@ Callbacks on `fetch`:
|
|||||||
- beforeFetch
|
- beforeFetch
|
||||||
- afterFetch
|
- afterFetch
|
||||||
|
|
||||||
|
Callbacks on `fetchAll`:
|
||||||
|
- beforeFetchAll
|
||||||
|
- afterFetchAll
|
||||||
|
|
||||||
Callbacks on `create`:
|
Callbacks on `create`:
|
||||||
- beforeCreate
|
- beforeCreate
|
||||||
- afterCreate
|
- afterCreate
|
||||||
@ -334,20 +338,12 @@ module.exports = {
|
|||||||
/**
|
/**
|
||||||
* Triggered before user creation.
|
* Triggered before user creation.
|
||||||
*/
|
*/
|
||||||
beforeCreate: function (next) {
|
beforeCreate: async (model) => {
|
||||||
// Hash password.
|
// Hash password.
|
||||||
strapi.api.user.services.user.hashPassword(this.password)
|
const passwordHashed = await strapi.api.user.services.user.hashPassword(this.password);
|
||||||
.then((passwordHashed) => {
|
|
||||||
// Set the password.
|
|
||||||
this.password = passwordHashed;
|
|
||||||
|
|
||||||
// Execute the callback.
|
// Set the password.
|
||||||
next();
|
model.password = passwordHashed;
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
next(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -363,21 +359,12 @@ module.exports = {
|
|||||||
/**
|
/**
|
||||||
* Triggered before user creation.
|
* Triggered before user creation.
|
||||||
*/
|
*/
|
||||||
beforeCreate: function (model, attrs, options) {
|
beforeCreate: async (model, attrs, options) => {
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// Hash password.
|
// Hash password.
|
||||||
strapi.api.user.services.user.hashPassword(model.attributes.password)
|
const passwordHashed = await strapi.api.user.services.user.hashPassword(model.attributes.password);
|
||||||
.then((passwordHashed) => {
|
|
||||||
// Set the password.
|
|
||||||
model.set('password', passwordHashed);
|
|
||||||
|
|
||||||
// Execute the callback.
|
// Set the password.
|
||||||
resolve();
|
model.set('password', passwordHashed);
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,81 +5,50 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
// Before saving a value.
|
// Before saving a value.
|
||||||
// Fired before an `insert` or `update` query.
|
// Fired before an `insert` or `update` query.
|
||||||
// beforeSave: function (next) {
|
// beforeSave: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After saving a value.
|
// After saving a value.
|
||||||
// Fired after an `insert` or `update` query.
|
// Fired after an `insert` or `update` query.
|
||||||
// afterSave: function (doc, next) {
|
// afterSave: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before fetching all values.
|
// Before fetching all values.
|
||||||
// Fired before a `fetchAll` operation.
|
// Fired before a `fetchAll` operation.
|
||||||
// beforeFetchAll: function (next) {
|
// beforeFetchAll: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After fetching all values.
|
// After fetching all values.
|
||||||
// Fired after a `fetchAll` operation.
|
// Fired after a `fetchAll` operation.
|
||||||
// afterFetchAll: function (doc, next) {
|
// afterFetchAll: async (model, results) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Fired before a `fetch` operation.
|
// Fired before a `fetch` operation.
|
||||||
// beforeFetch: function (next) {
|
// beforeFetch: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After fetching a value.
|
// After fetching a value.
|
||||||
// Fired after a `fetch` operation.
|
// Fired after a `fetch` operation.
|
||||||
// afterFetch: function (doc, next) {
|
// afterFetch: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before creating a value.
|
// Before creating a value.
|
||||||
// Fired before `insert` query.
|
// Fired before `insert` query.
|
||||||
// beforeCreate: function (next) {
|
// beforeCreate: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After creating a value.
|
// After creating a value.
|
||||||
// Fired after `insert` query.
|
// Fired after `insert` query.
|
||||||
// afterCreate: function (doc, next) {
|
// afterCreate: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before updating a value.
|
// Before updating a value.
|
||||||
// Fired before an `update` query.
|
// Fired before an `update` query.
|
||||||
// beforeUpdate: function (next) {
|
// beforeUpdate: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After updating a value.
|
// After updating a value.
|
||||||
// Fired after an `update` query.
|
// Fired after an `update` query.
|
||||||
// afterUpdate: function (doc, next) {
|
// afterUpdate: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before destroying a value.
|
// Before destroying a value.
|
||||||
// Fired before a `delete` query.
|
// Fired before a `delete` query.
|
||||||
// beforeDestroy: function (next) {
|
// beforeDestroy: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After destroying a value.
|
// After destroying a value.
|
||||||
// Fired after a `delete` query.
|
// Fired after a `delete` query.
|
||||||
// afterDestroy: function (doc, next) {
|
// afterDestroy: async (model, result) => {}
|
||||||
// next();
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -113,7 +113,9 @@ module.exports = function (strapi) {
|
|||||||
|
|
||||||
_.forEach(postLifecycle, (fn, key) => {
|
_.forEach(postLifecycle, (fn, key) => {
|
||||||
if (_.isFunction(target[model.toLowerCase()][fn])) {
|
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))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -51,10 +51,10 @@ class EditForm extends React.Component {
|
|||||||
render() {
|
render() {
|
||||||
const source = getQueryParameters(this.props.location.search, 'source');
|
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 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
|
// Remove `id` field
|
||||||
const displayedFields = merge(currentLayout, omit(currentSchema.fields, 'id'));
|
const displayedFields = merge(get(currentLayout), omit(currentSchema.fields, 'id'));
|
||||||
|
|
||||||
// List fields inputs
|
// List fields inputs
|
||||||
const fields = Object.keys(displayedFields).map(attr => {
|
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 validationsIndex = findIndex(this.props.formValidations, ['name', attr]);
|
||||||
const validations = get(this.props.formValidations[validationsIndex], 'validations') || {};
|
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]) ?
|
acc[current] = isFunction(currentLayout[attr][current]) ?
|
||||||
currentLayout[attr][current](this) :
|
currentLayout[attr][current](this) :
|
||||||
currentLayout[attr][current];
|
currentLayout[attr][current];
|
||||||
|
|||||||
@ -5,81 +5,50 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
// Before saving a value.
|
// Before saving a value.
|
||||||
// Fired before an `insert` or `update` query.
|
// Fired before an `insert` or `update` query.
|
||||||
// beforeSave: (model) => {
|
// beforeSave: async (model) => {},
|
||||||
// return Promise.resolve();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After saving a value.
|
// After saving a value.
|
||||||
// Fired after an `insert` or `update` query.
|
// Fired after an `insert` or `update` query.
|
||||||
// afterSave: function (doc, next) {
|
// afterSave: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before fetching all values.
|
// Before fetching all values.
|
||||||
// Fired before a `fetchAll` operation.
|
// Fired before a `fetchAll` operation.
|
||||||
beforeFetchAll: (model) => {
|
// beforeFetchAll: async (model) => {},
|
||||||
// Use `this` to get your current object
|
|
||||||
console.log(model);
|
|
||||||
return Promise.resolve();
|
|
||||||
},
|
|
||||||
|
|
||||||
// After fetching all values.
|
// After fetching all values.
|
||||||
// Fired after a `fetchAll` operation.
|
// Fired after a `fetchAll` operation.
|
||||||
// afterFetchAll: function (doc, next) {
|
// afterFetchAll: async (model, results) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Fired before a `fetch` operation.
|
// Fired before a `fetch` operation.
|
||||||
// beforeFetch: function (next) {
|
// beforeFetch: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After fetching a value.
|
// After fetching a value.
|
||||||
// Fired after a `fetch` operation.
|
// Fired after a `fetch` operation.
|
||||||
// afterFetch: function (doc, next) {
|
// afterFetch: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before creating a value.
|
// Before creating a value.
|
||||||
// Fired before `insert` query.
|
// Fired before `insert` query.
|
||||||
// beforeCreate: function (next) {
|
// beforeCreate: async (model) => {},
|
||||||
// Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After creating a value.
|
// After creating a value.
|
||||||
// Fired after `insert` query.
|
// Fired after `insert` query.
|
||||||
// afterCreate: function (doc, next) {
|
// afterCreate: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before updating a value.
|
// Before updating a value.
|
||||||
// Fired before an `update` query.
|
// Fired before an `update` query.
|
||||||
// beforeUpdate: function (next) {
|
// beforeUpdate: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After updating a value.
|
// After updating a value.
|
||||||
// Fired after an `update` query.
|
// Fired after an `update` query.
|
||||||
// afterUpdate: function (doc, next) {
|
// afterUpdate: async (model, result) => {},
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// Before destroying a value.
|
// Before destroying a value.
|
||||||
// Fired before a `delete` query.
|
// Fired before a `delete` query.
|
||||||
// beforeDestroy: function (next) {
|
// beforeDestroy: async (model) => {},
|
||||||
// // Use `this` to get your current object
|
|
||||||
// next();
|
|
||||||
// },
|
|
||||||
|
|
||||||
// After destroying a value.
|
// After destroying a value.
|
||||||
// Fired after a `delete` query.
|
// Fired after a `delete` query.
|
||||||
// afterDestroy: function (doc, next) {
|
// afterDestroy: async (model, result) => {}
|
||||||
// next();
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -39,10 +39,6 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
add: async (values) => {
|
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'))));
|
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 }));
|
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(data), { values }));
|
||||||
return data;
|
return data;
|
||||||
@ -58,10 +54,6 @@ module.exports = {
|
|||||||
// Note: The current method will return the full response of Mongo.
|
// Note: The current method will return the full response of Mongo.
|
||||||
// To get the updated object, you have to execute the `findOne()` method
|
// To get the updated object, you have to execute the `findOne()` method
|
||||||
// or use the `findOneOrUpdate()` method with `{ new:true }` option.
|
// 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 }));
|
await strapi.hook.mongoose.manageRelations('user', _.merge(_.clone(params), { values }));
|
||||||
return strapi.plugins['users-permissions'].models.user.update(params, values, { multi: true });
|
return strapi.plugins['users-permissions'].models.user.update(params, values, { multi: true });
|
||||||
},
|
},
|
||||||
@ -91,9 +83,9 @@ module.exports = {
|
|||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
|
|
||||||
hashPassword: function (user) {
|
hashPassword: function (user = {}) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
if (!user.hasOwnProperty('password') || !user.password || this.isHashed(user.password)) {
|
if (!user.password || this.isHashed(user.password)) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
} else {
|
} else {
|
||||||
bcrypt.hash(user.password, 10, (err, hash) => {
|
bcrypt.hash(user.password, 10, (err, hash) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user