2020-04-19 18:58:26 +00:00
|
|
|
import React, { forwardRef, useEffect, useRef } from 'react';
|
2020-03-26 04:58:37 +00:00
|
|
|
import { Subuser } from '@/state/server/subusers';
|
2020-03-27 21:23:13 +00:00
|
|
|
import { Form, Formik, FormikHelpers, useFormikContext } from 'formik';
|
2020-03-26 04:58:37 +00:00
|
|
|
import { array, object, string } from 'yup';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
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';
|
|
|
|
import TitledGreyBox from '@/components/elements/TitledGreyBox';
|
|
|
|
import Checkbox from '@/components/elements/Checkbox';
|
2020-07-03 21:19:05 +00:00
|
|
|
import styled from 'styled-components/macro';
|
2020-03-27 21:23:13 +00:00
|
|
|
import createOrUpdateSubuser from '@/api/server/users/createOrUpdateSubuser';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
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';
|
|
|
|
import Label from '@/components/elements/Label';
|
|
|
|
import Input from '@/components/elements/Input';
|
2020-03-26 04:58:37 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
subuser?: Subuser;
|
|
|
|
} & RequiredModalProps;
|
|
|
|
|
|
|
|
interface Values {
|
|
|
|
email: string;
|
|
|
|
permissions: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const PermissionLabel = styled.label`
|
2020-03-29 21:19:17 +00:00
|
|
|
${tw`flex items-center border border-transparent rounded p-2`};
|
2020-03-26 04:58:37 +00:00
|
|
|
text-transform: none;
|
|
|
|
|
2020-03-29 21:19:17 +00:00
|
|
|
&:not(.disabled) {
|
|
|
|
${tw`cursor-pointer`};
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
${tw`border-neutral-500 bg-neutral-800`};
|
|
|
|
}
|
2020-03-26 04:58:37 +00:00
|
|
|
}
|
2020-04-19 18:58:26 +00:00
|
|
|
|
|
|
|
&.disabled {
|
|
|
|
${tw`opacity-50`};
|
|
|
|
|
|
|
|
& input[type="checkbox"]:not(:checked) {
|
|
|
|
${tw`border-0`};
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 04:58:37 +00:00
|
|
|
`;
|
|
|
|
|
2020-03-27 21:23:13 +00:00
|
|
|
const EditSubuserModal = forwardRef<HTMLHeadingElement, Props>(({ subuser, ...props }, ref) => {
|
2020-03-26 04:58:37 +00:00
|
|
|
const { values, isSubmitting, setFieldValue } = useFormikContext<Values>();
|
2020-04-19 18:58:26 +00:00
|
|
|
const [ canEditUser ] = usePermissions(subuser ? [ 'user.update' ] : [ 'user.create' ]);
|
|
|
|
const permissions = useStoreState(state => state.permissions.data);
|
|
|
|
|
2020-05-31 18:14:31 +00:00
|
|
|
const user = useStoreState(state => state.user.data!);
|
|
|
|
|
2020-04-19 18:58:26 +00:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
// 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-05-31 18:14:31 +00:00
|
|
|
if (user.rootAdmin || (loggedInPermissions.length === 1 && loggedInPermissions[0] === '*')) {
|
2020-04-19 18:58:26 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.filter(key => loggedInPermissions.indexOf(key) >= 0);
|
2020-07-04 23:26:07 +00:00
|
|
|
}, [ permissions, loggedInPermissions ]);
|
2020-03-26 04:58:37 +00:00
|
|
|
|
|
|
|
return (
|
2020-04-03 23:39:55 +00:00
|
|
|
<Modal {...props} top={false} showSpinnerOverlay={isSubmitting}>
|
2020-07-04 23:26:07 +00:00
|
|
|
<h2 css={tw`text-2xl`} ref={ref}>
|
2020-03-29 21:19:17 +00:00
|
|
|
{subuser ?
|
|
|
|
`${canEditUser ? 'Modify' : 'View'} permissions for ${subuser.email}`
|
|
|
|
:
|
|
|
|
'Create new subuser'
|
|
|
|
}
|
2020-07-04 23:26:07 +00:00
|
|
|
</h2>
|
2020-07-03 21:19:05 +00:00
|
|
|
<FlashMessageRender byKey={'user:edit'} css={tw`mt-4`}/>
|
2020-05-31 18:14:31 +00:00
|
|
|
{(!user.rootAdmin && loggedInPermissions[0] !== '*') &&
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`mt-4 pl-4 py-2 border-l-4 border-cyan-400`}>
|
|
|
|
<p css={tw`text-sm text-neutral-300`}>
|
2020-04-19 18:58:26 +00:00
|
|
|
Only permissions which your account is currently assigned may be selected when creating or
|
|
|
|
modifying other users.
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-05-31 18:14:31 +00:00
|
|
|
}
|
2020-03-27 22:40:24 +00:00
|
|
|
{!subuser &&
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2020-03-26 04:58:37 +00:00
|
|
|
<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>
|
2020-03-27 22:40:24 +00:00
|
|
|
}
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`my-6`}>
|
2020-03-26 04:58:37 +00:00
|
|
|
{Object.keys(permissions).filter(key => key !== 'websocket').map((key, index) => (
|
|
|
|
<TitledGreyBox
|
|
|
|
key={key}
|
|
|
|
title={
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`flex items-center`}>
|
|
|
|
<p css={tw`text-sm uppercase flex-1`}>{key}</p>
|
2020-07-04 23:26:07 +00:00
|
|
|
{canEditUser &&
|
|
|
|
<Input
|
2020-03-26 04:58:37 +00:00
|
|
|
type={'checkbox'}
|
|
|
|
onClick={e => {
|
|
|
|
if (e.currentTarget.checked) {
|
|
|
|
setFieldValue('permissions', [
|
|
|
|
...values.permissions,
|
|
|
|
...Object.keys(permissions[key].keys)
|
|
|
|
.map(pkey => `${key}.${pkey}`)
|
|
|
|
.filter(permission => values.permissions.indexOf(permission) === -1),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
setFieldValue('permissions', [
|
|
|
|
...values.permissions.filter(
|
|
|
|
permission => Object.keys(permissions[key].keys)
|
|
|
|
.map(pkey => `${key}.${pkey}`)
|
|
|
|
.indexOf(permission) < 0,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2020-03-29 21:19:17 +00:00
|
|
|
}
|
2020-03-26 04:58:37 +00:00
|
|
|
</div>
|
|
|
|
}
|
2020-07-04 23:26:07 +00:00
|
|
|
css={index > 0 ? tw`mt-4` : undefined}
|
2020-03-26 04:58:37 +00:00
|
|
|
>
|
2020-07-03 21:19:05 +00:00
|
|
|
<p css={tw`text-sm text-neutral-400 mb-4`}>
|
2020-03-26 04:58:37 +00:00
|
|
|
{permissions[key].description}
|
|
|
|
</p>
|
|
|
|
{Object.keys(permissions[key].keys).map((pkey, index) => (
|
|
|
|
<PermissionLabel
|
2020-03-29 21:19:17 +00:00
|
|
|
key={`permission_${key}_${pkey}`}
|
2020-03-26 04:58:37 +00:00
|
|
|
htmlFor={`permission_${key}_${pkey}`}
|
2020-07-05 02:01:49 +00:00
|
|
|
css={[
|
|
|
|
tw`transition-colors duration-75`,
|
|
|
|
index > 0 ? tw`mt-2` : undefined,
|
|
|
|
]}
|
|
|
|
className={(!canEditUser || editablePermissions.indexOf(`${key}.${pkey}`) < 0) ? 'disabled' : undefined}
|
2020-03-26 04:58:37 +00:00
|
|
|
>
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`p-2`}>
|
2020-03-26 04:58:37 +00:00
|
|
|
<Checkbox
|
|
|
|
id={`permission_${key}_${pkey}`}
|
|
|
|
name={'permissions'}
|
|
|
|
value={`${key}.${pkey}`}
|
2020-07-03 21:19:05 +00:00
|
|
|
css={tw`w-5 h-5 mr-2`}
|
2020-04-19 18:58:26 +00:00
|
|
|
disabled={!canEditUser || editablePermissions.indexOf(`${key}.${pkey}`) < 0}
|
2020-03-26 04:58:37 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`flex-1`}>
|
2020-07-04 23:26:07 +00:00
|
|
|
<Label css={tw`font-medium`}>{pkey}</Label>
|
2020-03-26 04:58:37 +00:00
|
|
|
{permissions[key].keys[pkey].length > 0 &&
|
2020-07-03 21:19:05 +00:00
|
|
|
<p css={tw`text-xs text-neutral-400 mt-1`}>
|
2020-03-26 04:58:37 +00:00
|
|
|
{permissions[key].keys[pkey]}
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</PermissionLabel>
|
|
|
|
))}
|
|
|
|
</TitledGreyBox>
|
|
|
|
))}
|
|
|
|
</div>
|
2020-04-19 18:58:26 +00:00
|
|
|
<Can action={subuser ? 'user.update' : 'user.create'}>
|
2020-07-03 21:19:05 +00:00
|
|
|
<div css={tw`pb-6 flex justify-end`}>
|
2020-07-04 23:26:07 +00:00
|
|
|
<Button type={'submit'}>
|
2020-03-29 21:19:17 +00:00
|
|
|
{subuser ? 'Save' : 'Invite User'}
|
2020-07-04 23:26:07 +00:00
|
|
|
</Button>
|
2020-03-29 21:19:17 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2020-03-26 04:58:37 +00:00
|
|
|
</Modal>
|
|
|
|
);
|
2020-03-27 21:23:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default ({ subuser, ...props }: Props) => {
|
|
|
|
const ref = useRef<HTMLHeadingElement>(null);
|
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const appendSubuser = ServerContext.useStoreActions(actions => actions.subusers.appendSubuser);
|
|
|
|
|
|
|
|
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
|
|
|
|
|
|
|
const submit = (values: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
clearFlashes('user:edit');
|
|
|
|
createOrUpdateSubuser(uuid, values, subuser)
|
|
|
|
.then(subuser => {
|
|
|
|
appendSubuser(subuser);
|
|
|
|
props.onDismissed();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
setSubmitting(false);
|
|
|
|
addError({ key: 'user:edit', message: httpErrorToHuman(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-04-19 18:58:26 +00:00
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes('user:edit');
|
|
|
|
}, []);
|
|
|
|
|
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({
|
|
|
|
email: string().email('A valid email address must be provided.').required('A valid email address must be provided.'),
|
|
|
|
permissions: array().of(string()),
|
|
|
|
})}
|
|
|
|
>
|
2020-03-27 21:23:13 +00:00
|
|
|
<Form>
|
|
|
|
<EditSubuserModal ref={ref} subuser={subuser} {...props}/>
|
|
|
|
</Form>
|
2020-03-26 04:58:37 +00:00
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|