mirror of
https://github.com/umami-software/umami.git
synced 2026-02-14 09:35:36 +01:00
Prevent admin from resetting their own role.
This commit is contained in:
parent
2e871af05b
commit
f25cd93012
10 changed files with 66 additions and 31 deletions
91
src/app/(main)/settings/users/[userId]/UserEditForm.tsx
Normal file
91
src/app/(main)/settings/users/[userId]/UserEditForm.tsx
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import {
|
||||
Dropdown,
|
||||
Item,
|
||||
Form,
|
||||
FormRow,
|
||||
FormButtons,
|
||||
FormInput,
|
||||
TextField,
|
||||
SubmitButton,
|
||||
PasswordField,
|
||||
} from 'react-basics';
|
||||
import { useApi, useLogin, useMessages } from 'components/hooks';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { useContext, useRef } from 'react';
|
||||
import { UserContext } from './UserProvider';
|
||||
|
||||
export function UserEditForm({ userId, onSave }: { userId: string; onSave?: () => void }) {
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const { post, useMutation } = useApi();
|
||||
const { mutate, error } = useMutation({
|
||||
mutationFn: ({
|
||||
username,
|
||||
password,
|
||||
role,
|
||||
}: {
|
||||
username: string;
|
||||
password: string;
|
||||
role: string;
|
||||
}) => post(`/users/${userId}`, { username, password, role }),
|
||||
});
|
||||
const ref = useRef(null);
|
||||
const user = useContext(UserContext);
|
||||
const { user: login } = useLogin();
|
||||
|
||||
const handleSubmit = async (data: any) => {
|
||||
mutate(data, {
|
||||
onSuccess: async () => {
|
||||
ref.current.reset(data);
|
||||
onSave?.();
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const renderValue = (value: string) => {
|
||||
if (value === ROLES.user) {
|
||||
return formatMessage(labels.user);
|
||||
}
|
||||
if (value === ROLES.admin) {
|
||||
return formatMessage(labels.administrator);
|
||||
}
|
||||
if (value === ROLES.viewOnly) {
|
||||
return formatMessage(labels.viewOnly);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Form ref={ref} onSubmit={handleSubmit} error={error} values={user} style={{ width: 300 }}>
|
||||
<FormRow label={formatMessage(labels.username)}>
|
||||
<FormInput name="username">
|
||||
<TextField />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
<FormRow label={formatMessage(labels.password)}>
|
||||
<FormInput
|
||||
name="password"
|
||||
rules={{
|
||||
minLength: { value: 8, message: formatMessage(messages.minPasswordLength, { n: 8 }) },
|
||||
}}
|
||||
>
|
||||
<PasswordField autoComplete="new-password" />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
{user.id !== login.id && (
|
||||
<FormRow label={formatMessage(labels.role)}>
|
||||
<FormInput name="role" rules={{ required: formatMessage(labels.required) }}>
|
||||
<Dropdown renderValue={renderValue}>
|
||||
<Item key={ROLES.viewOnly}>{formatMessage(labels.viewOnly)}</Item>
|
||||
<Item key={ROLES.user}>{formatMessage(labels.user)}</Item>
|
||||
<Item key={ROLES.admin}>{formatMessage(labels.administrator)}</Item>
|
||||
</Dropdown>
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
)}
|
||||
<FormButtons>
|
||||
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
|
||||
</FormButtons>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
|
||||
export default UserEditForm;
|
||||
|
|
@ -1,6 +1,11 @@
|
|||
'use client';
|
||||
import UserSettings from './UserSettings';
|
||||
import UserProvider from './UserProvider';
|
||||
|
||||
export default function ({ userId }: { userId: string }) {
|
||||
return <UserSettings userId={userId} />;
|
||||
return (
|
||||
<UserProvider userId={userId}>
|
||||
<UserSettings userId={userId} />
|
||||
</UserProvider>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
24
src/app/(main)/settings/users/[userId]/UserProvider.tsx
Normal file
24
src/app/(main)/settings/users/[userId]/UserProvider.tsx
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { createContext, ReactNode, useEffect } from 'react';
|
||||
import { useModified, useUser } from 'components/hooks';
|
||||
import { Loading } from 'react-basics';
|
||||
|
||||
export const UserContext = createContext(null);
|
||||
|
||||
export function UserProvider({ userId, children }: { userId: string; children: ReactNode }) {
|
||||
const { modified } = useModified(`user:${userId}`);
|
||||
const { data: user, isFetching, isLoading, refetch } = useUser(userId);
|
||||
|
||||
useEffect(() => {
|
||||
if (modified) {
|
||||
refetch();
|
||||
}
|
||||
}, [modified]);
|
||||
|
||||
if (isFetching && isLoading) {
|
||||
return <Loading position="page" />;
|
||||
}
|
||||
|
||||
return <UserContext.Provider value={user}>{children}</UserContext.Provider>;
|
||||
}
|
||||
|
||||
export default UserProvider;
|
||||
|
|
@ -1,19 +1,21 @@
|
|||
import { Key, useState } from 'react';
|
||||
import { Item, Loading, Tabs } from 'react-basics';
|
||||
import { Key, useContext, useState } from 'react';
|
||||
import { Item, Tabs, useToasts } from 'react-basics';
|
||||
import Icons from 'components/icons';
|
||||
import UserEditForm from '../UserEditForm';
|
||||
import UserEditForm from './UserEditForm';
|
||||
import PageHeader from 'components/layout/PageHeader';
|
||||
import { useMessages, useUser } from 'components/hooks';
|
||||
import { useMessages } from 'components/hooks';
|
||||
import UserWebsites from './UserWebsites';
|
||||
import { UserContext } from './UserProvider';
|
||||
|
||||
export function UserSettings({ userId }: { userId: string }) {
|
||||
const { formatMessage, labels } = useMessages();
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const [tab, setTab] = useState<Key>('details');
|
||||
const { data: user, isLoading } = useUser(userId, { gcTime: 0 });
|
||||
const user = useContext(UserContext);
|
||||
const { showToast } = useToasts();
|
||||
|
||||
if (isLoading) {
|
||||
return <Loading />;
|
||||
}
|
||||
const handleSave = () => {
|
||||
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
|
|
@ -22,7 +24,7 @@ export function UserSettings({ userId }: { userId: string }) {
|
|||
<Item key="details">{formatMessage(labels.details)}</Item>
|
||||
<Item key="websites">{formatMessage(labels.websites)}</Item>
|
||||
</Tabs>
|
||||
{tab === 'details' && <UserEditForm userId={userId} data={user} />}
|
||||
{tab === 'details' && <UserEditForm userId={userId} onSave={handleSave} />}
|
||||
{tab === 'websites' && <UserWebsites userId={userId} />}
|
||||
</>
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue