smeserver-email/root/etc/e-smith/events/actions/update-user-pseudonyms

108 lines
3.4 KiB
Perl

#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 1999-2005 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#----------------------------------------------------------------------
package esmith;
use strict;
use Errno;
use esmith::AccountsDB;
my $adb = esmith::AccountsDB->open();
#------------------------------------------------------------
# This program should be called by the post-upgrade or post-restore
# events. It will add missing First.Last and First_Last pseudonyms
# for users that don't already have them. It should be called AFTER
# the update-uids action.
#------------------------------------------------------------
sub byUid
{
$a->prop('Uid') <=> $b->prop('Uid');
}
sub makePseudonyms ($$)
{
#------------------------------------------------------------
# Generate First.Last and First_Last pseudonyms
#------------------------------------------------------------
my ($firstName, $lastName) = @_;
my $dot_pseudonym = "$firstName $lastName";
$dot_pseudonym =~ s/^\s+//; # Strip leading whitespace
$dot_pseudonym =~ s/\s+$//; # Strip trailing whitespace
$dot_pseudonym =~ s/\s+/ /g; # Multiple spaces become single spaces
$dot_pseudonym =~ s/\s/./g; # Change all spaces to dots
$dot_pseudonym = lc $dot_pseudonym; # Change to lower case
my $underbar_pseudonym = $dot_pseudonym;
$underbar_pseudonym =~ s/\./_/g; # Change dots to underbars
return ($dot_pseudonym, $underbar_pseudonym);
}
###########################################################################
# Handle each of the users. Process each user account in order of
# increasing user id. This will hopefully preserve the order in which they
# were created thereby ensuring seniority in the creation of pseudonyms.
my %users;
foreach my $account ( sort byUid $adb->users )
{
my $firstName = $account->prop('FirstName');
my $lastName = $account->prop('LastName');
my ($dot_pseudonym, $underbar_pseudonym)
= makePseudonyms($firstName, $lastName);
my $a = $adb->get($dot_pseudonym);
if (defined $a)
{
my $user = $a->prop('Account');
warn "$dot_pseudonym pseudonym is assigned to $user instead of " .
$account->key . ".\n" if ($user ne $account->key)
}
else
{
$adb->new_record($dot_pseudonym, { type => 'pseudonym', Account => $account->key });
}
$a = $adb->get($underbar_pseudonym);
if (defined $a)
{
my $user = $a->prop('Account');
warn "$underbar_pseudonym pseudonym is assigned to $user instead of " .
$account->key . ".\n" if ($user ne $account->key)
}
else
{
$adb->new_record($underbar_pseudonym, { type => 'pseudonym', Account => $account->key });
}
}
exit (0);