|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +<template> |
| 7 | + <div class="identify-methods-editor"> |
| 8 | + <div v-if="entries.length === 0" class="identify-methods-editor__empty"> |
| 9 | + {{ t('libresign', 'No identification methods available.') }} |
| 10 | + </div> |
| 11 | + |
| 12 | + <div v-for="(identifyMethod, index) in entries" |
| 13 | + :key="identifyMethod.name" |
| 14 | + class="identify-methods-editor__method"> |
| 15 | + <hr v-if="index !== 0"> |
| 16 | + |
| 17 | + <NcCheckboxRadioSwitch type="switch" |
| 18 | + :model-value="identifyMethod.enabled" |
| 19 | + @update:modelValue="onMethodToggle(index, $event)"> |
| 20 | + {{ identifyMethod.friendly_name ?? identifyMethod.name }} |
| 21 | + </NcCheckboxRadioSwitch> |
| 22 | + |
| 23 | + <div v-if="identifyMethod.enabled" class="identify-methods-editor__method-details"> |
| 24 | + <fieldset v-if="identifyMethod.name === 'email'" class="identify-methods-editor__sub-section"> |
| 25 | + <NcCheckboxRadioSwitch :model-value="Boolean(identifyMethod.can_create_account)" |
| 26 | + @update:modelValue="onCanCreateAccountToggle(index, $event)"> |
| 27 | + {{ t('libresign', 'Request to create account when the user does not have an account') }} |
| 28 | + </NcCheckboxRadioSwitch> |
| 29 | + </fieldset> |
| 30 | + |
| 31 | + <fieldset class="identify-methods-editor__sub-section"> |
| 32 | + <legend>{{ t('libresign', 'Signature methods') }}</legend> |
| 33 | + <NcCheckboxRadioSwitch v-for="(signatureMethod, signatureMethodName) in identifyMethod.signatureMethods" |
| 34 | + :key="signatureMethodName" |
| 35 | + type="radio" |
| 36 | + :name="identifyMethod.name" |
| 37 | + :value="signatureMethodName" |
| 38 | + :model-value="identifyMethod.signatureMethodEnabled" |
| 39 | + @update:modelValue="onSignatureMethodChange(index, String($event))"> |
| 40 | + {{ signatureMethod.label ?? signatureMethodName }} |
| 41 | + </NcCheckboxRadioSwitch> |
| 42 | + </fieldset> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | +</template> |
| 47 | + |
| 48 | +<script setup lang="ts"> |
| 49 | +import { computed } from 'vue' |
| 50 | +
|
| 51 | +import { t } from '@nextcloud/l10n' |
| 52 | +import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch' |
| 53 | +
|
| 54 | +import type { EffectivePolicyValue } from '../../../../../types/index' |
| 55 | +import { |
| 56 | + normalizeIdentifyMethodsPolicy, |
| 57 | + serializeIdentifyMethodsPolicy, |
| 58 | + type IdentifyMethodPolicyEntry, |
| 59 | +} from './model' |
| 60 | +
|
| 61 | +defineOptions({ |
| 62 | + name: 'IdentifyMethodsRuleEditor', |
| 63 | +}) |
| 64 | +
|
| 65 | +const props = defineProps<{ |
| 66 | + modelValue: EffectivePolicyValue |
| 67 | +}>() |
| 68 | +
|
| 69 | +const emit = defineEmits<{ |
| 70 | + 'update:modelValue': [value: IdentifyMethodPolicyEntry[]] |
| 71 | +}>() |
| 72 | +
|
| 73 | +const entries = computed(() => { |
| 74 | + const normalized = normalizeIdentifyMethodsPolicy(props.modelValue) |
| 75 | + return ensureSignatureMethodSelection(normalized) |
| 76 | +}) |
| 77 | +
|
| 78 | +function onMethodToggle(index: number, enabled: boolean): void { |
| 79 | + const nextEntries = [...entries.value] |
| 80 | + nextEntries[index] = { |
| 81 | + ...nextEntries[index], |
| 82 | + enabled, |
| 83 | + } |
| 84 | + emit('update:modelValue', serializeIdentifyMethodsPolicy(ensureSignatureMethodSelection(nextEntries).filter((entry) => entry.enabled))) |
| 85 | +} |
| 86 | +
|
| 87 | +function onCanCreateAccountToggle(index: number, canCreateAccount: boolean): void { |
| 88 | + const nextEntries = [...entries.value] |
| 89 | + nextEntries[index] = { |
| 90 | + ...nextEntries[index], |
| 91 | + can_create_account: canCreateAccount, |
| 92 | + } |
| 93 | + emit('update:modelValue', serializeIdentifyMethodsPolicy(ensureSignatureMethodSelection(nextEntries).filter((entry) => entry.enabled))) |
| 94 | +} |
| 95 | +
|
| 96 | +function onSignatureMethodChange(index: number, signatureMethodName: string): void { |
| 97 | + const nextEntries = [...entries.value] |
| 98 | + nextEntries[index] = { |
| 99 | + ...nextEntries[index], |
| 100 | + signatureMethodEnabled: signatureMethodName, |
| 101 | + } |
| 102 | + emit('update:modelValue', serializeIdentifyMethodsPolicy(ensureSignatureMethodSelection(nextEntries).filter((entry) => entry.enabled))) |
| 103 | +} |
| 104 | +
|
| 105 | +function ensureSignatureMethodSelection(entries: IdentifyMethodPolicyEntry[]): IdentifyMethodPolicyEntry[] { |
| 106 | + return entries.map((entry) => { |
| 107 | + const signatureMethodNames = Object.keys(entry.signatureMethods) |
| 108 | + if (signatureMethodNames.length === 0) { |
| 109 | + return { |
| 110 | + ...entry, |
| 111 | + signatureMethodEnabled: undefined, |
| 112 | + } |
| 113 | + } |
| 114 | +
|
| 115 | + let selectedSignatureMethod = entry.signatureMethodEnabled |
| 116 | + if (!selectedSignatureMethod || !signatureMethodNames.includes(selectedSignatureMethod)) { |
| 117 | + selectedSignatureMethod = signatureMethodNames.find((signatureMethodName) => entry.signatureMethods[signatureMethodName]?.enabled) |
| 118 | + ?? signatureMethodNames[0] |
| 119 | + } |
| 120 | +
|
| 121 | + const signatureMethods = Object.fromEntries( |
| 122 | + signatureMethodNames.map((signatureMethodName) => [ |
| 123 | + signatureMethodName, |
| 124 | + { |
| 125 | + ...entry.signatureMethods[signatureMethodName], |
| 126 | + enabled: signatureMethodName === selectedSignatureMethod, |
| 127 | + }, |
| 128 | + ]), |
| 129 | + ) |
| 130 | +
|
| 131 | + return { |
| 132 | + ...entry, |
| 133 | + signatureMethods, |
| 134 | + signatureMethodEnabled: selectedSignatureMethod, |
| 135 | + } |
| 136 | + }) |
| 137 | +} |
| 138 | +</script> |
| 139 | + |
| 140 | +<style scoped lang="scss"> |
| 141 | +.identify-methods-editor { |
| 142 | + display: flex; |
| 143 | + flex-direction: column; |
| 144 | + gap: 0.5rem; |
| 145 | +} |
| 146 | +
|
| 147 | +.identify-methods-editor__empty { |
| 148 | + color: var(--color-text-maxcontrast); |
| 149 | + font-size: 0.9rem; |
| 150 | +} |
| 151 | +
|
| 152 | +.identify-methods-editor__method { |
| 153 | + display: flex; |
| 154 | + flex-direction: column; |
| 155 | + gap: 0.5rem; |
| 156 | +} |
| 157 | +
|
| 158 | +.identify-methods-editor__method-details { |
| 159 | + display: flex; |
| 160 | + flex-direction: column; |
| 161 | + gap: 0.5rem; |
| 162 | +} |
| 163 | +
|
| 164 | +.identify-methods-editor__sub-section { |
| 165 | + display: flex; |
| 166 | + flex-direction: column; |
| 167 | + gap: 0.25rem; |
| 168 | + border: 0; |
| 169 | + margin: 0 0 0 1.5rem; |
| 170 | + padding: 0; |
| 171 | +} |
| 172 | +
|
| 173 | +.identify-methods-editor__sub-section legend { |
| 174 | + padding: 0; |
| 175 | + margin-bottom: 0.25rem; |
| 176 | + font-weight: 600; |
| 177 | +} |
| 178 | +</style> |
0 commit comments