Skip to content

Commit 5c079df

Browse files
nfebeAltahrim
authored andcommitted
feat(systemtags): toggle for system tag creation in admin settings
Signed-off-by: nfebe <fenn25.fn@gmail.com>
1 parent b0a9d12 commit 5c079df

5 files changed

Lines changed: 109 additions & 4 deletions

File tree

apps/settings/lib/Settings/Admin/Server.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function getForm() {
5353
$this->initialStateService->provideInitialState('profileEnabledGlobally', $this->profileManager->isProfileEnabled());
5454
$this->initialStateService->provideInitialState('profileEnabledByDefault', $this->isProfileEnabledByDefault($this->config));
5555

56+
// Basic settings
57+
$this->initialStateService->provideInitialState('onlyAdminsCanCreateSystemTags', $this->appConfig->getValueString('systemtags', 'only_admins_can_create', 'true'));
58+
5659
return new TemplateResponse('settings', 'settings/admin/server', [
5760
'profileEnabledGlobally' => $this->profileManager->isProfileEnabled(),
5861
], '');

apps/systemtags/src/components/SystemTagForm.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
aria-labelledby="system-tag-form-heading"
1010
@submit.prevent="handleSubmit"
1111
@reset="reset">
12-
<h3 id="system-tag-form-heading">
12+
<h4 id="system-tag-form-heading">
1313
{{ t('systemtags', 'Create or edit tags') }}
14-
</h3>
14+
</h4>
1515

1616
<div class="system-tag-form__group">
1717
<label for="system-tags-input">{{ t('systemtags', 'Search for a tag to edit') }}</label>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!--
2+
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
- SPDX-License-Identifier: AGPL-3.0-or-later
4+
-->
5+
6+
<template>
7+
<div id="system-tags-creation-control">
8+
<h4 class="inlineblock">
9+
{{ t('settings', 'System tag creation') }}
10+
</h4>
11+
12+
<p class="settings-hint">
13+
{{ t('settings', 'Allow only admins to create tags (enable or disable).') }}
14+
</p>
15+
16+
<NcCheckboxRadioSwitch type="switch"
17+
:checked.sync="onlyAdminsCanCreateEnabled"
18+
@update:checked="updateSystemTagsDefault">
19+
{{ t('settings', 'Enable') }}
20+
</NcCheckboxRadioSwitch>
21+
</div>
22+
</template>
23+
24+
<script lang="ts">
25+
import { loadState } from '@nextcloud/initial-state'
26+
import { showError, showSuccess } from '@nextcloud/dialogs'
27+
import { t } from '@nextcloud/l10n'
28+
import logger from '../logger.ts'
29+
import { updateOnlyAdminsCanCreateSystemTags } from '../services/api.js'
30+
31+
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
32+
33+
export default {
34+
name: 'SystemTagsCreationControl',
35+
36+
components: {
37+
NcCheckboxRadioSwitch,
38+
},
39+
40+
data() {
41+
return {
42+
onlyAdminsCanCreateEnabled: loadState('settings', 'onlyAdminsCanCreateSystemTags', '1') === '1',
43+
}
44+
},
45+
methods: {
46+
t,
47+
async updateSystemTagsDefault(isEnabled: boolean) {
48+
try {
49+
const responseData = await updateOnlyAdminsCanCreateSystemTags(isEnabled)
50+
console.debug('updateSystemTagsDefault', responseData)
51+
this.handleResponse({
52+
isEnabled,
53+
status: responseData.ocs?.meta?.status,
54+
})
55+
} catch (e) {
56+
this.handleResponse({
57+
errorMessage: t('settings', 'Unable to update setting'),
58+
error: e,
59+
})
60+
}
61+
},
62+
63+
handleResponse({ isEnabled, status, errorMessage, error }) {
64+
if (status === 'ok') {
65+
this.onlyAdminsCanCreateEnabled = isEnabled
66+
showSuccess(t('settings', `System tag creation is now ${isEnabled ? 'enabled' : 'disabled'} for non-admin users`))
67+
return
68+
}
69+
70+
if (errorMessage) {
71+
showError(errorMessage)
72+
logger.error(errorMessage, error)
73+
}
74+
},
75+
},
76+
}
77+
</script>

apps/systemtags/src/services/api.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import type { FileStat, ResponseDataDetailed, WebDAVClientError } from 'webdav'
77
import type { ServerTag, Tag, TagWithId } from '../types.js'
88

99
import axios from '@nextcloud/axios'
10-
import { generateUrl } from '@nextcloud/router'
10+
import { generateUrl, generateOcsUrl } from '@nextcloud/router'
1111
import { t } from '@nextcloud/l10n'
1212

1313
import { davClient } from './davClient.js'
1414
import { formatTag, parseIdFromLocation, parseTags } from '../utils'
1515
import logger from '../logger.ts'
1616
import { emit } from '@nextcloud/event-bus'
17+
import { confirmPassword } from '@nextcloud/password-confirmation'
1718

1819
export const fetchTagsPayload = `<?xml version="1.0"?>
1920
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
@@ -203,3 +204,25 @@ export const setTagObjects = async function(tag: TagWithId, type: string, object
203204
},
204205
})
205206
}
207+
208+
type OcsResponse = {
209+
ocs: NonNullable<unknown>,
210+
}
211+
212+
export const updateOnlyAdminsCanCreateSystemTags = async (isAllowed: boolean): Promise<OcsResponse> => {
213+
// Convert to string for compatibility
214+
const isAllowedString = isAllowed ? '1' : '0'
215+
216+
const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', {
217+
appId: 'systemtags',
218+
key: 'only_admins_can_create',
219+
})
220+
221+
await confirmPassword()
222+
223+
const res = await axios.post(url, {
224+
value: isAllowedString,
225+
})
226+
227+
return res.data
228+
}

apps/systemtags/src/views/SystemTagsSection.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<template>
77
<NcSettingsSection :name="t('systemtags', 'Collaborative tags')"
88
:description="t('systemtags', 'Collaborative tags are available for all users. Restricted tags are visible to users but cannot be assigned by them. Invisible tags are for internal use, since users cannot see or assign them.')">
9+
<SystemTagsCreationControl />
910
<NcLoadingIcon v-if="loadingTags"
1011
:name="t('systemtags', 'Loading collaborative tags …')"
1112
:size="32" />
12-
1313
<SystemTagForm v-else
1414
:tags="tags"
1515
@tag:created="handleCreate"
@@ -29,6 +29,7 @@ import { translate as t } from '@nextcloud/l10n'
2929
import { showError } from '@nextcloud/dialogs'
3030
3131
import SystemTagForm from '../components/SystemTagForm.vue'
32+
import SystemTagsCreationControl from '../components/SystemTagsCreationControl.vue'
3233
3334
import { fetchTags } from '../services/api.js'
3435
@@ -41,6 +42,7 @@ export default Vue.extend({
4142
NcLoadingIcon,
4243
NcSettingsSection,
4344
SystemTagForm,
45+
SystemTagsCreationControl,
4446
},
4547
4648
data() {

0 commit comments

Comments
 (0)