From f1067a0d8f6eaa1ab6505c152dbddbb3d4dcde63 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Thu, 13 Feb 2003 02:01:14 +0000 Subject: [PATCH] Added expiration overrides for groupings imported through the user importer. --- lib/WebGUI/DateTime.pm | 2 +- lib/WebGUI/Grouping.pm | 13 ++++++++++-- lib/WebGUI/User.pm | 8 ++++++-- sbin/userImport.pl | 45 +++++++++++++++++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/lib/WebGUI/DateTime.pm b/lib/WebGUI/DateTime.pm index 978daacd4..4050727ab 100644 --- a/lib/WebGUI/DateTime.pm +++ b/lib/WebGUI/DateTime.pm @@ -407,7 +407,7 @@ An integer which represents the amount of time for the interval. =item units -A string which represents the units of the interval. The string must be 'years', 'months', 'days', 'hours', 'minutes', or 'seconds'. +A string which represents the units of the interval. The string must be 'years', 'months', 'weeks', 'days', 'hours', 'minutes', or 'seconds'. =back diff --git a/lib/WebGUI/Grouping.pm b/lib/WebGUI/Grouping.pm index 1590df20c..b66249a82 100755 --- a/lib/WebGUI/Grouping.pm +++ b/lib/WebGUI/Grouping.pm @@ -83,7 +83,7 @@ sub addGroupsToGroups { #------------------------------------------------------------------- -=head2 addUsersToGroups ( users, groups ) +=head2 addUsersToGroups ( users, groups [, expireOffset ] ) Adds users to the specified groups. @@ -97,13 +97,22 @@ An array reference containing a list of users. An array reference containing a list of groups. +=item expireOffset + +An override for the default offset of the grouping. Specified in seconds. + =back =cut sub addUsersToGroups { foreach my $gid (@{$_[1]}) { - my ($expireOffset) = WebGUI::SQL->quickArray("select expireOffset from groups where groupId=$gid"); + my $expireOffset; + if ($_[2]) { + $expireOffset = $_[2]; + } else { + ($expireOffset) = WebGUI::SQL->quickArray("select expireOffset from groups where groupId=$gid"); + } foreach my $uid (@{$_[0]}) { my ($isIn) = WebGUI::SQL->quickArray("select count(*) from groupings where groupId=$gid and userId=$uid"); unless ($isIn) { diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index f733e8cc4..21fdd93a6 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -65,7 +65,7 @@ sub _create { #------------------------------------------------------------------- -=head2 addToGroups ( groups ) +=head2 addToGroups ( groups [, expireOffset ] ) Adds this user to the specified groups. @@ -75,12 +75,16 @@ Adds this user to the specified groups. An array reference containing a list of groups. +=item expireOffset + +An override for the default offset of the grouping. Specified in seconds. + =back =cut sub addToGroups { - WebGUI::Grouping::addUsersToGroups([$_[0]->{_userId}],$_[1]); + WebGUI::Grouping::addUsersToGroups([$_[0]->{_userId}],$_[1],$_[2]); } #------------------------------------------------------------------- diff --git a/sbin/userImport.pl b/sbin/userImport.pl index b05c23958..8838d4003 100644 --- a/sbin/userImport.pl +++ b/sbin/userImport.pl @@ -18,6 +18,7 @@ BEGIN { use Digest::MD5; use Getopt::Long; use strict; +use WebGUI::DateTime; use WebGUI::Grouping; use WebGUI::Session; use WebGUI::SQL; @@ -35,6 +36,8 @@ my $authMethod = 'WebGUI'; my $groups; my $ldapUrl; my $status = 'Active'; +my $expireOffset; +my $expireUnits = 'seconds'; GetOptions( 'usersfile=s'=>\$usersFile, @@ -45,7 +48,9 @@ GetOptions( 'password|identifier:s'=>\$defaultIdentifier, 'groups:s'=>\$groups, 'ldapUrl:s'=>\$ldapUrl, - 'status:s'=>\$status + 'status:s'=>\$status, + 'expireOffset:i'=>\$expireOffset, + 'expireUnits:s'=>\$expireUnits ); @@ -72,6 +77,20 @@ Options: --delimiter The string that separates each field in the import file. Defaults to tab. + --expireOffset The the amount of time before the user will + be expired from the groups they are added + to. Defaults to the expire offset set in + the group definition within WebGUI. May be + overridden in the import file. + + --expireUnits Valid values are "seconds", "minutes", + "hours", "days", "weeks", "months", "years", + or "epoch". Defaults to "seconds". This is + the units of the expire offset. If set to + "epoch" the system will assume that the + expire offset is an epoch date rather than + an interval. + --groups A comma separated list of group ids that each user in the import file will be set to. Can be overridden in the import file. @@ -102,7 +121,7 @@ User File Format: -Valid field names: username password authMethod status - ldapUrl connectDN groups + ldapUrl connectDN groups expireOffset -In addition to the field names above, you may use any valid profile field name. @@ -172,6 +191,8 @@ while() { $user{authMethod} = $authMethod if ($user{authMethod} eq ""); $user{groups} = $groups if ($user{groups} eq ""); $user{status} = $status if ($user{status} eq ""); + $user{expireOffset} = $expireOffset if ($user{expireOffset} eq ""); + $user{expireOffset} = calculateExpireOffset($user{expireOffset},$expireUnits); # process user my ($duplicate) = WebGUI::SQL->quickArray("select count(*) from users where username=".quote($user{username})); @@ -197,7 +218,7 @@ while() { } if ($user{groups} ne "") { my @groups = split(/,/,$user{groups}); - $u->addToGroups(\@groups); + $u->addToGroups(\@groups,$user{expireOffset}); } } } @@ -208,6 +229,20 @@ WebGUI::Session::close(); print "OK\n"; - - +#------------------------------------------------- +# calculateExpireOffset(expireOffset,expireUnits) +# return: offsetInSeconds +sub calculateExpireOffset { + my ($offset, $units) = @_; + return undef if ($offset < 1); + if ($units eq "epoch") { + my $seconds = (WebGUI::DateTime::time() - $offset); + if ($seconds < 1) { + return undef; + } else { + return $seconds; + } + } + return WebGUI::DateTime::intervalToSeconds($offset, $units) +}