fix(ui): Automatically disable and uncheck user permissions with unmet requirements (#941)

pull/964/head
TheCatLady 4 years ago committed by GitHub
parent e28dfadaf5
commit c9a150b1db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -115,18 +115,21 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
name: intl.formatMessage(messages.request4k), name: intl.formatMessage(messages.request4k),
description: intl.formatMessage(messages.request4kDescription), description: intl.formatMessage(messages.request4kDescription),
permission: Permission.REQUEST_4K, permission: Permission.REQUEST_4K,
requires: [{ permissions: [Permission.REQUEST] }],
children: [ children: [
{ {
id: 'request4k-movies', id: 'request4k-movies',
name: intl.formatMessage(messages.request4kMovies), name: intl.formatMessage(messages.request4kMovies),
description: intl.formatMessage(messages.request4kMoviesDescription), description: intl.formatMessage(messages.request4kMoviesDescription),
permission: Permission.REQUEST_4K_MOVIE, permission: Permission.REQUEST_4K_MOVIE,
requires: [{ permissions: [Permission.REQUEST] }],
}, },
{ {
id: 'request4k-tv', id: 'request4k-tv',
name: intl.formatMessage(messages.request4kTv), name: intl.formatMessage(messages.request4kTv),
description: intl.formatMessage(messages.request4kTvDescription), description: intl.formatMessage(messages.request4kTvDescription),
permission: Permission.REQUEST_4K_TV, permission: Permission.REQUEST_4K_TV,
requires: [{ permissions: [Permission.REQUEST] }],
}, },
], ],
}, },
@ -135,6 +138,7 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
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,
requires: [{ permissions: [Permission.REQUEST] }],
children: [ children: [
{ {
id: 'autoapprovemovies', id: 'autoapprovemovies',
@ -143,6 +147,7 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
messages.autoapproveMoviesDescription messages.autoapproveMoviesDescription
), ),
permission: Permission.AUTO_APPROVE_MOVIE, permission: Permission.AUTO_APPROVE_MOVIE,
requires: [{ permissions: [Permission.REQUEST] }],
}, },
{ {
id: 'autoapprovetv', id: 'autoapprovetv',
@ -151,6 +156,7 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
messages.autoapproveSeriesDescription messages.autoapproveSeriesDescription
), ),
permission: Permission.AUTO_APPROVE_TV, permission: Permission.AUTO_APPROVE_TV,
requires: [{ permissions: [Permission.REQUEST] }],
}, },
], ],
}, },
@ -159,6 +165,12 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
name: intl.formatMessage(messages.autoapprove4k), name: intl.formatMessage(messages.autoapprove4k),
description: intl.formatMessage(messages.autoapprove4kDescription), description: intl.formatMessage(messages.autoapprove4kDescription),
permission: Permission.AUTO_APPROVE_4K, permission: Permission.AUTO_APPROVE_4K,
requires: [
{
permissions: [Permission.REQUEST, Permission.REQUEST_4K],
type: 'and',
},
],
children: [ children: [
{ {
id: 'autoapprove4k-movies', id: 'autoapprove4k-movies',
@ -167,6 +179,15 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
messages.autoapprove4kMoviesDescription messages.autoapprove4kMoviesDescription
), ),
permission: Permission.AUTO_APPROVE_4K_MOVIE, permission: Permission.AUTO_APPROVE_4K_MOVIE,
requires: [
{
permissions: [Permission.REQUEST],
},
{
permissions: [Permission.REQUEST_4K, Permission.REQUEST_4K_MOVIE],
type: 'or',
},
],
}, },
{ {
id: 'autoapprove4k-tv', id: 'autoapprove4k-tv',
@ -175,6 +196,15 @@ export const PermissionEdit: React.FC<PermissionEditProps> = ({
messages.autoapprove4kSeriesDescription messages.autoapprove4kSeriesDescription
), ),
permission: Permission.AUTO_APPROVE_4K_TV, permission: Permission.AUTO_APPROVE_4K_TV,
requires: [
{
permissions: [Permission.REQUEST],
},
{
permissions: [Permission.REQUEST_4K, Permission.REQUEST_4K_TV],
type: 'or',
},
],
}, },
], ],
}, },

@ -8,6 +8,12 @@ export interface PermissionItem {
description: string; description: string;
permission: Permission; permission: Permission;
children?: PermissionItem[]; children?: PermissionItem[];
requires?: PermissionRequirement[];
}
interface PermissionRequirement {
permissions: Permission[];
type?: 'and' | 'or';
} }
interface PermissionOptionProps { interface PermissionOptionProps {
@ -47,7 +53,13 @@ const PermissionOption: React.FC<PermissionOptionProps> = ({
(user && user.id !== 1 && option.permission === Permission.ADMIN) || (user && user.id !== 1 && option.permission === Permission.ADMIN) ||
(user && (user &&
!hasPermission(Permission.MANAGE_SETTINGS, user.permissions) && !hasPermission(Permission.MANAGE_SETTINGS, user.permissions) &&
option.permission === Permission.MANAGE_SETTINGS) option.permission === Permission.MANAGE_SETTINGS) ||
(option.requires &&
!option.requires.every((requirement) =>
hasPermission(requirement.permissions, currentPermission, {
type: requirement.type ?? 'and',
})
))
? 'opacity-50' ? 'opacity-50'
: '' : ''
}`} }`}
@ -69,7 +81,13 @@ const PermissionOption: React.FC<PermissionOptionProps> = ({
option.permission === Permission.ADMIN) || option.permission === Permission.ADMIN) ||
(user && (user &&
!hasPermission(Permission.MANAGE_SETTINGS, user.permissions) && !hasPermission(Permission.MANAGE_SETTINGS, user.permissions) &&
option.permission === Permission.MANAGE_SETTINGS) option.permission === Permission.MANAGE_SETTINGS) ||
(option.requires &&
!option.requires.every((requirement) =>
hasPermission(requirement.permissions, currentPermission, {
type: requirement.type ?? 'and',
})
))
} }
onChange={() => { onChange={() => {
onUpdate( onUpdate(
@ -79,11 +97,20 @@ const PermissionOption: React.FC<PermissionOptionProps> = ({
); );
}} }}
checked={ checked={
hasPermission(option.permission, currentPermission) || (hasPermission(option.permission, currentPermission) ||
(!!parent?.permission && (!!parent?.permission &&
hasPermission(parent.permission, currentPermission)) || hasPermission(parent.permission, currentPermission)) ||
(autoApprovePermissions.includes(option.permission) && (autoApprovePermissions.includes(option.permission) &&
hasPermission(Permission.MANAGE_REQUESTS, currentPermission)) hasPermission(
Permission.MANAGE_REQUESTS,
currentPermission
))) &&
(!option.requires ||
option.requires.every((requirement) =>
hasPermission(requirement.permissions, currentPermission, {
type: requirement.type ?? 'and',
})
))
} }
/> />
</div> </div>

Loading…
Cancel
Save