feat(admin-settings): 登録設定のトグルと承認待ち一覧を追加
- 一般設定に「登録」セクションを追加 (Settings.ReadWrite 権限が必要) - 登録受付 / Web 登録 / Discord 登録 / 管理者承認を切り替えた時点で保存し、失敗したら元に戻す - 承認待ちアカウントをユーザー名・メール・登録元・申請日時で一覧表示する - 承認ボタンで有効化し、拒否は確認ダイアログの後にアカウントを削除する
This commit is contained in:
1 parent
cf505b3065
commit
ce462dbe20
3 files changed
+443
No files matched your search
@@ -0,0 +1,266 @@
|
||||
<template>
|
||||
<div v-if="canManage" id="registration-section">
|
||||
<h2 class="oc-py-s" v-text="$gettext('Registration')" />
|
||||
<p v-if="loadError" class="registration-load-error" v-text="loadError" />
|
||||
<template v-else-if="loaded">
|
||||
<div class="registration-settings">
|
||||
<oc-switch
|
||||
id="registration-allow"
|
||||
:checked="settings.allow_registration"
|
||||
:label="$gettext('Allow new registrations')"
|
||||
@update:checked="(value: boolean) => update('allow_registration', value)"
|
||||
/>
|
||||
<oc-switch
|
||||
id="registration-allow-web"
|
||||
class="oc-mt-s"
|
||||
:checked="settings.allow_web"
|
||||
:label="$gettext('Allow email/password registration')"
|
||||
@update:checked="(value: boolean) => update('allow_web', value)"
|
||||
/>
|
||||
<oc-switch
|
||||
id="registration-allow-discord"
|
||||
class="oc-mt-s"
|
||||
:checked="settings.allow_discord"
|
||||
:label="$gettext('Allow Discord registration')"
|
||||
@update:checked="(value: boolean) => update('allow_discord', value)"
|
||||
/>
|
||||
<oc-switch
|
||||
id="registration-require-approval"
|
||||
class="oc-mt-s"
|
||||
:checked="settings.require_approval"
|
||||
:label="$gettext('Require admin approval')"
|
||||
@update:checked="(value: boolean) => update('require_approval', value)"
|
||||
/>
|
||||
<p
|
||||
v-if="!settings.allow_registration"
|
||||
class="registration-closed-hint oc-text-muted oc-mt-s"
|
||||
v-text="$gettext('New accounts cannot be created while registrations are closed.')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<h3 class="oc-mt-l oc-mb-s" v-text="$gettext('Pending approvals')" />
|
||||
<p
|
||||
v-if="!pending.length"
|
||||
class="registration-pending-empty oc-text-muted"
|
||||
v-text="$gettext('No accounts are waiting for approval.')"
|
||||
/>
|
||||
<oc-table-simple v-else class="registration-pending-table">
|
||||
<oc-thead>
|
||||
<oc-tr>
|
||||
<oc-th>{{ $gettext('Username') }}</oc-th>
|
||||
<oc-th>{{ $gettext('Email') }}</oc-th>
|
||||
<oc-th>{{ $gettext('Source') }}</oc-th>
|
||||
<oc-th>{{ $gettext('Requested') }}</oc-th>
|
||||
<oc-th class="oc-invisible-sr">{{ $gettext('Actions') }}</oc-th>
|
||||
</oc-tr>
|
||||
</oc-thead>
|
||||
<oc-tbody>
|
||||
<oc-tr v-for="p in pending" :key="p.user_id" class="registration-pending-row">
|
||||
<oc-td translate="no">{{ p.username }}</oc-td>
|
||||
<oc-td translate="no">{{ p.mail || '-' }}</oc-td>
|
||||
<oc-td>
|
||||
<oc-tag size="small" class="registration-source">{{ sourceLabel(p.source) }}</oc-tag>
|
||||
</oc-td>
|
||||
<oc-td>{{ formatDate(p.requested_at) }}</oc-td>
|
||||
<oc-td>
|
||||
<div class="registration-actions">
|
||||
<oc-button
|
||||
class="registration-approve-btn"
|
||||
size="small"
|
||||
variation="primary"
|
||||
appearance="filled"
|
||||
:disabled="busy.has(p.user_id)"
|
||||
@click="approve(p)"
|
||||
>
|
||||
{{ $gettext('Approve') }}
|
||||
</oc-button>
|
||||
<oc-button
|
||||
class="registration-reject-btn"
|
||||
size="small"
|
||||
variation="danger"
|
||||
:disabled="busy.has(p.user_id)"
|
||||
@click="confirmReject(p)"
|
||||
>
|
||||
{{ $gettext('Reject') }}
|
||||
</oc-button>
|
||||
</div>
|
||||
</oc-td>
|
||||
</oc-tr>
|
||||
</oc-tbody>
|
||||
</oc-table-simple>
|
||||
</template>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
import { useGettext } from 'vue3-gettext'
|
||||
import {
|
||||
formatDateFromJSDate,
|
||||
useAbility,
|
||||
useClientService,
|
||||
useMessages,
|
||||
useModals
|
||||
} from '@ownclouders/web-pkg'
|
||||
|
||||
defineOptions({
|
||||
name: 'RegistrationSection'
|
||||
})
|
||||
|
||||
export type RegistrationSettings = {
|
||||
allow_registration: boolean
|
||||
allow_web: boolean
|
||||
allow_discord: boolean
|
||||
require_approval: boolean
|
||||
}
|
||||
|
||||
export type PendingUser = {
|
||||
user_id: string
|
||||
username: string
|
||||
display_name?: string
|
||||
mail?: string
|
||||
source: 'web' | 'discord' | string
|
||||
requested_at: string
|
||||
}
|
||||
|
||||
type Key = keyof RegistrationSettings
|
||||
|
||||
const SETTINGS_URL = '/oauth2/discord/admin/registration'
|
||||
const PENDING_URL = '/oauth2/discord/admin/pending'
|
||||
|
||||
const ability = useAbility()
|
||||
const clientService = useClientService()
|
||||
const { showMessage, showErrorMessage } = useMessages()
|
||||
const { dispatchModal } = useModals()
|
||||
const language = useGettext()
|
||||
const { $gettext } = language
|
||||
|
||||
const canManage = computed(() => ability.can('update-all', 'Setting'))
|
||||
|
||||
const loaded = ref(false)
|
||||
const loadError = ref('')
|
||||
const settings = reactive<RegistrationSettings>({
|
||||
allow_registration: false,
|
||||
allow_web: false,
|
||||
allow_discord: false,
|
||||
require_approval: false
|
||||
})
|
||||
const pending = ref<PendingUser[]>([])
|
||||
const busy = reactive(new Set<string>())
|
||||
|
||||
const sourceLabel = (source: string) => {
|
||||
switch (source) {
|
||||
case 'web':
|
||||
return $gettext('Web')
|
||||
case 'discord':
|
||||
return $gettext('Discord')
|
||||
default:
|
||||
return source
|
||||
}
|
||||
}
|
||||
|
||||
const formatDate = (iso: string) => {
|
||||
const d = new Date(iso)
|
||||
if (!iso || isNaN(d.getTime())) {
|
||||
return '-'
|
||||
}
|
||||
return formatDateFromJSDate(d, language.current)
|
||||
}
|
||||
|
||||
const load = async () => {
|
||||
try {
|
||||
const [s, p] = await Promise.all([
|
||||
clientService.httpAuthenticated.get<RegistrationSettings>(SETTINGS_URL),
|
||||
clientService.httpAuthenticated.get<{ value: PendingUser[] }>(PENDING_URL)
|
||||
])
|
||||
Object.assign(settings, s.data)
|
||||
pending.value = (p.data as { value?: PendingUser[] })?.value || []
|
||||
loaded.value = true
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
loadError.value = $gettext('The registration settings could not be loaded.')
|
||||
}
|
||||
}
|
||||
|
||||
const update = async (key: Key, value: boolean) => {
|
||||
const previous = settings[key]
|
||||
settings[key] = value
|
||||
try {
|
||||
const { data } = await clientService.httpAuthenticated.put<RegistrationSettings>(
|
||||
SETTINGS_URL,
|
||||
{ ...settings }
|
||||
)
|
||||
Object.assign(settings, data)
|
||||
showMessage({ title: $gettext('Registration settings were saved') })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
settings[key] = previous
|
||||
showErrorMessage({
|
||||
title: $gettext('Failed to save the registration settings'),
|
||||
errors: [e as Error]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const decide = async (p: PendingUser, action: 'approve' | 'reject') => {
|
||||
busy.add(p.user_id)
|
||||
try {
|
||||
await clientService.httpAuthenticated.post(
|
||||
`${PENDING_URL}/${encodeURIComponent(p.user_id)}/${action}`
|
||||
)
|
||||
pending.value = pending.value.filter((u) => u.user_id !== p.user_id)
|
||||
showMessage({
|
||||
title:
|
||||
action === 'approve'
|
||||
? $gettext('Account "%{user}" was approved', { user: p.username })
|
||||
: $gettext('Account "%{user}" was rejected', { user: p.username })
|
||||
})
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
showErrorMessage({
|
||||
title:
|
||||
action === 'approve'
|
||||
? $gettext('Failed to approve account "%{user}"', { user: p.username })
|
||||
: $gettext('Failed to reject account "%{user}"', { user: p.username }),
|
||||
errors: [e as Error]
|
||||
})
|
||||
} finally {
|
||||
busy.delete(p.user_id)
|
||||
}
|
||||
}
|
||||
|
||||
const approve = (p: PendingUser) => decide(p, 'approve')
|
||||
const reject = (p: PendingUser) => decide(p, 'reject')
|
||||
|
||||
const confirmReject = (p: PendingUser) => {
|
||||
dispatchModal({
|
||||
variation: 'danger',
|
||||
title: $gettext('Reject account "%{user}"?', { user: p.username }),
|
||||
confirmText: $gettext('Reject'),
|
||||
message: $gettext('The account will be deleted. This cannot be undone.'),
|
||||
hasInput: false,
|
||||
onConfirm: () => reject(p)
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (canManage.value) {
|
||||
load()
|
||||
}
|
||||
})
|
||||
|
||||
defineExpose({ settings, pending, update, approve, reject, confirmReject })
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.registration-settings {
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
.registration-actions {
|
||||
display: flex;
|
||||
gap: var(--oc-space-xsmall);
|
||||
justify-content: flex-end;
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
@@ -13,6 +13,7 @@
|
||||
<InfoSection />
|
||||
<AppearanceSection />
|
||||
<DiscordSection />
|
||||
<RegistrationSection />
|
||||
</div>
|
||||
</template>
|
||||
</app-template>
|
||||
@@ -25,6 +26,7 @@ import AppTemplate from '../components/AppTemplate.vue'
|
||||
import InfoSection from '../components/General/InfoSection.vue'
|
||||
import AppearanceSection from '../components/General/AppearanceSection.vue'
|
||||
import DiscordSection from '../components/General/DiscordSection.vue'
|
||||
import RegistrationSection from '../components/General/RegistrationSection.vue'
|
||||
import DetailsPanel from '../components/General/SideBar/DetailsPanel.vue'
|
||||
import { useGettext } from 'vue3-gettext'
|
||||
import { useSideBar } from '@ownclouders/web-pkg'
|
||||
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
import RegistrationSection from '../../../../src/components/General/RegistrationSection.vue'
|
||||
import {
|
||||
defaultComponentMocks,
|
||||
defaultPlugins,
|
||||
mockAxiosResolve,
|
||||
shallowMount
|
||||
} from '@ownclouders/web-test-helpers'
|
||||
import { flushPromises } from '@vue/test-utils'
|
||||
import { useMessages, useModals } from '@ownclouders/web-pkg'
|
||||
|
||||
const SETTINGS_URL = '/oauth2/discord/admin/registration'
|
||||
const PENDING_URL = '/oauth2/discord/admin/pending'
|
||||
|
||||
const stored = {
|
||||
allow_registration: true,
|
||||
allow_web: false,
|
||||
allow_discord: true,
|
||||
require_approval: true
|
||||
}
|
||||
|
||||
const pendingUsers = [
|
||||
{
|
||||
user_id: 'u1',
|
||||
username: 'alice',
|
||||
mail: 'alice@example.com',
|
||||
source: 'discord',
|
||||
requested_at: '2026-11-01T10:00:00Z'
|
||||
},
|
||||
{
|
||||
user_id: 'u2',
|
||||
username: 'carol',
|
||||
mail: 'carol@example.com',
|
||||
source: 'web',
|
||||
requested_at: '2026-11-01T11:00:00Z'
|
||||
}
|
||||
]
|
||||
|
||||
describe('RegistrationSection', () => {
|
||||
it('is hidden and does not load without the settings permission', async () => {
|
||||
const { wrapper, mocks } = getWrapper({ canManage: false })
|
||||
await flushPromises()
|
||||
expect(wrapper.find('#registration-section').exists()).toBeFalsy()
|
||||
expect(mocks.$clientService.httpAuthenticated.get).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('loads the settings and the pending accounts', async () => {
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
expect(mocks.$clientService.httpAuthenticated.get).toHaveBeenCalledWith(SETTINGS_URL)
|
||||
expect(mocks.$clientService.httpAuthenticated.get).toHaveBeenCalledWith(PENDING_URL)
|
||||
expect(wrapper.vm.settings).toEqual(stored)
|
||||
expect(wrapper.findAll('.registration-pending-row').length).toBe(2)
|
||||
const badges = wrapper.findAll('.registration-source').map((b) => b.text())
|
||||
expect(badges).toEqual(['Discord', 'Web'])
|
||||
})
|
||||
|
||||
it('shows a notice when no account is pending', async () => {
|
||||
const { wrapper } = getWrapper({ pending: [] })
|
||||
await flushPromises()
|
||||
expect(wrapper.find('.registration-pending-empty').exists()).toBeTruthy()
|
||||
expect(wrapper.find('.registration-pending-table').exists()).toBeFalsy()
|
||||
})
|
||||
|
||||
it('saves all settings when a toggle changes', async () => {
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
const next = { ...stored, allow_web: true }
|
||||
mocks.$clientService.httpAuthenticated.put.mockResolvedValue(mockAxiosResolve(next))
|
||||
await wrapper.vm.update('allow_web', true)
|
||||
expect(mocks.$clientService.httpAuthenticated.put).toHaveBeenCalledWith(SETTINGS_URL, next)
|
||||
expect(wrapper.vm.settings.allow_web).toBe(true)
|
||||
const { showMessage } = useMessages()
|
||||
expect(showMessage).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reverts the toggle when saving fails', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => undefined)
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
mocks.$clientService.httpAuthenticated.put.mockRejectedValue(new Error('500'))
|
||||
await wrapper.vm.update('require_approval', false)
|
||||
expect(wrapper.vm.settings.require_approval).toBe(true)
|
||||
const { showErrorMessage } = useMessages()
|
||||
expect(showErrorMessage).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('approves a pending account and removes it from the list', async () => {
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
mocks.$clientService.httpAuthenticated.post.mockResolvedValue(mockAxiosResolve(undefined))
|
||||
await wrapper.vm.approve(pendingUsers[0])
|
||||
expect(mocks.$clientService.httpAuthenticated.post).toHaveBeenCalledWith(
|
||||
`${PENDING_URL}/u1/approve`
|
||||
)
|
||||
expect(wrapper.vm.pending.map((p) => p.user_id)).toEqual(['u2'])
|
||||
})
|
||||
|
||||
it('keeps the account in the list when approving fails', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => undefined)
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
mocks.$clientService.httpAuthenticated.post.mockRejectedValue(new Error('502'))
|
||||
await wrapper.vm.approve(pendingUsers[0])
|
||||
expect(wrapper.vm.pending.length).toBe(2)
|
||||
const { showErrorMessage } = useMessages()
|
||||
expect(showErrorMessage).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('asks for confirmation before rejecting', async () => {
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
wrapper.vm.confirmReject(pendingUsers[1])
|
||||
const { dispatchModal } = useModals()
|
||||
expect(dispatchModal).toHaveBeenCalledWith(expect.objectContaining({ variation: 'danger' }))
|
||||
expect(mocks.$clientService.httpAuthenticated.post).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('rejects a pending account and removes it from the list', async () => {
|
||||
const { wrapper, mocks } = getWrapper()
|
||||
await flushPromises()
|
||||
mocks.$clientService.httpAuthenticated.post.mockResolvedValue(mockAxiosResolve(undefined))
|
||||
await wrapper.vm.reject(pendingUsers[1])
|
||||
expect(mocks.$clientService.httpAuthenticated.post).toHaveBeenCalledWith(
|
||||
`${PENDING_URL}/u2/reject`
|
||||
)
|
||||
expect(wrapper.vm.pending.map((p) => p.user_id)).toEqual(['u1'])
|
||||
})
|
||||
|
||||
it('shows an error when the settings cannot be loaded', async () => {
|
||||
vi.spyOn(console, 'error').mockImplementation(() => undefined)
|
||||
const { wrapper } = getWrapper({ loadFails: true })
|
||||
await flushPromises()
|
||||
expect(wrapper.find('.registration-load-error').exists()).toBeTruthy()
|
||||
expect(wrapper.find('.registration-settings').exists()).toBeFalsy()
|
||||
})
|
||||
})
|
||||
|
||||
function getWrapper({
|
||||
canManage = true,
|
||||
settings = stored,
|
||||
pending = pendingUsers,
|
||||
loadFails = false
|
||||
}: {
|
||||
canManage?: boolean
|
||||
settings?: typeof stored
|
||||
pending?: typeof pendingUsers
|
||||
loadFails?: boolean
|
||||
} = {}) {
|
||||
const mocks = defaultComponentMocks()
|
||||
if (loadFails) {
|
||||
mocks.$clientService.httpAuthenticated.get.mockRejectedValue(new Error('500'))
|
||||
} else {
|
||||
mocks.$clientService.httpAuthenticated.get.mockImplementation((url: string) =>
|
||||
Promise.resolve(
|
||||
url === PENDING_URL ? mockAxiosResolve({ value: pending }) : mockAxiosResolve(settings)
|
||||
)
|
||||
)
|
||||
}
|
||||
return {
|
||||
mocks,
|
||||
wrapper: shallowMount(RegistrationSection, {
|
||||
global: {
|
||||
provide: mocks,
|
||||
mocks,
|
||||
// Render the table rows inside the stubbed design-system components.
|
||||
renderStubDefaultSlot: true,
|
||||
plugins: [
|
||||
...defaultPlugins({
|
||||
abilities: canManage ? [{ action: 'update-all', subject: 'Setting' }] : []
|
||||
})
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user