Limitare i campi dall'essere impostato in vele.js Model
Domanda
Quindi ho un modello con alcuni campi come:
// ...
slug: {
type: 'string',
required: true,
alphanumeric: true,
minLength: 3,
maxLength: 16
},
loggedinAt: 'date',
// ...
.
Sto usando la struttura del blueprint delle vele in modo che mapponi automaticamente tutto.Tuttavia, a volte ho campi come loggedinAt
che sono strettamente interne e non voglio che siano in grado di essere impostati dall'utente.
AS SEALS SE IS SE FAI UNA POSTA RICHIESTA CON IL loggedinAt
Il campo lo imposterà.Come posso limitare questo?
Soluzione
È possibile utilizzare una politica per limitare questo comportamento.In API / Policies / RestrictSerCreate.js :
module.exports = function (req, res, next) {
// Check for the "loggedInAt" field in the request
if (req.param('loggedInAt')) {
return res.badRequest("Nuh uh, you can't set that!");
}
return next();
}
.
o, per ignorare alcuni campi (vele v0.10.x solo), utilizzare Blacklist :
module.exports = function (req, res, next) {
// Make sure blacklist object exists, and use existing one if possible,
// since this policy could be chained
req.options.values = req.options.values || {};
req.options.values.blacklist = req.options.values.blacklist || [];
// Add restricted fields to the blacklist
req.options.values.blacklist.push('loggedinAt');
return next();
}
.
Allora in Config / Policies.js :
// Assuming your controller is "UserController"
UserController: {
create: "restrictUserCreate"
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow