Casbin = way to authorization
A place for Casbin developers and users
现在注册
已注册用户请  登录
主题样式选择
默认主题样式 ✅
知乎主题样式 
margelatu
Casnode  ›  问与答

custom function in policy file

  •  
  •  
    margelatu · 5年前 · 141 次点击 
    这是一个创建于 1646 天前的主题,其中的信息可能已经有所发展或是发生改变。

    I ve created a custom function that I want to use in policy file (CSV) . My function needs 2 parameters but the policy file is not parsed in Casbin as a real CSV file ...it justs splits the row by the comma.Is there any standard way to put a custom function with more than 1 parameter in the policy file? Thanks,

    141 次点击  ∙  0 人收藏  
      Tweet Weibo 忽略主题 
    7 条回复   2020-07-17 20:35:14 +08:00
    hsluoyz
    hsluoyz5年前

    Custom function is defined in the programming language, e.g., Golang code for Go's Casbin. It cannot be persited in a CSV file.

    margelatu
    margelatu5年前

    I am trying to use ABAC in Casbin by getting User Atribbutes and Object Attributes and writing some custom functions to decide access allowed/deny but the policy file supports only some basic stuff like "p, r.sub.Age <60, /data2, write" . Does that mean I could end up without using only the Model .conf files ? so no central storage for the Model .conf files? Thanks,

    hsluoyz
    hsluoyz5年前

    You can define a function to consume two values, even three:

    p, 55, data, read
    m = isAgeRightForData(r.age, p.age, p.obj) && r.obj == p.obj && r.act == p.act
    margelatu
    margelatu5年前

    If i have in Model .conf like:

    matchers m = eval(p.sub_rule) && r.obj == p.obj && r.act == p.act

    then in policy file the following stuff works: p, my_custom_func(r.sub.Age), /data1, read

    BUT not the following kind of stuff: p, my_custom_func(r.sub.Age,r.sub.MaxAge), /data1, read

    ....because the policy file is splitted by the ',' (COMMA) and my_custom_func takes 2 parameters ...so it will be 1 more extra comma in policy row

    Casbin: " panic: invalid policy size: ...."

    hsluoyz
    hsluoyz5年前

    Oh! I finally got what you mean. Actually, we never expect the user to use eval() like this (my_custom_func(r.sub.Age)) :)

    Maybe we need to improve the CSV parser to NOT split the "," inside a function call. Can you raise a GitHub issue here: https://github.com/casbin/casbin ? So we can find someone to fix it.

    RuiInCasbin
    RuiInCasbin4年前

    This is a brilliant question.

    关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   40 人在线   最高记录 40   ·     选择语言  ·     选择编辑器
    创意工作者们的社区
    World is powered by code
    VERSION: 2c477f2 · 3ms · UTC 12:03 · PVG 20:03 · LAX 05:03 · JFK 08:03
    ♥ Do have faith in what you're doing.