PostgreSQL USER EXPIRE

PostgreSQL USER EXPIRE

PostgreSQL de expire süreleri ile ilgili ihtiyacım oldu ve internet üzerinde araştırma yaparken böyle bir şey olmadığı için yazma gereği duydum. PostgreSQL de expire şifrenin belirtilen sürede kullanıcının giriş yapılamaması için gerekmektedir. Expire süresini aşağıda bulunan sql cümlesi ile bütün userları +/-  gün artırabilir veya azaltabilir ya da sadece bir userı -/+ gün arttırabilir ya da azaltabilirsiniz.   Aşağıdaki  belirtilen expiredate fonksiyonu ile spesific kullanıcıya yada bütün kullanıcılara belirtilen tarih kadar gün eklenir.

 

User oluştururken expire süresi belirtilmedi ise fonksiyon çalıştırıldığı zamanı baz alarak ekleme ya da çıkarma işlemleri yapılır.

create or replace function expiredate(deger text,sayi int ,userr text = NULL )
RETURNS  text AS $$
declare 
user_name text;
BEGIN
IF userr is not null and deger ='+' THEN
FOR user_name IN 
select 
case when valuntil is null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date+sayi ||''';'
when  valuntil='infinity'
then 'ALTER USER ' || usename|| ' VALID UNTIL '''||  TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date+sayi ||''';'
when valuntil is not null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(valuntil,'YYYY,MM,DD,HH24:MI')::date+sayi ||''';' 
 end as expiredate
from pg_user  where  usename <> 'postgres'and usename=userr LOOP
EXECUTE user_name ;
END LOOP;
END IF;
IF userr is not null and  deger ='-' THEN
FOR user_name IN 
select 
case when valuntil is null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date-sayi ||''';'
when  valuntil='infinity'
then 'ALTER USER ' || usename|| ' VALID UNTIL '''||  TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date-sayi ||''';'
when valuntil is not null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(valuntil,'YYYY,MM,DD,HH24:MI')::date-sayi ||''';' 
 end as expiredate
from pg_user  where  usename <> 'postgres' and usename=userr LOOP
EXECUTE user_name ;
END LOOP;
END IF;
IF userr is  null and deger ='+' THEN
FOR user_name IN 
select 
case when valuntil is null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date+sayi ||''';'
when  valuntil='infinity'
then 'ALTER USER ' || usename|| ' VALID UNTIL '''||  TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date+sayi ||''';'
when valuntil is not null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(valuntil,'YYYY,MM,DD,HH24:MI')::date+sayi ||''';' 
 end as expiredate
from pg_user  where  usename <> 'postgres' LOOP
EXECUTE user_name ;
END LOOP;
END IF;
IF userr is  null and deger ='-' THEN
FOR user_name IN 
select 
case when valuntil is null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date-sayi ||''';'
when  valuntil='infinity'
then 'ALTER USER ' || usename|| ' VALID UNTIL '''||  TO_CHAR(now(),'YYYY,MM,DD,HH24:MI')::date-sayi ||''';'
when valuntil is not null 
then 'ALTER USER ' || usename|| ' VALID UNTIL '''|| TO_CHAR(valuntil,'YYYY,MM,DD,HH24:MI')::date-sayi ||''';' 
 end as expiredate
from pg_user  where  usename <> 'postgres'LOOP
EXECUTE user_name ;
END LOOP;
END IF;
return  ' Expire süresi ' ||deger||''||sayi|| ' gün olarak  değiştirilmiştir.';
end;
$$ language plpgsql VOLATILE 
;

Aşağıdaki sql cümlesi yardımıyla postgresql üzerinde bulunan bütün userların şifresini -1 olarak değiştiriyoruz.

 

select expiredate('-', 1);

Userların hepsinin expire süresini 10 gün artırmak için aşağıdaki komutu kullanabiliriz.

select expiredate('+', 10);

Sadece belirli bir userın süresini arttırmak ya da azaltmak için aşağıdaki gibi kullabilirsiniz.

 

select expiredate('-', 1,'farukerdem');

 

Loading