Files
Sebastian UnterschützandClaude Opus 5.5 6d3b1dea5d
ci / image (pull_request) Successful in 2m22s
ci / build (pull_request) Successful in 6m46s
ci / lint (pull_request) Successful in 12m12s
ci / test (pull_request) Successful in 14m8s
feat(api): OpenAPI documentation with Swagger UI
/api/openapi.json is generated from a table of endpoints and the Go types
the handlers bind and return, including the limits of their binding
tags, so the document follows the code. A test fails for routes missing
from the table or documented but not served. /api/docs shows it with
Swagger UI from our own assets, usable without internet access; "Try it
out" works with the browser session.

Closes #84

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

468 lines
16 KiB
Templ
Raw Permalink 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"
"strconv"
"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
// Scopes are the resources an assignment can be limited to; values are
// "<resource_type>:<id>".
Scopes []ScopeOption
}
// ScopeOption is a resource an assignment can be limited to.
type ScopeOption struct {
Value string
Label string
}
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")
<label class="mt-3 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "members.scope") }</span>
<select name="scope" data-scope class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="">{ i18n.T(ctx, "members.scope_org") }</option>
for _, sc := range v.Scopes {
<option value={ sc.Value }>{ sc.Label }</option>
}
</select>
</label>
@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
CreatedAt string
ExpiresAt string
LastUsedAt string
}
// TokensView is the data of the tokens page.
type TokensView struct {
Tokens []Token
Grantable []string
// ShowOwner is set when the viewer oversees all tokens of the org.
ShowOwner bool
// MaxDays is the org's token lifetime limit, 0 if none.
MaxDays int
// ExpiryDays are the lifetimes offered when creating a token; 0 means
// no expiry.
ExpiryDays []int
}
templ TokensPage(p Page, me Me, v TokensView) {
@consoleShell(p, me) {
if v.ShowOwner {
@settingsHeading("tokens.title", "tokens.intro_all")
} else {
@settingsHeading("tokens.title", "tokens.intro")
}
<p class="-mt-4 mb-8 text-sm"><a href="/api/docs" class="underline underline-offset-2">{ i18n.T(ctx, "apidocs.title") }</a></p>
if v.MaxDays > 0 {
<p class="-mt-4 mb-8 text-sm">{ i18n.T(ctx, "tokens.policy", "Count", v.MaxDays) }</p>
}
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.permissions") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "tokens.created") }</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 class="align-top">
<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">
<ul class="flex max-w-xs flex-wrap gap-1 text-xs">
for _, perm := range t.Permissions {
<li class="border border-rule px-1 py-px">{ i18n.T(ctx, "perm."+perm) }</li>
}
</ul>
</td>
<td class="py-2.5 pr-4 whitespace-nowrap font-mono text-xs">{ t.CreatedAt }</td>
<td class="py-2.5 pr-4 whitespace-nowrap font-mono text-xs">{ t.ExpiresAt }</td>
<td class="py-2.5 pr-4 whitespace-nowrap 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">
for i, d := range v.ExpiryDays {
if d == 0 {
<option value="0">{ i18n.T(ctx, "tokens.never") }</option>
} else {
<option value={ strconv.Itoa(d) } selected?={ i == 0 }>{ i18n.T(ctx, "tokens.days", "Count", d) }</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
// TokenMaxDays is 0 when API token lifetime is unlimited.
TokenMaxDays int
}
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-6 ml-6 text-xs text-muted">{ i18n.T(ctx, "org.require_sso_hint") }</p>
<label class="mb-1 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "org.token_max_days") }</span>
<input
type="number"
name="api_token_max_days"
data-number
min="0"
max="3650"
value={ maxDaysValue(v.TokenMaxDays) }
placeholder={ i18n.T(ctx, "org.token_max_days_none") }
class="w-40 border border-rule bg-transparent px-2.5 py-1.5 font-mono text-sm outline-none focus:border-ink"
/>
</label>
<p class="mb-6 text-xs text-muted">{ i18n.T(ctx, "org.token_max_days_hint") }</p>
@submit(i18n.T(ctx, "common.save"))
</form>
</section>
@orgDataSection(me)
}
}
// 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>
}
}
}
}
func maxDaysValue(d int) string {
if d == 0 {
return ""
}
return strconv.Itoa(d)
}