Files
uptimesebi/internal/web/regions.templ
T
Sebastian UnterschützandClaude Opus 5.5 fe5c4fb24c
ci / build (pull_request) Successful in 5m13s
ci / lint (pull_request) Successful in 5m45s
ci / image (pull_request) Successful in 6m20s
ci / test (pull_request) Successful in 11m46s
feat: sign up and sign in with platform identity providers
Platform admins register identity providers for the whole installation:
any OpenID Connect provider, checked by discovery, and GitHub or GitHub
Enterprise through OAuth, which reads the primary email GitHub verified.
Sign-in pages offer them; the sign-up page offers those open to sign-up,
also where email sign-up is closed for lack of a mail server.

Accounts are reached only through identities stored by subject. A new
identity signs up only with an email the provider confirmed, within the
per-address limit and sign-up mode, and never takes over an existing
account, including through an alias of its address: its owner links
the provider in the security settings. The account's TOTP still applies
after the provider. Unlinking refuses to remove the last way to sign in,
and removing a provider refuses while accounts depend on it.

The test browser takes the CSRF token from pages, as the console does.

Closes #114

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

386 lines
15 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 "git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
// Region is a row in a region list.
type Region struct {
ID string
Key string
Name string
// Platform regions are shared by all organizations.
Platform bool
// Manageable regions can be deleted on the current page.
Manageable bool
Instances int64
}
// Agent is a checker or status page server.
type Agent struct {
ID string
Kind string
Name string
Region string
Prefix string
LastSeen string
// AllowPrivate is set for an organization's checkers: whether they
// may check private addresses.
AllowPrivate *bool
}
// RegionsView is the data of the org regions page and the admin panel.
type RegionsView struct {
Regions []Region
Agents []Agent
// APIBase is the prefix of the endpoints the forms post to, either
// /org or /admin.
APIBase string
// Next is the page to reload after a change.
Next string
// OwnRegions are the regions agents can be created in.
OwnRegions []Region
}
templ regionsBody(v RegionsView) {
<section class="mb-12">
@sectionHeading("regions.list")
<table class="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>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.key") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.owner") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "regions.instances") }</th>
<th class="py-2"></th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
for _, r := range v.Regions {
<tr>
<td class="py-2.5 pr-4">{ r.Name }</td>
<td class="py-2.5 pr-4 font-mono text-xs">{ r.Key }</td>
<td class="py-2.5 pr-4">
if r.Platform {
<span class="text-muted">{ i18n.T(ctx, "regions.platform") }</span>
} else {
{ i18n.T(ctx, "regions.own") }
}
</td>
<td class="py-2.5 pr-4 font-mono text-xs">
<span class={ templ.KV("text-up", r.Instances > 0), templ.KV("text-unknown", r.Instances == 0) }>●</span> { i18n.T(ctx, "regions.live", "Count", int(r.Instances)) }
</td>
<td class="py-2.5 text-right">
if r.Manageable {
<button type="button" data-delete={ v.APIBase + "/regions/" + r.ID } data-confirm={ i18n.T(ctx, "regions.delete_confirm") } class="text-muted hover:text-down">{ i18n.T(ctx, "regions.delete") }</button>
}
</td>
</tr>
}
</tbody>
</table>
</section>
<div class="mb-12 grid gap-12 md:grid-cols-2">
<section>
@sectionHeading("regions.create")
<form data-api={ v.APIBase + "/regions" } data-next={ v.Next }>
@formError()
@valueField(i18n.T(ctx, "regions.key"), "key", "", "office-berlin", true)
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "Office Berlin", false)
@submit(i18n.T(ctx, "regions.create"))
</form>
</section>
<section>
@sectionHeading("agents.create")
<form data-api={ v.APIBase + "/agents" } data-show-link>
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "checker-1", true)
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "agents.kind") }</span>
<select name="kind" class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="checker">{ i18n.T(ctx, "agents.kind_checker") }</option>
<option value="statuspage">{ i18n.T(ctx, "agents.kind_statuspage") }</option>
</select>
</label>
<label class="mb-4 block">
<span class="mb-1 block text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "agents.region") }</span>
<select name="region_id" data-nullable class="w-full border border-rule bg-transparent px-2 py-1.5 text-sm">
<option value="">{ i18n.T(ctx, "agents.no_region") }</option>
for _, r := range v.OwnRegions {
<option value={ r.ID }>{ r.Name }</option>
}
</select>
</label>
if v.APIBase == "/org" {
@checkbox(i18n.T(ctx, "agents.allow_private"), "allow_private", true)
<p class="-mt-2 mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "agents.allow_private_hint") }</p>
}
@submit(i18n.T(ctx, "agents.create"))
<div data-link-output hidden class="mt-6 border border-ink p-4">
<p class="mb-2 text-sm">{ i18n.T(ctx, "agents.token_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>
</div>
if len(v.Agents) > 0 {
<section>
@sectionHeading("agents.list")
<ul class="divide-y divide-rule border-y border-rule text-sm">
for _, a := range v.Agents {
<li class="flex items-center justify-between gap-4 py-2">
<span>
{ a.Name }
<span class="text-muted">· { i18n.T(ctx, "agents.kind_"+a.Kind) }</span>
if a.Region != "" {
<span class="text-muted">· { a.Region }</span>
}
<span class="block font-mono text-xs text-muted">{ a.Prefix }… · { a.LastSeen }</span>
</span>
if a.AllowPrivate != nil {
<form data-api={ v.APIBase + "/agents/" + a.ID } data-method="patch" data-autosave class="ml-auto">
<label class="flex items-center gap-2 text-xs text-muted">
<input type="checkbox" name="allow_private" checked?={ *a.AllowPrivate } class="accent-ink"/>
{ i18n.T(ctx, "agents.allow_private_short") }
</label>
<p data-error class="hidden text-xs text-down"></p>
</form>
}
<button type="button" data-delete={ v.APIBase + "/agents/" + a.ID } data-confirm={ i18n.T(ctx, "agents.revoke_confirm") } class="text-muted hover:text-down">{ i18n.T(ctx, "members.revoke") }</button>
</li>
}
</ul>
</section>
}
}
templ RegionsPage(p Page, me Me, v RegionsView) {
@consoleShell(p, me) {
@settingsHeading("regions.title", "regions.intro")
@regionsBody(v)
}
}
templ AdminPage(p Page, me Me, v RegionsView, storage StorageView, checkTypes []CheckTypeOption, platformPrivate bool, limits LimitsView, idps IDPAdminView) {
@consoleShell(p, me) {
@settingsHeading("admin.title", "admin.intro")
@adminLinks()
@idpSection(idps)
@limitsSection(limits)
@regionsBody(v)
@checkerNetworkSection(platformPrivate)
@checkTypesSection(checkTypes, "/admin/check-types", "/admin", "admin.check_types_intro")
@storageSection(storage)
}
}
// CheckTypeOption is a check type in the admin panel.
type CheckTypeOption struct {
Type string
Enabled bool
}
templ checkerNetworkSection(allow bool) {
<section class="mt-12 max-w-3xl">
@sectionHeading("admin.checker_network")
<form data-api="/admin/checker-network" data-method="put" data-next="/admin">
@formError()
@checkbox(i18n.T(ctx, "admin.platform_private"), "allow_private", allow)
<p class="mb-4 ml-6 text-xs text-muted">{ i18n.T(ctx, "admin.platform_private_hint") }</p>
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "common.save") }</button>
</form>
</section>
}
// checkTypesSection lets the platform or an organization choose the
// check types it offers.
templ checkTypesSection(types []CheckTypeOption, api, next, introID string) {
<section class="mt-12 max-w-3xl">
@sectionHeading("admin.check_types")
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, introID) }</p>
<form data-api={ api } data-method="put" data-next={ next }>
@formError()
<div class="mb-4 grid grid-cols-2 gap-x-6 gap-y-1.5 text-sm sm:grid-cols-3">
for _, t := range types {
<label class="flex items-center gap-2">
<input type="checkbox" name="enabled" value={ t.Type } data-multi checked?={ t.Enabled } class="accent-ink"/>
{ i18n.T(ctx, "monitors.type_"+t.Type) }
</label>
}
</div>
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "common.save") }</button>
</form>
</section>
}
// ModerationView lists public status pages for platform admins.
type ModerationView struct {
Search string
Pages []ModeratedPage
Pager Pager
// Next is the page to return to after an action.
Next string
}
// ModeratedPage is a status page with the organization and account
// behind it.
type ModeratedPage struct {
ID, Title, Slug, URL, Created string
Published bool
Suspended string
Org ModeratedTarget
Owner *ModeratedTarget
}
// ModeratedTarget is an organization or account that can be suspended or
// deleted. Suspended holds the reason, or "-" without one, when suspended.
type ModeratedTarget struct {
ID, Name, Detail, Suspended string
// Admin marks platform admins, who are out of reach here.
Admin bool
}
func (v ModerationView) next() string { return v.Next }
templ moderationSection(v ModerationView) {
<section id="moderation" class="mb-12">
@sectionHeading("moderation.title")
<p class="mb-4 max-w-3xl text-sm text-muted">{ i18n.T(ctx, "moderation.intro") }</p>
<form method="get" action="/admin/status-pages" class="mb-4 flex max-w-xl gap-2">
<input
type="search"
name="q"
value={ v.Search }
placeholder={ i18n.T(ctx, "moderation.search") }
aria-label={ i18n.T(ctx, "moderation.search") }
class="w-full border border-rule bg-transparent px-2.5 py-1.5 text-sm outline-none focus:border-ink"
/>
<button type="submit" class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "moderation.search_button") }</button>
</form>
if len(v.Pages) == 0 {
<p class="text-sm text-muted">{ i18n.T(ctx, "moderation.none") }</p>
} else {
<div class="overflow-x-auto">
<table class="w-full min-w-[42rem] 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, "moderation.page") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.org") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.owner") }</th>
<th class="py-2 pr-4 font-medium">{ i18n.T(ctx, "moderation.created") }</th>
</tr>
</thead>
<tbody class="divide-y divide-rule">
for _, pg := range v.Pages {
<tr class="align-top">
<td class="py-2.5 pr-4">
<a href={ templ.SafeURL(pg.URL) } target="_blank" rel="noopener noreferrer" class="hover:underline">{ pg.Title }</a>
<span class="block font-mono text-xs text-muted">{ pg.Slug }</span>
@moderationState(pg.Suspended, !pg.Published)
</td>
<td class="py-2.5 pr-4">
{ pg.Org.Name }
<span class="block font-mono text-xs text-muted">{ pg.Org.Detail }</span>
@moderationState(pg.Org.Suspended, false)
</td>
<td class="py-2.5 pr-4">
if pg.Owner != nil {
<span class="break-all">{ pg.Owner.Name }</span>
@moderationState(pg.Owner.Suspended, false)
} else {
<span class="text-muted">–</span>
}
</td>
<td class="py-2.5 pr-4 font-mono text-xs text-muted">{ pg.Created }</td>
</tr>
<tr>
<td colspan="4" class="pb-3">
<details class="text-sm">
<summary class="cursor-pointer text-muted hover:text-ink">{ i18n.T(ctx, "moderation.actions") }</summary>
<div class="mt-3 grid gap-6 md:grid-cols-3">
@moderationActions("page", "/admin/status-pages/"+pg.ID, pg.Suspended, pg.Title, v.next())
@moderationActions("org", "/admin/orgs/"+pg.Org.ID, pg.Org.Suspended, pg.Org.Name, v.next())
if pg.Owner != nil && pg.Owner.Admin {
<p class="text-xs text-muted">{ i18n.T(ctx, "moderation.owner_is_admin") }</p>
} else if pg.Owner != nil {
@moderationActions("owner", "/admin/users/"+pg.Owner.ID, pg.Owner.Suspended, pg.Owner.Name, v.next())
}
</div>
</details>
</td>
</tr>
}
</tbody>
</table>
</div>
}
@pager(v.Pager, "newer", "older")
</section>
}
templ moderationState(suspended string, draft bool) {
if suspended != "" {
<span class="mt-1 inline-block text-xs font-medium text-down">
{ i18n.T(ctx, "moderation.suspended") }
if suspended != "-" {
<span class="font-normal text-muted">{ "· " + suspended }</span>
}
</span>
} else if draft {
<span class="mt-1 inline-block text-xs text-muted">{ i18n.T(ctx, "pages.draft") }</span>
}
}
// moderationActions suspends, reinstates or deletes one target; kind is
// "page", "org" or "owner".
templ moderationActions(kind, api, suspended, name, next string) {
<div>
<h4 class="mb-2 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "moderation.target_"+kind) }</h4>
<form data-api={ api + "/suspension" } data-method="put" data-next={ next } class="mb-2">
@formError()
if suspended != "" {
<input type="checkbox" name="suspended" hidden/>
<button type="submit" class="border border-rule px-2.5 py-1 hover:border-ink">{ i18n.T(ctx, "moderation.reinstate") }</button>
} else {
<input type="checkbox" name="suspended" checked hidden/>
<input
name="reason"
maxlength="500"
placeholder={ i18n.T(ctx, "moderation.reason") }
aria-label={ i18n.T(ctx, "moderation.reason") }
class="mb-2 w-full border border-rule bg-transparent px-2 py-1 text-sm outline-none focus:border-ink"
/>
<button type="submit" class="border border-down px-2.5 py-1 text-down hover:bg-down hover:text-paper">{ i18n.T(ctx, "moderation.suspend") }</button>
}
</form>
<button
type="button"
data-delete={ api }
data-next={ next }
data-confirm={ i18n.T(ctx, "moderation.delete_confirm_"+kind, "Name", name) }
class="text-muted hover:text-down"
>{ i18n.T(ctx, "moderation.delete_"+kind) }</button>
</div>
}
templ adminLinks() {
<p class="-mt-4 mb-8 flex flex-wrap gap-x-4 gap-y-1 text-sm">
<a href="/admin/status-pages" class="underline underline-offset-2">{ i18n.T(ctx, "moderation.title") }</a>
<a href="/admin/orgs" class="underline underline-offset-2">{ i18n.T(ctx, "orgs.title") }</a>
<a href="/admin/audit" class="underline underline-offset-2">{ i18n.T(ctx, "audit.platform_title") }</a>
<a href="/admin/legal" class="underline underline-offset-2">{ i18n.T(ctx, "legal.platform_title") }</a>
</p>
}
templ AdminStatusPagesPage(p Page, me Me, v ModerationView) {
@consoleShell(p, me) {
<p class="mb-4 text-sm"><a href="/admin" class="text-muted hover:text-ink">← { i18n.T(ctx, "admin.title") }</a></p>
@moderationSection(v)
}
}
templ AdminOrgsPage(p Page, me Me, v OrgsView) {
@consoleShell(p, me) {
<p class="mb-4 text-sm"><a href="/admin" class="text-muted hover:text-ink">← { i18n.T(ctx, "admin.title") }</a></p>
@orgsSection(v)
}
}