From 5c3bc8cacccd41fa35998d5216b1b5f5ec699d1d Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Thu, 21 Sep 2023 20:14:44 +0530 Subject: [PATCH] Fix #175 Refactor authorisation middleware to check RBAC for a user --- src/middleware/middleware.go | 16 +++------------- src/rbac/rbac.go | 21 --------------------- 2 files changed, 3 insertions(+), 34 deletions(-) diff --git a/src/middleware/middleware.go b/src/middleware/middleware.go index aa7873e..352c594 100644 --- a/src/middleware/middleware.go +++ b/src/middleware/middleware.go @@ -175,20 +175,10 @@ func Authorize(e *casbin.Enforcer) Middleware { var role string - orgID, ok := mux.Vars(r)["organizationID"] - if !ok { - orgID, ok = mux.Vars(r)["orgID"] - } - if !ok && len(roles) > 0 { - orgID = user.Orgs[0].OrgID.Hex() - } - - if rbac.IsUser(roles) { - role = rbac.ROLE_USER - } - - if rbac.IsOrgAdmin(roles, orgID) { + if len(roles) > 0 { role = rbac.ROLE_ADMIN + } else { + role = rbac.ROLE_USER } // casbin enforce diff --git a/src/rbac/rbac.go b/src/rbac/rbac.go index e614dcb..cb20a05 100644 --- a/src/rbac/rbac.go +++ b/src/rbac/rbac.go @@ -1,28 +1,7 @@ package rbac -import ( - "github.com/bb-consent/api/src/user" -) - // RBAC User Roles const ( ROLE_USER string = "user" ROLE_ADMIN string = "organisation_admin" ) - -// IsOrgAdmin is user an admin in the organisation -func IsOrgAdmin(roles []user.Role, orgID string) bool { - for _, item := range roles { - if item.RoleID == 1 { - if item.OrgID == orgID { - return true - } - } - } - return false -} - -// IsUser is User Role user -func IsUser(roles []user.Role) bool { - return len(roles) == 0 -}