set postgresql.extra_errors to 'all';
create table users
(
id bigserial primary key not null,
tag varchar unique not null,
email varchar unique not null
);
alter table users enable row level security;
create table posts
(
id bigserial primary key not null,
user_id bigint not null,
content text not null,
foreign key (user_id) references users (id) on delete restrict
);
alter table posts enable row level security;
create table comments
(
id bigserial primary key not null,
user_id bigint not null,
post_id bigint not null,
text text not null,
foreign key (user_id) references users (id) on delete restrict,
foreign key (post_id) references posts (id) on delete restrict
);
alter table comments enable row level security;
-- ========== Заполнение таблиц данными ==========
insert
INTO
users(tag, email)
values
('user1', 'user1@email.com'),
('user2', 'user2@email.com');
insert into posts(user_id, content)
values
(1, 'post 1 by user1'),
(2, 'post 2 by user2');
insert
into
comments(user_id, post_id, text)
values
(1, 1, 'comment by user1 (post 1)'),
(2, 1, 'comment by user2 (post 1)');
-- ========== Заполнение таблиц данными ==========
-- ========== Администратор ==========
create role administrator;
grant all privileges on users to administrator;
grant all privileges on posts to administrator;
grant all privileges on comments to administrator;
create policy admin_users_policy on users for all using(true);
create policy admin_posts_policy on posts for all using(true);
create policy admin_comments_policy on comments for all using(true);
grant usage, select on all sequences in schema public to administrator;
-- ========== Тестирование прав доступа ==========
set role administrator;
-- select
select * from users;
select * from posts;
select * from comments;
-- update
update users set email = '::' || email;
update posts set content = content || '.';
update comments set text = text || '.';
-- insert
insert
into
users(tag, email)
values
('user3', 'user3@email.com');
-- delete
delete from users where tag = 'user3';
reset role;
-- ========== Тестирование прав доступа ==========
-- ========== Администратор ==========
-- ========== Пользователь ==========
create role registered_user;
grant select on users to registered_user;
grant select on posts to registered_user;
grant select on comments to registered_user;
grant update (email) on users to registered_user;
grant update (content) on posts to registered_user;
grant update (text) on comments to registered_user;
create policy registered_user_users_update_policy on users for update to registered_user
using (tag = current_user);
create role user1;
create role user2;
grant registered_user to user1;
grant registered_user to user2;
-- ========== Тестирование прав доступа ==========
set role user1;
-- select
select * from users;
select * from posts;
select * from comments;
-- update
update users set email = '::' || email where tag = 'user2';
select * from users;
reset role;
-- ========== Тестирование прав доступа ==========
-- ========== Пользователь ==========
drop table comments;
drop table posts;
drop table users;
drop role user2;
drop role user1;
drop role registered_user;
drop role administrator;