Repository navigation
Expand file tree
/
Copy path01-after-workshop.sql
More file actions
234 lines (179 loc) · 8.16 KB
/
Copy path01-after-workshop.sql
File metadata and controls
234 lines (179 loc) · 8.16 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
-- This file replays all the SQL from the workshop README, in README order.
-- It is meant to run on a FRESHLY RESTORED database (initial_db.backup /
-- 00-initial-db.sql.gz). To catch up to a given point of the workshop,
-- run it from the top down to the "end of section N" marker you need.
-- Section 5 - Smart tags
comment on table app_public.municipality is E'@omit';
comment on table app_public.municipality is NULL;
comment on table app_public.srtm is E'@omit';
comment on table app_public.landcover is E'@name clc_landcover';
comment on column app_public.landcover.label3 is E'@name label';
comment on constraint population_dico_fkey on app_public.population is
E'@foreignFieldName population\n@fieldName municipality\nDocumentation here.';
-- end of section 5
-- Section 6.1 - Computed columns
create or replace function app_public.parcels_area(p app_public.parcels)
returns real as $$
select ST_Area(p.geom,true);
$$ language sql stable;
create or replace function app_public.parcels_clc_landcover(p app_public.parcels)
returns varchar[] as $$
SELECT array_agg(distinct l.label3)
FROM app_public.landcover AS l
WHERE ST_Intersects(p.geom,l.geom)
$$ language sql stable;
DROP TYPE IF EXISTS srtm_stats CASCADE;
CREATE TYPE srtm_stats AS (
"min" real,
"max" real,
"mean" real
);
create or replace function app_public.parcels_srtm(p app_public.parcels)
returns setof srtm_stats as $$
WITH t AS (
SELECT st_summarystats(ST_Union(ST_Clip(r.rast, ST_Transform(p.geom,3763),true))) as stats
FROM app_public.srtm AS r
WHERE ST_Intersects(ST_Transform(p.geom,3763),r.rast)
)
SELECT (stats).min,(stats).max,(stats).mean FROM t;
$$ language sql stable;
-- end of section 6.1
-- Section 6.2 - Custom queries
create or replace function app_public.get_landcover(geometry JSON, distance real DEFAULT NULL)
returns SETOF app_public.landcover as $$
declare
g_geom geometry;
BEGIN
IF distance IS NOT NULL AND distance > 0 THEN
IF distance > 10001 THEN
RAISE EXCEPTION 'Maximum allowed distance for this operation is 10 km.';
ELSE
g_geom=ST_SetSRID(st_buffer(ST_GeomFromGeoJSON(geometry)::geography,distance)::geometry,4326);
END IF;
ELSE
g_geom=ST_SetSRID(ST_GeomFromGeoJSON(geometry),4326);
END IF;
RETURN QUERY
SELECT *
FROM app_public.landcover AS l
WHERE ST_Intersects(g_geom,l.geom);
END;
$$ language plpgsql stable;
-- end of section 6.2
-- Section 7 - CRUD mutations
--First lets remove any row without geometry
DELETE FROM app_public.parcels WHERE geom is NULL;
ALTER TABLE app_public.parcels ALTER COLUMN geom SET NOT NULL;
comment on column app_public.parcels.id is E'@omit create,update,delete';
-- end of section 7
-- Section 8 - Authentication
create table IF NOT EXISTS app_public.person (
id serial primary key,
name text unique not null check (char_length(name) < 80),
about text,
created_at timestamp default now()
);
comment on table app_public.person is 'A user of the app.';
comment on column app_public.person.id is 'The primary unique identifier for the person.';
comment on column app_public.person.name is 'The person’s name.';
comment on column app_public.person.about is 'A short description about the user, written by the user.';
comment on column app_public.person.created_at is 'The time this person was created.';
create table IF NOT EXISTS app_private.person (
person_id integer primary key references app_public.person(id) on delete cascade,
email text not null unique check (email ~* '^.+@.+\..+$'),
password_hash text not null
);
comment on table app_private.person is 'Private information about a person’s account.';
comment on column app_private.person.person_id is 'The id of the person associated with this account.';
comment on column app_private.person.email is 'The email address of the person.';
comment on column app_private.person.password_hash is 'An opaque hash of the person’s password.';
create extension if not exists "pgcrypto";
create function app_public.register_person(
name text,
email text,
password text
) returns app_public.person as $$
declare
person app_public.person;
begin
insert into app_public.person (name) values
(name)
returning * into person;
insert into app_private.person (person_id, email, password_hash) values
(person.id, email, crypt(password, gen_salt('bf')));
return person;
end;
$$ language plpgsql strict security definer;
comment on function app_public.register_person(text, text, text) is 'Registers a single user and creates an account into the app.';
drop role IF EXISTS app_postgraphile;
create role app_postgraphile login password 'postgis';
drop role IF EXISTS app_anonymous;
create role app_anonymous;
grant app_anonymous to app_postgraphile;
drop role IF EXISTS app_person;
create role app_person;
grant app_person to app_postgraphile;
create type app_public.jwt_token as (
role text,
person_id integer,
exp bigint
);
create function app_public.authenticate(
email text,
password text
) returns app_public.jwt_token as $$
declare
account app_private.person;
begin
select a.* into account
from app_private.person as a
where a.email = $1;
if account.password_hash = crypt(password, account.password_hash) then
return ('app_person', account.person_id, extract(epoch from (now() + interval '2 days')))::app_public.jwt_token;
else
return null;
end if;
end;
$$ language plpgsql strict security definer;
comment on function app_public.authenticate(text, text) is 'Creates a JWT token that will securely identify a person and give them certain permissions. This token expires in 2 days.';
create function app_public.current_person() returns app_public.person as $$
select *
from app_public.person
where id = nullif(current_setting('jwt.claims.person_id', true), '')::integer
$$ language sql stable;
comment on function app_public.current_person() is 'Gets the person who was identified by our JWT.';
alter default privileges revoke execute on functions from public;
grant usage on schema app_public to app_anonymous, app_person;
grant select on table app_public.person to app_anonymous, app_person;
grant update, delete on table app_public.person to app_person;
grant select on table app_public.landcover to app_anonymous, app_person;
grant select on table app_public.municipality to app_anonymous, app_person;
grant select on table app_public.population to app_anonymous, app_person;
grant select on table app_public.srtm to app_anonymous, app_person;
grant select on table app_public.parcels to app_anonymous, app_person;
grant insert, update, delete on table app_public.parcels to app_person;
grant usage on sequence app_public.parcels_id_seq to app_person;
grant execute on function app_public.get_landcover(json, real) to app_anonymous, app_person;
grant execute on function app_public.parcels_area(app_public.parcels) to app_anonymous, app_person;
grant execute on function app_public.parcels_clc_landcover(app_public.parcels) to app_anonymous, app_person;
grant execute on function app_public.parcels_srtm(app_public.parcels) to app_anonymous, app_person;
grant execute on function app_public.authenticate(text, text) to app_anonymous, app_person;
grant execute on function app_public.current_person() to app_anonymous, app_person;
grant execute on function app_public.register_person(text, text, text) to app_anonymous;
alter table app_public.person enable row level security;
alter table app_public.parcels enable row level security;
create policy select_person on app_public.person for select
using (true);
create policy select_parcels on app_public.parcels for select
using (true);
create policy update_person on app_public.person for update to app_person
using (id = nullif(current_setting('jwt.claims.person_id', true), '')::integer);
create policy delete_person on app_public.person for delete to app_person
using (id = nullif(current_setting('jwt.claims.person_id', true), '')::integer);
create policy person_parcels_insert on app_public.parcels for insert to app_person
WITH CHECK (true);
create policy person_parcels_update on app_public.parcels for update to app_person
USING (true);
create policy person_parcels_delete on app_public.parcels for delete to app_person
USING (true);
-- end of section 8