Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Libresign\Service\Envelope\EnvelopeService;
use OCA\Libresign\Service\Policy\PolicyService;
use OCA\Libresign\Service\Policy\Provider\Confetti\ConfettiPolicy;
use OCA\Libresign\Service\Policy\Provider\DrawInkColor\DrawInkColorPolicy;
use OCA\Libresign\Service\SignatureTextService;
use OCA\Libresign\Service\SignerElementsService;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function getCapabilities(): array {
'full-signature-height' => $this->signatureTextService->getFullSignatureHeight(),
'signature-width' => $this->signatureTextService->getSignatureWidth(),
'signature-height' => $this->signatureTextService->getSignatureHeight(),
'draw-ink-color' => (string)$this->policyService->resolve(DrawInkColorPolicy::KEY)->getEffectiveValue(),
],
'envelope' => [
'is-available' => $this->envelopeService->isEnabled(),
Expand Down
1 change: 1 addition & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,7 @@
* full-signature-height: float,
* signature-width: float,
* signature-height: float,
* draw-ink-color: string,
* },
* envelope: array{
* is-available: bool,
Expand Down
56 changes: 56 additions & 0 deletions lib/Service/Policy/Provider/DrawInkColor/DrawInkColorPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Service\Policy\Provider\DrawInkColor;

use OCA\Libresign\Service\Policy\Contract\IPolicyDefinition;
use OCA\Libresign\Service\Policy\Contract\IPolicyDefinitionProvider;
use OCA\Libresign\Service\Policy\Model\PolicySpec;
use OCA\Libresign\Service\Policy\Provider\Helper\PolicyKeyNormalizer;

/**
* Initial ink color of the draw signature pad. The signer can still
* choose another color in the pad, so only the system scope is supported.
*/
final class DrawInkColorPolicy implements IPolicyDefinitionProvider {
public const KEY = 'draw_ink_color';
public const SYSTEM_APP_CONFIG_KEY = self::KEY;
public const DEFAULT_COLOR = '#000000';
private const COLOR_PATTERN = '/^#[0-9a-f]{6}$/';

#[\Override]
public function keys(): array {
return [
self::KEY,
];
}

#[\Override]
public function get(string|\BackedEnum $policyKey): IPolicyDefinition {
return match (PolicyKeyNormalizer::normalize($policyKey)) {
self::KEY => new PolicySpec(
key: self::KEY,
defaultSystemValue: self::DEFAULT_COLOR,
allowedValues: [],
normalizer: static fn (mixed $rawValue): string => strtolower(trim((string)$rawValue)),
validator: static function (mixed $value): void {
if (!is_string($value) || preg_match(self::COLOR_PATTERN, $value) !== 1) {
throw new \InvalidArgumentException('Invalid value for ' . self::KEY);
}
},
appConfigKey: self::SYSTEM_APP_CONFIG_KEY,
supportsUserPreference: false,
supportsRequestOverride: false,
requestLifecycle: PolicySpec::LIFECYCLE_RUNTIME,
supportedScopes: [PolicySpec::SCOPE_SYSTEM],
supportsGroupAdminDelegation: false,
),
default => throw new \InvalidArgumentException('Unknown policy key: ' . PolicyKeyNormalizer::normalize($policyKey)),
};
}
}
2 changes: 2 additions & 0 deletions lib/Service/Policy/Provider/PolicyProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Libresign\Service\Policy\Provider\CrlValidation\CrlValidationPolicy;
use OCA\Libresign\Service\Policy\Provider\DefaultUserFolder\DefaultUserFolderPolicy;
use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy;
use OCA\Libresign\Service\Policy\Provider\DrawInkColor\DrawInkColorPolicy;
use OCA\Libresign\Service\Policy\Provider\EmailSignerSignature\EmailSignerSignaturePolicy;
use OCA\Libresign\Service\Policy\Provider\Envelope\EnvelopePolicy;
use OCA\Libresign\Service\Policy\Provider\ExpirationRules\ExpirationRulesPolicy;
Expand Down Expand Up @@ -46,6 +47,7 @@ final class PolicyProviders {
CrlValidationPolicy::KEY => CrlValidationPolicy::class,
FooterPolicy::KEY => FooterPolicy::class,
DocMdpPolicy::KEY => DocMdpPolicy::class,
DrawInkColorPolicy::KEY => DrawInkColorPolicy::class,
EmailSignerSignaturePolicy::KEY => EmailSignerSignaturePolicy::class,
EnvelopePolicy::KEY => EnvelopePolicy::class,
ExpirationRulesPolicy::KEY_MAXIMUM_VALIDITY => ExpirationRulesPolicy::class,
Expand Down
6 changes: 5 additions & 1 deletion openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"full-signature-width",
"full-signature-height",
"signature-width",
"signature-height"
"signature-height",
"draw-ink-color"
],
"properties": {
"is-available": {
Expand All @@ -144,6 +145,9 @@
"signature-height": {
"type": "number",
"format": "double"
},
"draw-ink-color": {
"type": "string"
}
}
},
Expand Down
6 changes: 5 additions & 1 deletion openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@
"full-signature-width",
"full-signature-height",
"signature-width",
"signature-height"
"signature-height",
"draw-ink-color"
],
"properties": {
"is-available": {
Expand All @@ -278,6 +279,9 @@
"signature-height": {
"type": "number",
"format": "double"
},
"draw-ink-color": {
"type": "string"
}
}
},
Expand Down
6 changes: 5 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@
"full-signature-width",
"full-signature-height",
"signature-width",
"signature-height"
"signature-height",
"draw-ink-color"
],
"properties": {
"is-available": {
Expand All @@ -231,6 +232,9 @@
"signature-height": {
"type": "number",
"format": "double"
},
"draw-ink-color": {
"type": "string"
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/Draw/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ const capabilities = getCapabilities() as LibresignCapabilities
const signElementsConfig = capabilities.libresign?.config['sign-elements']
const canvasWidth = Number(signElementsConfig?.['signature-width'])
const canvasHeight = Number(signElementsConfig?.['signature-height'])
const color = ref('#000000')
const defaultInkColor = signElementsConfig?.['draw-ink-color'] || '#000000'
const color = ref(defaultInkColor)
const customPalette = [
'#000000',
'#ff0000',
Expand Down Expand Up @@ -196,7 +197,7 @@ onMounted(() => {
if (!canvas.value) {
return
}
canvas.value.signaturePad = new SignaturePad(canvas.value)
canvas.value.signaturePad = new SignaturePad(canvas.value, { penColor: color.value })
canvas.value.signaturePad.addEventListener('endStroke', () => {
canSave.value = !canvas.value?.signaturePad?.isEmpty()
})
Expand Down
30 changes: 30 additions & 0 deletions src/tests/components/Draw/Editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ describe('Editor.vue - Drawing Signature Editor', () => {
expect(wrapper.vm.color).toBe('#000000')
})

it('starts with the ink color configured by the administrator', async () => {
const { getCapabilities } = await import('@nextcloud/capabilities')
vi.mocked(getCapabilities).mockReturnValueOnce({
libresign: {
config: {
'sign-elements': {
'signature-width': 700,
'signature-height': 200,
'draw-ink-color': '#1f2a5a',
},
},
},
})
vi.resetModules()
const { default: FreshEditor } = await import('../../../components/Draw/Editor.vue')
const { default: FreshSignaturePad } = await import('signature_pad')
const wrapper = mount(FreshEditor, {
global: {
mocks: {
t,
},
},
})
await wrapper.vm.$nextTick()
await wrapper.vm.$nextTick()

expect(wrapper.vm.color).toBe('#1f2a5a')
expect(FreshSignaturePad).toHaveBeenCalledWith(expect.anything(), { penColor: '#1f2a5a' })
})

it('has predefined color palette', () => {
const wrapper = mount(Editor, {
global: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: 2026 LibreSign contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { mount } from '@vue/test-utils'
import { describe, expect, it, vi } from 'vitest'

import { createL10nMock } from '../../../../../testHelpers/l10n.js'
import DrawInkColorRuleEditor from '../../../../../../views/Settings/PolicyWorkbench/settings/draw-ink-color/DrawInkColorRuleEditor.vue'

vi.mock('@nextcloud/l10n', () => createL10nMock())

const colorPickerStub = {
props: ['modelValue'],
template: '<div class="picker-stub" :data-value="modelValue"><button class="picker-pick" @click="$emit(\'update:modelValue\', \'#1F2A5A\')" /><slot /></div>',
}

function mountEditor(modelValue: string | null) {
return mount(DrawInkColorRuleEditor, {
props: { modelValue },
global: { stubs: { NcColorPicker: colorPickerStub } },
})
}

describe('DrawInkColorRuleEditor.vue', () => {
it('shows the current color', () => {
const wrapper = mountEditor('#1f2a5a')

expect(wrapper.text()).toContain('Default ink color for drawn signatures')
expect(wrapper.find('.picker-stub').attributes('data-value')).toBe('#1f2a5a')
})

it('falls back to black for an invalid value', () => {
const wrapper = mountEditor('unexpected')

expect(wrapper.find('.picker-stub').attributes('data-value')).toBe('#000000')
})

it('emits the picked color normalized to lowercase', async () => {
const wrapper = mountEditor('#000000')

await wrapper.find('.picker-pick').trigger('click')

expect(wrapper.emitted('update:modelValue')?.[0]?.[0]).toBe('#1f2a5a')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-FileCopyrightText: 2026 LibreSign contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, it, vi } from 'vitest'

import { createL10nMock } from '../../../../../testHelpers/l10n.js'
import { drawInkColorRealDefinition } from '../../../../../../views/Settings/PolicyWorkbench/settings/draw-ink-color/realDefinition'
import { resolveDrawInkColor } from '../../../../../../views/Settings/PolicyWorkbench/settings/draw-ink-color/model'

vi.mock('@nextcloud/l10n', () => createL10nMock())

describe('drawInkColorRealDefinition', () => {
it('is a system-only setting that defaults to black', () => {
expect(drawInkColorRealDefinition.key).toBe('draw_ink_color')
expect(drawInkColorRealDefinition.supportedScopes).toEqual(['system'])
expect(drawInkColorRealDefinition.createEmptyValue()).toBe('#000000')
expect(drawInkColorRealDefinition.getFallbackSystemDefault(null, null)).toBe('#000000')
expect(drawInkColorRealDefinition.normalizeAllowChildOverride('system', true)).toBe(false)
expect(drawInkColorRealDefinition.groupAdminBehavior?.canRenderPolicy?.(null)).toBe(false)
})

it('keeps the stored system value as fallback default', () => {
expect(drawInkColorRealDefinition.getFallbackSystemDefault('#1f2a5a', 'system')).toBe('#1f2a5a')
expect(drawInkColorRealDefinition.getFallbackSystemDefault('#1f2a5a', 'group')).toBe('#000000')
})

it.each([
['#1F2A5A', '#1f2a5a'],
['#000000', '#000000'],
['red', 'Not configured'],
[true, 'Not configured'],
])('summarizes %s as %s', (value, summary) => {
expect(drawInkColorRealDefinition.summarizeValue(value)).toBe(summary)
})

it('normalizes draft values to a hex color', () => {
expect(drawInkColorRealDefinition.normalizeDraftValue(' #ABCDEF ')).toBe('#abcdef')
expect(drawInkColorRealDefinition.normalizeDraftValue('unexpected')).toBe('#000000')
expect(drawInkColorRealDefinition.hasSelectableDraftValue('unexpected')).toBe(false)
})
})

describe('resolveDrawInkColor', () => {
it.each([
['#1f2a5a', '#1f2a5a'],
[' #1F2A5A ', '#1f2a5a'],
['#fff', null],
['1f2a5a', null],
['#12345g', null],
[1, null],
[null, null],
])('resolves %s to %s', (value, expected) => {
expect(resolveDrawInkColor(value)).toBe(expected)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const expectedTopLevelKeys = [
'signature_stamp',
'show_confetti_after_signing',
'persist_email_signer_signatures',
'draw_ink_color',
'collect_metadata',
'signer_device_geolocation',
'signer_ip_geolocation',
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi-administration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ export type components = {
"signature-width": number;
/** Format: double */
"signature-height": number;
"draw-ink-color": string;
};
envelope: {
"is-available": boolean;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,7 @@ export type components = {
"signature-width": number;
/** Format: double */
"signature-height": number;
"draw-ink-color": string;
};
envelope: {
"is-available": boolean;
Expand Down
1 change: 1 addition & 0 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ export type components = {
"signature-width": number;
/** Format: double */
"signature-height": number;
"draw-ink-color": string;
};
envelope: {
"is-available": boolean;
Expand Down
Loading