2020-10-31 21:29:41 +00:00
|
|
|
import React, { useContext, useEffect, useRef } from 'react';
|
2020-03-26 04:58:37 +00:00
|
|
|
import { Subuser } from '@/state/server/subusers';
|
2020-10-31 21:29:41 +00:00
|
|
|
import { Form, Formik } from 'formik';
|
2020-03-26 04:58:37 +00:00
|
|
|
import { array, object, string } from 'yup';
|
|
|
|
import Field from '@/components/elements/Field';
|
2020-03-27 21:23:13 +00:00
|
|
|
import { Actions, useStoreActions, useStoreState } from 'easy-peasy';
|
2020-03-26 04:58:37 +00:00
|
|
|
import { ApplicationStore } from '@/state';
|
2020-03-27 21:23:13 +00:00
|
|
|
import createOrUpdateSubuser from '@/api/server/users/createOrUpdateSubuser';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-03-29 21:19:17 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
2020-08-26 04:25:31 +00:00
|
|
|
import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo';
|
2020-07-03 21:19:05 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-07-04 23:26:07 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
2020-10-31 21:29:41 +00:00
|
|
|
import PermissionTitleBox from '@/components/server/users/PermissionTitleBox';
|
|
|
|
import asModal from '@/hoc/asModal';
|
|
|
|
import PermissionRow from '@/components/server/users/PermissionRow';
|
|
|
|
import ModalContext from '@/context/ModalContext';
|
2020-03-26 04:58:37 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
subuser?: Subuser;
|
2020-10-31 21:29:41 +00:00
|
|
|
};
|
2020-03-26 04:58:37 +00:00
|
|
|
|
|
|
|
interface Values {
|
|
|
|
email: string;
|
|
|
|
permissions: string[];
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:29:41 +00:00
|
|
|
const EditSubuserModal = ({ subuser }: Props) => {
|
|
|
|
const ref = useRef<HTMLHeadingElement>(null);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const appendSubuser = ServerContext.useStoreActions(actions => actions.subusers.appendSubuser);
|
|
|
|
const { clearFlashes, clearAndAddHttpError } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2021-05-16 19:35:49 +00:00
|
|
|
const { dismiss, setPropOverrides } = useContext(ModalContext);
|
2020-09-23 04:41:35 +00:00
|
|
|
|
2020-10-31 21:29:41 +00:00
|
|
|
const isRootAdmin = useStoreState(state => state.user.data!.rootAdmin);
|
2020-04-19 18:58:26 +00:00
|
|
|
const permissions = useStoreState(state => state.permissions.data);
|
|
|
|
// The currently logged in user's permissions. We're going to filter out any permissions
|
|
|
|
// that they should not need.
|
|
|
|
const loggedInPermissions = ServerContext.useStoreState(state => state.server.permissions);
|
2020-10-31 21:29:41 +00:00
|
|
|
const [ canEditUser ] = usePermissions(subuser ? [ 'user.update' ] : [ 'user.create' ]);
|
2020-04-19 18:58:26 +00:00
|
|
|
|
|
|
|
// The permissions that can be modified by this user.
|
2020-08-26 04:25:31 +00:00
|
|
|
const editablePermissions = useDeepCompareMemo(() => {
|
2020-04-19 18:58:26 +00:00
|
|
|
const cleaned = Object.keys(permissions)
|
|
|
|
.map(key => Object.keys(permissions[key].keys).map(pkey => `${key}.${pkey}`));
|
|
|
|
|
|
|
|
const list: string[] = ([] as string[]).concat.apply([], Object.values(cleaned));
|
|
|
|
|
2020-10-31 21:29:41 +00:00
|
|
|
if (isRootAdmin || (loggedInPermissions.length === 1 && loggedInPermissions[0] === '*')) {
|
2020-04-19 18:58:26 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.filter(key => loggedInPermissions.indexOf(key) >= 0);
|
2020-10-31 21:29:41 +00:00
|
|
|
}, [ isRootAdmin, permissions, loggedInPermissions ]);
|
2020-03-26 04:58:37 +00:00
|
|
|
|
2020-10-31 21:29:41 +00:00
|
|
|
const submit = (values: Values) => {
|
2021-05-16 19:35:49 +00:00
|
|
|
setPropOverrides({ showSpinnerOverlay: true });
|
2020-03-27 21:23:13 +00:00
|
|
|
clearFlashes('user:edit');
|
2020-10-31 21:29:41 +00:00
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
createOrUpdateSubuser(uuid, values, subuser)
|
|
|
|
.then(subuser => {
|
|
|
|
appendSubuser(subuser);
|
2020-10-31 21:29:41 +00:00
|
|
|
dismiss();
|
2020-03-27 21:23:13 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2021-05-16 19:35:49 +00:00
|
|
|
setPropOverrides(null);
|
2020-09-23 04:41:35 +00:00
|
|
|
clearAndAddHttpError({ key: 'user:edit', error });
|
2020-03-26 04:58:37 +00:00
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
if (ref.current) {
|
|
|
|
ref.current.scrollIntoView();
|
|
|
|
}
|
|
|
|
});
|
2020-03-26 04:58:37 +00:00
|
|
|
};
|
|
|
|
|
2020-10-31 21:29:41 +00:00
|
|
|
useEffect(() => () => {
|
|
|
|
clearFlashes('user:edit');
|
2020-04-19 18:58:26 +00:00
|
|
|
}, []);
|
|
|
|
|
2020-03-26 04:58:37 +00:00
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{
|
2020-03-27 21:23:13 +00:00
|
|
|
email: subuser?.email || '',
|
|
|
|
permissions: subuser?.permissions || [],
|
2020-03-26 04:58:37 +00:00
|
|
|
} as Values}
|
|
|
|
validationSchema={object().shape({
|
2020-09-26 23:29:26 +00:00
|
|
|
email: string()
|
|
|
|
.max(191, 'Email addresses must not exceed 191 characters.')
|
|
|
|
.email('A valid email address must be provided.')
|
|
|
|
.required('A valid email address must be provided.'),
|
2020-03-26 04:58:37 +00:00
|
|
|
permissions: array().of(string()),
|
|
|
|
})}
|
|
|
|
>
|
2020-03-27 21:23:13 +00:00
|
|
|
<Form>
|
2020-10-31 21:29:41 +00:00
|
|
|
<div css={tw`flex justify-between`}>
|
|
|
|
<h2 css={tw`text-2xl`} ref={ref}>
|
|
|
|
{subuser ? `${canEditUser ? 'Modify' : 'View'} permissions for ${subuser.email}` : 'Create new subuser'}
|
|
|
|
</h2>
|
2021-06-05 15:38:24 +00:00
|
|
|
<div>
|
2020-10-31 21:29:41 +00:00
|
|
|
<Button type={'submit'} css={tw`w-full sm:w-auto`}>
|
|
|
|
{subuser ? 'Save' : 'Invite User'}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<FlashMessageRender byKey={'user:edit'} css={tw`mt-4`} />
|
|
|
|
{(!isRootAdmin && loggedInPermissions[0] !== '*') &&
|
|
|
|
<div css={tw`mt-4 pl-4 py-2 border-l-4 border-cyan-400`}>
|
|
|
|
<p css={tw`text-sm text-neutral-300`}>
|
|
|
|
Only permissions which your account is currently assigned may be selected when creating or
|
|
|
|
modifying other users.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{!subuser &&
|
|
|
|
<div css={tw`mt-6`}>
|
|
|
|
<Field
|
|
|
|
name={'email'}
|
|
|
|
label={'User Email'}
|
|
|
|
description={'Enter the email address of the user you wish to invite as a subuser for this server.'}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
<div css={tw`my-6`}>
|
|
|
|
{Object.keys(permissions).filter(key => key !== 'websocket').map((key, index) => (
|
|
|
|
<PermissionTitleBox
|
|
|
|
key={`permission_${key}`}
|
|
|
|
title={key}
|
|
|
|
isEditable={canEditUser}
|
|
|
|
permissions={Object.keys(permissions[key].keys).map(pkey => `${key}.${pkey}`)}
|
|
|
|
css={index > 0 ? tw`mt-4` : undefined}
|
|
|
|
>
|
|
|
|
<p css={tw`text-sm text-neutral-400 mb-4`}>
|
|
|
|
{permissions[key].description}
|
|
|
|
</p>
|
|
|
|
{Object.keys(permissions[key].keys).map(pkey => (
|
|
|
|
<PermissionRow
|
|
|
|
key={`permission_${key}.${pkey}`}
|
|
|
|
permission={`${key}.${pkey}`}
|
|
|
|
disabled={!canEditUser || editablePermissions.indexOf(`${key}.${pkey}`) < 0}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</PermissionTitleBox>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
<Can action={subuser ? 'user.update' : 'user.create'}>
|
|
|
|
<div css={tw`pb-6 flex justify-end`}>
|
|
|
|
<Button type={'submit'} css={tw`w-full sm:w-auto`}>
|
|
|
|
{subuser ? 'Save' : 'Invite User'}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Can>
|
2020-03-27 21:23:13 +00:00
|
|
|
</Form>
|
2020-03-26 04:58:37 +00:00
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|
2020-10-31 21:29:41 +00:00
|
|
|
|
|
|
|
export default asModal<Props>({
|
|
|
|
top: false,
|
|
|
|
})(EditSubuserModal);
|