-
Notifications
You must be signed in to change notification settings - Fork 0
/
010_CREATE_STRIKES_TABLE.sql
66 lines (61 loc) · 1.61 KB
/
010_CREATE_STRIKES_TABLE.sql
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
CREATE TABLE strikes (
id SERIAL PRIMARY KEY,
organization_id INT NOT NULL,
admin_id INT NOT NULL, -- the person who handed out the strike
reason TEXT,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (organization_id) REFERENCES organizations(id) on delete cascade,
FOREIGN KEY (admin_id) REFERENCES users(id)
);
create extension if not exists moddatetime schema extensions;
-- trigger to update "updated_at" field before every update to row
create trigger handle_updated_at before update on strikes
for each row execute procedure moddatetime (updated_at);
ALTER TABLE strikes ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Enable read access to admins of organization"
ON public.strikes
FOR SELECT
TO authenticated
USING (
EXISTS (
SELECT 1
FROM users as u
INNER JOIN memberships as m ON (u.id = m.user_id)
WHERE (
u.email = (auth.jwt() ->> 'email')
AND m.organization_id = public.strikes.organization_id
AND (m.role = 'ADMIN' OR m.role = 'CREATOR')
)
)
);
CREATE POLICY "Enable all access to site admins"
ON public.strikes
FOR ALL
TO authenticated
USING (
EXISTS (
SELECT 1
FROM permissions as p
INNER JOIN users as u ON (p.user_id = u.id)
WHERE (
u.email = auth.jwt() ->> 'email'
AND ( -- roles here
p.permission = 'ADMIN'
)
)
)
)
WITH CHECK (
EXISTS (
SELECT 1
FROM permissions as p
INNER JOIN users as u ON (p.user_id = u.id)
WHERE (
u.email = auth.jwt() ->> 'email'
AND ( -- roles here
p.permission = 'ADMIN'
)
)
)
);