Compare commits
3 Commits
988fcd9dca
...
4e5568bf19
Author | SHA1 | Date | |
---|---|---|---|
4e5568bf19 | |||
bea07c5ab7 | |||
c1db5741db |
@ -15,11 +15,17 @@ grant pg_read_all_stats to dba ;
|
||||
CREATE TABLE IF NOT EXISTS dba.pwdhistory
|
||||
(
|
||||
usename character varying COLLATE pg_catalog."default",
|
||||
usename_addres character varying COLLATE pg_catalog."default",
|
||||
application_name character varying COLLATE pg_catalog."default",
|
||||
password character varying COLLATE pg_catalog."default",
|
||||
changed_on timestamp without time zone
|
||||
)
|
||||
TABLESPACE pg_default;
|
||||
|
||||
-- alter if you come from a previous version of the table:
|
||||
-- alter table dba.pwdhistory add column usename_addres character varying ;
|
||||
-- alter table dba.pwdhistory add column application_name character varying ;
|
||||
|
||||
ALTER TABLE IF EXISTS dba.pwdhistory
|
||||
OWNER to dba;
|
||||
|
||||
@ -76,8 +82,12 @@ AS $BODY$
|
||||
declare
|
||||
_min_password_length int := 8; -- specify min length here
|
||||
_usename text := '';
|
||||
_useraddress text := '';
|
||||
_userapp text := '';
|
||||
begin
|
||||
select user into _usename;
|
||||
select client_addr into _useraddress from pg_stat_activity where pid = pg_backend_pid() ;
|
||||
select application_name into _userapp from pg_stat_activity where pid = pg_backend_pid() ;
|
||||
if length(_password) >= _min_password_length then
|
||||
EXECUTE format('ALTER USER %I WITH PASSWORD %L', _usename, _password);
|
||||
else -- also catches NULL
|
||||
@ -87,20 +97,18 @@ begin
|
||||
, detail = 'Please check your password.'
|
||||
, hint = 'Password must be at least ' || _min_password_length || ' characters.';
|
||||
end if;
|
||||
if user = 'postgres' then
|
||||
raise exception 'This function should not be run by user postgres'
|
||||
using errcode = '22024' -- 22023 = "invalid_parameter_value'
|
||||
, detail = 'Use a named user only.' ;
|
||||
else
|
||||
insert into dba.pwdhistory
|
||||
(usename, usename_addres, application_name, password, changed_on)
|
||||
values (_usename, _useraddress, _userapp, md5(_password),now());
|
||||
PERFORM dba.change_valid_until(_usename) ;
|
||||
end if;
|
||||
|
||||
insert into dba.pwdhistory
|
||||
(usename, password, changed_on)
|
||||
values (_usename, md5(_password),now());
|
||||
PERFORM dba.change_valid_until(_usename) ;
|
||||
-- EXECUTE format('update pg_catalog.pg_authid set rolvaliduntil=now() + interval ''120 days'' where rolname=''%I'' ', _usename);
|
||||
-- update pg_catalog.pg_authid
|
||||
-- set rolvaliduntil='2021-12-30 00:00:00+01' where rolname='dodger' ;
|
||||
return 0;
|
||||
exception
|
||||
-- trap existing error and re-raise with added detail
|
||||
when unique_violation then -- = error code 23505
|
||||
raise unique_violation
|
||||
using detail = 'Password already used earlier. Please try again with a different password.';
|
||||
end
|
||||
$BODY$;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user