fix: address coderabbit review on #6214
- Remove invalid null return from session callback (must return session object) - Prevent admin from disabling their own account (guard with isSelf check) - Hide disable UI when viewing own profile - Add user existence check to changeDisabled endpoint - Add OpenAPI metadata to changeDisabled mutation
This commit is contained in:
1 parent
fbac0c8752
commit
39a1a3695e
4 files changed
+35
-12
No files matched your search
+6
-2
@@ -10,9 +10,10 @@ import { useI18n } from "@homarr/translation/client";
|
|||||||
|
|
||||||
interface DisableUserButtonProps {
|
interface DisableUserButtonProps {
|
||||||
user: RouterOutputs["user"]["getById"];
|
user: RouterOutputs["user"]["getById"];
|
||||||
|
isSelf?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DisableUserButton = ({ user }: DisableUserButtonProps) => {
|
export const DisableUserButton = ({ user, isSelf = false }: DisableUserButtonProps) => {
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
const [isPending, startTransition] = useTransition();
|
const [isPending, startTransition] = useTransition();
|
||||||
const changeDisabledMutation = clientApi.user.changeDisabled.useMutation();
|
const changeDisabledMutation = clientApi.user.changeDisabled.useMutation();
|
||||||
@@ -32,6 +33,8 @@ export const DisableUserButton = ({ user }: DisableUserButtonProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleToggle = () => {
|
const handleToggle = () => {
|
||||||
|
if (isSelf) return;
|
||||||
|
|
||||||
startTransition(async () => {
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
await changeDisabledMutation.mutateAsync({
|
await changeDisabledMutation.mutateAsync({
|
||||||
@@ -65,12 +68,13 @@ export const DisableUserButton = ({ user }: DisableUserButtonProps) => {
|
|||||||
const getButtonColor = () => (isDisabled ? "green" : "yellow");
|
const getButtonColor = () => (isDisabled ? "green" : "yellow");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip label={getTooltipLabel()} position="top">
|
<Tooltip label={isSelf ? t("user.action.disableSelf.disabled", { defaultValue: "Cannot disable your own account" }) : getTooltipLabel()} position="top">
|
||||||
<Button
|
<Button
|
||||||
onClick={handleToggle}
|
onClick={handleToggle}
|
||||||
loading={isPending || changeDisabledMutation.isPending}
|
loading={isPending || changeDisabledMutation.isPending}
|
||||||
color={getButtonColor()}
|
color={getButtonColor()}
|
||||||
variant="light"
|
variant="light"
|
||||||
|
disabled={isSelf}
|
||||||
>
|
>
|
||||||
{getButtonLabel()}
|
{getButtonLabel()}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -110,11 +110,13 @@ export default async function EditUserPage(props: Props) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<DangerZoneRoot>
|
<DangerZoneRoot>
|
||||||
<DangerZoneItem
|
{!isSelf && (
|
||||||
label={getDisableItemLabel()}
|
<DangerZoneItem
|
||||||
description={getDisableItemDescription()}
|
label={getDisableItemLabel()}
|
||||||
action={<DisableUserButton user={user} />}
|
description={getDisableItemDescription()}
|
||||||
/>
|
action={<DisableUserButton user={user} isSelf={isSelf} />}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<DangerZoneItem
|
<DangerZoneItem
|
||||||
label={t("user.action.delete.label")}
|
label={t("user.action.delete.label")}
|
||||||
description={t("user.action.delete.description")}
|
description={t("user.action.delete.description")}
|
||||||
|
|||||||
@@ -790,7 +790,29 @@ export const userRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.output(z.void())
|
.output(z.void())
|
||||||
|
.meta({
|
||||||
|
openapi: {
|
||||||
|
method: "PATCH",
|
||||||
|
path: "/api/users/disabled",
|
||||||
|
tags: ["users"],
|
||||||
|
protect: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
|
const dbUser = await ctx.db.query.users.findFirst({
|
||||||
|
columns: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
|
where: eq(users.id, input.userId),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!dbUser) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
message: "User not found",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await ctx.db
|
await ctx.db
|
||||||
.update(users)
|
.update(users)
|
||||||
.set({
|
.set({
|
||||||
|
|||||||
@@ -47,14 +47,9 @@ export const createSessionCallback = (db: Database): NextAuthCallbackOf<"session
|
|||||||
where: eq(users.id, user.id),
|
where: eq(users.id, user.id),
|
||||||
columns: {
|
columns: {
|
||||||
colorScheme: true,
|
colorScheme: true,
|
||||||
disabled: true,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (additionalProperties?.disabled) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...session,
|
...session,
|
||||||
user: {
|
user: {
|
||||||
|
|||||||
Reference in new issue
Block a user