2017-08-18 9 views

答えて

1

はい。関数を作成し、その関数にユーザーの役割に基づいて、いくつかの特定のフィールドを削除することができます。この関数は、このルールを適用する各リモートメソッドの後に呼び出す必要があります(*を使用して、すべてのリモートメソッドの後にこの関数を適用できます)。ここで

は、私はそれがあなたの役に立てば幸いサンプルコードです:

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 a role 
     } else { 
      const role = roleMapping.role().name; 
      if (role === 'admin') { 
      // Remove some fields from remoteMethodOutput 
      } 
     } 
     next(); 
     }); 
    } else { 
     // This user is not logged in, So it is a guest! 
     next(); 
    } 
}; 

SampleModel.afterRemote('search', filterBasedOnRole); // Search is an example method, you can use whatever you want! 
関連する問題