Skip to content

Commit a413550

Browse files
committed
Add Casbin middleware.
1 parent 26594d1 commit a413550

5 files changed

Lines changed: 71 additions & 0 deletions

File tree

casbin_middleware/__init__.py

Whitespace-only changes.

casbin_middleware/authz_model.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = (g(r.sub, p.sub) || p.sub == "*") && keyMatch(r.obj, p.obj) && (r.act == p.act || p.act == "*")

casbin_middleware/authz_policy.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
p, anonymous, /, GET
2+
p, admin, *, *
3+
g, alice, admin

casbin_middleware/middleware.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2019 The Casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import casbin
16+
17+
from django.core.exceptions import PermissionDenied
18+
19+
20+
class CasbinMiddleware:
21+
"""
22+
Casbin middleware.
23+
"""
24+
25+
def __init__(self, get_response):
26+
self.get_response = get_response
27+
self.enforcer = casbin.Enforcer("casbin_middleware/authz_model.conf", "casbin_middleware/authz_policy.csv")
28+
# One-time configuration and initialization.
29+
30+
def __call__(self, request):
31+
# Code to be executed for each request before
32+
# the view (and later middleware) are called.
33+
34+
if not self.check_permission(request):
35+
raise PermissionDenied
36+
37+
response = self.get_response(request)
38+
39+
# Code to be executed for each request/response after
40+
# the view is called.
41+
42+
return response
43+
44+
def check_permission(self, request):
45+
user = request.user.username
46+
if request.user.is_anonymous:
47+
user = 'anonymous'
48+
path = request.path
49+
method = request.method
50+
return self.enforcer.enforce(user, path, method)
51+
52+
def require_permission(self,):
53+
raise PermissionDenied

django_example/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
'django.contrib.auth.middleware.AuthenticationMiddleware',
4848
'django.contrib.messages.middleware.MessageMiddleware',
4949
'django.middleware.clickjacking.XFrameOptionsMiddleware',
50+
'casbin_middleware.middleware.CasbinMiddleware',
5051
]
5152

5253
ROOT_URLCONF = 'django_example.urls'

0 commit comments

Comments
 (0)