mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-03 07:40:28 +08:00
* refactor(postgres): use pg_temp to store auto user procedures * refactor(postgres): code review suggestions * test(postgres): replace logrus with slog * chore(postgres): move switch statement to a map * test(postgres): with wrong slog call * test(postgres): use slog context functions
22 lines
790 B
PL/PgSQL
22 lines
790 B
PL/PgSQL
CREATE OR REPLACE PROCEDURE pg_temp.teleport_delete_user(username varchar, inout state varchar default 'TP003')
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
DECLARE
|
|
role_ varchar;
|
|
BEGIN
|
|
-- Only drop if the user doesn't have other active sessions.
|
|
IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username) THEN
|
|
RAISE NOTICE 'User has active connections';
|
|
ELSE
|
|
BEGIN
|
|
EXECUTE FORMAT('DROP USER IF EXISTS %I', username);
|
|
EXCEPTION
|
|
WHEN SQLSTATE '2BP01' THEN
|
|
state := 'TP004';
|
|
-- Drop user/role will fail if user has dependent objects.
|
|
-- In this scenario, fallback into disabling the user.
|
|
CALL pg_temp.teleport_deactivate_user(username);
|
|
END;
|
|
END IF;
|
|
END;$$;
|