Skip to content

Commit

Permalink
casbin rbac sample enforcer
Browse files Browse the repository at this point in the history
  • Loading branch information
sanudutta45 committed Nov 7, 2024
1 parent 347a1c2 commit 88c500e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 7 additions & 1 deletion backend/api/UsersView.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
from starlette.responses import JSONResponse, Response
from common.paths import api_base_url
from backend.managers.UsersManager import UsersManager
from backend.managers.CasbinRoleManager import CasbinRoleManager
from backend.pagination import parse_pagination_params
from aiosqlite import IntegrityError
from backend.schemas import UserSchema

class UsersView:
def __init__(self):
self.um = UsersManager()
self.cb = CasbinRoleManager()

async def get(self, id: str):
user = await self.um.retrieve_user(id)
if user is None:
return JSONResponse(status_code=404, headers={"error": "User not found"})
return JSONResponse(user.model_dump(), status_code=200)

async def post(self, body: dict):
async def post(self,token_info, body: dict):
enforcer = self.cb.get_enforcer()
if not enforcer.enforce(token_info["role"], 'user', 'POST'):
return JSONResponse({"message": "Permission denied"}, status_code=403)

try:
id = await self.um.create_user(body['name'], body['email'])
return JSONResponse({"id": id}, status_code=201, headers={'Location': f'{api_base_url}/users/{id}'})
Expand Down
5 changes: 2 additions & 3 deletions backend/rbac_model.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
r = role, res_id, act

[policy_definition]
p = role, res_id, act, eft
p = role, res_id, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = (r.res_id == p.res_id && r.act == p.act && r.role == p.role) ||
(r.res_id != p.res_id && p.res_id == "DEFAULT" && r.act == p.act && r.role == p.role)
m = (r.res_id == p.res_id && r.act == p.act && r.role == p.role) || (p.res_id == "DEFAULT" && r.act == p.act && r.role == p.role)

[role_definition]
g = _, _

0 comments on commit 88c500e

Please sign in to comment.