-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_staff_role_rls.sql
More file actions
39 lines (35 loc) · 1.54 KB
/
Copy pathmigration_staff_role_rls.sql
File metadata and controls
39 lines (35 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-- ============================================================
-- Migration: Fix staff role update (RLS + check constraint)
-- Run this in your Supabase SQL Editor (safe to re-run)
-- ============================================================
-- ── 1. Fix the CHECK constraint ───────────────────────────────
-- The existing constraint blocks 'manager' from being set.
-- Drop and recreate it to include all valid roles.
ALTER TABLE public.profiles
DROP CONSTRAINT IF EXISTS profiles_role_check;
ALTER TABLE public.profiles
ADD CONSTRAINT profiles_role_check
CHECK (role IN ('super_admin', 'company_admin', 'manager', 'staff', 'optician'));
-- ── 2. Fix the RLS UPDATE policy ─────────────────────────────
-- Drop the policy if it already exists so we can recreate cleanly
DROP POLICY IF EXISTS "company_admins_can_manage_staff" ON public.profiles;
-- Allow company admins (and managers) to UPDATE any profile
-- that belongs to the same company (enables role + location changes).
CREATE POLICY "company_admins_can_manage_staff"
ON public.profiles
FOR UPDATE
USING (
-- The row being updated must belong to the same company as the caller
company_id IN (
SELECT company_id FROM public.profiles
WHERE id = auth.uid()
AND role IN ('company_admin', 'manager')
)
)
WITH CHECK (
company_id IN (
SELECT company_id FROM public.profiles
WHERE id = auth.uid()
AND role IN ('company_admin', 'manager')
)
);