go-loose/internal/role/role.go

133 lines
4.6 KiB
Go
Raw Normal View History

2026-10-04 16:57:30 +02:00
// Package role defines the permission vocabulary that authorizes console and
// management behavior.
2026-10-04 12:24:13 +02:00
//
2026-10-04 16:57:30 +02:00
// Roles are rows in the roles table and their permissions live in
// role_permissions, so a tenant can define its own and authorization is
// resolved by the database rather than from a matrix held here. What belongs in
// this package is the closed set of permissions a role may be granted, plus the
// labels the console renders.
//
// Keep the permission list in step with the role_permissions_permission_check
// constraint in migration 0006: a name that is not in both places cannot be
// stored.
2026-10-04 12:24:13 +02:00
package role
// Permission is a capability granted by a role.
type Permission string
const (
2026-10-04 16:57:30 +02:00
// ReadConsole opens the management console. Self-service members lack it so
// that they are routed to their profile instead.
ReadConsole Permission = "console.read"
// ReadUsers lists a tenant's members and their roles.
ReadUsers Permission = "users.read"
// ManageApplications registers applications and configures their client
// credentials.
2026-10-04 12:24:13 +02:00
ManageApplications Permission = "applications.manage"
2026-10-04 16:57:30 +02:00
// ManageKeys issues and revokes API keys.
ManageKeys Permission = "keys.manage"
// ManageContracts imports OpenAPI contracts.
ManageContracts Permission = "contracts.manage"
// ManageUsers invites members and changes the roles and application grants
// of existing ones.
ManageUsers Permission = "users.manage"
// ManageRoles creates and edits the tenant's own roles. Kept separate from
// ManageUsers because defining roles is what defines privilege.
ManageRoles Permission = "roles.manage"
// ManageTenants creates tenants and administers the installation.
ManageTenants Permission = "tenants.manage"
// EditProfile changes one's own display name and picture.
EditProfile Permission = "profile.edit"
2026-10-04 12:24:13 +02:00
)
2026-10-04 16:57:30 +02:00
// ordered lists every permission from most to least sensitive. The order is the
// one the console renders checkboxes in.
2026-10-04 12:24:13 +02:00
var ordered = []Permission{
2026-10-04 16:57:30 +02:00
ManageTenants,
ManageRoles,
ManageUsers,
ManageApplications,
ManageKeys,
ManageContracts,
ReadUsers,
ReadConsole,
EditProfile,
2026-10-04 12:24:13 +02:00
}
2026-10-04 16:57:30 +02:00
var known = map[Permission]bool{
ReadConsole: true,
ReadUsers: true,
ManageApplications: true,
ManageKeys: true,
ManageContracts: true,
ManageUsers: true,
ManageRoles: true,
ManageTenants: true,
EditProfile: true,
2026-10-04 12:24:13 +02:00
}
2026-10-04 16:57:30 +02:00
// Entry describes a permission for the console and for API clients.
type Entry struct {
Value string `json:"value"`
Label string `json:"label"`
Description string `json:"description"`
2026-10-04 12:24:13 +02:00
}
2026-10-04 16:57:30 +02:00
var details = map[Permission]struct{ label, description string }{
ReadConsole: {"Read console", "Open the management console and pick tenants."},
ReadUsers: {"Read members", "List the members of a tenant and the roles they hold."},
ManageApplications: {"Manage applications", "Register applications and configure client credentials."},
ManageKeys: {"Manage API keys", "Issue and revoke API keys."},
ManageContracts: {"Manage contracts", "Import OpenAPI contracts and their endpoints."},
ManageUsers: {"Manage members", "Invite members and change their roles and application grants."},
ManageRoles: {"Manage roles", "Create and edit the roles of this tenant."},
ManageTenants: {"Manage tenants", "Create tenants and administer the installation."},
EditProfile: {"Edit own profile", "Change one's own display name and picture."},
2026-10-04 12:24:13 +02:00
}
2026-10-04 16:57:30 +02:00
// Permissions returns every permission in display order.
func Permissions() []Permission {
result := make([]Permission, len(ordered))
copy(result, ordered)
2026-10-04 12:24:13 +02:00
return result
}
2026-10-04 16:57:30 +02:00
// Parse validates a submitted permission name. Unknown values are rejected so
// that a crafted payload cannot invent capabilities.
func Parse(value string) (Permission, bool) {
candidate := Permission(value)
if !known[candidate] {
2026-10-04 12:24:13 +02:00
return "", false
}
return candidate, true
}
2026-10-04 16:57:30 +02:00
// Valid reports whether the value names a permission.
func (p Permission) Valid() bool {
return known[p]
2026-10-04 12:24:13 +02:00
}
2026-10-04 16:57:30 +02:00
// Catalog returns every permission with a label and description so that the
// console and API clients never hardcode the list.
func Catalog() []Entry {
result := make([]Entry, 0, len(ordered))
2026-10-04 12:24:13 +02:00
for _, permission := range ordered {
2026-10-04 16:57:30 +02:00
detail := details[permission]
result = append(result, Entry{
Value: string(permission),
Label: detail.label,
Description: detail.description,
})
2026-10-04 12:24:13 +02:00
}
return result
}
2026-10-04 16:57:30 +02:00
// Values returns every permission as strings, for use as a SQL array parameter.
func Values() []string {
result := make([]string, 0, len(ordered))
for _, permission := range ordered {
result = append(result, string(permission))
2026-10-04 12:24:13 +02:00
}
return result
}