From 5e6b888478cf7445ba3003341c0f06105e40f85d Mon Sep 17 00:00:00 2001 From: emmdim Date: Fri, 24 Jan 2025 12:58:42 +0100 Subject: [PATCH] * Adds `any` user role to represent generic membership * Modifies `HasRoleFor` to accept `any` role --- db/const.go | 4 ++++ db/types.go | 13 +++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/db/const.go b/db/const.go index 11a7b4d..c612f48 100644 --- a/db/const.go +++ b/db/const.go @@ -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" @@ -31,6 +32,7 @@ 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 @@ -38,6 +40,7 @@ var UserRolesNames = map[UserRole]string{ AdminRole: "Admin", ManagerRole: "Manager", ViewerRole: "Viewer", + AnyRole: "Any", } // HasWriteAccess function checks if the user role has write access @@ -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 diff --git a/db/types.go b/db/types.go index 5e3e7c1..bb6eb9b 100644 --- a/db/types.go +++ b/db/types.go @@ -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 } }