Skip to content

Commit d91cdab

Browse files
authored
add staff scopes UI (#8290)
* add staff scopes UI * show included scopes
1 parent 7a81fcb commit d91cdab

2 files changed

Lines changed: 126 additions & 15 deletions

File tree

‎apps/frontend/src/composables/auth/scopes.ts‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,30 @@ const scopeDefinitions = [
615615
label: scopeMessages.sessionAccessLabel,
616616
desc: scopeMessages.sessionAccessDescription,
617617
},
618+
{
619+
id: 'MODERATION_READ',
620+
value: BigInt(1) << BigInt(48),
621+
label: 'Read moderation data',
622+
desc: 'View content and data only visible to moderators',
623+
},
624+
{
625+
id: 'MODERATION_WRITE',
626+
value: BigInt(1) << BigInt(49),
627+
label: 'Write moderation data',
628+
desc: 'Perform moderation actions',
629+
},
630+
{
631+
id: 'ADMIN_READ',
632+
value: BigInt(1) << BigInt(50),
633+
label: 'Read admin data',
634+
desc: 'View content and data only visible to admins',
635+
},
636+
{
637+
id: 'ADMIN_WRITE',
638+
value: BigInt(1) << BigInt(51),
639+
label: 'Write admin data',
640+
desc: 'Perform admin actions',
641+
},
618642
]
619643

620644
const Scopes = scopeDefinitions.reduce(
@@ -638,10 +662,26 @@ export const restrictedScopes = [
638662
Scopes.PERFORM_ANALYTICS,
639663
]
640664

665+
export const moderationScopeList = ['MODERATION_READ', 'MODERATION_WRITE']
666+
667+
export const adminScopeList = ['ADMIN_READ', 'ADMIN_WRITE']
668+
669+
const staffScopeIds = new Set([...moderationScopeList, ...adminScopeList])
670+
641671
export const scopeList = Object.entries(Scopes)
642-
.filter(([_, value]) => !restrictedScopes.includes(value))
672+
.filter(([key, value]) => !restrictedScopes.includes(value) && !staffScopeIds.has(key))
643673
.map(([key, _]) => key)
644674

675+
export const scopeAccent = (scope: string) => {
676+
if (moderationScopeList.includes(scope)) {
677+
return 'moderation'
678+
}
679+
if (adminScopeList.includes(scope)) {
680+
return 'admin'
681+
}
682+
return null
683+
}
684+
645685
export const getScopeValue = (scope: string) => {
646686
return Scopes[scope]
647687
}
@@ -707,7 +747,9 @@ export const useScopes = () => {
707747
if (!scopeDefinition) {
708748
throw new Error(`Scope ${scope} not found`)
709749
}
710-
return formatMessage(scopeDefinition.desc)
750+
return typeof scopeDefinition.desc === 'string'
751+
? scopeDefinition.desc
752+
: formatMessage(scopeDefinition.desc)
711753
})
712754
}
713755

@@ -720,7 +762,9 @@ export const useScopes = () => {
720762
if (!scopeDefinition) {
721763
throw new Error(`Scope ${scope} not found`)
722764
}
723-
return formatMessage(scopeDefinition.label)
765+
return typeof scopeDefinition.label === 'string'
766+
? scopeDefinition.label
767+
: formatMessage(scopeDefinition.label)
724768
})
725769
}
726770

‎apps/frontend/src/pages/settings/pats.vue‎

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,27 @@
4242
:key="category.name"
4343
class="flex flex-col gap-1.5"
4444
>
45-
<h4 class="m-0 border-b border-divider text-base font-medium text-primary">
45+
<h4
46+
class="m-0 border-b border-divider text-base font-medium"
47+
:class="category.headingClass || 'text-primary'"
48+
>
4649
{{ category.name }}
4750
</h4>
4851
<div class="flex flex-col gap-2">
49-
<Checkbox
52+
<span
5053
v-for="scope in category.scopes"
5154
:key="scope"
52-
:label="scopesToLabels(getScopeValue(scope)).join(', ')"
53-
:model-value="hasScope(scopesVal, scope)"
54-
@update:model-value="scopesVal = toggleScope(scopesVal, scope)"
55-
/>
55+
v-tooltip="impliedStaffTooltip(scope)"
56+
class="w-fit"
57+
>
58+
<Checkbox
59+
:label="scopesToLabels(getScopeValue(scope)).join(', ')"
60+
:label-class="category.labelClass"
61+
:disabled="category.disabled || !!impliedStaffTooltip(scope)"
62+
:model-value="hasScope(scopesVal, scope) || !!impliedStaffTooltip(scope)"
63+
@update:model-value="scopesVal = toggleScope(scopesVal, scope)"
64+
/>
65+
</span>
5666
</div>
5767
</div>
5868
</div>
@@ -247,12 +257,12 @@
247257
</p>
248258
<div v-else :id="`pat-permissions-${pat.id}`" class="grid gap-1 sm:grid-cols-2">
249259
<div
250-
v-for="(scope, index) in patScopes(pat)"
251-
:key="`${pat.id}-scope-${index}`"
260+
v-for="scope in patScopes(pat)"
261+
:key="`${pat.id}-scope-${scope.id}`"
252262
class="flex items-center gap-1"
253263
>
254264
<CheckIcon class="size-5 shrink-0 text-green" />
255-
{{ scope }}
265+
<span :class="scopeTextClass(scope.accent)">{{ scope.text }}</span>
256266
</div>
257267
</div>
258268
</div>
@@ -292,11 +302,17 @@ import {
292302
useRelativeTime,
293303
useVIntl,
294304
} from '@modrinth/ui'
305+
import { isAdmin, isStaff } from '@modrinth/utils'
295306
import { useQuery, useQueryClient } from '@tanstack/vue-query'
296307
308+
import { useAuth } from '~/composables/auth'
297309
import {
310+
adminScopeList,
311+
decodeScopes,
298312
getScopeValue,
299313
hasScope,
314+
moderationScopeList,
315+
scopeAccent,
300316
scopeCategoryMessages,
301317
scopeList,
302318
toggleScope,
@@ -425,6 +441,7 @@ useHead({
425441
})
426442
427443
const data = useNuxtApp()
444+
const auth = await useAuth()
428445
const { scopesToLabels, scopesToDefinitions } = useScopes()
429446
const patModal = ref()
430447
const minimumPatExpiry = data.$dayjs().add(1, 'day').format('YYYY-MM-DD')
@@ -456,7 +473,7 @@ const displayPats = computed(() => {
456473
})
457474
458475
const scopeCategories = computed(() => {
459-
return [
476+
const categories = [
460477
{
461478
name: formatMessage(scopeCategoryMessages.categoryUserAccount),
462479
scopes: scopeList.filter((s) => s.startsWith('USER_')),
@@ -507,7 +524,27 @@ const scopeCategories = computed(() => {
507524
(s) => s.startsWith('ANALYTICS') || s.startsWith('PERFORM_ANALYTICS'),
508525
),
509526
},
510-
].filter((c) => c.scopes.length > 0)
527+
]
528+
529+
if (isStaff(auth.value.user)) {
530+
categories.push(
531+
{
532+
name: 'Moderation',
533+
scopes: moderationScopeList,
534+
labelClass: 'text-orange',
535+
headingClass: 'text-orange',
536+
},
537+
{
538+
name: 'Admin',
539+
scopes: adminScopeList,
540+
labelClass: 'text-red',
541+
headingClass: 'text-red',
542+
disabled: !isAdmin(auth.value.user),
543+
},
544+
)
545+
}
546+
547+
return categories.filter((c) => c.scopes.length > 0)
511548
})
512549
513550
function openCreatePat() {
@@ -543,8 +580,38 @@ function isPatExpired(pat) {
543580
return new Date(pat.expires) <= new Date()
544581
}
545582
583+
function impliedStaffTooltip(scope) {
584+
if (scope === 'MODERATION_READ' && hasScope(scopesVal.value, 'ADMIN_READ')) {
585+
return 'Included with Admin read'
586+
}
587+
if (scope === 'MODERATION_WRITE' && hasScope(scopesVal.value, 'ADMIN_WRITE')) {
588+
return 'Included with Admin write'
589+
}
590+
}
591+
546592
function patScopes(pat) {
547-
return scopesToDefinitions(BigInt(pat.scopes || 0))
593+
const selected = decodeScopes(BigInt(pat.scopes || 0))
594+
if (selected.includes('ADMIN_READ') && !selected.includes('MODERATION_READ')) {
595+
selected.push('MODERATION_READ')
596+
}
597+
if (selected.includes('ADMIN_WRITE') && !selected.includes('MODERATION_WRITE')) {
598+
selected.push('MODERATION_WRITE')
599+
}
600+
return selected.map((scope) => ({
601+
id: scope,
602+
text: scopesToDefinitions(getScopeValue(scope))[0],
603+
accent: scopeAccent(scope),
604+
}))
605+
}
606+
607+
function scopeTextClass(accent) {
608+
if (accent === 'moderation') {
609+
return 'text-orange'
610+
}
611+
if (accent === 'admin') {
612+
return 'text-red'
613+
}
614+
return undefined
548615
}
549616
550617
async function createPat() {

0 commit comments

Comments
 (0)