Esempio n. 1
0
def get_policies(enforcer: casbin.Enforcer, user='', path='', method=''):
    if enforcer is None:
        enforcer = init_enforcer()
    rules = enforcer.get_filtered_policy(0, user, path, method)
    # TODO: Make sure the /protocol/* cases are handled properly.
    return [{
        'user': rule[0],
        'path': rule[1],
        'method': rule[2]
    } for rule in rules]
Esempio n. 2
0
def get_authorized_user(
    req: Request,
    enforcer: Enforcer = Depends(get_enforcer),
    me: User = Depends(get_current_user),
) -> User:
    sub = me.role
    obj = req.url.path.removeprefix(settings.root_path)
    act = req.method
    if enforcer.enforce(sub, obj, act):
        return me
    raise HTTPException(status_code=403)
Esempio n. 3
0
def get_enforcer() -> Enforcer:
    dir_ = path.dirname(__file__)
    return Enforcer(
        path.join(dir_, "ac_model.conf"),
        path.join(dir_, "ac_policies.csv"),
    )
Esempio n. 4
0
def get_all_roles(enforcer: casbin.Enforcer):
    if enforcer is None:
        enforcer = init_enforcer()
    return enforcer.get_all_roles()
Esempio n. 5
0
def get_roles(enforcer: casbin.Enforcer, user):
    if enforcer is None:
        enforcer = init_enforcer()
    return enforcer.get_roles_for_user(user)
Esempio n. 6
0
def delete_policy(enforcer: casbin.Enforcer, user='', path='', method=''):
    if enforcer is None:
        enforcer = init_enforcer()
    return enforcer.remove_filtered_policy(0, user, path, method)
Esempio n. 7
0
def add_policy(enforcer: casbin.Enforcer, user, path, method):
    if enforcer is None:
        enforcer = init_enforcer()
    return enforcer.add_permission_for_user(user, path, method)
Esempio n. 8
0
def check_access(enforcer: casbin.Enforcer, user, path, method):
    if enforcer is None:
        enforcer = init_enforcer()
    return enforcer.enforce(user, path, method)
Esempio n. 9
0
 def _create_casbin_enforcer(self, app):
     self.enforcer = Enforcer(model=app.config.get('CASBIN_MODEL'),
                              adapter=app.config.get('CASBIN_POLICY'),
                              enable_log=True)