-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004_CREATE_ROOMS_TABLE.sql
64 lines (57 loc) · 1.44 KB
/
004_CREATE_ROOMS_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
-- List of rooms that are available for use
CREATE TABLE rooms (
id SERIAL PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
floor INT NULL,
approval_required BOOLEAN DEFAULT false,
available_days VARCHAR(255) DEFAULT 'MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY',
comments TEXT NULL,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
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 rooms
for each row execute procedure moddatetime (updated_at);
ALTER TABLE rooms ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Enable read access to all authenticated users"
ON public.rooms
FOR SELECT
TO authenticated
USING (
EXISTS (
SELECT 1
FROM users AS u
WHERE (u.email = (auth.jwt() ->> 'email'))
)
);
CREATE POLICY "Enable all access to site admins"
ON public.rooms
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'
)
)
)
);