-- /usr/share/postgresql/17/extension/c77_rbac_laravel--1.0.sql
\echo Use "CREATE EXTENSION c77_rbac_laravel" to load this file. \quit

CREATE OR REPLACE FUNCTION public.c77_rbac_laravel_auth_id() RETURNS TEXT AS $$
BEGIN
    RETURN current_setting('c77_rbac.external_id', true);
EXCEPTION WHEN OTHERS THEN
    RETURN NULL;
END;
$$ LANGUAGE plpgsql STABLE;

CREATE OR REPLACE FUNCTION public.c77_rbac_laravel_assign_user(
    p_laravel_id BIGINT,
    p_role_name TEXT,
    p_scope_type TEXT,
    p_scope_id TEXT
) RETURNS VOID AS $$
BEGIN
    PERFORM public.c77_rbac_assign_subject(p_laravel_id::TEXT, p_role_name, p_scope_type, p_scope_id);
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
