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 4982874ef..0b10b425c 100644 --- a/docs/upgrades/upgrade_5.0.3-5.1.0.sql +++ b/docs/upgrades/upgrade_5.0.3-5.1.0.sql @@ -26,7 +26,8 @@ INSERT INTO userProfileField VALUES ('signature','WebGUI::International::get(859 INSERT INTO userProfileField VALUES ('publicProfile','WebGUI::International::get(861)',1,0,'yesNo','','[1]',9,4,0); INSERT INTO userProfileField VALUES ('publicEmail','WebGUI::International::get(860)',1,0,'yesNo','','[1]',10,4,0); 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; diff --git a/lib/WebGUI/Form.pm b/lib/WebGUI/Form.pm index 6f3b17ff0..53e2cd415 100644 --- a/lib/WebGUI/Form.pm +++ b/lib/WebGUI/Form.pm @@ -510,18 +510,26 @@ If you want to add anything special to this form element like javascript actions 'onChange="this.form.submit()"' +=item excludeGroups + +An array reference containing a list of groups to exclude from the list. + =back =cut sub group { - my (%hash, $value); + my (%hash, $value, $where); $value = $_[0]->{value}; if ($$value[0] eq "") { #doing long form otherwise arrayRef didn't work $value = [7]; } tie %hash, 'Tie::IxHash'; - %hash = WebGUI::SQL->buildHash("select groupId,groupName from groups where groupName<>'Reserved' order by groupName"); + my $exclude = $_[0]->{excludeGroups}; + if ($$exclude[0] ne "") { + $where = "where groupId not in (".join(",",@$exclude).")"; + } + %hash = WebGUI::SQL->buildHash("select groupId,groupName from groups $where order by groupName"); return selectList({ options=>\%hash, name=>$_[0]->{name}, diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm new file mode 100755 index 000000000..f4501c5c6 --- /dev/null +++ b/lib/WebGUI/Group.pm @@ -0,0 +1,338 @@ +package WebGUI::Group; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2003 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 + ------------------------------------------------------------------- + +=cut + +use strict; +use Tie::CPHash; +use WebGUI::DateTime; +use WebGUI::Grouping; +use WebGUI::Session; +use WebGUI::SQL; + +=head1 NAME + +Package WebGUI::Group + +=head1 DESCRIPTION + +This package provides an object-oriented way of managing WebGUI groups and groupings. + +=head1 SYNOPSIS + + 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"); + + $g->addGroups(\@arr); + $g->deleteGroups(\@arr); + $g->delete; + +=head1 METHODS + +These methods are available from this class: + +=cut + + +#------------------------------------------------------------------- +sub _create { + my $groupId = getNextId("groupId"); + WebGUI::SQL->write("insert into groups (groupId,dateCreated,expireAfter,karmaThreshold) values + ($groupId,".time().",314496000,1000000000)"); + return $groupId; +} + +#------------------------------------------------------------------- + +=head2 addGroups ( groups ) + +Adds groups to this group. + +=over + +=item groups + +An array reference containing the list of group ids to add to this group. + +=back + +=cut + +sub addGroups { + WebGUI::Grouping::addGroupsToGroups($_[1],[$_[0]->{_groupId}]); +} + +#------------------------------------------------------------------- + +=head2 dateCreated ( ) + +Returns the epoch for when this group was created. + +=cut + +sub dateCreated { + return $_[0]->{_group}{dateCreated}; +} + + +#------------------------------------------------------------------- + +=head2 delete ( ) + +Deletes this group and all references to it. + +=cut + +sub delete { + WebGUI::SQL->write("delete from groups where groupId=".$_[0]->{_groupId}); + WebGUI::SQL->write("delete from groupings where groupId=".$_[0]->{_groupId}); + WebGUI::SQL->write("delete from groupGroupings where inGroup=".$_[0]->{_groupId}." or groupId=".$_[0]->{_groupId}); +} + +#------------------------------------------------------------------- + +=head2 deleteGroups ( groups ) + +Deletes groups from this group. + +=over + +=item groups + +An array reference containing the list of group ids to delete from this group. + +=back + +=cut + +sub deleteGroups { + WebGUI::Grouping::deleteGroupsFromGroups($_[1],[$_[0]->{_groupId}]); +} + + +#------------------------------------------------------------------- + +=head2 description ( [ value ] ) + +Returns the description of this group. + +=over + +=item value + +If specified, the description is set to this value. + +=back + +=cut + +sub description { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"description"} = $value; + WebGUI::SQL->write("update groups set description=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"description"}; +} + + +#------------------------------------------------------------------- + +=head2 expireAfter ( [ value ] ) + +Returns the number of seconds any grouping with this group should expire after. + +=over + +=item value + +If specified, expireAfter is set to this value. + +=back + +=cut + +sub expireAfter { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"expireAfter"} = $value; + WebGUI::SQL->write("update groups set expireAfter=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"expireAfter"}; +} + + +#------------------------------------------------------------------- + +=head2 groupId ( ) + +Returns the groupId for this group. + +=cut + +sub groupId { + return $_[0]->{_groupId}; +} + + +#------------------------------------------------------------------- + +=head2 karmaThreshold ( [ value ] ) + +Returns the amount of karma required to be in this group. + +=over + +=item value + +If specified, the karma threshold is set to this value. + +=back + +=cut + +sub karmaThreshold { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"karmaThreshold"} = $value; + WebGUI::SQL->write("update groups set karmaThreshold=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"karamThreshold"}; +} + + +#------------------------------------------------------------------- + +=head2 ipFilter ( [ value ] ) + +Returns the ip address range(s) the user must be a part of to belong to this group. + +=over + +=item value + +If specified, the ipFilter is set to this value. + +=back + +=cut + +sub ipFilter { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"ipFilter"} = $value; + WebGUI::SQL->write("update groups set ipFilter=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"ipFilter"}; +} + + +#------------------------------------------------------------------- + +=head2 lastUpdated ( ) + +Returns the epoch for when this group was last modified. + +=cut + +sub lastUpdated { + return $_[0]->{_group}{lastUpdated}; +} + + +#------------------------------------------------------------------- + +=head2 name ( [ value ] ) + +Returns the name of this group. + +=over + +=item value + +If specified, the name is set to this value. + +=back + +=cut + +sub name { + my ($class, $value); + $class = shift; + $value = shift; + if (defined $value) { + $class->{_group}{"groupName"} = $value; + WebGUI::SQL->write("update groups set groupName=".quote($value).", + lastUpdated=".time()." where groupId=$class->{_groupId}"); + } + return $class->{_group}{"groupName"}; +} + + +#------------------------------------------------------------------- + +=head2 new ( groupId ) + +Constructor. + +=over + +=item groupId + +The groupId of the group you're creating an object reference for. If specified as "new" then a new group will be created and assigned the next available groupId. If left blank then the object methods will just return default values for everything. + +=back + +=cut + +sub new { + my ($class, $groupId, %default, $value, $key, %group, %profile); + tie %group, 'Tie::CPHash'; + $class = shift; + $groupId = shift; + $groupId = _create() if ($groupId eq "new"); + if ($groupId eq "") { + $group{expireAfter} = 314496000; + $group{karmaThreshold} = 1000000000; + $group{groupName} = "New Group"; + } else { + %group = WebGUI::SQL->quickHash("select * from groups where groupId='$groupId'"); + } + bless {_groupId => $groupId, _group => \%group }, $class; +} + + + +1; diff --git a/lib/WebGUI/Grouping.pm b/lib/WebGUI/Grouping.pm new file mode 100755 index 000000000..4f0d103e1 --- /dev/null +++ b/lib/WebGUI/Grouping.pm @@ -0,0 +1,299 @@ +package WebGUI::Grouping; + +=head1 LEGAL + + ------------------------------------------------------------------- + WebGUI is Copyright 2001-2003 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 + ------------------------------------------------------------------- + +=cut + +use strict; +use WebGUI::DateTime; +use WebGUI::Session; +use WebGUI::SQL; + +=head1 NAME + +Package WebGUI::Grouping + +=head1 DESCRIPTION + +This package provides an interface for managing WebGUI user and group groupings. + +=head1 SYNOPSIS + + use WebGUI::Grouping; + WebGUI::Grouping::addGroupsToGroups(\@groups, \@toGroups); + WebGUI::Grouping::addUsersToGroups(\@users, \@toGroups); + WebGUI::Grouping::deleteGroupsFromGroups(\@groups, \@fromGroups); + WebGUI::Grouping::deleteUsersFromGroups(\@users, \@fromGroups); + $arrayRef = WebGUI::Grouping::getGroupsForGroup($groupId); + $arrayRef = WebGUI::Grouping::getGroupsForUser($userId); + $arrayRef = WebGUI::Grouping::getGroupsInGroup($groupId); + $arrayRef = WebGUI::Grouping::getUsersInGroup($groupId); + $epoch = WebGUI::Grouping::userGroupExpireDate($userId,$groupId); + +=head1 METHODS + +These functions are available from this package: + +=cut + + + +#------------------------------------------------------------------- + +=head2 addGroupsToGroups ( groups, toGroups ) + +Adds groups to a group. + +=over + +=item groups + +An array reference containing the list of group ids to add. + +=item toGroups + +An array reference containing the list of group ids to add the first list to. + +=back + +=cut + +sub addGroupsToGroups { + foreach my $gid (@{$_[0]}) { + foreach my $toGid (@{$_[1]}) { + my ($isIn) = WebGUI::SQL->quickArray("select count(*) from groupGroupings + where groupId=$gid and inGroup=$toGid"); + unless ($isIn) { + WebGUI::SQL->write("insert into groupGroupings (groupId,inGroup) values ($gid,$toGid)"); + } + } + } +} + + +#------------------------------------------------------------------- + +=head2 addUsersToGroups ( users, groups ) + +Adds users to the specified groups. + +=over + +=item users + +An array reference containing a list of users. + +=item groups + +An array reference containing a list of groups. + +=back + +=cut + +sub addUsersToGroups { + foreach my $gid (@{$_[1]}) { + my ($expireAfter) = WebGUI::SQL->quickArray("select expireAfter 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).")"); + } + } + } +} + + +#------------------------------------------------------------------- + +=head2 deleteGroupsFromGroups ( groups, fromGroups ) + +Deletes groups from these groups. + +=over + +=item groups + +An array reference containing the list of group ids to delete. + +=item fromGroups + +An array reference containing the list of group ids to delete from. + +=back + +=cut + +sub deleteGroupsFromGroups { + foreach my $gid (@{$_[0]}) { + foreach my $fromGid (@{$_[1]}) { + WebGUI::SQL->write("delete from groupGroupings where groupId=$gid and inGroup=".$fromGid); + } + } +} + + +#------------------------------------------------------------------- + +=head2 deleteUsersFromGroups ( users, groups ) + +Deletes a list of users from the specified groups. + +=over + +=item users + +An array reference containing a list of users. + +=item groups + +An array reference containing a list of groups. + +=back + +=cut + +sub deleteUsersFromGroups { + foreach my $gid (@{$_[1]}) { + foreach my $uid (@{$_[0]}) { + WebGUI::SQL->write("delete from groupings where groupId=$gid and userId=$uid"); + } + } +} + + +#------------------------------------------------------------------- + +=head2 getGroupsForGroup ( groupId ) + +Returns an array reference containing a list of groups the specified group is in. + +=over + +=item groupId + +A unique identifier for the group. + +=back + +=cut + +sub getGroupsForGroup { + return WebGUI::SQL->buildArrayRef("select inGroup from groupGroupings where groupId=$_[0]"); +} + + +#------------------------------------------------------------------- + +=head2 getGroupsForUser ( userId ) + +Returns an array reference containing a list of groups the specified user is in. + +=over + +=item userId + +A unique identifier for the user. + +=back + +=cut + +sub getGroupsForUser { + return WebGUI::SQL->buildArrayRef("select groupId from groupings where userId=$_[0]"); +} + + +#------------------------------------------------------------------- + +=head2 getGroupsInGroup ( groupId ) + +Returns an array reference containing a list of groups that belong to the specified group. + +=over + +=item groupId + +A unique identifier for the group. + +=back + +=cut + +sub getGroupsInGroup { + return WebGUI::SQL->buildArrayRef("select groupId from groupGroupings where inGroup=$_[0]"); +} + + +#------------------------------------------------------------------- + +=head2 getUsersInGroup ( groupId ) + +Returns an array reference containing a list of users that belong to the specified group. + +=over + +=item groupId + +A unique identifier for the group. + +=back + +=cut + +sub getUsersInGroup { + return WebGUI::SQL->buildArrayRef("select userId from groupings where groupId=$_[0]"); +} + + + +#------------------------------------------------------------------- + +=head2 userGroupExpireDate ( userId, groupId [, epoch ] ) + +Returns the epoch date that this grouping will expire. + +=over + +=item userId + +An integer that is the unique identifier for a user. + +=item groupId + +An integer that is the unique identifier for a group. + +=item epoch + +If specified the expire date will be set to this value. + +=back + +=cut + +sub userGroupExpireDate { + if ($_[2]) { + WebGUI::SQL->write("update groupings set expireDate=$_[2] where groupId=$_[1] and userId=$_[0]"); + return $_[2]; + } else { + my ($expireDate) = WebGUI::SQL->quickArray("select expireDate from groupings + where groupId=$_[1] and userId=$_[0]"); + return $expireDate; + } +} + + + +1; + diff --git a/lib/WebGUI/HTMLForm.pm b/lib/WebGUI/HTMLForm.pm index 72213035f..b25ab87c7 100644 --- a/lib/WebGUI/HTMLForm.pm +++ b/lib/WebGUI/HTMLForm.pm @@ -599,7 +599,7 @@ sub file { #------------------------------------------------------------------- -=head2 group ( name [ label, value, size, multiple, extras, subtext, uiLevel ] ) +=head2 group ( name [ label, value, size, multiple, extras, subtext, uiLevel, excludeGroups ] ) Adds a group pull-down to this form. A group pull down provides a select list that provides name value pairs for all the groups in the WebGUI system. @@ -639,6 +639,10 @@ Extra text to describe this form element or to provide special instructions. The UI level for this field. See the WebGUI developer's site for details. Defaults to "0". +=item excludeGroups + +An array reference containing a list of groups to exclude from the list. + =back =cut @@ -646,15 +650,16 @@ The UI level for this field. See the WebGUI developer's site for details. Defaul sub group { my ($output); my ($self, @p) = @_; - my ($name, $label, $value, $size, $multiple, $extras, $subtext, $uiLevel) = - rearrange([qw(name label value size multiple extras subtext uiLevel)], @p); + my ($name, $label, $value, $size, $multiple, $extras, $subtext, $uiLevel, $excludeGroups) = + rearrange([qw(name label value size multiple extras subtext uiLevel excludeGroups)], @p); if (_uiLevelChecksOut($uiLevel)) { $output = WebGUI::Form::group({ "name"=>$name, "size"=>$size, "value"=>$value, "multiple"=>$multiple, - "extras"=>$extras + "extras"=>$extras, + excludeGroups=>$excludeGroups }); $output .= _subtext($subtext); $output = $self->_tableFormRow($label,$output); diff --git a/lib/WebGUI/Operation/Group.pm b/lib/WebGUI/Operation/Group.pm index a80a52c4e..10ab039ca 100644 --- a/lib/WebGUI/Operation/Group.pm +++ b/lib/WebGUI/Operation/Group.pm @@ -14,6 +14,8 @@ use Exporter; use strict; use Tie::CPHash; use WebGUI::DateTime; +use WebGUI::Group; +use WebGUI::Grouping; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; @@ -54,9 +56,7 @@ sub www_addGroupsToGroupSave { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); my (@groups, $group); @groups = $session{cgi}->param('groups'); - foreach $group (@groups) { - WebGUI::SQL->write("insert into groupGroupings values ($group,$session{form}{gid})"); - } + WebGUI::Grouping::addGroupsToGroups(\@groups,[$session{form}{gid}]); return www_manageGroupsInGroup(); } @@ -79,46 +79,42 @@ sub www_deleteGroup { sub www_deleteGroupConfirm { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); return WebGUI::Privilege::vitalComponent() if ($session{form}{gid} < 26); - WebGUI::SQL->write("delete from groups where groupId=$session{form}{gid}"); - WebGUI::SQL->write("delete from groupings where groupId=$session{form}{gid}"); + my $g = WebGUI::Group->new($session{form}{gid}); + $g->delete; return www_listGroups(); } #------------------------------------------------------------------- sub www_deleteGroupGrouping { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - WebGUI::SQL->write("delete from groupGroupings where inGroup=$session{form}{gid} and groupId=$session{form}{delete}"); + WebGUI::Grouping::deleteGroupsFromGroups([$session{form}{delete}],[$session{form}{gid}]); return www_manageGroupsInGroup(); } #------------------------------------------------------------------- sub www_editGroup { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - my ($output, %group, $f); - tie %group, 'Tie::CPHash'; + my ($output, $f, $g); if ($session{form}{gid} eq "new") { - $group{expireAfter} = 314496000; - $group{karmaThreshold} = 1000000000; + $g = WebGUI::Group->new(""); } else { - %group = WebGUI::SQL->quickHash("select * from groups where groupId=$session{form}{gid}"); + $g = WebGUI::Group->new($session{form}{gid}); } $output .= helpIcon(17); $output .= '

'.WebGUI::International::get(87).'

'; $f = WebGUI::HTMLForm->new; $f->hidden("op","editGroupSave"); $f->hidden("gid",$session{form}{gid}); - $f->readOnly($session{form}{gid},WebGUI::International::get(379)); - $f->text("groupName",WebGUI::International::get(84),$group{groupName}); - $f->textarea("description",WebGUI::International::get(85),$group{description}); - $f->interval("expireAfter",WebGUI::International::get(367), WebGUI::DateTime::secondsToInterval($group{expireAfter})); + $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)); if ($session{setting}{useKarma}) { - $f->integer("karmaThreshold",WebGUI::International::get(538),$group{karmaThreshold}); - } else { - $f->hidden("karmaThreshold",$group{karmaThreshold}); + $f->integer("karmaThreshold",WebGUI::International::get(538),$g->karmaThreshold); } $f->text( -name=>"ipFilter", - -value=>$group{ipFilter}, + -value=>$g->ipFilter, -label=>WebGUI::International::get(857) ); $f->submit; @@ -129,17 +125,12 @@ sub www_editGroup { #------------------------------------------------------------------- sub www_editGroupSave { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - if ($session{form}{gid} eq "new") { - $session{form}{gid} = getNextId("groupId"); - WebGUI::SQL->write("insert into groups (groupId) values ($session{form}{gid})"); - } - WebGUI::SQL->write("update groups set groupName=".quote($session{form}{groupName}).", - description=".quote($session{form}{description}).", - expireAfter='".WebGUI::DateTime::intervalToSeconds($session{form}{expireAfter_interval}, - $session{form}{expireAfter_units})."', - karmaThreshold='$session{form}{karmaThreshold}', - ipFilter=".quote($session{form}{ipFilter})." - where groupId=".$session{form}{gid}); + 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->karmaThreshold($session{form}{karmaThreshold}); + $g->ipFilter($session{form}{ipFilter}); return www_listGroups(); } @@ -218,21 +209,22 @@ sub www_listGroups { #------------------------------------------------------------------- sub www_manageGroupsInGroup { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - my ($output, $p, $group, $groups, @array, $f); + my ($output, $p, $group, $groups, $f); $output = '

'.WebGUI::International::get(813).'

'; $f = WebGUI::HTMLForm->new; $f->hidden("op","addGroupsToGroupSave"); $f->hidden("gid",$session{form}{gid}); - @array = WebGUI::SQL->buildArray("select groupId from groupGroupings where inGroup='$session{form}{gid}'"); - push(@array,$session{form}{gid}); - # push(@array,1); #visitors - # push(@array,2); #registered users - # push(@array,7); #everyone - $groups = WebGUI::SQL->buildHashRef("select groupId,groupName from groups where groupId not in (".join(",",@array).") order by groupName"); - $f->select("groups",$groups,WebGUI::International::get(605),[],5,1); + $groups = WebGUI::Grouping::getGroupsInGroup($session{form}{gid}); + push(@$groups,$session{form}{gid}); + $f->group( + -name=>"groups", + -excludeGroups=>$groups, + -label=>WebGUI::International::get(605), + -size=>5, + -multiple=>1 + ); $f->submit; $output .= $f->print; - $output .= '

'; $output .= ''; $p = WebGUI::Paginator->new(WebGUI::URL::page('op=manageGroupsInGroup')); diff --git a/lib/WebGUI/Operation/User.pm b/lib/WebGUI/Operation/User.pm index 5b1b2d5f3..077fbb15e 100644 --- a/lib/WebGUI/Operation/User.pm +++ b/lib/WebGUI/Operation/User.pm @@ -15,6 +15,8 @@ use strict qw(vars subs); use Tie::CPHash; use Tie::IxHash; use WebGUI::DateTime; +use WebGUI::Group; +use WebGUI::Grouping; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; @@ -55,7 +57,7 @@ sub _submenu { #------------------------------------------------------------------- sub www_addUser { - my (@array, $output, $groups, $f, $cmd, $html, %status); + my ($output, $f, $cmd, $html, %status); return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); $output .= helpIcon(5); $output .= '

'.WebGUI::International::get(163).'

'; @@ -73,11 +75,13 @@ sub www_addUser { Deactivated =>WebGUI::International::get(818) ); $f->select("status",\%status,WebGUI::International::get(816), ['Active']); - push(@array,1); #visitors - push(@array,2); #registered users - push(@array,7); #everyone - $groups = WebGUI::SQL->buildHashRef("select groupId,groupName from groups where groupId not in (".join(",",@array).") order by groupName"); - $f->select("groups",$groups,WebGUI::International::get(605),[],5,1); + $f->group( + -name=>"groups", + -excludeGroups=>[1,2,7], + -label=>WebGUI::International::get(605), + -size=>5, + -multiple=>1 + ); $f->select("authMethod",$session{authentication},WebGUI::International::get(164),[$session{setting}{authMethod}]); foreach (keys %{$session{authentication}}) { $f->raw(WebGUI::Authentication::adminForm(0,$_)); @@ -178,17 +182,17 @@ sub www_deleteUserConfirm { #------------------------------------------------------------------- sub www_editGrouping { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - my ($output, $username, $group, $expireDate, $f); + my ($output, $expireDate, $f); $output .= '

'.WebGUI::International::get(370).'

'; $f = WebGUI::HTMLForm->new; $f->hidden("op","editGroupingSave"); $f->hidden("uid",$session{form}{uid}); $f->hidden("gid",$session{form}{gid}); - ($username) = WebGUI::SQL->quickArray("select username from users where userId=$session{form}{uid}"); - ($group) = WebGUI::SQL->quickArray("select groupName from groups where groupId=$session{form}{gid}"); - ($expireDate) = WebGUI::SQL->quickArray("select expireDate from groupings where groupId=$session{form}{gid} and userId=$session{form}{uid}"); - $f->readOnly($username,WebGUI::International::get(50)); - $f->readOnly($group,WebGUI::International::get(84)); + my $u = WebGUI::User->new($session{form}{uid}); + my $g = WebGUI::Group->new($session{form}{gid}); + $expireDate = WebGUI::Grouping::userGroupExpireDate($session{form}{uid},$session{form}{gid}); + $f->readOnly($u->username,WebGUI::International::get(50)); + $f->readOnly($g->name,WebGUI::International::get(84)); $f->date("expireDate",WebGUI::International::get(369),$expireDate); $f->submit; $output .= $f->print; @@ -198,7 +202,7 @@ sub www_editGrouping { #------------------------------------------------------------------- sub www_editGroupingSave { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - WebGUI::SQL->write("update groupings set expireDate=".setToEpoch($session{form}{expireDate})." where groupId=$session{form}{gid} and userId=$session{form}{uid}"); + WebGUI::Grouping::userGroupExpireDate($session{form}{uid},$session{form}{gid},setToEpoch($session{form}{expireDate})); return www_editUserGroup(); } @@ -255,18 +259,23 @@ sub www_editUserSave { #------------------------------------------------------------------- sub www_editUserGroup { return WebGUI::Privilege::adminOnly() unless (WebGUI::Privilege::isInGroup(3)); - my ($output, $f, $groups, @array, $sth, %hash); + my ($output, $f, $groups, $sth, %hash); tie %hash, 'Tie::CPHash'; $output .= '

'.WebGUI::International::get(372).'

'; $f = WebGUI::HTMLForm->new; $f->hidden("op","addUserToGroupSave"); $f->hidden("uid",$session{form}{uid}); - @array = WebGUI::SQL->buildArray("select groupId from groupings where userId=$session{form}{uid}"); - push(@array,1); #visitors - push(@array,2); #registered users - push(@array,7); #everyone - $groups = WebGUI::SQL->buildHashRef("select groupId,groupName from groups where groupId not in (".join(",",@array).") order by groupName"); - $f->select("groups",$groups,WebGUI::International::get(605),[],5,1); + $groups = WebGUI::Grouping::getGroupsForUser($session{form}{uid}); + push(@$groups,1); #visitors + push(@$groups,2); #registered users + push(@$groups,7); #everyone + $f->group( + -name=>"groups", + -excludeGroups=>$groups, + -label=>WebGUI::International::get(605), + -size=>5, + -multiple=>1 + ); $f->submit; $output .= $f->print; $output .= '

'.WebGUI::International::get(84).'
'.WebGUI::International::get(89). diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index 6e4111a88..780c8ab42 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -47,6 +47,7 @@ Package for interfacing with SQL databases. This package implements Perl DBI fun $sth->finish; @arr = WebGUI::SQL->buildArray($sql); + $arrayRef = WebGUI::SQL->buildArrayRef($sql); %hash = WebGUI::SQL->buildHash($sql); $hashRef = WebGUI::SQL->buildHashRef($sql); @arr = WebGUI::SQL->quickArray($sql); @@ -112,6 +113,30 @@ sub buildArray { return @array; } +#------------------------------------------------------------------- + +=head2 buildArrayRef ( sql [, dbh ] ) + +Builds an array reference of data from a series of rows. + +=over + +=item sql + +An SQL query. The query must select only one column of data. + +=item dbh + +By default this method uses the WebGUI database handler. However, you may choose to pass in your own if you wish. + +=back + +=cut + +sub buildArrayRef { + my @array = $_[0]->buildArray($_[1],$_[2]); + return \@array; +} #------------------------------------------------------------------- diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index 24bb7c094..f733e8cc4 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -15,6 +15,8 @@ package WebGUI::User; =cut use strict; +use WebGUI::DateTime; +use WebGUI::Grouping; use WebGUI::HTMLForm; use WebGUI::International; use WebGUI::Session; @@ -78,14 +80,7 @@ An array reference containing a list of groups. =cut sub addToGroups { - my ($class, $groups, $gid, $expireAfter); - $class = shift; - $groups = shift; - foreach $gid (@$groups) { - ($expireAfter) = WebGUI::SQL->quickArray("select expireAfter from groups where groupId=$gid"); - WebGUI::SQL->write("insert into groupings values ($gid, $class->{_userId}, ".(time()+$expireAfter).")"); - } - WebGUI::SQL->write("update users set lastUpdated=".time()." where userId=".$class->{_userId}); + WebGUI::Grouping::addUsersToGroups([$_[0]->{_userId}],$_[1]); } #------------------------------------------------------------------- @@ -141,10 +136,14 @@ sub delete { $class = shift; WebGUI::SQL->write("delete from users where userId=".$class->{_userId}); WebGUI::SQL->write("delete from userProfileData where userId=".$class->{_userId}); - WebGUI::SQL->write("delete from groupings where userId=".$class->{_userId}); + WebGUI::Grouping::deleteUsersFromGroups([$class->{_userId}],WebGUI::Grouping::getGroupsForUser($class->{_userId})); WebGUI::SQL->write("delete from messageLog where userId=".$class->{_userId}); - WebGUI::SQL->write("delete from userSession where userId=".$class->{_userId}); WebGUI::Authentication::deleteParams($class->{_userId}); + my $sth = WebGUI::SQL->read("select sessionId from userSession where userId=$class->{_userId}"); + while (my ($sid) = $sth->array) { + WebGUI::Sesssion::end($sid); + } + $sth->finish; } #------------------------------------------------------------------- @@ -164,13 +163,7 @@ An array reference containing a list of groups. =cut sub deleteFromGroups { - my ($class, $groups, $gid); - $class = shift; - $groups = shift; - foreach $gid (@$groups) { - WebGUI::SQL->write("delete from groupings where groupId=$gid and userId=$class->{_userId}"); - } - WebGUI::SQL->write("update users set lastUpdated=".time()." where userId=".$class->{_userId}); + WebGUI::Grouping::deleteUsersFromGroups([$_[0]->{_userId}],$_[1]); } #-------------------------------------------------------------------