Skip to content

Commit

Permalink
feat(systemtags): toggle for system tag creation in admin settings
Browse files Browse the repository at this point in the history
Signed-off-by: nfebe <[email protected]>
  • Loading branch information
nfebe authored and Altahrim committed Jan 14, 2025
1 parent d23e7a7 commit 95cdc39
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
3 changes: 3 additions & 0 deletions apps/settings/lib/Settings/Admin/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public function getForm() {
$this->initialStateService->provideInitialState('profileEnabledGlobally', $this->profileManager->isProfileEnabled());
$this->initialStateService->provideInitialState('profileEnabledByDefault', $this->isProfileEnabledByDefault($this->config));

// Basic settings
$this->initialStateService->provideInitialState('onlyAdminsCanCreateSystemTags', $this->appConfig->getValueString('systemtags', 'only_admins_can_create', 'true'));

return new TemplateResponse('settings', 'settings/admin/server', [
'profileEnabledGlobally' => $this->profileManager->isProfileEnabled(),
], '');
Expand Down
4 changes: 2 additions & 2 deletions apps/systemtags/src/components/SystemTagForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
aria-labelledby="system-tag-form-heading"
@submit.prevent="handleSubmit"
@reset="reset">
<h3 id="system-tag-form-heading">
<h4 id="system-tag-form-heading">
{{ t('systemtags', 'Create or edit tags') }}
</h3>
</h4>

<div class="system-tag-form__group">
<label for="system-tags-input">{{ t('systemtags', 'Search for a tag to edit') }}</label>
Expand Down
77 changes: 77 additions & 0 deletions apps/systemtags/src/components/SystemTagsCreationControl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
<div id="system-tags-creation-control">
<h4 class="inlineblock">
{{ t('settings', 'System tag creation') }}
</h4>

<p class="settings-hint">
{{ t('settings', 'Allow only admins to create tags (enable or disable).') }}
</p>

<NcCheckboxRadioSwitch type="switch"
:checked.sync="onlyAdminsCanCreateEnabled"
@update:checked="updateSystemTagsDefault">
{{ t('settings', 'Enable') }}
</NcCheckboxRadioSwitch>
</div>
</template>

<script lang="ts">
import { loadState } from '@nextcloud/initial-state'
import { showError, showSuccess } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import logger from '../logger.ts'
import { updateOnlyAdminsCanCreateSystemTags } from '../services/api.js'

import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'

export default {
name: 'SystemTagsCreationControl',

components: {
NcCheckboxRadioSwitch,
},

data() {
return {
onlyAdminsCanCreateEnabled: loadState('settings', 'onlyAdminsCanCreateSystemTags', '1') === '1',
}
},
methods: {
t,
async updateSystemTagsDefault(isEnabled: boolean) {
try {
const responseData = await updateOnlyAdminsCanCreateSystemTags(isEnabled)
console.debug('updateSystemTagsDefault', responseData)
this.handleResponse({
isEnabled,
status: responseData.ocs?.meta?.status,
})
} catch (e) {
this.handleResponse({
errorMessage: t('settings', 'Unable to update setting'),
error: e,
})
}
},

handleResponse({ isEnabled, status, errorMessage, error }) {
if (status === 'ok') {
this.onlyAdminsCanCreateEnabled = isEnabled
showSuccess(t('settings', `System tag creation is now ${isEnabled ? 'enabled' : 'disabled'} for non-admin users`))
return
}

if (errorMessage) {
showError(errorMessage)
logger.error(errorMessage, error)
}
},
},
}
</script>
25 changes: 24 additions & 1 deletion apps/systemtags/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import type { FileStat, ResponseDataDetailed, WebDAVClientError } from 'webdav'
import type { ServerTag, Tag, TagWithId } from '../types.js'

import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { generateUrl, generateOcsUrl } from '@nextcloud/router'
import { t } from '@nextcloud/l10n'

import { davClient } from './davClient.js'
import { formatTag, parseIdFromLocation, parseTags } from '../utils'
import logger from '../logger.ts'
import { emit } from '@nextcloud/event-bus'
import { confirmPassword } from '@nextcloud/password-confirmation'

export const fetchTagsPayload = `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
Expand Down Expand Up @@ -203,3 +204,25 @@ export const setTagObjects = async function(tag: TagWithId, type: string, object
},
})
}

type OcsResponse = {
ocs: NonNullable<unknown>,
}

export const updateOnlyAdminsCanCreateSystemTags = async (isAllowed: boolean): Promise<OcsResponse> => {
// Convert to string for compatibility
const isAllowedString = isAllowed ? '1' : '0'

const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', {
appId: 'systemtags',
key: 'only_admins_can_create',
})

await confirmPassword()

const res = await axios.post(url, {
value: isAllowedString,
})

return res.data
}
4 changes: 3 additions & 1 deletion apps/systemtags/src/views/SystemTagsSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<template>
<NcSettingsSection :name="t('systemtags', 'Collaborative tags')"
: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.')">
<SystemTagsCreationControl />
<NcLoadingIcon v-if="loadingTags"
:name="t('systemtags', 'Loading collaborative tags …')"
:size="32" />

<SystemTagForm v-else
:tags="tags"
@tag:created="handleCreate"
Expand All @@ -29,6 +29,7 @@ import { translate as t } from '@nextcloud/l10n'
import { showError } from '@nextcloud/dialogs'

import SystemTagForm from '../components/SystemTagForm.vue'
import SystemTagsCreationControl from '../components/SystemTagsCreationControl.vue'

import { fetchTags } from '../services/api.js'

Expand All @@ -41,6 +42,7 @@ export default Vue.extend({
NcLoadingIcon,
NcSettingsSection,
SystemTagForm,
SystemTagsCreationControl,
},

data() {
Expand Down

0 comments on commit 95cdc39

Please sign in to comment.