Skip to content

Commit 9622d35

Browse files
authored
Merge pull request #183 from supabase/rls
feat: broadcast changes based on RLS (Row-Level Security) policies
2 parents 0ad9dd2 + ccedf2b commit 9622d35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+6824
-289
lines changed

examples/next-js/package-lock.json

Lines changed: 3190 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/next-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"start": "next start"
88
},
99
"dependencies": {
10-
"@supabase/realtime-js": "^1.0.6",
10+
"@supabase/realtime-js": "^1.2.1",
1111
"axios": "^0.21.2",
1212
"chart.js": "^2.9.4",
1313
"dotenv": "^8.2.0",

rls_setup/docker-compose.db.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3'
2+
services:
3+
db:
4+
image: supabase/postgres
5+
ports:
6+
- 5432:5432
7+
volumes:
8+
- ./sql/setup.sql:/docker-entrypoint-initdb.d/setup.sql
9+
- ./sql/walrus--0.1.sql:/docker-entrypoint-initdb.d/walrus--0.1.sql
10+
command:
11+
- postgres
12+
- -c
13+
- wal_level=logical
14+
environment:
15+
POSTGRES_USER: postgres
16+
POSTGRES_PASSWORD: postgres

rls_setup/docker-compose.dev.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: '3'
2+
services:
3+
realtime:
4+
image: realtime/walrus
5+
build: .
6+
ports:
7+
- 4000:4000
8+
environment:
9+
DB_HOST: db
10+
DB_NAME: postgres
11+
DB_USER: postgres
12+
DB_PASSWORD: postgres
13+
DB_PORT: 5432
14+
PORT: 4000
15+
JWT_SECRET: SOMETHING_SUPER_SECRET
16+
SECURE_CHANNELS: 'true'
17+
depends_on:
18+
- db
19+
db:
20+
image: supabase/postgres
21+
ports:
22+
- 5432:5432
23+
volumes:
24+
- ./sql/setup.sql:/docker-entrypoint-initdb.d/setup.sql
25+
- ./sql/walrus--0.1.sql:/docker-entrypoint-initdb.d/walrus--0.1.sql
26+
command:
27+
- postgres
28+
- -c
29+
- wal_level=logical
30+
environment:
31+
POSTGRES_USER: postgres
32+
POSTGRES_PASSWORD: postgres

rls_setup/sql/setup.sql

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
-- Copied from https://github.com/supabase/walrus/blob/f54b2b1657c8348ce9e22c92092eaa50bfa8993a/sql/setup.sql
2+
3+
/*
4+
SETUP
5+
*/
6+
-- Set up reatime
7+
create publication supabase_realtime for all tables;
8+
9+
-- Extension namespacing
10+
create schema extensions;
11+
create extension if not exists "uuid-ossp" with schema extensions;
12+
13+
-- Developer roles
14+
create role anon nologin noinherit;
15+
create role authenticated nologin noinherit; -- "logged in" user: web_user, app_user, etc
16+
create role service_role nologin noinherit bypassrls; -- allow developers to create JWT's that bypass their policies
17+
18+
create user authenticator noinherit;
19+
grant anon to authenticator;
20+
grant authenticated to authenticator;
21+
grant service_role to authenticator;
22+
23+
grant usage on schema public to postgres, anon, authenticated, service_role;
24+
alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role;
25+
alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role;
26+
alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role;
27+
28+
CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION postgres;
29+
30+
-- auth.users definition
31+
CREATE TABLE auth.users (
32+
instance_id uuid NULL,
33+
id uuid NOT NULL,
34+
aud varchar(255) NULL,
35+
"role" varchar(255) NULL,
36+
email varchar(255) NULL,
37+
encrypted_password varchar(255) NULL,
38+
confirmed_at timestamptz NULL,
39+
invited_at timestamptz NULL,
40+
confirmation_token varchar(255) NULL,
41+
confirmation_sent_at timestamptz NULL,
42+
recovery_token varchar(255) NULL,
43+
recovery_sent_at timestamptz NULL,
44+
email_change_token varchar(255) NULL,
45+
email_change varchar(255) NULL,
46+
email_change_sent_at timestamptz NULL,
47+
last_sign_in_at timestamptz NULL,
48+
raw_app_meta_data jsonb NULL,
49+
raw_user_meta_data jsonb NULL,
50+
is_super_admin bool NULL,
51+
created_at timestamptz NULL,
52+
updated_at timestamptz NULL,
53+
CONSTRAINT users_pkey PRIMARY KEY (id)
54+
);
55+
CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
56+
CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
57+
-- auth.refresh_tokens definition
58+
CREATE TABLE auth.refresh_tokens (
59+
instance_id uuid NULL,
60+
id bigserial NOT NULL,
61+
"token" varchar(255) NULL,
62+
user_id varchar(255) NULL,
63+
revoked bool NULL,
64+
created_at timestamptz NULL,
65+
updated_at timestamptz NULL,
66+
CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
67+
);
68+
CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
69+
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
70+
CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
71+
-- auth.instances definition
72+
CREATE TABLE auth.instances (
73+
id uuid NOT NULL,
74+
uuid uuid NULL,
75+
raw_base_config text NULL,
76+
created_at timestamptz NULL,
77+
updated_at timestamptz NULL,
78+
CONSTRAINT instances_pkey PRIMARY KEY (id)
79+
);
80+
-- auth.audit_log_entries definition
81+
CREATE TABLE auth.audit_log_entries (
82+
instance_id uuid NULL,
83+
id uuid NOT NULL,
84+
payload json NULL,
85+
created_at timestamptz NULL,
86+
CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
87+
);
88+
CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
89+
-- auth.schema_migrations definition
90+
CREATE TABLE auth.schema_migrations (
91+
"version" varchar(255) NOT NULL,
92+
CONSTRAINT schema_migrations_pkey PRIMARY KEY ("version")
93+
);
94+
INSERT INTO auth.schema_migrations (version)
95+
VALUES ('20171026211738'),
96+
('20171026211808'),
97+
('20171026211834'),
98+
('20180103212743'),
99+
('20180108183307'),
100+
('20180119214651'),
101+
('20180125194653');
102+
-- Gets the User ID from the request cookie
103+
create or replace function auth.uid() returns uuid as $$
104+
select nullif(current_setting('request.jwt.claim.sub', true), '')::uuid;
105+
$$ language sql stable;
106+
-- Gets the User Role from the request cookie
107+
create or replace function auth.role() returns text as $$
108+
select nullif(current_setting('request.jwt.claim.role', true), '')::text;
109+
$$ language sql stable;
110+
-- Gets the User Email from the request cookie
111+
create or replace function auth.email() returns text as $$
112+
select nullif(current_setting('request.jwt.claim.email', true), '')::text;
113+
$$ language sql stable;
114+
GRANT ALL PRIVILEGES ON SCHEMA auth TO postgres;
115+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO postgres;
116+
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO postgres;
117+
ALTER ROLE postgres SET search_path = "$user", public, auth;
118+
119+
GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;

0 commit comments

Comments
 (0)