-- Deploy camper:surroundings_ad to pg
-- requires: roles
-- requires: schema_camper
-- requires: company
-- requires: user_profile

begin;

set search_path to camper, public;

create table surroundings_ad (
	company_id integer not null primary key references company,
	media_id integer not null references media,
	title text not null,
	anchor text not null,
	href uri not null
);

grant select on table surroundings_ad to guest;
grant select on table surroundings_ad to employee;
grant select, insert, update, delete on table surroundings_ad to admin;

alter table surroundings_ad enable row level security;

create policy guest_ok
on surroundings_ad
for select
using (true)
;

create policy insert_to_company
on surroundings_ad
for insert
with check (
	company_id in (select company_id from user_profile)
)
;

create policy update_company
on surroundings_ad
for update
using (
	company_id in (select company_id from user_profile)
)
;

create policy delete_from_company
on surroundings_ad
for delete
using (
	company_id in (select company_id from user_profile)
)
;

commit;
