def get_casbin() -> casbin.Enforcer:
    adapter = casbin_sqlalchemy_adapter.Adapter(engine)

    e = casbin.Enforcer(settings.CASBIN_MODEL_PATH, adapter, True)
    # e.add_function("ParamsMatch", params_match_func)

    return e
Beispiel #2
0
def get_casbin() -> casbin.Enforcer:
    adapter = casbin_sqlalchemy_adapter.Adapter(engine)
    # path = settings.CASBIN_MODEL_PATH
    # with open(path,'r') as f:
    #     data = f.read()
    #     print(data)
    e = casbin.Enforcer(settings.CASBIN_MODEL_PATH, adapter)
    # e.add_function("ParamsMatch", params_match_func)

    return e
Beispiel #3
0
from functools import wraps
import casbin
import casbin_sqlalchemy_adapter
from flask_jwt_extended import get_jwt
from common.response import fail
from settings import DB_URI

adapter = casbin_sqlalchemy_adapter.Adapter(DB_URI)
e = casbin.Enforcer("model.conf", adapter)


class Roles:
    superadmin = "superadmin"
    user = "******"


class Objects:
    users = "users"


class Actions:
    read = "read"
    write = "write"
    protected = "protected"  # Admin only


POLICIES = [
    [Roles.superadmin, Objects.users, Actions.read],
    [Roles.superadmin, Objects.users, Actions.write],
    [Roles.superadmin, Objects.users, Actions.protected],
    [Roles.user, Objects.users, Actions.read],
Beispiel #4
0
# @File    : casbin_custom_orm.py
# @Software: PyCharm
# @Github  : github/CoderCharm
# @Email   : [email protected]
# @Desc    :
"""

加一个自定义的解析函数

"""

import casbin
from casbin import util
import casbin_sqlalchemy_adapter

adapter = casbin_sqlalchemy_adapter.Adapter('sqlite:///test.db')

e = casbin.Enforcer('./custom_model.conf', adapter, True)

# 添加
# e.add_policy("999", "/api/user", "GET")
# 删除
# e.remove_policy("999", "/api/user", "GET")


def params_match(full_name_k1: str, key2: str):
    """
    去掉url ?后面的参数 只取路径
    :param full_name_k1:
    :param key2:
    :return:
Beispiel #5
0
import json
import casbin
import casbin_sqlalchemy_adapter

from functools import wraps
from six.moves.urllib.request import urlopen
from jose import jwt

from flask import request, _request_ctx_stack
from flask_cors import cross_origin

from server import app, api

# CASBIN Setup ----------------------------------------------------------------

casbin_adapter = casbin_sqlalchemy_adapter.Adapter(
    app.config['CASBIN_SQLALCHEMY_DATABASE_URI'])
casbin_enforcer = casbin.Enforcer(app.config['CASBIN_MODEL'], casbin_adapter)

# Auth Errors -----------------------------------------------------------------


class AuthError(Exception):
    """Represents a failure during authentication"""
    def __init__(self, error, status_code=401):
        super(AuthError, self).__init__(error)
        self.error = error
        self.status_code = status_code


@api.errorhandler(AuthError)
@cross_origin()
Beispiel #6
0
 def __init__(self):
     self.adapter = casbin_sqlalchemy_adapter.Adapter(config.casbin_db)
     self.efc = casbin.Enforcer(config.casbin_model, self.adapter)
Beispiel #7
0
def init_enforcer():
    casbin_adapter = casbin_sqlalchemy_adapter.Adapter(
        settings.casbin_database_uri)
    return casbin.Enforcer(settings.casbin_model, casbin_adapter)