Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #175 Refactor authorisation middleware to check RBAC for a user #178

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 0 additions & 21 deletions src/rbac/rbac.go
Original file line number Diff line number Diff line change
@@ -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
}