Let non-admin accounts be granted single services, starting with /receipt/
Access was admin or nothing, so letting a friend print receipts meant making them a full admin. Users now carry a list of permissions (User.Permissions, a JSON column), each a key from the registry in services/permissions.go; admins pass every check. The only key so far is `receipt`. GET /auth/validate/:permission answers like /auth/validate-admin (200/403/401, with the same refresh-token fallback, now shared) plus 404 for an unknown key, and reads the user row rather than the token, so a grant or revoke applies on the next request. On 200 it returns the username in X-Auth-User. nginx gets a permission gate beside the admin gate: a location sets $auth_permission and auth_requests /internal/auth/validate. /receipt/ moves onto it, forwarding X-Remote-User to the receipt server. 403 now goes to /no-access rather than back to the login page. The variable must not be given a server-level default: the subrequest re-runs the server rewrite phase and would blank it. The admin panel's Manage Users tab ticks permissions per user (setUserPermissions, permissionTypes). The login page is no longer admin-only in wording or destination: a non-admin lands on /account, which lists the services they can open; /no-access is the same view with a notice. /auth/validate-admin and every other admin-gated service are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Route `/admin/login`. On success redirects to `?redirect=` or to /admin.
|
||||
* Route `/admin/login`, the one login page for everyone: admins and accounts
|
||||
* granted a gated service (the receipt printer, say). On success redirects to
|
||||
* `?redirect=`, or else to /admin for an admin and /account for anyone else
|
||||
* (a non-admin would only bounce off /admin's guard back to here).
|
||||
*
|
||||
* The `?redirect=` is set by two different gates: the SPA router guard
|
||||
* (`meta.requiresAdmin`) and nginx's `auth_request` gate in front of the
|
||||
* proxied containers (/notes/, /sb/). The latter means the
|
||||
* proxied containers (/notes/, /sb/, /receipt/). The latter means the
|
||||
* destination is frequently NOT a Vue route, so see goTo() below.
|
||||
*
|
||||
* auth.logIn() never throws — it swallows the error — so success is detected by
|
||||
@@ -31,8 +34,8 @@ const errorMsg = ref("");
|
||||
|
||||
// Send the browser to `dest`, choosing SPA navigation vs. a real page load.
|
||||
//
|
||||
// nginx bounces /notes/ and /sb/ here when the admin cookie is
|
||||
// missing; those paths belong to other containers, so router.push() would just
|
||||
// nginx bounces /notes/, /sb/, /receipt/ and the like here when the session
|
||||
// is missing; those paths belong to other containers, so router.push() would just
|
||||
// match the catch-all 404 route and never leave the SPA. Resolving the path
|
||||
// first tells us which it is: the catch-all matches everything, so landing on
|
||||
// the route named "404" means no real Vue route exists and the browser needs a
|
||||
@@ -42,7 +45,7 @@ const errorMsg = ref("");
|
||||
// site-relative path — a leading "//" or a scheme would be an open redirect.
|
||||
function goTo(dest) {
|
||||
if (typeof dest !== "string" || !dest.startsWith("/") || dest.startsWith("//")) {
|
||||
router.push("/admin");
|
||||
router.push(home());
|
||||
return;
|
||||
}
|
||||
if (router.resolve(dest).name !== "404") {
|
||||
@@ -52,6 +55,11 @@ function goTo(dest) {
|
||||
}
|
||||
}
|
||||
|
||||
// Where a signed-in user lands when nothing asked for anywhere in particular.
|
||||
function home() {
|
||||
return auth.user.admin ? "/admin" : "/account";
|
||||
}
|
||||
|
||||
async function handleLogin() {
|
||||
if (submitting.value) return;
|
||||
submitting.value = true;
|
||||
@@ -61,7 +69,7 @@ async function handleLogin() {
|
||||
// `loggedIn` rather than by catching.
|
||||
await auth.logIn(username.value, password.value);
|
||||
if (auth.loggedIn) {
|
||||
goTo(route.query.redirect || "/admin");
|
||||
goTo(route.query.redirect || home());
|
||||
} else {
|
||||
errorMsg.value = "Invalid username or password";
|
||||
}
|
||||
@@ -81,7 +89,9 @@ function handleLogout() {
|
||||
<h1>Already signed in</h1>
|
||||
<p>You're logged in as <code>{{ auth.user.username }}</code>.</p>
|
||||
<div class="login-actions">
|
||||
<Button @click="router.push('/admin')">Open Admin</Button>
|
||||
<Button @click="router.push(home())">
|
||||
{{ auth.user.admin ? "Open Admin" : "Your Services" }}
|
||||
</Button>
|
||||
<Button @click="handleLogout">Log Out</Button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -91,7 +101,7 @@ function handleLogout() {
|
||||
<form v-else class="login-card bdr-1" @submit.prevent="handleLogin">
|
||||
<h1>Login</h1>
|
||||
<small class="login-hint">
|
||||
Admin access is required beyond this point.
|
||||
Sign in to continue.
|
||||
</small>
|
||||
|
||||
<label class="login-field">
|
||||
|
||||
Reference in New Issue
Block a user