feat: add separate auto approve permissions for Movies/Series

closes #268
pull/503/head
sct 4 years ago
parent 02969d5426
commit 4809257816

@ -7,6 +7,8 @@ export enum Permission {
REQUEST = 32, REQUEST = 32,
VOTE = 64, VOTE = 64,
AUTO_APPROVE = 128, AUTO_APPROVE = 128,
AUTO_APPROVE_MOVIE = 256,
AUTO_APPROVE_TV = 512,
} }
/** /**

@ -127,12 +127,16 @@ requestRoutes.post(
media, media,
requestedBy: req.user, requestedBy: req.user,
// If the user is an admin or has the "auto approve" permission, automatically approve the request // If the user is an admin or has the "auto approve" permission, automatically approve the request
status: req.user?.hasPermission(Permission.AUTO_APPROVE) status:
? MediaRequestStatus.APPROVED req.user?.hasPermission(Permission.AUTO_APPROVE) ||
: MediaRequestStatus.PENDING, req.user?.hasPermission(Permission.AUTO_APPROVE_MOVIE)
modifiedBy: req.user?.hasPermission(Permission.AUTO_APPROVE) ? MediaRequestStatus.APPROVED
? req.user : MediaRequestStatus.PENDING,
: undefined, modifiedBy:
req.user?.hasPermission(Permission.AUTO_APPROVE) ||
req.user?.hasPermission(Permission.AUTO_APPROVE_MOVIE)
? req.user
: undefined,
}); });
await requestRepository.save(request); await requestRepository.save(request);
@ -172,19 +176,25 @@ requestRoutes.post(
} as Media, } as Media,
requestedBy: req.user, requestedBy: req.user,
// If the user is an admin or has the "auto approve" permission, automatically approve the request // If the user is an admin or has the "auto approve" permission, automatically approve the request
status: req.user?.hasPermission(Permission.AUTO_APPROVE) status:
? MediaRequestStatus.APPROVED req.user?.hasPermission(Permission.AUTO_APPROVE) ||
: MediaRequestStatus.PENDING, req.user?.hasPermission(Permission.AUTO_APPROVE_TV)
modifiedBy: req.user?.hasPermission(Permission.AUTO_APPROVE) ? MediaRequestStatus.APPROVED
? req.user : MediaRequestStatus.PENDING,
: undefined, modifiedBy:
req.user?.hasPermission(Permission.AUTO_APPROVE) ||
req.user?.hasPermission(Permission.AUTO_APPROVE_TV)
? req.user
: undefined,
seasons: finalSeasons.map( seasons: finalSeasons.map(
(sn) => (sn) =>
new SeasonRequest({ new SeasonRequest({
seasonNumber: sn, seasonNumber: sn,
status: req.user?.hasPermission(Permission.AUTO_APPROVE) status:
? MediaRequestStatus.APPROVED req.user?.hasPermission(Permission.AUTO_APPROVE) ||
: MediaRequestStatus.PENDING, req.user?.hasPermission(Permission.AUTO_APPROVE_TV)
? MediaRequestStatus.APPROVED
: MediaRequestStatus.PENDING,
}) })
), ),
}); });

@ -0,0 +1,97 @@
import React from 'react';
import { hasPermission } from '../../../server/lib/permissions';
import { Permission, User } from '../../hooks/useUser';
export interface PermissionItem {
id: string;
name: string;
description: string;
permission: Permission;
children?: PermissionItem[];
}
interface PermissionOptionProps {
option: PermissionItem;
currentPermission: number;
user?: User;
parent?: PermissionItem;
onUpdate: (newPermissions: number) => void;
}
const PermissionOption: React.FC<PermissionOptionProps> = ({
option,
currentPermission,
onUpdate,
user,
parent,
}) => {
return (
<>
<div
className={`relative flex items-start first:mt-0 mt-4 ${
(option.permission !== Permission.ADMIN &&
hasPermission(Permission.ADMIN, currentPermission)) ||
(!!parent?.permission &&
hasPermission(parent.permission, currentPermission)) ||
(user && user.id !== 1 && option.permission === Permission.ADMIN) ||
(user &&
!hasPermission(Permission.MANAGE_SETTINGS, user.permissions) &&
option.permission === Permission.MANAGE_SETTINGS)
? 'opacity-50'
: ''
}`}
>
<div className="flex items-center h-5">
<input
id={option.id}
name="permissions"
type="checkbox"
className="w-4 h-4 text-indigo-600 transition duration-150 ease-in-out rounded-md form-checkbox"
disabled={
(option.permission !== Permission.ADMIN &&
hasPermission(Permission.ADMIN, currentPermission)) ||
(!!parent?.permission &&
hasPermission(parent.permission, currentPermission)) ||
(user &&
user.id !== 1 &&
option.permission === Permission.ADMIN) ||
(user &&
!hasPermission(Permission.MANAGE_SETTINGS, user.permissions) &&
option.permission === Permission.MANAGE_SETTINGS)
}
onClick={() => {
onUpdate(
hasPermission(option.permission, currentPermission)
? currentPermission - option.permission
: currentPermission + option.permission
);
}}
checked={
hasPermission(option.permission, currentPermission) ||
(!!parent?.permission &&
hasPermission(parent.permission, currentPermission))
}
/>
</div>
<div className="ml-3 text-sm leading-5">
<label htmlFor={option.id} className="font-medium">
{option.name}
</label>
<p className="text-gray-500">{option.description}</p>
</div>
</div>
{(option.children ?? []).map((child) => (
<div key={`permission-child-${child.id}`} className="pl-6 mt-4">
<PermissionOption
option={child}
currentPermission={currentPermission}
onUpdate={(newPermission) => onUpdate(newPermission)}
parent={option}
/>
</div>
))}
</>
);
};
export default PermissionOption;

@ -68,7 +68,8 @@ const MovieRequestModal: React.FC<RequestModalProps> = ({
if (response.data) { if (response.data) {
if (onComplete) { if (onComplete) {
onComplete( onComplete(
hasPermission(Permission.AUTO_APPROVE) hasPermission(Permission.AUTO_APPROVE) ||
hasPermission(Permission.AUTO_APPROVE_MOVIE)
? MediaStatus.PROCESSING ? MediaStatus.PROCESSING
: MediaStatus.PENDING : MediaStatus.PENDING
); );

@ -10,7 +10,7 @@ import { defineMessages, useIntl } from 'react-intl';
import { useUser, Permission } from '../../hooks/useUser'; import { useUser, Permission } from '../../hooks/useUser';
import { useToasts } from 'react-toast-notifications'; import { useToasts } from 'react-toast-notifications';
import { messages as permissionMessages } from '../UserEdit'; import { messages as permissionMessages } from '../UserEdit';
import { hasPermission } from '../../../server/lib/permissions'; import PermissionOption, { PermissionItem } from '../PermissionOption';
const messages = defineMessages({ const messages = defineMessages({
generalsettings: 'General Settings', generalsettings: 'General Settings',
@ -27,13 +27,6 @@ const messages = defineMessages({
defaultPermissions: 'Default User Permissions', defaultPermissions: 'Default User Permissions',
}); });
interface PermissionOption {
id: string;
name: string;
description: string;
permission: Permission;
}
const SettingsMain: React.FC = () => { const SettingsMain: React.FC = () => {
const { addToast } = useToasts(); const { addToast } = useToasts();
const { hasPermission: userHasPermission } = useUser(); const { hasPermission: userHasPermission } = useUser();
@ -63,7 +56,7 @@ const SettingsMain: React.FC = () => {
return <LoadingSpinner />; return <LoadingSpinner />;
} }
const permissionList: PermissionOption[] = [ const permissionList: PermissionItem[] = [
{ {
id: 'admin', id: 'admin',
name: intl.formatMessage(permissionMessages.admin), name: intl.formatMessage(permissionMessages.admin),
@ -96,12 +89,6 @@ const SettingsMain: React.FC = () => {
description: intl.formatMessage(permissionMessages.requestDescription), description: intl.formatMessage(permissionMessages.requestDescription),
permission: Permission.REQUEST, permission: Permission.REQUEST,
}, },
{
id: 'vote',
name: intl.formatMessage(permissionMessages.vote),
description: intl.formatMessage(permissionMessages.voteDescription),
permission: Permission.VOTE,
},
{ {
id: 'autoapprove', id: 'autoapprove',
name: intl.formatMessage(permissionMessages.autoapprove), name: intl.formatMessage(permissionMessages.autoapprove),
@ -109,6 +96,24 @@ const SettingsMain: React.FC = () => {
permissionMessages.autoapproveDescription permissionMessages.autoapproveDescription
), ),
permission: Permission.AUTO_APPROVE, permission: Permission.AUTO_APPROVE,
children: [
{
id: 'autoapprovemovies',
name: intl.formatMessage(permissionMessages.autoapproveMovies),
description: intl.formatMessage(
permissionMessages.autoapproveMoviesDescription
),
permission: Permission.AUTO_APPROVE_MOVIE,
},
{
id: 'autoapprovetv',
name: intl.formatMessage(permissionMessages.autoapproveSeries),
description: intl.formatMessage(
permissionMessages.autoapproveSeriesDescription
),
permission: Permission.AUTO_APPROVE_TV,
},
],
}, },
]; ];
@ -230,65 +235,18 @@ const SettingsMain: React.FC = () => {
</div> </div>
<div className="mt-4 sm:mt-0 sm:col-span-2"> <div className="mt-4 sm:mt-0 sm:col-span-2">
<div className="max-w-lg"> <div className="max-w-lg">
{permissionList.map((permissionOption) => ( {permissionList.map((permissionItem) => (
<div <PermissionOption
className={`relative flex items-start first:mt-0 mt-4 ${ key={`permission-option-${permissionItem.id}`}
permissionOption.permission !== option={permissionItem}
Permission.ADMIN && currentPermission={values.defaultPermissions}
hasPermission( onUpdate={(newPermissions) =>
Permission.ADMIN, setFieldValue(
values.defaultPermissions 'defaultPermissions',
newPermissions
) )
? 'opacity-50' }
: '' />
}`}
key={`permission-option-${permissionOption.id}`}
>
<div className="flex items-center h-5">
<input
id={permissionOption.id}
name="permissions"
type="checkbox"
className="w-4 h-4 text-indigo-600 transition duration-150 ease-in-out rounded-md form-checkbox"
disabled={
permissionOption.permission !==
Permission.ADMIN &&
hasPermission(
Permission.ADMIN,
values.defaultPermissions
)
}
onClick={() => {
setFieldValue(
'defaultPermissions',
hasPermission(
permissionOption.permission,
values.defaultPermissions
)
? values.defaultPermissions -
permissionOption.permission
: values.defaultPermissions +
permissionOption.permission
);
}}
checked={hasPermission(
permissionOption.permission,
values.defaultPermissions
)}
/>
</div>
<div className="ml-3 text-sm leading-5">
<label
htmlFor={permissionOption.id}
className="font-medium"
>
{permissionOption.name}
</label>
<p className="text-gray-500">
{permissionOption.description}
</p>
</div>
</div>
))} ))}
</div> </div>
</div> </div>

@ -2,12 +2,12 @@ import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import LoadingSpinner from '../Common/LoadingSpinner'; import LoadingSpinner from '../Common/LoadingSpinner';
import { Permission, useUser } from '../../hooks/useUser'; import { Permission, useUser } from '../../hooks/useUser';
import { hasPermission } from '../../../server/lib/permissions';
import Button from '../Common/Button'; import Button from '../Common/Button';
import { useIntl, defineMessages, FormattedMessage } from 'react-intl'; import { useIntl, defineMessages, FormattedMessage } from 'react-intl';
import axios from 'axios'; import axios from 'axios';
import { useToasts } from 'react-toast-notifications'; import { useToasts } from 'react-toast-notifications';
import Header from '../Common/Header'; import Header from '../Common/Header';
import PermissionOption, { PermissionItem } from '../PermissionOption';
export const messages = defineMessages({ export const messages = defineMessages({
edituser: 'Edit User', edituser: 'Edit User',
@ -35,25 +35,24 @@ export const messages = defineMessages({
autoapprove: 'Auto Approve', autoapprove: 'Auto Approve',
autoapproveDescription: autoapproveDescription:
'Grants auto approval for any requests made by this user.', 'Grants auto approval for any requests made by this user.',
autoapproveMovies: 'Auto Approve Movies',
autoapproveMoviesDescription:
'Grants auto approve for movie requests made by this user.',
autoapproveSeries: 'Auto Approve Series',
autoapproveSeriesDescription:
'Grants auto approve for series requests made by this user.',
save: 'Save', save: 'Save',
saving: 'Saving...', saving: 'Saving...',
usersaved: 'User saved', usersaved: 'User saved',
userfail: 'Something went wrong saving the user.', userfail: 'Something went wrong saving the user.',
}); });
interface PermissionOption {
id: string;
name: string;
description: string;
permission: Permission;
}
const UserEdit: React.FC = () => { const UserEdit: React.FC = () => {
const router = useRouter(); const router = useRouter();
const intl = useIntl(); const intl = useIntl();
const { addToast } = useToasts(); const { addToast } = useToasts();
const [isUpdating, setIsUpdating] = useState(false); const [isUpdating, setIsUpdating] = useState(false);
const { user: currentUser, hasPermission: currentHasPermission } = useUser(); const { user: currentUser } = useUser();
const { user, error, revalidate } = useUser({ const { user, error, revalidate } = useUser({
id: Number(router.query.userId), id: Number(router.query.userId),
}); });
@ -97,7 +96,7 @@ const UserEdit: React.FC = () => {
return <LoadingSpinner />; return <LoadingSpinner />;
} }
const permissionList: PermissionOption[] = [ const permissionList: PermissionItem[] = [
{ {
id: 'admin', id: 'admin',
name: intl.formatMessage(messages.admin), name: intl.formatMessage(messages.admin),
@ -139,6 +138,24 @@ const UserEdit: React.FC = () => {
name: intl.formatMessage(messages.autoapprove), name: intl.formatMessage(messages.autoapprove),
description: intl.formatMessage(messages.autoapproveDescription), description: intl.formatMessage(messages.autoapproveDescription),
permission: Permission.AUTO_APPROVE, permission: Permission.AUTO_APPROVE,
children: [
{
id: 'autoapprovemovies',
name: intl.formatMessage(messages.autoapproveMovies),
description: intl.formatMessage(
messages.autoapproveMoviesDescription
),
permission: Permission.AUTO_APPROVE_MOVIE,
},
{
id: 'autoapprovetv',
name: intl.formatMessage(messages.autoapproveSeries),
description: intl.formatMessage(
messages.autoapproveSeriesDescription
),
permission: Permission.AUTO_APPROVE_TV,
},
],
}, },
]; ];
@ -231,74 +248,16 @@ const UserEdit: React.FC = () => {
</div> </div>
<div className="mt-4 sm:mt-0 sm:col-span-2"> <div className="mt-4 sm:mt-0 sm:col-span-2">
<div className="max-w-lg"> <div className="max-w-lg">
{permissionList.map((permissionOption) => ( {permissionList.map((permissionItem) => (
<div <PermissionOption
className={`relative flex items-start first:mt-0 mt-4 ${ key={`permission-option-${permissionItem.id}`}
(permissionOption.permission !== Permission.ADMIN && option={permissionItem}
hasPermission( user={currentUser}
Permission.ADMIN, currentPermission={currentPermission}
currentPermission onUpdate={(newPermission) =>
)) || setCurrentPermission(newPermission)
(currentUser?.id !== 1 && }
permissionOption.permission === Permission.ADMIN) || />
(!currentHasPermission(Permission.MANAGE_SETTINGS) &&
permissionOption.permission ===
Permission.MANAGE_SETTINGS)
? 'opacity-50'
: ''
}`}
key={`permission-option-${permissionOption.id}`}
>
<div className="flex items-center h-5">
<input
id={permissionOption.id}
name="permissions"
type="checkbox"
className="w-4 h-4 text-indigo-600 transition duration-150 ease-in-out rounded-md form-checkbox"
disabled={
(permissionOption.permission !==
Permission.ADMIN &&
hasPermission(
Permission.ADMIN,
currentPermission
)) ||
(currentUser?.id !== 1 &&
permissionOption.permission ===
Permission.ADMIN) ||
(!currentHasPermission(
Permission.MANAGE_SETTINGS
) &&
permissionOption.permission ===
Permission.MANAGE_SETTINGS)
}
onClick={() => {
setCurrentPermission((current) =>
hasPermission(
permissionOption.permission,
currentPermission
)
? current - permissionOption.permission
: current + permissionOption.permission
);
}}
checked={hasPermission(
permissionOption.permission,
currentPermission
)}
/>
</div>
<div className="ml-3 text-sm leading-5">
<label
htmlFor={permissionOption.id}
className="font-medium"
>
{permissionOption.name}
</label>
<p className="text-gray-500">
{permissionOption.description}
</p>
</div>
</div>
))} ))}
</div> </div>
</div> </div>

Loading…
Cancel
Save