forked from fahrPool/fahrPool
fix: Unterschrift-Canvas mobil-korrekt skalieren, Leer-Upload verhindern, Fahrer-UPDATE-Policy
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dimk8Jo64b4wULp2yAzSNh
This commit is contained in:
parent
5c7b289cb9
commit
93fbfc3fb0
|
|
@ -39,6 +39,35 @@ create policy "Fahrer laedt Unterschrift bei Fahrtende hoch"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- fahrtBeenden laedt mit { upsert: true } hoch: der erste Upload pro Auftrag
|
||||||
|
-- ist ein INSERT (Policy oben), ein erneuter Absende-Versuch trifft dasselbe
|
||||||
|
-- Objekt und wird zum UPDATE. Ohne diese Policy koennte der Fahrer seine
|
||||||
|
-- eigene Unterschrift nicht noch einmal absenden. Praedikat identisch zur
|
||||||
|
-- INSERT-Policy; fuer UPDATE braucht es sowohl using (...) als auch
|
||||||
|
-- with check (...). Name bewusst < 63 Byte (Postgres-Identifier-Limit).
|
||||||
|
create policy "Fahrer aktualisiert eigene Unterschrift bei Fahrtende"
|
||||||
|
on storage.objects for update
|
||||||
|
using (
|
||||||
|
bucket_id = 'auftrag-unterschriften'
|
||||||
|
and public.user_role() = 'fahrer'
|
||||||
|
and exists (
|
||||||
|
select 1 from public.auftraege
|
||||||
|
where id = (storage.foldername(name))[1]::uuid
|
||||||
|
and zugewiesen_an = auth.uid()
|
||||||
|
and status = 'unterwegs'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with check (
|
||||||
|
bucket_id = 'auftrag-unterschriften'
|
||||||
|
and public.user_role() = 'fahrer'
|
||||||
|
and exists (
|
||||||
|
select 1 from public.auftraege
|
||||||
|
where id = (storage.foldername(name))[1]::uuid
|
||||||
|
and zugewiesen_an = auth.uid()
|
||||||
|
and status = 'unterwegs'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
create policy "Admin aktualisiert Unterschriften im Storage"
|
create policy "Admin aktualisiert Unterschriften im Storage"
|
||||||
on storage.objects for update
|
on storage.objects for update
|
||||||
using (bucket_id = 'auftrag-unterschriften' and public.user_role() = 'admin');
|
using (bucket_id = 'auftrag-unterschriften' and public.user_role() = 'admin');
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
begin;
|
begin;
|
||||||
select plan(5);
|
select plan(6);
|
||||||
|
|
||||||
insert into auth.users (id, email) values
|
insert into auth.users (id, email) values
|
||||||
('aa555555-5555-5555-5555-555555555555', 'admin-unterschrift@auftraege.test.local'),
|
('aa555555-5555-5555-5555-555555555555', 'admin-unterschrift@auftraege.test.local'),
|
||||||
|
|
@ -101,7 +101,20 @@ select lives_ok(
|
||||||
'Fahrer kann eine Unterschrift fuer den eigenen Auftrag bei status = unterwegs hochladen'
|
'Fahrer kann eine Unterschrift fuer den eigenen Auftrag bei status = unterwegs hochladen'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- 4) INSERT-RLS: Fahrer "eins" kann fuer den eigenen Auftrag B (status =
|
-- 4) UPDATE-RLS: Fahrer "eins" darf die eben hochgeladene eigene Unterschrift
|
||||||
|
-- fuer Auftrag A (status = 'unterwegs') erneut ueberschreiben. fahrtBeenden
|
||||||
|
-- laedt mit { upsert: true } hoch -- ein erneuter Absende-Versuch wird zum
|
||||||
|
-- UPDATE auf dasselbe Objekt.
|
||||||
|
select lives_ok(
|
||||||
|
$$
|
||||||
|
update storage.objects set owner = owner
|
||||||
|
where bucket_id = 'auftrag-unterschriften'
|
||||||
|
and name = '21000000-0000-0000-0000-000000000001/unterschrift.png'
|
||||||
|
$$,
|
||||||
|
'Fahrer darf die eigene Unterschrift bei status = unterwegs erneut ueberschreiben (upsert)'
|
||||||
|
);
|
||||||
|
|
||||||
|
-- 5) INSERT-RLS: Fahrer "eins" kann fuer den eigenen Auftrag B (status =
|
||||||
-- 'zugewiesen', noch nicht gestartet) KEINE Unterschrift hochladen.
|
-- 'zugewiesen', noch nicht gestartet) KEINE Unterschrift hochladen.
|
||||||
select throws_ok(
|
select throws_ok(
|
||||||
$$
|
$$
|
||||||
|
|
@ -116,7 +129,7 @@ select throws_ok(
|
||||||
reset request.jwt.claims;
|
reset request.jwt.claims;
|
||||||
set local request.jwt.claims to '{"sub": "aa555555-5555-5555-5555-555555555555", "role": "authenticated"}';
|
set local request.jwt.claims to '{"sub": "aa555555-5555-5555-5555-555555555555", "role": "authenticated"}';
|
||||||
|
|
||||||
-- 5) SELECT-RLS: Admin sieht die Unterschrift eines fremden Auftrags C.
|
-- 6) SELECT-RLS: Admin sieht die Unterschrift eines fremden Auftrags C.
|
||||||
select isnt_empty(
|
select isnt_empty(
|
||||||
$$ select id from storage.objects where bucket_id = 'auftrag-unterschriften' and name like '21000000-0000-0000-0000-000000000003/%' $$,
|
$$ select id from storage.objects where bucket_id = 'auftrag-unterschriften' and name like '21000000-0000-0000-0000-000000000003/%' $$,
|
||||||
'Admin sieht Unterschriften aller Auftraege, auch nicht selbst zugewiesener'
|
'Admin sieht Unterschriften aller Auftraege, auch nicht selbst zugewiesener'
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,70 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useRef, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
export function UnterschriftCanvas({ name }: { name: string }) {
|
export function UnterschriftCanvas({ name }: { name: string }) {
|
||||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||||
const zeichnetRef = useRef(false);
|
const zeichnetRef = useRef(false);
|
||||||
|
// hatInhaltRef spiegelt hatInhalt synchron wider, damit onPointerUp
|
||||||
|
// (kein Re-Render dazwischen) zuverlaessig weiss, ob wirklich gezeichnet
|
||||||
|
// wurde.
|
||||||
|
const hatInhaltRef = useRef(false);
|
||||||
const [hatInhalt, setHatInhalt] = useState(false);
|
const [hatInhalt, setHatInhalt] = useState(false);
|
||||||
const [dataUrl, setDataUrl] = useState("");
|
const [dataUrl, setDataUrl] = useState("");
|
||||||
|
|
||||||
|
// Setzt die Bitmap-Aufloesung des Canvas auf die tatsaechlich dargestellte
|
||||||
|
// CSS-Groesse * devicePixelRatio und skaliert den 2D-Context so, dass in
|
||||||
|
// CSS-Pixeln gezeichnet werden kann. Ohne das rendert das Element (w-full)
|
||||||
|
// breiter als seine 320px-Bitmap: der Finger zeichnet versetzt und der
|
||||||
|
// rechte Teil der Unterschrift landet ausserhalb der Bitmap. Wird beim
|
||||||
|
// Mount und bei jedem Resize aufgerufen; vorhandene Zeichnung wird dabei
|
||||||
|
// skaliert uebernommen.
|
||||||
|
const canvasSkalieren = useCallback(() => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
const canvas = canvasRef.current;
|
||||||
|
if (!canvas) return;
|
||||||
|
|
||||||
|
const rect = canvas.getBoundingClientRect();
|
||||||
|
if (rect.width === 0 || rect.height === 0) return;
|
||||||
|
|
||||||
|
const dpr = window.devicePixelRatio || 1;
|
||||||
|
const zielBreite = Math.round(rect.width * dpr);
|
||||||
|
const zielHoehe = Math.round(rect.height * dpr);
|
||||||
|
if (canvas.width === zielBreite && canvas.height === zielHoehe) return;
|
||||||
|
|
||||||
|
const altBreite = canvas.width;
|
||||||
|
const altHoehe = canvas.height;
|
||||||
|
const altCtx = canvas.getContext("2d");
|
||||||
|
let vorschau: HTMLCanvasElement | null = null;
|
||||||
|
if (altCtx && altBreite > 0 && altHoehe > 0 && hatInhaltRef.current) {
|
||||||
|
vorschau = document.createElement("canvas");
|
||||||
|
vorschau.width = altBreite;
|
||||||
|
vorschau.height = altHoehe;
|
||||||
|
vorschau.getContext("2d")?.drawImage(canvas, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.width = zielBreite;
|
||||||
|
canvas.height = zielHoehe;
|
||||||
|
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
if (!ctx) return;
|
||||||
|
if (vorschau) {
|
||||||
|
// In Geraetepixeln zeichnen, bevor der Massstab gesetzt wird.
|
||||||
|
ctx.drawImage(vorschau, 0, 0, altBreite, altHoehe, 0, 0, zielBreite, zielHoehe);
|
||||||
|
}
|
||||||
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
||||||
|
ctx.lineWidth = 2;
|
||||||
|
ctx.lineCap = "round";
|
||||||
|
ctx.lineJoin = "round";
|
||||||
|
ctx.strokeStyle = "#1a1a1a";
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
canvasSkalieren();
|
||||||
|
window.addEventListener("resize", canvasSkalieren);
|
||||||
|
return () => window.removeEventListener("resize", canvasSkalieren);
|
||||||
|
}, [canvasSkalieren]);
|
||||||
|
|
||||||
function getPos(event: React.PointerEvent<HTMLCanvasElement>) {
|
function getPos(event: React.PointerEvent<HTMLCanvasElement>) {
|
||||||
const rect = event.currentTarget.getBoundingClientRect();
|
const rect = event.currentTarget.getBoundingClientRect();
|
||||||
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
return { x: event.clientX - rect.left, y: event.clientY - rect.top };
|
||||||
|
|
@ -32,14 +89,23 @@ export function UnterschriftCanvas({ name }: { name: string }) {
|
||||||
const { x, y } = getPos(event);
|
const { x, y } = getPos(event);
|
||||||
ctx.lineWidth = 2;
|
ctx.lineWidth = 2;
|
||||||
ctx.lineCap = "round";
|
ctx.lineCap = "round";
|
||||||
|
ctx.lineJoin = "round";
|
||||||
ctx.strokeStyle = "#1a1a1a";
|
ctx.strokeStyle = "#1a1a1a";
|
||||||
ctx.lineTo(x, y);
|
ctx.lineTo(x, y);
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
setHatInhalt(true);
|
if (!hatInhaltRef.current) {
|
||||||
|
hatInhaltRef.current = true;
|
||||||
|
setHatInhalt(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onPointerUp() {
|
function onPointerUp() {
|
||||||
zeichnetRef.current = false;
|
zeichnetRef.current = false;
|
||||||
|
// Nur wenn wirklich ein Strich gezeichnet wurde, wird das versteckte
|
||||||
|
// Feld mit einer Data-URL befuellt. Ein unberuehrtes Canvas oder ein
|
||||||
|
// versehentliches Antippen (Down + Up ohne Bewegung) laesst den Wert
|
||||||
|
// "" -> fahrtBeenden ueberspringt den Upload.
|
||||||
|
if (!hatInhaltRef.current) return;
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
if (canvas) {
|
if (canvas) {
|
||||||
setDataUrl(canvas.toDataURL("image/png"));
|
setDataUrl(canvas.toDataURL("image/png"));
|
||||||
|
|
@ -50,8 +116,12 @@ export function UnterschriftCanvas({ name }: { name: string }) {
|
||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
const ctx = canvas?.getContext("2d");
|
const ctx = canvas?.getContext("2d");
|
||||||
if (canvas && ctx) {
|
if (canvas && ctx) {
|
||||||
|
ctx.save();
|
||||||
|
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
ctx.restore();
|
||||||
}
|
}
|
||||||
|
hatInhaltRef.current = false;
|
||||||
setHatInhalt(false);
|
setHatInhalt(false);
|
||||||
setDataUrl("");
|
setDataUrl("");
|
||||||
}
|
}
|
||||||
|
|
@ -59,13 +129,15 @@ export function UnterschriftCanvas({ name }: { name: string }) {
|
||||||
return (
|
return (
|
||||||
<div className="mb-4">
|
<div className="mb-4">
|
||||||
<canvas
|
<canvas
|
||||||
|
id="unterschrift-canvas"
|
||||||
ref={canvasRef}
|
ref={canvasRef}
|
||||||
width={320}
|
width={320}
|
||||||
height={160}
|
height={160}
|
||||||
onPointerDown={onPointerDown}
|
onPointerDown={onPointerDown}
|
||||||
onPointerMove={onPointerMove}
|
onPointerMove={onPointerMove}
|
||||||
onPointerUp={onPointerUp}
|
onPointerUp={onPointerUp}
|
||||||
className="touch-none w-full rounded-md border border-aluminium-grey bg-white"
|
onPointerCancel={onPointerUp}
|
||||||
|
className="h-40 w-full touch-none rounded-md border border-aluminium-grey bg-white"
|
||||||
/>
|
/>
|
||||||
<input type="hidden" name={name} value={dataUrl} />
|
<input type="hidden" name={name} value={dataUrl} />
|
||||||
{hatInhalt && (
|
{hatInhalt && (
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue