mirror of
https://github.com/tale/headplane.git
synced 2026-08-29 08:27:08 +00:00
chore: general cleanup/pr improvements
This commit is contained in:
@@ -17,6 +17,20 @@ export async function authKeysAction({ request, context }: Route.ActionArgs) {
|
||||
});
|
||||
}
|
||||
|
||||
async function checkSelfServiceOwnership(userId: string) {
|
||||
if (canGenerateAny || !canGenerateOwn) return;
|
||||
const [targetUser] = await api.getUsers(userId);
|
||||
if (!targetUser) {
|
||||
throw data("User not found.", { status: 404 });
|
||||
}
|
||||
const targetSubject = targetUser.providerId?.split("/").pop();
|
||||
if (targetSubject !== session.user.subject) {
|
||||
throw data("You do not have permission to manage this user's pre-auth keys", {
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const formData = await request.formData();
|
||||
const action = formData.get("action_id")?.toString();
|
||||
if (!action) {
|
||||
@@ -40,18 +54,8 @@ export async function authKeysAction({ request, context }: Route.ActionArgs) {
|
||||
});
|
||||
}
|
||||
|
||||
// Only allow self-service users to create keys for themselves
|
||||
if (!canGenerateAny && canGenerateOwn && user) {
|
||||
const [targetUser] = await api.getUsers(user);
|
||||
if (!targetUser) {
|
||||
return data("User not found.", { status: 404 });
|
||||
}
|
||||
const targetSubject = targetUser.providerId?.split("/").pop();
|
||||
if (targetSubject !== session.user.subject) {
|
||||
throw data("You can only create pre-auth keys for your own user", {
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
if (user) {
|
||||
await checkSelfServiceOwnership(user);
|
||||
}
|
||||
|
||||
const expiry = formData.get("expiry")?.toString();
|
||||
@@ -104,19 +108,7 @@ export async function authKeysAction({ request, context }: Route.ActionArgs) {
|
||||
});
|
||||
}
|
||||
|
||||
// Only allow self-service users to expire their own keys
|
||||
if (!canGenerateAny && canGenerateOwn) {
|
||||
const [targetUser] = await api.getUsers(user);
|
||||
if (!targetUser) {
|
||||
return data("User not found.", { status: 404 });
|
||||
}
|
||||
const targetSubject = targetUser.providerId?.split("/").pop();
|
||||
if (targetSubject !== session.user.subject) {
|
||||
throw data("You can only expire pre-auth keys for your own user", {
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
}
|
||||
await checkSelfServiceOwnership(user);
|
||||
|
||||
await api.expirePreAuthKey(user, key);
|
||||
return data("Pre-auth key expired");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { PreAuthKey, User } from "~/types";
|
||||
|
||||
import Attribute from "~/components/Attribute";
|
||||
import type { PreAuthKey, User } from "~/types";
|
||||
import { getUserDisplayName } from "~/utils/user";
|
||||
|
||||
import ExpireAuthKey from "./dialogs/expire-auth-key";
|
||||
|
||||
@@ -14,7 +14,7 @@ export default function AuthKeyRow({ authKey, user }: Props) {
|
||||
const expiration = new Date(authKey.expiration).toLocaleString();
|
||||
const isExpired =
|
||||
(authKey.used && !authKey.reusable) || new Date(authKey.expiration) < new Date();
|
||||
const userDisplay = user ? user.name || user.displayName || user.email || user.id : "(Tag Only)";
|
||||
const userDisplay = user ? getUserDisplayName(user) : "(Tag Only)";
|
||||
|
||||
return (
|
||||
<div className="w-full">
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { Key, useEffect, useRef, useState } from "react";
|
||||
import { useFetcher } from "react-router";
|
||||
|
||||
import type { User } from "~/types";
|
||||
|
||||
import Button from "~/components/Button";
|
||||
import Code from "~/components/Code";
|
||||
import Dialog from "~/components/Dialog";
|
||||
@@ -11,7 +9,9 @@ import Link from "~/components/Link";
|
||||
import NumberInput from "~/components/NumberInput";
|
||||
import Select from "~/components/Select";
|
||||
import Switch from "~/components/Switch";
|
||||
import type { User } from "~/types";
|
||||
import toast from "~/utils/toast";
|
||||
import { getUserDisplayName } from "~/utils/user";
|
||||
|
||||
interface AddAuthKeyProps {
|
||||
users: User[];
|
||||
@@ -152,9 +152,7 @@ export default function AddAuthKey({
|
||||
selectedKey={userId}
|
||||
>
|
||||
{availableUsers.map((user) => (
|
||||
<Select.Item key={user.id}>
|
||||
{user.name || user.displayName || user.email || user.id}
|
||||
</Select.Item>
|
||||
<Select.Item key={user.id}>{getUserDisplayName(user)}</Select.Item>
|
||||
))}
|
||||
</Select>
|
||||
)}
|
||||
|
||||
@@ -2,20 +2,18 @@ import { FileKey2 } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
import { Link as RemixLink } from "react-router";
|
||||
|
||||
import type { PreAuthKey } from "~/types";
|
||||
import type { User } from "~/types/User";
|
||||
|
||||
import Code from "~/components/Code";
|
||||
import Link from "~/components/Link";
|
||||
import Notice from "~/components/Notice";
|
||||
import Select from "~/components/Select";
|
||||
import TableList from "~/components/TableList";
|
||||
import { Capabilities } from "~/server/web/roles";
|
||||
import type { PreAuthKey } from "~/types";
|
||||
import type { User } from "~/types/User";
|
||||
import log from "~/utils/log";
|
||||
import { filterUsersWithValidIds, getUserDisplayName } from "~/utils/user";
|
||||
import { getUserDisplayName } from "~/utils/user";
|
||||
|
||||
import type { Route } from "./+types/overview";
|
||||
|
||||
import { authKeysAction } from "./actions";
|
||||
import AuthKeyRow from "./auth-key-row";
|
||||
import AddAuthKey from "./dialogs/add-auth-key";
|
||||
@@ -63,15 +61,17 @@ export async function loader({ request, context }: Route.LoaderArgs) {
|
||||
| { success: false; user: User; error: unknown; preAuthKeys: [] };
|
||||
|
||||
const results: FetchResult[] = await Promise.all(
|
||||
filterUsersWithValidIds(users).map(async (user) => {
|
||||
try {
|
||||
const preAuthKeys = await api.getPreAuthKeys(user.id);
|
||||
return { success: true as const, user, preAuthKeys };
|
||||
} catch (error) {
|
||||
log.error("api", "GET /v1/preauthkey for %s: %o", user.name, error);
|
||||
return { success: false as const, user, error, preAuthKeys: [] as const };
|
||||
}
|
||||
}),
|
||||
users
|
||||
.filter((u) => u.id?.length > 0)
|
||||
.map(async (user) => {
|
||||
try {
|
||||
const preAuthKeys = await api.getPreAuthKeys(user.id);
|
||||
return { success: true as const, user, preAuthKeys };
|
||||
} catch (error) {
|
||||
log.error("api", "GET /v1/preauthkey for %s: %o", user.name, error);
|
||||
return { success: false as const, user, error, preAuthKeys: [] as const };
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
keys = results
|
||||
|
||||
Reference in New Issue
Block a user