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:
+41
-5
@@ -23,6 +23,11 @@ import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
import { gql } from "@/graphql";
|
||||
|
||||
// The user fields every query here selects, so `user` has the same shape
|
||||
// whichever one filled it. `permissions` is the list of gated services
|
||||
// (/receipt/ and so on) a non-admin may open; see views/Account.vue.
|
||||
export const USER_FIELDS = "id username admin permissions";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
const user = ref({});
|
||||
const loggedIn = computed(() => !!user.value.username);
|
||||
@@ -71,7 +76,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
if (resolved.value) return Promise.resolve();
|
||||
if (!pending) {
|
||||
const requestedAt = epoch;
|
||||
pending = gql(`query Me { me { id username admin } }`)
|
||||
pending = gql(`query Me { me { ${USER_FIELDS} } }`)
|
||||
.then((data) => adoptSession(data.me, requestedAt))
|
||||
.catch((err) => console.error("me query failed:", err))
|
||||
.finally(() => {
|
||||
@@ -98,7 +103,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
async function logIn(username, password) {
|
||||
try {
|
||||
const data = await gql(
|
||||
`mutation Login($input: LoginInput!) { login(input: $input) { user { id username admin } } }`,
|
||||
`mutation Login($input: LoginInput!) { login(input: $input) { user { ${USER_FIELDS} } } }`,
|
||||
{ input: { username, password } },
|
||||
);
|
||||
user.value = data.login.user;
|
||||
@@ -113,7 +118,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
async function createUser(username, password) {
|
||||
try {
|
||||
const data = await gql(
|
||||
`mutation CreateUser($input: CreateUserInput!) { createUser(input: $input) { id username admin } }`,
|
||||
`mutation CreateUser($input: CreateUserInput!) { createUser(input: $input) { ${USER_FIELDS} } }`,
|
||||
{ input: { username, password } },
|
||||
);
|
||||
return data.createUser;
|
||||
@@ -128,7 +133,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
async function refreshToken() {
|
||||
try {
|
||||
const data = await gql(
|
||||
`mutation { refreshToken { user { id username admin } } }`,
|
||||
`mutation { refreshToken { user { ${USER_FIELDS} } } }`,
|
||||
);
|
||||
user.value = data.refreshToken.user;
|
||||
markSessionChanged();
|
||||
@@ -141,7 +146,7 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
async function setUserAdmin(userId, admin) {
|
||||
try {
|
||||
const data = await gql(
|
||||
`mutation SetUserAdmin($id: ID!, $admin: Boolean!) { setUserAdmin(id: $id, admin: $admin) { id username admin } }`,
|
||||
`mutation SetUserAdmin($id: ID!, $admin: Boolean!) { setUserAdmin(id: $id, admin: $admin) { ${USER_FIELDS} } }`,
|
||||
{ id: userId, admin },
|
||||
);
|
||||
return data.setUserAdmin;
|
||||
@@ -151,6 +156,34 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
}
|
||||
}
|
||||
|
||||
/** Admin-only: replace a user's permission list. Returns the updated user;
|
||||
* re-throws. */
|
||||
async function setUserPermissions(userId, permissions) {
|
||||
try {
|
||||
const data = await gql(
|
||||
`mutation SetUserPermissions($id: ID!, $permissions: [String!]!) { setUserPermissions(id: $id, permissions: $permissions) { ${USER_FIELDS} } }`,
|
||||
{ id: userId, permissions },
|
||||
);
|
||||
return data.setUserPermissions;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** Signed-in only: every grantable service, as [{ key, label, path }].
|
||||
* Re-throws. */
|
||||
async function fetchPermissionTypes() {
|
||||
const data = await gql(`query { permissionTypes { key label path } }`);
|
||||
return data.permissionTypes;
|
||||
}
|
||||
|
||||
/** Whether the signed-in user may open the service gated on `key`. Admins
|
||||
* may open them all, as on the server. */
|
||||
function can(key) {
|
||||
return !!user.value.admin || (user.value.permissions ?? []).includes(key);
|
||||
}
|
||||
|
||||
return {
|
||||
user,
|
||||
|
||||
@@ -165,5 +198,8 @@ export const useAuthStore = defineStore("auth", () => {
|
||||
logOut,
|
||||
createUser,
|
||||
setUserAdmin,
|
||||
setUserPermissions,
|
||||
fetchPermissionTypes,
|
||||
can,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -25,7 +25,7 @@ import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { gql } from "@/graphql";
|
||||
import axios from "axios";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useAuthStore, USER_FIELDS } from "@/stores/auth";
|
||||
|
||||
export const useHomeDataStore = defineStore("homeData", () => {
|
||||
const loaded = ref(false);
|
||||
@@ -66,7 +66,7 @@ export const useHomeDataStore = defineStore("homeData", () => {
|
||||
bookmarks { id category name link }
|
||||
giteaFeed { avatarUrl repoUrl repoName opType commitMessage createdAt }
|
||||
steamStatus { online recentGames { appId name playtime2Weeks playtimeForever headerImageUrl } }
|
||||
me { id username admin }
|
||||
me { ${USER_FIELDS} }
|
||||
}
|
||||
`),
|
||||
fetchRadioStatus(),
|
||||
|
||||
Reference in New Issue
Block a user