loopbackjs - How to implement attribute level mask depending on the Roles in loopback.js? -
i have few roles created in loopback application. there way in can hide property of model depending on roles of user?
yes. can create function , in function based on role of user, can delete specific fields. function should called after each remote method want apply rule (you can use * apply function after remote methods).
here sample code hope helps you:
const filterbasedonrole= function (ctx, remotemethodoutput, next) { const rolemapping = samplemodel.app.loopback.rolemapping; if (ctx.req.accesstoken && ctx.req.accesstoken.userid) { rolemapping.findone({ where: { principalid: ctx.req.accesstoken.userid }, include: 'role', }, (err, rolemapping) => { if (err) { return next(err); } if (!rolemapping) { //user doesn't have role } else { const role = rolemapping.role().name; if (role === 'admin') { // remove fields remotemethodoutput } } next(); }); } else { // user not logged in, guest! next(); } }; samplemodel.afterremote('search', filterbasedonrole); // search example method, can use whatever want!
Comments
Post a Comment