diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm index 45eb40369..a8c601b11 100755 --- a/lib/WebGUI/Group.pm +++ b/lib/WebGUI/Group.pm @@ -105,7 +105,8 @@ Adds groups to this group. =head3 groups -An array reference containing the list of group ids to add. +An array reference containing the list of group ids to add. Group Visitor may +not be added to any group. Groups may not be added to themselves. =cut @@ -115,6 +116,7 @@ sub addGroups { $self->session->stow->delete("isInGroup"); foreach my $gid (@{$groups}) { next if ($gid eq '1'); + next if ($gid eq $self->getId); my ($isIn) = $self->session->db->quickArray("select count(*) from groupGroupings where groupId=? and inGroup=?", [$gid, $self->getId]); my $group = WebGUI::Group->new($self->session, $gid); my $recursive = isIn($self->getId, @{$group->getGroupsIn(1)}); @@ -122,6 +124,7 @@ sub addGroups { $self->session->db->write("insert into groupGroupings (groupId,inGroup) values (?,?)",[$gid, $self->getId]); } } + $self->session->stow->delete("gotGroupsInGroup"); return 1; } @@ -501,7 +504,8 @@ sub getGroupsIn { my $gotGroupsInGroup = $self->session->stow->get("gotGroupsInGroup"); if ($isRecursive && exists($gotGroupsInGroup->{recursive}{$self->getId})) { return $gotGroupsInGroup->{recursive}{$self->getId}; - } elsif (exists $gotGroupsInGroup->{direct}{$self->getId}) { + } + elsif (!$isRecursive and exists($gotGroupsInGroup->{direct}{$self->getId})) { return $gotGroupsInGroup->{direct}{$self->getId}; } my $groups = $self->session->db->buildArrayRef("select groupId from groupGroupings where inGroup=?",[$self->getId]); @@ -518,6 +522,7 @@ sub getGroupsIn { my %unique = map { $_ => 1 } @groupsOfGroups; @groupsOfGroups = keys %unique; $gotGroupsInGroup->{recursive}{$self->getId} = \@groupsOfGroups; + $self->session->stow->set("gotGroupsInGroup", $gotGroupsInGroup); return \@groupsOfGroups; } $gotGroupsInGroup->{direct}{$self->getId} = $groups; diff --git a/t/Group.t b/t/Group.t index 57be22db5..295cb7b6e 100644 --- a/t/Group.t +++ b/t/Group.t @@ -80,28 +80,28 @@ ok( ($gA->name eq 'Group A' and $gB->name eq 'Group B'), 'object name assignment $gB->addGroups([$gA->getId]); -cmp_bag([$gA->getId, 3], $gB->getGroupsIn(1) ,'Group A is in Group B, recursively'); -cmp_bag([$gA->getId, 3], $gB->getGroupsIn() ,'Group A is in Group B'); -cmp_bag([$gB->getId], $gA->getGroupsFor() ,'Group B contains Group A'); -cmp_bag([3], $gA->getGroupsIn() ,'Admin added to group A automatically'); +cmp_bag($gB->getGroupsIn(1), [$gA->getId, 3], 'Group A is in Group B, recursively'); +cmp_bag($gB->getGroupsIn(), [$gA->getId, 3], 'Group A is in Group B'); +cmp_bag($gA->getGroupsFor(), [$gB->getId], 'Group B contains Group A'); +cmp_bag($gA->getGroupsIn(), [3], 'Admin added to group A automatically'); $gA->addGroups([$gB->getId]); -cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to create recursive group loops'); +cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to create recursive group loops'); $gA->addGroups([1]); -cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to add group Visitor to a group'); +cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to add group Visitor to a group'); $gA->addGroups([$gA->getId]); -cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to add myself to my group'); +cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to add myself to my group'); my $gC = WebGUI::Group->new($session, "new"); $gC->name('Group C'); $gA->addGroups([$gC->getId]); -cmp_bag([$gA->getId], $gC->getGroupsFor() ,'Group A contains Group C'); -cmp_bag([3], $gA->getGroupsIn() ,'Group C is a member of Group A, cached'); -cmp_bag([$gA->getId, 3], $gB->getGroupsIn() ,'Group A is in Group B, cached result'); -cmp_bag([$gA->getId, 3], $gB->getGroupsIn(1) ,'Group C is in Group B, recursively, cached result'); +cmp_bag($gC->getGroupsFor(), [$gA->getId], 'Group A contains Group C'); +cmp_bag($gA->getGroupsIn(), [$gC->getId, 3], 'Group C is a member of Group A, cached'); +cmp_bag($gB->getGroupsIn(1), [$gC->getId, $gA->getId, 3], 'Group C is in Group B, recursively, cached result'); +cmp_bag($gB->getGroupsIn(), [$gA->getId, 3], 'Group A is in Group B, cached result'); END { (defined $gA and ref $gA eq 'WebGUI::Group') and $gA->delete;