Skip to content

Commit

Permalink
* Adds any user role to represent generic membership
Browse files Browse the repository at this point in the history
* Modifies `HasRoleFor` to accept `any` role
  • Loading branch information
emmdim committed Jan 24, 2025
1 parent 06ab97c commit 5e6b888
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 4 additions & 0 deletions db/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
AdminRole UserRole = "admin"
ManagerRole UserRole = "manager"
ViewerRole UserRole = "viewer"
AnyRole UserRole = "any"
// organization types
AssemblyType OrganizationType = "assembly"
AssociationType OrganizationType = "association"
Expand All @@ -31,13 +32,15 @@ var writableRoles = map[UserRole]bool{
AdminRole: true,
ManagerRole: true,
ViewerRole: false,
AnyRole: false,
}

// UserRoleNames is a map that contains the user role names by role
var UserRolesNames = map[UserRole]string{
AdminRole: "Admin",
ManagerRole: "Manager",
ViewerRole: "Viewer",
AnyRole: "Any",
}

// HasWriteAccess function checks if the user role has write access
Expand Down Expand Up @@ -81,6 +84,7 @@ var validRoles = map[UserRole]bool{
AdminRole: true,
ManagerRole: true,
ViewerRole: true,
AnyRole: true,
}

// IsValidUserRole function checks if the user role is valid
Expand Down
13 changes: 3 additions & 10 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@ type UserVerification struct {

func (u *User) HasRoleFor(address string, role UserRole) bool {
for _, org := range u.Organizations {
if org.Address == address && string(org.Role) == string(role) {
return true
}
}
return false
}

func (u *User) IsMemberOf(address string) bool {
for _, org := range u.Organizations {
if org.Address == address {
if org.Address == address &&
// Check if the role is "any: or if the role matches the organization role
(string(role) == string(AnyRole) || string(org.Role) == string(role)) {
return true
}
}
Expand Down

0 comments on commit 5e6b888

Please sign in to comment.