Files
Sebastian UnterschützandClaude Opus 5.5 db3ff19983
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m19s
ci / test (pull_request) Successful in 9m25s
ci / build (pull_request) Successful in 15m40s
ci / image (pull_request) Successful in 4m26s
feat: several owners per organization, and handing ownership over
An organization may now have several owners; the unique index allowing
only one is gone. Owners make members owners or take it back, their own
included, through PUT /api/v1/org/members/:user/owner; handing an
organization over is making someone else owner and stepping down. The
last owner cannot step down, and changes serialize on a per-organization
lock so two owners stepping down at once cannot leave none. A former
owner without roles keeps working as Admin. Owners still cannot be
removed while they are owners.

Deleting an account now takes along only the organizations it is the
last owner of, counted under the same lock; it just leaves those with
further owners. The members page offers "Make owner", "Remove as owner"
and "Step down as owner" to owners.

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

520 lines
18 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
Pager Pager
Roles []Role
Invitations []PendingInvite
CanManage bool
// Scopes are the resources an assignment can be limited to; values are
// "<resource_type>:<id>".
Scopes []ScopeOption
// IsOwner is set when the viewer owns the organization and so decides
// who else does; Self is the viewer, Owners counts the owners.
IsOwner bool
Self string
Owners int
}
// 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">
@ownerAction(v, m)
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>
@pager(v.Pager, "previous", "next")
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")
}
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")
@tokenUsage()
<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 class="mt-3">
@tokenUsage()
</div>
</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
// CheckTypes are the types the platform offers, enabled unless the
// organization switched them off.
CheckTypes []CheckTypeOption
Usage UsageView
}
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>
@usageSection(v.Usage)
@checkTypesSection(v.CheckTypes, "/org/check-types", "/settings/org", "org.check_types_intro")
@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
Consent SignupConsent
}
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)
@consentField(v.Consent)
@submit(i18n.T(ctx, "invite.create_account"))
</form>
}
}
}
}
func maxDaysValue(d int) string {
if d == 0 {
return ""
}
return strconv.Itoa(d)
}
// tokenUsage explains how to send a token and links the API documentation.
templ tokenUsage() {
<p class="mb-4 text-sm text-muted">
{ i18n.T(ctx, "tokens.usage") }
<code class="font-mono text-xs text-ink">Authorization: Bearer ust_…</code>
·
<a href="/api/docs" class="text-ink underline underline-offset-2">{ i18n.T(ctx, "apidocs.title") }</a>
</p>
}
// ownerAction lets owners make a member an owner or take it back, their
// own included, as long as another owner remains.
templ ownerAction(v MembersView, m Member) {
if v.IsOwner {
switch {
case !m.IsOwner:
@ownerForm(m.UserID, true, i18n.T(ctx, "members.make_owner"), i18n.T(ctx, "members.make_owner_confirm", "Email", m.Email))
case v.Owners > 1 && m.UserID == v.Self:
@ownerForm(m.UserID, false, i18n.T(ctx, "members.step_down"), i18n.T(ctx, "members.step_down_confirm"))
case v.Owners > 1:
@ownerForm(m.UserID, false, i18n.T(ctx, "members.remove_owner"), i18n.T(ctx, "members.remove_owner_confirm", "Email", m.Email))
}
}
}
templ ownerForm(user string, owner bool, label, confirm string) {
<form data-api={ "/org/members/" + user + "/owner" } data-method="put" data-next="/settings/members" data-confirm={ confirm } class="mb-2 inline-block">
<input type="checkbox" name="owner" checked?={ owner } hidden/>
<button type="submit" class="text-sm text-muted underline-offset-2 hover:text-ink hover:underline">{ label }</button>
<p data-error class="hidden text-xs text-down"></p>
</form>
}