feature/houses #11
@@ -66,7 +66,10 @@ const AdminSetPasswordForm = ({ data, onSubmit }: FormProps) => {
|
|||||||
</form.AppField>
|
</form.AppField>
|
||||||
<form.AppField name="password">
|
<form.AppField name="password">
|
||||||
{(field) => (
|
{(field) => (
|
||||||
<field.TextField label={m.change_password_form_new_password()} />
|
<field.TextField
|
||||||
|
label={m.change_password_form_new_password()}
|
||||||
|
type="password"
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</form.AppField>
|
</form.AppField>
|
||||||
<Field>
|
<Field>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
|
import useHasPermission from '@/hooks/use-has-permission';
|
||||||
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
import { LockIcon } from '@phosphor-icons/react';
|
import { LockIcon } from '@phosphor-icons/react';
|
||||||
|
import { useRouteContext } from '@tanstack/react-router';
|
||||||
import { UserWithRole } from 'better-auth/plugins';
|
import { UserWithRole } from 'better-auth/plugins';
|
||||||
import { createContext, useContext, useState } from 'react';
|
import { createContext, useContext, useState } from 'react';
|
||||||
import BanUserForm from '../form/admin-ban-user-form';
|
import BanUserForm from '../form/admin-ban-user-form';
|
||||||
@@ -39,6 +41,9 @@ type BanContextProps = {
|
|||||||
const BanContext = createContext<BanContextProps | null>(null);
|
const BanContext = createContext<BanContextProps | null>(null);
|
||||||
|
|
||||||
const BanUserAction = ({ data }: ChangeUserStatusProps) => {
|
const BanUserAction = ({ data }: ChangeUserStatusProps) => {
|
||||||
|
const { session } = useRouteContext({ from: '__root__' });
|
||||||
|
const isCurrentUser = session?.user.id === data.id;
|
||||||
|
const { hasPermission, isLoading } = useHasPermission('user', 'ban');
|
||||||
const [_open, _setOpen] = useState(false);
|
const [_open, _setOpen] = useState(false);
|
||||||
const [_openConfirm, _setOpenConfirm] = useState(false);
|
const [_openConfirm, _setOpenConfirm] = useState(false);
|
||||||
const [_confirmData, _setConfirmData] = useState<SubmitValue>({
|
const [_confirmData, _setConfirmData] = useState<SubmitValue>({
|
||||||
@@ -48,6 +53,9 @@ const BanUserAction = ({ data }: ChangeUserStatusProps) => {
|
|||||||
});
|
});
|
||||||
const prevent = usePreventAutoFocus();
|
const prevent = usePreventAutoFocus();
|
||||||
|
|
||||||
|
if (isCurrentUser || isLoading) return null;
|
||||||
|
|
||||||
|
if (hasPermission) {
|
||||||
return (
|
return (
|
||||||
<BanContext
|
<BanContext
|
||||||
value={{
|
value={{
|
||||||
@@ -98,6 +106,8 @@ const BanUserAction = ({ data }: ChangeUserStatusProps) => {
|
|||||||
<BanUserConfirm data={data} />
|
<BanUserConfirm data={data} />
|
||||||
</BanContext>
|
</BanContext>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default BanUserAction;
|
export default BanUserAction;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import useHasPermission from '@/hooks/use-has-permission';
|
||||||
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
import { UserGearIcon } from '@phosphor-icons/react';
|
import { UserGearIcon } from '@phosphor-icons/react';
|
||||||
@@ -23,7 +24,11 @@ type SetRoleProps = {
|
|||||||
const ChangeRoleAction = ({ data }: SetRoleProps) => {
|
const ChangeRoleAction = ({ data }: SetRoleProps) => {
|
||||||
const [_open, _setOpen] = useState(false);
|
const [_open, _setOpen] = useState(false);
|
||||||
const prevent = usePreventAutoFocus();
|
const prevent = usePreventAutoFocus();
|
||||||
|
const { hasPermission, isLoading } = useHasPermission('user', 'set-role');
|
||||||
|
|
||||||
|
if (isLoading) return null;
|
||||||
|
|
||||||
|
if (hasPermission) {
|
||||||
return (
|
return (
|
||||||
<Dialog open={_open} onOpenChange={_setOpen}>
|
<Dialog open={_open} onOpenChange={_setOpen}>
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
@@ -62,6 +67,7 @@ const ChangeRoleAction = ({ data }: SetRoleProps) => {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ChangeRoleAction;
|
export default ChangeRoleAction;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import useHasPermission from '@/hooks/use-has-permission';
|
||||||
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
import { PenIcon } from '@phosphor-icons/react';
|
import { PenIcon } from '@phosphor-icons/react';
|
||||||
@@ -23,7 +24,11 @@ type EditUserProps = {
|
|||||||
const EditUserAction = ({ data }: EditUserProps) => {
|
const EditUserAction = ({ data }: EditUserProps) => {
|
||||||
const [_open, _setOpen] = useState(false);
|
const [_open, _setOpen] = useState(false);
|
||||||
const prevent = usePreventAutoFocus();
|
const prevent = usePreventAutoFocus();
|
||||||
|
const { hasPermission, isLoading } = useHasPermission('user', 'update');
|
||||||
|
|
||||||
|
if (isLoading) return null;
|
||||||
|
|
||||||
|
if (hasPermission) {
|
||||||
return (
|
return (
|
||||||
<Dialog open={_open} onOpenChange={_setOpen}>
|
<Dialog open={_open} onOpenChange={_setOpen}>
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
@@ -61,6 +66,9 @@ const EditUserAction = ({ data }: EditUserProps) => {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default EditUserAction;
|
export default EditUserAction;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import useHasPermission from '@/hooks/use-has-permission';
|
||||||
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
import { KeyIcon } from '@phosphor-icons/react';
|
import { KeyIcon } from '@phosphor-icons/react';
|
||||||
@@ -23,7 +24,11 @@ type UpdatePasswordProps = {
|
|||||||
const SetPasswordAction = ({ data }: UpdatePasswordProps) => {
|
const SetPasswordAction = ({ data }: UpdatePasswordProps) => {
|
||||||
const [_open, _setOpen] = useState(false);
|
const [_open, _setOpen] = useState(false);
|
||||||
const prevent = usePreventAutoFocus();
|
const prevent = usePreventAutoFocus();
|
||||||
|
const { hasPermission, isLoading } = useHasPermission('user', 'set-password');
|
||||||
|
|
||||||
|
if (isLoading) return null;
|
||||||
|
|
||||||
|
if (hasPermission) {
|
||||||
return (
|
return (
|
||||||
<Dialog open={_open} onOpenChange={_setOpen}>
|
<Dialog open={_open} onOpenChange={_setOpen}>
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
@@ -62,6 +67,7 @@ const SetPasswordAction = ({ data }: UpdatePasswordProps) => {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default SetPasswordAction;
|
export default SetPasswordAction;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import useHasPermission from '@/hooks/use-has-permission';
|
||||||
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
import usePreventAutoFocus from '@/hooks/use-prevent-auto-focus';
|
||||||
import { m } from '@/paraglide/messages';
|
import { m } from '@/paraglide/messages';
|
||||||
import { usersQueries } from '@/service/queries';
|
import { usersQueries } from '@/service/queries';
|
||||||
@@ -5,6 +6,7 @@ import { unbanUser } from '@/service/user.api';
|
|||||||
import { ReturnError } from '@/types/common';
|
import { ReturnError } from '@/types/common';
|
||||||
import { LockOpenIcon, ShieldWarningIcon } from '@phosphor-icons/react';
|
import { LockOpenIcon, ShieldWarningIcon } from '@phosphor-icons/react';
|
||||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||||
|
import { useRouteContext } from '@tanstack/react-router';
|
||||||
import { UserWithRole } from 'better-auth/plugins';
|
import { UserWithRole } from 'better-auth/plugins';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
@@ -28,6 +30,9 @@ type UnbanUserProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const UnbanUserAction = ({ data }: UnbanUserProps) => {
|
const UnbanUserAction = ({ data }: UnbanUserProps) => {
|
||||||
|
const { session } = useRouteContext({ from: '__root__' });
|
||||||
|
const isCurrentUser = session?.user.id === data.id;
|
||||||
|
const { hasPermission, isLoading } = useHasPermission('user', 'ban');
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const [_open, _setOpen] = useState(false);
|
const [_open, _setOpen] = useState(false);
|
||||||
@@ -59,6 +64,9 @@ const UnbanUserAction = ({ data }: UnbanUserProps) => {
|
|||||||
unbanMutation({ data: { id: data.id } });
|
unbanMutation({ data: { id: data.id } });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (isCurrentUser || isLoading) return null;
|
||||||
|
|
||||||
|
if (hasPermission) {
|
||||||
return (
|
return (
|
||||||
<Dialog open={_open} onOpenChange={_setOpen}>
|
<Dialog open={_open} onOpenChange={_setOpen}>
|
||||||
<Tooltip>
|
<Tooltip>
|
||||||
@@ -114,6 +122,9 @@ const UnbanUserAction = ({ data }: UnbanUserProps) => {
|
|||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default UnbanUserAction;
|
export default UnbanUserAction;
|
||||||
|
|||||||
29
src/hooks/use-has-permission.ts
Normal file
29
src/hooks/use-has-permission.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { authClient } from '@/lib/auth-client';
|
||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
function useHasPermission(resource: string, action: string) {
|
||||||
|
const [hasPermission, setHasPermission] = useState(false);
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const checkPermission = async () => {
|
||||||
|
try {
|
||||||
|
const access = await authClient.admin.hasPermission({
|
||||||
|
permissions: {
|
||||||
|
[resource]: [action],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
setHasPermission(access.data?.success ?? false);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Permission check failed:', error);
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
checkPermission();
|
||||||
|
}, [resource, action]);
|
||||||
|
|
||||||
|
return { hasPermission, isLoading };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useHasPermission;
|
||||||
Reference in New Issue
Block a user