簡體   English   中英

僅當從服務器完成呼叫或用戶為管理員時,才允許使用方法

[英]Allow method only if call is done from server or if user is admin

我正在使用Feathersjs,需要保護服務的補丁方法。 我使用feathers-hooks-common的掛鈎。 僅在從服務器進行調用或由管理員完成調用時,才需要允許使用patch方法。

const {disallow, isNot, iff, isProvider} = require('feathers-hooks-common'); 
const isAdmin = context => { return context.params.user.isAdmin;}
module.exports = {
    patch: [
        iff(isProvider('external') && isNot(isAdmin), disallow()), 
        iff(isNot(isProvider('server')), disallow())
    ],
}

第二條規則iff(isNot(isProvider('server')), disallow())可以正常工作,但是我無法獲得第一條規則來允許服務器調用。

掛鈎不能與條件語句結合使用,但是由於您已經在使用iff ,因此可以將其設為嵌套語句:

const {disallow, isNot, iff, isProvider} = require('feathers-hooks-common'); 
const isAdmin = context => { return context.params.user.isAdmin;}
module.exports = {
    patch: [
        iff(isProvider('external'),
          iff(isNot(isAdmin), disallow())
        )
    ],
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM