
DECLARE
groups int[];
g INT;
BEGIN
    SELECT "value" INTO val FROM luticate_permissions_users WHERE "user_id" = _user_id AND "name" = _permisson_name;
    IF val is null THEN
	SELECT lpg."value" into val FROM luticate_permissions_groups lpg
	INNER JOIN luticate_groups lg ON lg."id" = lpg."group_id"
	INNER JOIN luticate_users_groups lug ON lug."user_id" = _user_id AND lug."group_id" = lg."id"
	WHERE lpg."name" = _permisson_name and lpg."value" = FALSE;
	IF val is null then
	    SELECT lpg."value" into val FROM luticate_permissions_groups lpg
      	    INNER JOIN luticate_groups lg ON lg."id" = lpg."group_id"
	    INNER JOIN luticate_users_groups lug ON lug."user_id" = _user_id AND lug."group_id" = lg."id"
	    WHERE lpg."name" = _permisson_name and lpg."value" = TRUE;
	    IF val is null then
		SELECT "value" INTO val FROM luticate_permissions WHERE "name" = _permisson_name;
	    END IF;
	end if;
    END IF;
END;
