set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.unset_acting_usr()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
begin
  perform set_config('dnim.usr_uid', '', false);
end;
$function$
;

CREATE OR REPLACE FUNCTION public.get_acting_usr()
 RETURNS usr
 LANGUAGE plpgsql
AS $function$
declare
  acting_usr public.usr;
begin
  if nullif(current_setting('dnim.usr_uid', true), '') is null then
    acting_usr := public.usr_root();
  else
    select u.*
    from public.usr u
    where u.uid = human_uuid.huid_of_string(current_setting('dnim.usr_uid', true))
    into acting_usr;
  end if;

  return coalesce(acting_usr, public.usr_root());
end;
$function$
;

CREATE OR REPLACE FUNCTION public.set_acting_usr(uid text)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
begin
  if uid is not null and uid != '' then
    perform set_config('dnim.usr_uid', uid, false);
  end if;
end;
$function$
;
