mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Prevent admin from resetting their own role.
This commit is contained in:
parent
2e871af05b
commit
f25cd93012
@ -7,6 +7,7 @@
|
||||
background: var(--base75);
|
||||
border-bottom: 1px solid var(--base300);
|
||||
padding: 0 20px;
|
||||
z-index: 200;
|
||||
}
|
||||
|
||||
.logo {
|
||||
|
@ -1,4 +1,3 @@
|
||||
'use client';
|
||||
import { useEffect, useCallback, useState } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { Button } from 'react-basics';
|
||||
|
@ -8,13 +8,13 @@ import {
|
||||
TextField,
|
||||
SubmitButton,
|
||||
PasswordField,
|
||||
useToasts,
|
||||
} from 'react-basics';
|
||||
import { useApi, useMessages } from 'components/hooks';
|
||||
import { useApi, useLogin, useMessages } from 'components/hooks';
|
||||
import { ROLES } from 'lib/constants';
|
||||
import { useRef } from 'react';
|
||||
import { useContext, useRef } from 'react';
|
||||
import { UserContext } from './UserProvider';
|
||||
|
||||
export function UserEditForm({ userId, data }: { userId: string; data: object }) {
|
||||
export function UserEditForm({ userId, onSave }: { userId: string; onSave?: () => void }) {
|
||||
const { formatMessage, labels, messages } = useMessages();
|
||||
const { post, useMutation } = useApi();
|
||||
const { mutate, error } = useMutation({
|
||||
@ -29,13 +29,14 @@ export function UserEditForm({ userId, data }: { userId: string; data: object })
|
||||
}) => post(`/users/${userId}`, { username, password, role }),
|
||||
});
|
||||
const ref = useRef(null);
|
||||
const { showToast } = useToasts();
|
||||
const user = useContext(UserContext);
|
||||
const { user: login } = useLogin();
|
||||
|
||||
const handleSubmit = async (data: any) => {
|
||||
mutate(data, {
|
||||
onSuccess: async () => {
|
||||
showToast({ message: formatMessage(messages.saved), variant: 'success' });
|
||||
ref.current.reset(data);
|
||||
onSave?.();
|
||||
},
|
||||
});
|
||||
};
|
||||
@ -53,7 +54,7 @@ export function UserEditForm({ userId, data }: { userId: string; data: object })
|
||||
};
|
||||
|
||||
return (
|
||||
<Form ref={ref} onSubmit={handleSubmit} error={error} values={data} style={{ width: 300 }}>
|
||||
<Form ref={ref} onSubmit={handleSubmit} error={error} values={user} style={{ width: 300 }}>
|
||||
<FormRow label={formatMessage(labels.username)}>
|
||||
<FormInput name="username">
|
||||
<TextField />
|
||||
@ -69,15 +70,17 @@ export function UserEditForm({ userId, data }: { userId: string; data: object })
|
||||
<PasswordField autoComplete="new-password" />
|
||||
</FormInput>
|
||||
</FormRow>
|
||||
<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>
|
||||
{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>
|
@ -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} />}
|
||||
</>
|
||||
);
|
||||
|
@ -11,6 +11,6 @@
|
||||
top: 0;
|
||||
background: var(--base50);
|
||||
border-bottom: 1px solid var(--base300);
|
||||
z-index: var(--z-index-overlay);
|
||||
z-index: 1;
|
||||
padding: 10px 0;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import WebsiteReportsPage from './WebsiteReportsPage';
|
||||
|
||||
export default function WebsiteReportsPage({ params: { websiteId } }) {
|
||||
export default function ({ params: { websiteId } }) {
|
||||
return <WebsiteReportsPage websiteId={websiteId} />;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
.menu {
|
||||
background: var(--base50);
|
||||
min-width: 260px;
|
||||
}
|
||||
|
||||
.heading {
|
||||
|
@ -29,7 +29,7 @@ export function TeamsButton({ teamId }: { teamId: string }) {
|
||||
</Button>
|
||||
<Popup alignment="end">
|
||||
{(close: () => void) => (
|
||||
<Menu variant="popup" onSelect={handleSelect.bind(null, close)}>
|
||||
<Menu className={styles.menu} variant="popup" onSelect={handleSelect.bind(null, close)}>
|
||||
<div className={styles.heading}>{formatMessage(labels.myAccount)}</div>
|
||||
<Item key={user.id} className={classNames({ [styles.selected]: !teamId })}>
|
||||
<Flexbox gap={10} alignItems="center">
|
||||
|
Loading…
Reference in New Issue
Block a user