From 226ed9abcfe293d2249cabeb1b83f9d8852d1e59 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sun, 2 Feb 2003 03:43:45 +0000 Subject: [PATCH] Added group expiration notifications. --- docs/upgrades/upgrade_5.0.3-5.1.0.sql | 15 +++ lib/WebGUI/Group.pm | 156 +++++++++++++++++++++++--- lib/WebGUI/Grouping.pm | 5 +- lib/WebGUI/Mail.pm | 2 +- lib/WebGUI/MessageLog.pm | 4 +- lib/WebGUI/Operation/Group.pm | 28 ++++- lib/WebGUI/Operation/User.pm | 2 +- sbin/Hourly/DeleteExpiredGroupings.pm | 11 +- sbin/Hourly/NotifyExpiredGroupings.pm | 41 +++++++ sbin/userImport.pl | 8 +- 10 files changed, 241 insertions(+), 31 deletions(-) create mode 100644 sbin/Hourly/NotifyExpiredGroupings.pm diff --git a/docs/upgrades/upgrade_5.0.3-5.1.0.sql b/docs/upgrades/upgrade_5.0.3-5.1.0.sql index 0b10b425c..13482f7e4 100644 --- a/docs/upgrades/upgrade_5.0.3-5.1.0.sql +++ b/docs/upgrades/upgrade_5.0.3-5.1.0.sql @@ -28,6 +28,21 @@ INSERT INTO userProfileField VALUES ('publicEmail','WebGUI::International::get(8 insert into international (internationalId,languageId,namespace,message,lastUpdated) values (862,1,'WebGUI','This user\'s profile is not public.', 1043881275); alter table groups add column dateCreated int not null default 997938000; alter table groups add column lastUpdated int not null default 997938000; +alter table groups add column deleteOffset int not null default 14; +alter table groups add column expireNotifyOffset int not null default -14; +alter table groups add column expireNotifyMessage text; +alter table groups add column expireNotify int not null default 0; +alter table groups change expireAfter expireOffset int not null default 314496000; +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (866,1,'WebGUI','Expire Notifcation Message', 1044127055); +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (865,1,'WebGUI','Notify user about expiration?', 1044126938); +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (864,1,'WebGUI','Expire Notification Offset', 1044126838); +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (863,1,'WebGUI','Delete Offset', 1044126633); +delete from international where languageId=1 and namespace='WebGUI' and internationalId=367; +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (367,1,'WebGUI','Expire Offset', 1044126611); +insert into international (internationalId,languageId,namespace,message,lastUpdated) values (867,1,'WebGUI','Loss of Privilege', 1044133143); + + + diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm index f4501c5c6..22e30d8a7 100755 --- a/lib/WebGUI/Group.pm +++ b/lib/WebGUI/Group.pm @@ -34,14 +34,18 @@ This package provides an object-oriented way of managing WebGUI groups and group use WebGUI::Group; $g = WebGUI::Group->new(3); or $g = WebGUI::User->new("new"); - $dateCreated = $g->dateCreated; - $description = $g->description("Those really smart dudes."); - $expireAfter = $g->expireAfter(360000); - $groupId = $g->groupId; - $karmaThreshold = $g->karmaThreshold(5000); - $ipFilter = $g->ipFilter("10.;192.168.1."); - $lastUpdated = $g->lastUpdated; - $name = $g->name("Nerds"); + $epoch = $g->dateCreated; + $integer = $g->deleteOffset(14); + $text = $g->description("Those really smart dudes."); + $integer = $g->expireNotify(1); + $integer = $g->expireNotifyMessage("You're outta here!"); + $integer = $g->expireNotifyOffset(-14); + $integer = $g->expireOffset(360000); + $integer = $g->groupId; + $integer = $g->karmaThreshold(5000); + $string = $g->ipFilter("10.;192.168.1."); + $epoch = $g->lastUpdated; + $string = $g->name("Nerds"); $g->addGroups(\@arr); $g->deleteGroups(\@arr); @@ -57,7 +61,7 @@ These methods are available from this class: #------------------------------------------------------------------- sub _create { my $groupId = getNextId("groupId"); - WebGUI::SQL->write("insert into groups (groupId,dateCreated,expireAfter,karmaThreshold) values + WebGUI::SQL->write("insert into groups (groupId,dateCreated,expireOffset,karmaThreshold) values ($groupId,".time().",314496000,1000000000)"); return $groupId; } @@ -130,6 +134,35 @@ sub deleteGroups { } +#------------------------------------------------------------------- + +=head2 deleteOffset ( [ value ] ) + +Returns the number of days after the expiration to delete the grouping. + +=over + +=item value + +If specified, deleteOffset is set to this value. Defaults to "-14". + +=back + +=cut + +sub deleteOffset { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"deleteOffset"} = $value; + WebGUI::SQL->write("update groups set deleteOffset=$value, + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"deleteOffset"}; +} + + #------------------------------------------------------------------- =head2 description ( [ value ] ) @@ -161,7 +194,95 @@ sub description { #------------------------------------------------------------------- -=head2 expireAfter ( [ value ] ) +=head2 expireNotify ( [ value ] ) + +Returns a boolean value whether or not to notify the user of the group expiry. + +=over + +=item value + +If specified, expireNotify is set to this value. + +=back + +=cut + +sub expireNotify { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"expireNotify"} = $value; + WebGUI::SQL->write("update groups set expireNotify=$value, + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"expireNotify"}; +} + + +#------------------------------------------------------------------- + +=head2 expireNotifyMessage ( [ value ] ) + +Returns the message to send to the user about expiration. + +=over + +=item value + +If specified, expireNotifyMessage is set to this value. + +=back + +=cut + +sub expireNotifyMessage { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"expireNotifyMessage"} = $value; + WebGUI::SQL->write("update groups set expireNotifyMessage=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"expireNotifyMessage"}; +} + + + +#------------------------------------------------------------------- + +=head2 expireNotifyOffset ( [ value ] ) + +Returns the number of days after the expiration to notify the user. + +=over + +=item value + +If specified, expireNotifyOffset is set to this value. + +=back + +=cut + +sub expireNotifyOffset { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"expireNotifyOffset"} = $value; + WebGUI::SQL->write("update groups set expireNotifyOffset=$value, + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"expireNotifyOffset"}; +} + + +#------------------------------------------------------------------- + +=head2 expireOffset ( [ value ] ) Returns the number of seconds any grouping with this group should expire after. @@ -169,22 +290,22 @@ Returns the number of seconds any grouping with this group should expire after. =item value -If specified, expireAfter is set to this value. +If specified, expireOffset is set to this value. =back =cut -sub expireAfter { +sub expireOffset { my ($class, $value); $class = shift; $value = shift; if (defined $value) { - $class->{_group}{"expireAfter"} = $value; - WebGUI::SQL->write("update groups set expireAfter=".quote($value).", + $class->{_group}{"expireOffset"} = $value; + WebGUI::SQL->write("update groups set expireOffset=".quote($value).", lastUpdated=".time()." where groupId=$class->{_groupId}"); } - return $class->{_group}{"expireAfter"}; + return $class->{_group}{"expireOffset"}; } @@ -324,9 +445,12 @@ sub new { $groupId = shift; $groupId = _create() if ($groupId eq "new"); if ($groupId eq "") { - $group{expireAfter} = 314496000; + $group{expireOffset} = 314496000; $group{karmaThreshold} = 1000000000; $group{groupName} = "New Group"; + $group{expireNotifyOffset} = -14; + $group{deleteOffset} = 14; + $group{expireNotify} = 0; } else { %group = WebGUI::SQL->quickHash("select * from groups where groupId='$groupId'"); } diff --git a/lib/WebGUI/Grouping.pm b/lib/WebGUI/Grouping.pm index 4f0d103e1..1590df20c 100755 --- a/lib/WebGUI/Grouping.pm +++ b/lib/WebGUI/Grouping.pm @@ -103,18 +103,17 @@ An array reference containing a list of groups. sub addUsersToGroups { foreach my $gid (@{$_[1]}) { - my ($expireAfter) = WebGUI::SQL->quickArray("select expireAfter from groups where groupId=$gid"); + my ($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) { WebGUI::SQL->write("insert into groupings (groupId,userId,expireDate) - values ($gid, $uid, ".(time()+$expireAfter).")"); + values ($gid, $uid, ".(time()+$expireOffset).")"); } } } } - #------------------------------------------------------------------- =head2 deleteGroupsFromGroups ( groups, fromGroups ) diff --git a/lib/WebGUI/Mail.pm b/lib/WebGUI/Mail.pm index eb91545b4..5cf5e5cd3 100644 --- a/lib/WebGUI/Mail.pm +++ b/lib/WebGUI/Mail.pm @@ -90,7 +90,7 @@ sub send { #body $message .= $_[2]."\n"; #footer - $message .= "\n".$session{setting}{mailFooter} + $message .= "\n".$session{setting}{mailFooter}; #process macros on message $message = WebGUI::Macro::process($message); if ($session{setting}{smtpServer} =~ /\/sendmail/) { diff --git a/lib/WebGUI/MessageLog.pm b/lib/WebGUI/MessageLog.pm index 53542de86..e3350510e 100644 --- a/lib/WebGUI/MessageLog.pm +++ b/lib/WebGUI/MessageLog.pm @@ -17,6 +17,7 @@ package WebGUI::MessageLog; use strict; use Tie::CPHash; +use WebGUI::DateTime; use WebGUI::International; use WebGUI::Macro; use WebGUI::Mail; @@ -126,7 +127,8 @@ sub addEntry { foreach $user (@users) { $u = WebGUI::User->new($user); if ($u->userId ne "") { - WebGUI::SQL->write("insert into messageLog values ($messageLogId,".$u->userId.", + WebGUI::SQL->write("insert into messageLog (messageLogId, userId, message, url, dateOfEntry, + subject, status) values ($messageLogId,".$u->userId.", ".quote($message).",".quote($url).",".time().",".quote($subject).", ".quote($status).")"); if ($url ne "") { $message .= "\n".WebGUI::URL::append('http://'.$session{env}{HTTP_HOST}.$url,'mlog='.$messageLogId); diff --git a/lib/WebGUI/Operation/Group.pm b/lib/WebGUI/Operation/Group.pm index 10ab039ca..150f28927 100644 --- a/lib/WebGUI/Operation/Group.pm +++ b/lib/WebGUI/Operation/Group.pm @@ -108,7 +108,27 @@ sub www_editGroup { $f->readOnly($g->groupId,WebGUI::International::get(379)); $f->text("groupName",WebGUI::International::get(84),$g->name); $f->textarea("description",WebGUI::International::get(85),$g->description); - $f->interval("expireAfter",WebGUI::International::get(367), WebGUI::DateTime::secondsToInterval($g->expireAfter)); + $f->interval("expireOffset",WebGUI::International::get(367), WebGUI::DateTime::secondsToInterval($g->expireOffset)); + $f->yesNo( + -name=>"expireNotify", + -value=>$g->expireNotify, + -label=>WebGUI::International::get(865) + ); + $f->integer( + -name=>"expireNotifyOffset", + -value=>$g->expireNotifyOffset, + -label=>WebGUI::International::get(864) + ); + $f->textarea( + -name=>"expireNotifyMessage", + -value=>$g->expireNotifyMessage, + -label=>WebGUI::International::get(866) + ); + $f->integer( + -name=>"deleteOffset", + -value=>$g->deleteOffset, + -label=>WebGUI::International::get(863) + ); if ($session{setting}{useKarma}) { $f->integer("karmaThreshold",WebGUI::International::get(538),$g->karmaThreshold); } @@ -128,9 +148,13 @@ sub www_editGroupSave { my $g = WebGUI::Group->new($session{form}{gid}); $g->description($session{form}{description}); $g->name($session{form}{groupName}); - $g->expireAfter(WebGUI::DateTime::intervalToSeconds($session{form}{expireAfter_interval},$session{form}{expireAfter_units})); + $g->expireOffset(WebGUI::DateTime::intervalToSeconds($session{form}{expireOffset_interval},$session{form}{expireOffset_units})); $g->karmaThreshold($session{form}{karmaThreshold}); $g->ipFilter($session{form}{ipFilter}); + $g->expireNotify($session{form}{expireNotify}); + $g->expireNotifyOffset($session{form}{expireNotifyOffset}); + $g->expireNotifyMessage($session{form}{expireNotifyMessage}); + $g->deleteOffset($session{form}{deleteOffset}); return www_listGroups(); } diff --git a/lib/WebGUI/Operation/User.pm b/lib/WebGUI/Operation/User.pm index 077fbb15e..c700f0dca 100644 --- a/lib/WebGUI/Operation/User.pm +++ b/lib/WebGUI/Operation/User.pm @@ -93,7 +93,7 @@ sub www_addUser { #------------------------------------------------------------------- sub www_addUserSave { - my (@groups, $uid, $u, $gid, $encryptedPassword, $expireAfter, $cmd); + my (@groups, $uid, $u, $gid, $encryptedPassword, $cmd); return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); ($uid) = WebGUI::SQL->quickArray("select userId from users where username=".quote($session{form}{username})); unless ($uid) { diff --git a/sbin/Hourly/DeleteExpiredGroupings.pm b/sbin/Hourly/DeleteExpiredGroupings.pm index 8a74b8e86..2292beb9e 100644 --- a/sbin/Hourly/DeleteExpiredGroupings.pm +++ b/sbin/Hourly/DeleteExpiredGroupings.pm @@ -17,9 +17,14 @@ use WebGUI::SQL; #----------------------------------------- sub process { - if ($session{config}{DeleteExpiredGroupings_offset} ne "") { - WebGUI::SQL->write("delete from groupings where expireDate < " - .(time()-(86400*$session{config}{DeleteExpiredGroupings_offset}))); + my @date = WebGUI::DateTime::localtime(); + if ($date[4] == 3) { # only occurs at 3am on the day in question. + my $sth = WebGUI::SQL->read("select groupId,deleteOffset from groups"); + while (my $data = $sth->hashRef) { + WebGUI::SQL->write("delete from groupings where groupId=$data->{groupId} and expireDate < " + .(time()-(86400*$data->{deleteOffset}))); + } + $sth->finish; } } diff --git a/sbin/Hourly/NotifyExpiredGroupings.pm b/sbin/Hourly/NotifyExpiredGroupings.pm new file mode 100644 index 000000000..ce9aa6dea --- /dev/null +++ b/sbin/Hourly/NotifyExpiredGroupings.pm @@ -0,0 +1,41 @@ +package Hourly::NotifyExpiredGroupings; + +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2002 Plain Black LLC. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use strict; +use WebGUI::DateTime; +use WebGUI::International; +use WebGUI::MessageLog; +use WebGUI::Session; +use WebGUI::SQL; + +#----------------------------------------- +sub process { + my @date = WebGUI::DateTime::localtime(); + if ($date[4] == 1) { # only occurs at 1am on the day in question. + my $now = time(); + my $a = WebGUI::SQL->read("select groupId,expireNotifyOffset,expireNotifyMessage from groups + where expireNotify=1"); + while (my $group = $a->hashRef) { + my $start = $now + (86400 * $group->{expireNotifyOffset}); + my $end = $start + 86400; + my $b = WebGUI::SQL->read("select userId from groupings where expireDate>=".$start." and expireDate<=".$end); + while (my ($userId) = $b->array) { + WebGUI::MessageLog::addEntry($userId,"",WebGUI::International::get(867),$group->{expireNotifyMessage}); + } + $b->finish; + } + $a->finish; + } +} + +1; + diff --git a/sbin/userImport.pl b/sbin/userImport.pl index 4e19360a7..bceda5a0a 100644 --- a/sbin/userImport.pl +++ b/sbin/userImport.pl @@ -56,7 +56,7 @@ $|=1; print "Starting...\n"; -my ($i, $dbh, @row, %user, @field, $userId, $first, $dup, $lineNumber, $expireAfter, @group); +my ($i, $dbh, @row, %user, @field, $userId, $first, $dup, $lineNumber, $expireOffset, @group); $first = 1; $dbh = connectToDb(); open(FILE,"<".$usersFile); @@ -109,13 +109,13 @@ while() { values ($user{userId},'LDAP','$_',".$dbh->quote($user{$_}).")"); } } - ($expireAfter) = WebGUI::SQL->quickArray("select expireAfter from groups where groupId=2",$dbh); + ($expireOffset) = WebGUI::SQL->quickArray("select expireOffset from groups where groupId=2",$dbh); $user{groups} =~ s/ //g; @group = split(/,/,$user{groups}); foreach (@group) { - ($expireAfter) = WebGUI::SQL->quickArray("select expireAfter from groups where groupId=$_",$dbh); + ($expireOffset) = WebGUI::SQL->quickArray("select expireOffset from groups where groupId=$_",$dbh); WebGUI::SQL->write("insert into groupings (groupId,userId,expireDate) values - ($user{userId},$_,".(time()+$expireAfter).")",$dbh); + ($user{userId},$_,".(time()+$expireOffset).")",$dbh); } } }