-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001_CREATE_USER_TABLE.sql
57 lines (51 loc) · 1.4 KB
/
001_CREATE_USER_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
CREATE TABLE users (
-- UUID PRIMARY KEY REFERENCES auth.users ON DELETE CASCADE,
id SERIAL PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email TEXT UNIQUE NOT NULL, -- REFERENCES auth.users(email) ON DELETE CASCADE <- supabase doesn't support this
grad_year INT NULL,
picture VARCHAR(255) NULL, -- server should generate a default pfp
is_faculty BOOLEAN DEFAULT false NOT NULL,
active BOOLEAN DEFAULT true NOT 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 users
for each row execute procedure moddatetime (updated_at);
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- POLICIES
CREATE POLICY "Enable read access to authenticated users only"
ON public.users
FOR SELECT
TO authenticated
USING (true);
CREATE OR REPLACE FUNCTION public.is_admin(u_id INT)
RETURNS BOOLEAN
LANGUAGE plpgsql
SECURITY definer
SET search_path = public
stable
AS $$
BEGIN
PERFORM
FROM public.permissions
WHERE (
user_id = u_id
AND permission = 'ADMIN'
);
RETURN FOUND;
END;
$$;
CREATE POLICY "Enable all access to site admins"
ON public.users
FOR ALL
TO authenticated
USING (
public.is_admin(id)
)
WITH CHECK (
public.is_admin(id)
);