34 lines
996 B
Go
34 lines
996 B
Go
package auth
|
|||
|
|
|
||
|
|
// Role is the shared account-management role used by the web applications.
|
||
|
|
type Role string
|
||
|
|
|
||
|
|
const (
|
||
|
|
RoleSuperAdmin Role = "superadmin"
|
||
|
|
RoleAdmin Role = "admin"
|
||
|
|
RoleUser Role = "user"
|
||
|
|
)
|
||
|
|
|
||
|
|
// NormalizeRole keeps persisted role values inside the shared contract.
|
||
|
|
func NormalizeRole(value string) Role {
|
||
|
|
switch Role(value) {
|
||
|
|
case RoleSuperAdmin:
|
||
|
|
return RoleSuperAdmin
|
||
|
|
case RoleAdmin:
|
||
|
|
return RoleAdmin
|
||
|
|
default:
|
||
|
|
return RoleUser
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// CanManageUsers is the common authorization gate for account CRUD.
|
||
|
|
func CanManageUsers(role string) bool {
|
||
|
|
role = string(NormalizeRole(role))
|
||
|
|
return role == string(RoleSuperAdmin) || role == string(RoleAdmin)
|
||
|
|
}
|
||
|
|
|
||
|
|
// CanAssignRole prevents an administrator from creating or promoting another
|
||
|
|
// administrator. Only the first-created superadmin can delegate admin access.
|
||
|
|
func CanAssignRole(actorRole, targetRole string) bool {
|
||
|
|
return NormalizeRole(actorRole) == RoleSuperAdmin && NormalizeRole(targetRole) != RoleSuperAdmin
|
||
|
|
}
|