Repository navigation
Expand file tree
/
Copy pathadmin_hierarchy.sql
More file actions
456 lines (420 loc) · 15.8 KB
/
Copy pathadmin_hierarchy.sql
File metadata and controls
456 lines (420 loc) · 15.8 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
-- ============================================
-- SEVASETU ADMIN HIERARCHY SYSTEM
-- ============================================
-- Hierarchy: System Admin > State Admin > Department Admin > Office Admin > Worker
-- Each level can manage the level below them
-- ============================================
-- SECTION 1: Create admin_roles table
CREATE TABLE IF NOT EXISTS admin_roles (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
role TEXT NOT NULL CHECK (role IN ('system_admin', 'state_admin', 'department_admin', 'office_admin', 'worker')),
-- Scope of authority
state TEXT, -- For state_admin and below
department TEXT, -- For department_admin and below
office_id UUID REFERENCES offices(id) ON DELETE SET NULL, -- For office_admin and worker
-- Who created this role
created_by UUID REFERENCES auth.users(id),
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW(),
is_active BOOLEAN DEFAULT TRUE,
-- Ensure unique role per scope
UNIQUE(user_id, role, state, department, office_id)
);
-- SECTION 2: Create indexes
CREATE INDEX IF NOT EXISTS idx_admin_roles_user ON admin_roles(user_id);
CREATE INDEX IF NOT EXISTS idx_admin_roles_role ON admin_roles(role);
CREATE INDEX IF NOT EXISTS idx_admin_roles_state ON admin_roles(state);
CREATE INDEX IF NOT EXISTS idx_admin_roles_department ON admin_roles(department);
CREATE INDEX IF NOT EXISTS idx_admin_roles_office ON admin_roles(office_id);
CREATE INDEX IF NOT EXISTS idx_admin_roles_created_by ON admin_roles(created_by);
-- SECTION 3: Enable RLS
ALTER TABLE admin_roles ENABLE ROW LEVEL SECURITY;
-- SECTION 4: RLS Policies for admin_roles
-- System admins can see all roles
DROP POLICY IF EXISTS "system_admin_full_access" ON admin_roles;
CREATE POLICY "system_admin_full_access" ON admin_roles
FOR ALL TO authenticated
USING (
EXISTS (
SELECT 1 FROM admin_roles ar
WHERE ar.user_id = auth.uid()
AND ar.role = 'system_admin'
AND ar.is_active = TRUE
)
);
-- Users can see their own roles
DROP POLICY IF EXISTS "users_view_own_roles" ON admin_roles;
CREATE POLICY "users_view_own_roles" ON admin_roles
FOR SELECT TO authenticated
USING (user_id = auth.uid());
-- State admins can see roles in their state
DROP POLICY IF EXISTS "state_admin_view_state_roles" ON admin_roles;
CREATE POLICY "state_admin_view_state_roles" ON admin_roles
FOR SELECT TO authenticated
USING (
EXISTS (
SELECT 1 FROM admin_roles ar
WHERE ar.user_id = auth.uid()
AND ar.role = 'state_admin'
AND ar.state = admin_roles.state
AND ar.is_active = TRUE
)
);
-- SECTION 5: Function to check if user has a specific role
CREATE OR REPLACE FUNCTION has_role(p_user_id UUID, p_role TEXT)
RETURNS BOOLEAN AS $$
BEGIN
RETURN EXISTS (
SELECT 1 FROM admin_roles
WHERE user_id = p_user_id
AND role = p_role
AND is_active = TRUE
);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 6: Function to check if user can manage another user
CREATE OR REPLACE FUNCTION can_manage_user(p_manager_id UUID, p_target_user_id UUID)
RETURNS BOOLEAN AS $$
DECLARE
manager_role TEXT;
manager_state TEXT;
manager_dept TEXT;
manager_office UUID;
target_role TEXT;
target_state TEXT;
target_dept TEXT;
target_office UUID;
BEGIN
-- Get manager's highest role
SELECT role, state, department, office_id
INTO manager_role, manager_state, manager_dept, manager_office
FROM admin_roles
WHERE user_id = p_manager_id AND is_active = TRUE
ORDER BY
CASE role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
WHEN 'worker' THEN 5
END
LIMIT 1;
-- Get target's role
SELECT role, state, department, office_id
INTO target_role, target_state, target_dept, target_office
FROM admin_roles
WHERE user_id = p_target_user_id AND is_active = TRUE
ORDER BY
CASE role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
WHEN 'worker' THEN 5
END
LIMIT 1;
-- System admin can manage everyone except other system admins
IF manager_role = 'system_admin' THEN
RETURN target_role != 'system_admin' OR target_role IS NULL;
END IF;
-- State admin can manage department_admin, office_admin, worker in their state
IF manager_role = 'state_admin' THEN
RETURN target_role IN ('department_admin', 'office_admin', 'worker')
AND target_state = manager_state;
END IF;
-- Department admin can manage office_admin, worker in their department
IF manager_role = 'department_admin' THEN
RETURN target_role IN ('office_admin', 'worker')
AND target_state = manager_state
AND target_dept = manager_dept;
END IF;
-- Office admin can manage workers in their office
IF manager_role = 'office_admin' THEN
RETURN target_role = 'worker'
AND target_office = manager_office;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 7: Function to get user's role info
CREATE OR REPLACE FUNCTION get_user_role(p_user_id UUID)
RETURNS TABLE (
role TEXT,
state TEXT,
department TEXT,
office_id UUID,
office_name TEXT,
is_active BOOLEAN
) AS $$
BEGIN
RETURN QUERY
SELECT
ar.role,
ar.state,
ar.department,
ar.office_id,
o.name as office_name,
ar.is_active
FROM admin_roles ar
LEFT JOIN offices o ON ar.office_id = o.id
WHERE ar.user_id = p_user_id AND ar.is_active = TRUE
ORDER BY
CASE ar.role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
WHEN 'worker' THEN 5
END
LIMIT 1;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 8: Function to add a new admin (with permission check)
CREATE OR REPLACE FUNCTION add_admin_role(
p_target_user_id UUID,
p_role TEXT,
p_state TEXT DEFAULT NULL,
p_department TEXT DEFAULT NULL,
p_office_id UUID DEFAULT NULL
)
RETURNS UUID AS $$
DECLARE
caller_id UUID;
caller_role TEXT;
caller_state TEXT;
caller_dept TEXT;
new_role_id UUID;
BEGIN
caller_id := auth.uid();
-- Get caller's role
SELECT role, state, department INTO caller_role, caller_state, caller_dept
FROM admin_roles
WHERE user_id = caller_id AND is_active = TRUE
ORDER BY
CASE role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
END
LIMIT 1;
-- Permission checks
IF caller_role IS NULL THEN
RAISE EXCEPTION 'You do not have permission to add admin roles';
END IF;
-- System admin can add state_admin
IF p_role = 'state_admin' AND caller_role != 'system_admin' THEN
RAISE EXCEPTION 'Only system admin can add state admins';
END IF;
-- State admin can add department_admin in their state
IF p_role = 'department_admin' THEN
IF caller_role NOT IN ('system_admin', 'state_admin') THEN
RAISE EXCEPTION 'Only system/state admin can add department admins';
END IF;
IF caller_role = 'state_admin' AND caller_state != p_state THEN
RAISE EXCEPTION 'You can only add department admins in your state';
END IF;
END IF;
-- Department admin can add office_admin in their department
IF p_role = 'office_admin' THEN
IF caller_role NOT IN ('system_admin', 'state_admin', 'department_admin') THEN
RAISE EXCEPTION 'You do not have permission to add office admins';
END IF;
IF caller_role = 'department_admin' AND (caller_state != p_state OR caller_dept != p_department) THEN
RAISE EXCEPTION 'You can only add office admins in your department';
END IF;
END IF;
-- Office admin can add workers in their office
IF p_role = 'worker' THEN
IF caller_role NOT IN ('system_admin', 'state_admin', 'department_admin', 'office_admin') THEN
RAISE EXCEPTION 'You do not have permission to add workers';
END IF;
END IF;
-- Insert the new role
INSERT INTO admin_roles (user_id, role, state, department, office_id, created_by)
VALUES (p_target_user_id, p_role, p_state, p_department, p_office_id, caller_id)
RETURNING id INTO new_role_id;
RETURN new_role_id;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 9: Function to remove admin role (with permission check)
CREATE OR REPLACE FUNCTION remove_admin_role(p_role_id UUID)
RETURNS BOOLEAN AS $$
DECLARE
caller_id UUID;
target_user_id UUID;
BEGIN
caller_id := auth.uid();
SELECT user_id INTO target_user_id FROM admin_roles WHERE id = p_role_id;
IF NOT can_manage_user(caller_id, target_user_id) THEN
RAISE EXCEPTION 'You do not have permission to remove this role';
END IF;
UPDATE admin_roles SET is_active = FALSE, updated_at = NOW() WHERE id = p_role_id;
RETURN TRUE;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 10: Function to get users manageable by current user
CREATE OR REPLACE FUNCTION get_manageable_users()
RETURNS TABLE (
user_id UUID,
email TEXT,
role TEXT,
state TEXT,
department TEXT,
office_name TEXT,
created_at TIMESTAMP
) AS $$
DECLARE
caller_id UUID;
caller_role TEXT;
caller_state TEXT;
caller_dept TEXT;
caller_office UUID;
BEGIN
caller_id := auth.uid();
SELECT ar.role, ar.state, ar.department, ar.office_id
INTO caller_role, caller_state, caller_dept, caller_office
FROM admin_roles ar
WHERE ar.user_id = caller_id AND ar.is_active = TRUE
ORDER BY
CASE ar.role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
END
LIMIT 1;
IF caller_role = 'system_admin' THEN
RETURN QUERY
SELECT ar.user_id, u.email, ar.role, ar.state, ar.department, o.name, ar.created_at
FROM admin_roles ar
JOIN auth.users u ON ar.user_id = u.id
LEFT JOIN offices o ON ar.office_id = o.id
WHERE ar.is_active = TRUE AND ar.role != 'system_admin';
ELSIF caller_role = 'state_admin' THEN
RETURN QUERY
SELECT ar.user_id, u.email, ar.role, ar.state, ar.department, o.name, ar.created_at
FROM admin_roles ar
JOIN auth.users u ON ar.user_id = u.id
LEFT JOIN offices o ON ar.office_id = o.id
WHERE ar.is_active = TRUE
AND ar.state = caller_state
AND ar.role IN ('department_admin', 'office_admin', 'worker');
ELSIF caller_role = 'department_admin' THEN
RETURN QUERY
SELECT ar.user_id, u.email, ar.role, ar.state, ar.department, o.name, ar.created_at
FROM admin_roles ar
JOIN auth.users u ON ar.user_id = u.id
LEFT JOIN offices o ON ar.office_id = o.id
WHERE ar.is_active = TRUE
AND ar.state = caller_state
AND ar.department = caller_dept
AND ar.role IN ('office_admin', 'worker');
ELSIF caller_role = 'office_admin' THEN
RETURN QUERY
SELECT ar.user_id, u.email, ar.role, ar.state, ar.department, o.name, ar.created_at
FROM admin_roles ar
JOIN auth.users u ON ar.user_id = u.id
LEFT JOIN offices o ON ar.office_id = o.id
WHERE ar.is_active = TRUE
AND ar.office_id = caller_office
AND ar.role = 'worker';
END IF;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 11: Function to get issues for admin based on their scope
CREATE OR REPLACE FUNCTION get_admin_issues(
p_status TEXT DEFAULT NULL,
p_limit INTEGER DEFAULT 50,
p_offset INTEGER DEFAULT 0
)
RETURNS TABLE (
id UUID,
category TEXT,
description TEXT,
address TEXT,
status TEXT,
priority_score INTEGER,
is_high_priority BOOLEAN,
created_at TIMESTAMP,
assigned_department TEXT,
assigned_office_name TEXT
) AS $$
DECLARE
caller_id UUID;
caller_role TEXT;
caller_state TEXT;
caller_dept TEXT;
caller_office UUID;
BEGIN
caller_id := auth.uid();
SELECT ar.role, ar.state, ar.department, ar.office_id
INTO caller_role, caller_state, caller_dept, caller_office
FROM admin_roles ar
WHERE ar.user_id = caller_id AND ar.is_active = TRUE
ORDER BY
CASE ar.role
WHEN 'system_admin' THEN 1
WHEN 'state_admin' THEN 2
WHEN 'department_admin' THEN 3
WHEN 'office_admin' THEN 4
WHEN 'worker' THEN 5
END
LIMIT 1;
IF caller_role = 'system_admin' THEN
RETURN QUERY
SELECT ci.id, ci.category, ci.description, ci.address, ci.status,
ci.priority_score, ci.is_high_priority, ci.created_at,
ci.assigned_department, o.name
FROM civic_issues ci
LEFT JOIN offices o ON ci.assigned_office_id = o.id
WHERE (p_status IS NULL OR ci.status = p_status)
ORDER BY ci.priority_score DESC, ci.created_at DESC
LIMIT p_limit OFFSET p_offset;
ELSIF caller_role = 'state_admin' THEN
RETURN QUERY
SELECT ci.id, ci.category, ci.description, ci.address, ci.status,
ci.priority_score, ci.is_high_priority, ci.created_at,
ci.assigned_department, o.name
FROM civic_issues ci
LEFT JOIN offices o ON ci.assigned_office_id = o.id
WHERE ci.state = caller_state
AND (p_status IS NULL OR ci.status = p_status)
ORDER BY ci.priority_score DESC, ci.created_at DESC
LIMIT p_limit OFFSET p_offset;
ELSIF caller_role = 'department_admin' THEN
RETURN QUERY
SELECT ci.id, ci.category, ci.description, ci.address, ci.status,
ci.priority_score, ci.is_high_priority, ci.created_at,
ci.assigned_department, o.name
FROM civic_issues ci
LEFT JOIN offices o ON ci.assigned_office_id = o.id
WHERE ci.state = caller_state
AND ci.assigned_department = caller_dept
AND (p_status IS NULL OR ci.status = p_status)
ORDER BY ci.priority_score DESC, ci.created_at DESC
LIMIT p_limit OFFSET p_offset;
ELSIF caller_role IN ('office_admin', 'worker') THEN
RETURN QUERY
SELECT ci.id, ci.category, ci.description, ci.address, ci.status,
ci.priority_score, ci.is_high_priority, ci.created_at,
ci.assigned_department, o.name
FROM civic_issues ci
LEFT JOIN offices o ON ci.assigned_office_id = o.id
WHERE ci.assigned_office_id = caller_office
AND (p_status IS NULL OR ci.status = p_status)
ORDER BY ci.priority_score DESC, ci.created_at DESC
LIMIT p_limit OFFSET p_offset;
END IF;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- SECTION 12: Add comments
COMMENT ON TABLE admin_roles IS 'Hierarchical admin roles: system_admin > state_admin > department_admin > office_admin > worker';
COMMENT ON FUNCTION has_role IS 'Check if a user has a specific role';
COMMENT ON FUNCTION can_manage_user IS 'Check if one user can manage another based on hierarchy';
COMMENT ON FUNCTION add_admin_role IS 'Add a new admin role with permission checks';
COMMENT ON FUNCTION get_admin_issues IS 'Get issues visible to the current admin based on their scope';
-- ============================================
-- ADMIN HIERARCHY MIGRATION COMPLETE!
-- ============================================