Code cleanup

tags/smeserver-expire-accounts-0.1.7-1
Daniel Berteaud 9 years ago
parent ab7648d9c0
commit 40cfe05301
  1. 26
      root/sbin/e-smith/expire-accounts

@ -61,7 +61,7 @@ my $now = DateTime->now;
foreach my $user ($a->users){ foreach my $user ($a->users){
my $pass = $user->prop('PasswordSet') || 'no'; my $pass = $user->prop('PasswordSet') || 'no';
if ($pass ne 'yes'){ if ( $pass ne 'yes' ){
print $user->key . ": Account already locked\n" if $debug; print $user->key . ": Account already locked\n" if $debug;
my $delete = $user->prop('ExpireDeleteAfterLock') || 'never'; my $delete = $user->prop('ExpireDeleteAfterLock') || 'never';
my $locked_date = $user->prop('ExpireLockedOn') || ''; my $locked_date = $user->prop('ExpireLockedOn') || '';
@ -80,11 +80,11 @@ foreach my $user ($a->users){
next; next;
} }
my $lock_date = $user->prop('ExpireLockOn') || 'never'; my $lock_date = $user->prop('ExpireLockOn') || 'never';
if ($lock_date eq 'never'){ if ( $lock_date eq 'never' ){
print $user->key . ": No expiration date set\n" if $debug; print $user->key . ": No expiration date set\n" if $debug;
next; next;
} }
elsif ($lock_date !~ m/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/){ elsif ( $lock_date !~ m/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/ ){
print $user->key . ": Invalide expiration date ($lock_date)\n" if $debug; print $user->key . ": Invalide expiration date ($lock_date)\n" if $debug;
next; next;
} }
@ -94,7 +94,7 @@ foreach my $user ($a->users){
day => $3, day => $3,
); );
if ($exp < $now){ if ( $exp < $now ){
print $user->key . ": Expiration date is passed, account must be locked\n" if $debug; print $user->key . ": Expiration date is passed, account must be locked\n" if $debug;
push @lock, $user->key; push @lock, $user->key;
} }
@ -113,7 +113,7 @@ foreach my $u (@lock){
# Accounts to be deleted # Accounts to be deleted
foreach my $u (@delete){ foreach my $u (@delete){
my $user = $a->get($u); my $user = $a->get($u);
if (!$user || !$user->prop('type') || $user->prop('type') ne 'user'){ if ( !$user || !$user->prop('type') || $user->prop('type') ne 'user' ){
print $u . ": This is not a user account\n"; print $u . ": This is not a user account\n";
next; next;
} }
@ -122,7 +122,7 @@ foreach my $u (@delete){
next; next;
} }
my $archive = $user->prop('ExpireArchiveBeforeDelete') || 'yes'; my $archive = $user->prop('ExpireArchiveBeforeDelete') || 'yes';
if ($archive =~ m/^yes|enabled|1|on$/){ if ( $archive =~ m/^yes|enabled|1|on$/ ){
my $tar = $archive_path . '/' . $u . '-' . $now->ymd . '.' . $archive_ext; my $tar = $archive_path . '/' . $u . '-' . $now->ymd . '.' . $archive_ext;
my $res = system( my $res = system(
'tar', 'tar',
@ -134,9 +134,9 @@ foreach my $u (@delete){
getpwnam($u)->dir, getpwnam($u)->dir,
getpwnam($u)->dir getpwnam($u)->dir
); );
if ($res == 0){ if ( $res == 0 ){
print $u . ": Data archived as $tar. The user account will now be deleted\n" if ($debug); print $u . ": Data archived as $tar. The user account will now be deleted\n" if ($debug);
$user->set_prop(type => 'user-deleted'); $user->set_prop( type => 'user-deleted' );
event_signal('user-delete', $u); event_signal('user-delete', $u);
$a = esmith::AccountsDB->open || die "Couldn't open AccountsDB\n"; $a = esmith::AccountsDB->open || die "Couldn't open AccountsDB\n";
$a->get($u)->delete; $a->get($u)->delete;
@ -147,16 +147,16 @@ foreach my $u (@delete){
# Send a warning for accounts which will be locked soon # Send a warning for accounts which will be locked soon
my $send_warn_for = {}; my $send_warn_for = {};
foreach my $user (keys %$rem){ foreach my $user (keys %$rem){
my $rec = $a->get($user) || next; my $rec = $a->get($user) || next;
my $last_notif = $rec->prop('ExpireLastNotifiedOn') || 'never'; my $last_notif = $rec->prop('ExpireLastNotifiedOn') || 'never';
my $warn_user = $rec->prop('ExpireWarnUser') || 'disabled'; my $warn_user = $rec->prop('ExpireWarnUser') || 'disabled';
if ($last_notif =~ m/^(\d{4})\-(\d{1,2})\-(\d{1,2})/){ if ( $last_notif =~ m/^(\d{4})\-(\d{1,2})\-(\d{1,2})/ ){
$last_notif = DateTime->new( $last_notif = DateTime->new(
year => $1, year => $1,
month => $2, month => $2,
day => $3, day => $3,
); );
if (($last_notif - $now)->in_units('days') < $days_between_warn){ if ( ($last_notif - $now)->in_units('days') < $days_between_warn ){
next; next;
} }
} }
@ -166,7 +166,7 @@ foreach my $user (keys %$rem){
# Use templates-custom version by preference if it exists # Use templates-custom version by preference if it exists
-f "${templates}-custom${source}" and $templates .= "-custom"; -f "${templates}-custom${source}" and $templates .= "-custom";
if ($warn_user =~ m/^enabled|yes|on|1$/){ if ( $warn_user =~ m/^enabled|yes|on|1$/ ){
my $t = new Text::Template(TYPE => 'FILE', my $t = new Text::Template(TYPE => 'FILE',
SOURCE => "${templates}${source}"); SOURCE => "${templates}${source}");

Loading…
Cancel
Save