Files
uptimesebi/internal/web/org.templ
T
Sebastian UnterschützandClaude Opus 5.5 d7d4821e3e feat(api): enforce roles, org sign-in policies, invitations and api tokens
Every org route resolves a principal from the session or a bearer token
and checks a permission. Nobody can grant, revoke or remove more than
they hold. Tokens are capped by their owner's current rights and cannot
manage credentials. Orgs can require TOTP or SSO; the owner keeps a
password fallback for SSO.

Closes #6
Closes #7
Closes #8
Closes #9
Closes #41
Closes #45

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 00:49:35 +01:00

397 lines
13 KiB
Templ
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"slices"
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// Role is a built-in or custom role.
type Role struct {
ID string
Name string
BuiltIn bool
Permissions []string
}
// Assignment is one role a member holds.
type Assignment struct {
ID string
RoleName string
// Scope is empty for organization-wide assignments.
Scope string
}
// Member is a row on the members page.
type Member struct {
UserID string
Email string
DisplayName string
IsOwner bool
Assignments []Assignment
}
// PendingInvite is an invitation that was not accepted yet.
type PendingInvite struct {
ID string
Email string
RoleName string
}
// MembersView is the data of the members page.
type MembersView struct {
Members []Member
Roles []Role
Invitations []PendingInvite
CanManage bool
}
templ settingsHeading(titleID, introID string) {
<h1 class="mb-2 text-lg font-semibold">{ i18n.T(ctx, titleID) }</h1>
if introID != "" {
<p class="mb-8 max-w-2xl text-sm text-muted">{ i18n.T(ctx, introID) }</p>
}
}
templ sectionHeading(id string) {
<h2 class="mb-4 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, id) }</h2>
}
templ roleSelect(roles []Role, name string) {
<select name={ name } class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm" required>
for _, r := range roles {
<option value={ r.ID }>{ r.Name }</option>
}
</select>
}
templ MembersPage(p Page, me Me, v MembersView) {
@consoleShell(p, me) {
@settingsHeading("members.title", "members.intro")
<table class="mb-12 w-full text-left text-sm">
<thead class="border-b border-rule text-xs uppercase tracking-wider text-muted">
<tr>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "auth.field.email") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "members.roles") }</th>
<th class="py-2"></th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
for _, m := range v.Members {
<tr class="align-top">
<td class="py-2.5 pr-4">
{ m.Email }
if m.DisplayName != "" {
<span class="block text-xs text-muted">{ m.DisplayName }</span>
}
</td>
<td class="py-2.5 pr-4">
if m.IsOwner {
<span class="font-medium">{ i18n.T(ctx, "members.owner") }</span>
}
<ul class="flex flex-wrap gap-1.5">
for _, a := range m.Assignments {
<li class="inline-flex items-center gap-1.5 border border-rule px-1.5 py-0.5 text-xs">
{ a.RoleName }
if a.Scope != "" {
<span class="text-muted">· { a.Scope }</span>
}
if v.CanManage {
<button
type="button"
data-delete={ "/org/assignments/" + a.ID }
class="text-muted hover:text-down"
aria-label={ i18n.T(ctx, "members.unassign") }
>×</button>
}
</li>
}
</ul>
</td>
<td class="py-2.5 text-right">
if v.CanManage && !m.IsOwner {
<details class="inline-block text-left">
<summary class="cursor-pointer text-muted hover:text-ink">{ i18n.T(ctx, "sso.action.edit") }</summary>
<div class="mt-3 w-64">
<form data-api={ "/org/members/" + m.UserID + "/roles" } data-next="/settings/members">
@formError()
@roleSelect(v.Roles, "role_id")
@submit(i18n.T(ctx, "members.assign"))
</form>
<button
type="button"
data-delete={ "/org/members/" + m.UserID }
data-confirm={ i18n.T(ctx, "members.remove_confirm") }
class="mt-4 text-sm text-down hover:underline"
>{ i18n.T(ctx, "members.remove") }</button>
</div>
</details>
}
</td>
</tr>
}
</tbody>
</table>
if v.CanManage {
<div class="grid gap-12 md:grid-cols-2">
<section>
@sectionHeading("members.invite")
<form data-api="/org/invitations" data-show-link>
@formError()
@field(i18n.T(ctx, "auth.field.email"), "email", "email", "off", true)
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "members.role") }</span>
@roleSelect(v.Roles, "role_id")
</label>
@submit(i18n.T(ctx, "members.invite_submit"))
<div data-link-output hidden class="mt-6 border border-ink p-4">
<p class="mb-2 text-sm">{ i18n.T(ctx, "members.invite_link") }</p>
<input data-link readonly class="w-full border border-rule bg-transparent px-2 py-1.5 font-mono text-xs"/>
</div>
</form>
</section>
if len(v.Invitations) > 0 {
<section>
@sectionHeading("members.pending")
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, inv := range v.Invitations {
<li class="flex items-center justify-between gap-4 py-2">
<span>{ inv.Email } <span class="text-muted">· { inv.RoleName }</span></span>
<button type="button" data-delete={ "/org/invitations/" + inv.ID } class="text-muted hover:text-down">{ i18n.T(ctx, "members.revoke") }</button>
</li>
}
</ul>
</section>
}
</div>
}
}
}
// RolesView is the data of the roles page.
type RolesView struct {
Roles []Role
Permissions []string
// Grantable lists the permissions the viewer may put into a role.
Grantable []string
CanManage bool
}
templ permissionChecks(all, grantable, checked []string) {
<fieldset class="mb-4 grid grid-cols-1 gap-1 sm:grid-cols-2">
for _, perm := range all {
<label class={ "flex items-center gap-2 text-sm", templ.KV("text-muted", !slices.Contains(grantable, perm)) }>
<input
type="checkbox"
name="permissions"
value={ perm }
data-multi
checked?={ slices.Contains(checked, perm) }
disabled?={ !slices.Contains(grantable, perm) }
class="accent-ink"
/>
{ i18n.T(ctx, "perm."+perm) }
</label>
}
</fieldset>
}
templ RolesPage(p Page, me Me, v RolesView) {
@consoleShell(p, me) {
@settingsHeading("roles.title", "roles.intro")
<div class="mb-12 space-y-6">
for _, r := range v.Roles {
<section class="border-t border-rule pt-4">
<div class="mb-2 flex items-baseline justify-between gap-4">
<h2 class="font-medium">
{ r.Name }
if r.BuiltIn {
<span class="ml-2 text-xs text-muted">{ i18n.T(ctx, "roles.built_in") }</span>
}
</h2>
if v.CanManage && !r.BuiltIn {
<button
type="button"
data-delete={ "/org/roles/" + r.ID }
data-confirm={ i18n.T(ctx, "roles.delete_confirm") }
class="text-sm text-down hover:underline"
>{ i18n.T(ctx, "roles.delete") }</button>
}
</div>
if v.CanManage && !r.BuiltIn {
<form data-api={ "/org/roles/" + r.ID } data-method="put" data-next="/settings/roles" class="max-w-xl">
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", r.Name, "", true)
@permissionChecks(v.Permissions, v.Grantable, r.Permissions)
@submit(i18n.T(ctx, "common.save"))
</form>
} else {
<ul class="flex flex-wrap gap-1.5 text-xs">
for _, perm := range r.Permissions {
<li class="border border-rule px-1.5 py-0.5">{ i18n.T(ctx, "perm."+perm) }</li>
}
</ul>
}
</section>
}
</div>
if v.CanManage {
<section class="max-w-xl">
@sectionHeading("roles.create")
<form data-api="/org/roles" data-next="/settings/roles">
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "", true)
@permissionChecks(v.Permissions, v.Grantable, nil)
@submit(i18n.T(ctx, "roles.create"))
</form>
</section>
}
}
}
// Token is a row on the tokens page.
type Token struct {
ID string
Name string
Prefix string
UserEmail string
Permissions []string
ExpiresAt string
LastUsedAt string
}
// TokensView is the data of the tokens page.
type TokensView struct {
Tokens []Token
Grantable []string
ShowOwner bool
}
templ TokensPage(p Page, me Me, v TokensView) {
@consoleShell(p, me) {
@settingsHeading("tokens.title", "tokens.intro")
if len(v.Tokens) > 0 {
<table class="mb-12 w-full text-left text-sm">
<thead class="border-b border-rule text-xs uppercase tracking-wider text-muted">
<tr>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "sso.field.name") }</th>
if v.ShowOwner {
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.owner") }</th>
}
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.expires") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.last_used") }</th>
<th class="py-2"></th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
for _, t := range v.Tokens {
<tr>
<td class="py-2.5 pr-4">{ t.Name } <span class="block font-mono text-xs text-muted">{ t.Prefix }…</span></td>
if v.ShowOwner {
<td class="py-2.5 pr-4">{ t.UserEmail }</td>
}
<td class="py-2.5 pr-4 font-mono text-xs">{ t.ExpiresAt }</td>
<td class="py-2.5 pr-4 font-mono text-xs">{ t.LastUsedAt }</td>
<td class="py-2.5 text-right">
<button
type="button"
data-delete={ "/org/tokens/" + t.ID }
data-confirm={ i18n.T(ctx, "tokens.revoke_confirm") }
class="text-muted hover:text-down"
>{ i18n.T(ctx, "members.revoke") }</button>
</td>
</tr>
}
</tbody>
</table>
}
<section class="max-w-xl">
@sectionHeading("tokens.create")
<form data-api="/org/tokens" data-show-link>
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "", true)
@permissionChecks(v.Grantable, v.Grantable, nil)
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "tokens.expires") }</span>
<select name="expires_in_days" data-number class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="30">{ i18n.T(ctx, "tokens.days", "Count", 30) }</option>
<option value="90" selected>{ i18n.T(ctx, "tokens.days", "Count", 90) }</option>
<option value="365">{ i18n.T(ctx, "tokens.days", "Count", 365) }</option>
<option value="0">{ i18n.T(ctx, "tokens.never") }</option>
</select>
</label>
@submit(i18n.T(ctx, "tokens.create"))
<div data-link-output hidden class="mt-6 border border-ink p-4">
<p class="mb-2 text-sm">{ i18n.T(ctx, "tokens.copy_now") }</p>
<input data-link readonly class="w-full border border-rule bg-transparent px-2 py-1.5 font-mono text-xs"/>
</div>
</form>
</section>
}
}
// OrgSettings is the data of the organization settings page.
type OrgSettings struct {
RequireTOTP bool
RequireSSO bool
}
templ OrgSettingsPage(p Page, me Me, v OrgSettings) {
@consoleShell(p, me) {
@settingsHeading("org.title", "")
<section class="max-w-xl">
@sectionHeading("org.policy")
<form data-api="/org/policy" data-method="put" data-next="/settings/org">
@formError()
@checkbox(i18n.T(ctx, "org.require_totp"), "require_totp", v.RequireTOTP)
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_totp_hint") }</p>
@checkbox(i18n.T(ctx, "org.require_sso"), "require_sso", v.RequireSSO)
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_sso_hint") }</p>
@submit(i18n.T(ctx, "common.save"))
</form>
</section>
}
}
// Invitation is the data of the invitation landing page.
type Invitation struct {
Token string
Valid bool
OrgName string
RoleName string
Email string
SignedInAs string
AccountExists bool
}
templ InvitationPage(p Page, v Invitation) {
@authShell(p) {
if !v.Valid {
<p class="text-sm">{ i18n.T(ctx, "error.invitation_invalid") }</p>
} else {
<p class="mb-6 text-sm">{ i18n.T(ctx, "invite.summary", "Org", v.OrgName, "Role", v.RoleName) }</p>
<p class="mb-6 font-mono text-sm">{ v.Email }</p>
switch {
case v.SignedInAs == v.Email:
<form data-api={ "/invitations/" + v.Token + "/accept" } data-next="/">
@formError()
@submit(i18n.T(ctx, "invite.accept"))
</form>
case v.SignedInAs != "":
<p class="text-sm text-down">{ i18n.T(ctx, "error.invitation_email_mismatch") }</p>
<button data-logout class="mt-4 text-sm underline underline-offset-2">{ i18n.T(ctx, "console.nav.logout") }</button>
case v.AccountExists:
<a href={ templ.SafeURL("/login?next=/invite/" + v.Token) } class="inline-block bg-ink px-3 py-2 text-sm text-paper">{ i18n.T(ctx, "invite.sign_in") }</a>
default:
<form data-api={ "/invitations/" + v.Token + "/signup" } data-next="/">
@formError()
@field(i18n.T(ctx, "auth.field.name"), "display_name", "text", "name", false)
@field(i18n.T(ctx, "auth.field.new_password", "Min", auth.MinPasswordLength), "password", "password", "new-password", true)
@submit(i18n.T(ctx, "invite.create_account"))
</form>
}
}
}
}