begin; -- Extend the exclusive identity to the selected company's active employees. -- Keep the original zero-argument RPC for old installed clients. alter table public.caterium_sidebar_brand_assignment add column if not exists workspace_id uuid references public.sun_workspaces(id) on delete set null; -- Never guess between several companies owned by the assigned account. update public.caterium_sidebar_brand_assignment a set workspace_id=w.id from public.sun_workspaces w where a.workspace_id is null and w.created_by=a.user_id and (select count(*) from public.sun_workspaces x where x.created_by=a.user_id)=1; create or replace function public.caterium_workspace_sidebar_brand(p_workspace uuid) returns jsonb language plpgsql stable security definer set search_path=public,pg_temp as $$ declare variant text:='caterium'; begin if auth.uid() is null or public.sun_member_role(p_workspace) is null or not exists(select 1 from auth.users where id=auth.uid() and email_confirmed_at is not null) then raise exception 'Нет доступа к компании'; end if; if exists( select 1 from public.caterium_sidebar_brand_assignment a join auth.users owner_account on owner_account.id=a.user_id and owner_account.email_confirmed_at is not null join public.sun_workspaces w on w.id=p_workspace where a.workspace_id=w.id or (a.workspace_id is null and w.created_by=a.user_id and (select count(*) from public.sun_workspaces x where x.created_by=a.user_id)=1) ) then variant:='solnce'; end if; return jsonb_build_object('variant',variant,'workspace_id',p_workspace); end $$; revoke all on function public.caterium_workspace_sidebar_brand(uuid) from public,anon; grant execute on function public.caterium_workspace_sidebar_brand(uuid) to authenticated; notify pgrst,'reload schema'; commit;