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 committed Jan 6, 2025
1 parent d8d3c7e commit 5820d00
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
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
82 changes: 82 additions & 0 deletions apps/systemtags/src/components/SystemTagsCreationControl.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!--
- 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', 'Enable or disable system tag creation for non-admin users.') }}
</p>

<NcCheckboxRadioSwitch type="switch"
:checked.sync="systemTagsCreationEnabled"
@update:checked="onSystemTagsDefaultChange">
{{ 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 { updateSystemTagCreationAllowed4All } from '../services/api.js'

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

export default {
name: 'SystemTagsCreationControl',

components: {
NcCheckboxRadioSwitch,
},

data() {
return {
systemTagsCreationEnabled: loadState('settings', 'systemTagsEnabledByDefault', true),
}
},

methods: {
t,
async onSystemTagsDefaultChange(isEnabled: boolean) {
await this.updateSystemTagsDefault(isEnabled)
},

async updateSystemTagsDefault(isEnabled) {
try {
const responseData = await updateSystemTagCreationAllowed4All(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.systemTagsCreationEnabled = 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 updateSystemTagCreationAllowed4All = 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 5820d00

Please sign in to comment.