Skip to content

Commit 8e59bf9

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 5133e99 commit 8e59bf9

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -53,6 +54,9 @@ public function getForm() {
5354
$this->initialStateService->provideInitialState('profileEnabledGlobally', $this->profileManager->isProfileEnabled());
5455
$this->initialStateService->provideInitialState('profileEnabledByDefault', $this->isProfileEnabledByDefault($this->config));
5556

57+
// Basic settings
58+
$this->initialStateService->provideInitialState('restrictSystemTagsCreationToAdmin', $this->appConfig->getValueString('systemtags', 'restrict_creation_to_admin', 'true'));
59+
5660
return new TemplateResponse('settings', 'settings/admin/server', [
5761
'profileEnabledGlobally' => $this->profileManager->isProfileEnabled(),
5862
], '');

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', 'If enabled, regular accounts will be restricted from creating new tags but will still be able to assign and remove them from their files.') }}
14+
</p>
15+
16+
<NcCheckboxRadioSwitch type="switch"
17+
:checked.sync="systemTagsCreationRestrictedToAdmin"
18+
@update:checked="updateSystemTagsDefault">
19+
{{ t('settings', 'Restrict tag creation to admins only') }}
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 { updateSystemTagsAdminRestriction } 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+
systemTagsCreationRestrictedToAdmin: loadState('settings', 'restrictSystemTagsCreationToAdmin', '1') === '1',
43+
}
44+
},
45+
methods: {
46+
t,
47+
async updateSystemTagsDefault(isRestricted: boolean) {
48+
try {
49+
const responseData = await updateSystemTagsAdminRestriction(isRestricted)
50+
console.debug('updateSystemTagsDefault', responseData)
51+
this.handleResponse({
52+
isRestricted,
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({ isRestricted, status, errorMessage, error }) {
64+
if (status === 'ok') {
65+
this.systemTagsCreationRestrictedToAdmin = isRestricted
66+
showSuccess(t('settings', `System tag creation is now ${isRestricted ? 'restricted to administrators' : 'allowed for everybody'}`))
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 updateSystemTagsAdminRestriction = 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: 'restrict_creation_to_admin',
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)