Change getGroups to always return safe copies of data.

Add tests to check for safe copies.
Add a test for getGroupIdsRecursive.
This commit is contained in:
Colin Kuskie 2008-08-02 22:48:32 +00:00
parent 19987b53b4
commit fd62170df6
2 changed files with 52 additions and 35 deletions

View file

@ -304,7 +304,8 @@ sub getFirstName {
=head2 getGroups ( [ withoutExpired ] )
Returns an array reference containing a list of groups this user is in.
Returns an array reference containing a list of groups this user is in. Group lookups are cached.
If a cached lookup is returned, it will be a safe copy of the data in the cache.
=head3 withoutExpired
@ -313,26 +314,30 @@ If set to "1" then the listing will not include expired groupings. Defaults to "
=cut
sub getGroups {
my $self = shift;
my $withoutExpired = shift;
my $clause = "";
if ($withoutExpired) {
$clause = "and expireDate>".$self->session->datetime->time();
}
my $gotGroupsForUser = $self->session->stow->get("gotGroupsForUser");
if (exists $gotGroupsForUser->{$self->userId}) {
return $gotGroupsForUser->{$self->userId};
} else {
my @groups = $self->session->db->buildArray("select groupId from groupings where userId=? $clause", [$self->userId]);
my $isInGroup = $self->session->stow->get("isInGroup");
foreach my $gid (@groups) {
$isInGroup->{$self->userId}{$gid} = 1;
}
$self->session->stow->set("isInGroup",$isInGroup);
$gotGroupsForUser->{$self->userId} = \@groups;
$self->session->stow->set("gotGroupsForUser",$gotGroupsForUser);
return \@groups;
my $self = shift;
my $withoutExpired = shift;
my $clause = "";
if ($withoutExpired) {
$clause = "and expireDate>".$self->session->datetime->time();
}
my $gotGroupsForUser = $self->session->stow->get("gotGroupsForUser");
if (exists $gotGroupsForUser->{$self->userId}) {
my $cachedGroups = $gotGroupsForUser->{$self->userId};
my @safeCopy = @{ $cachedGroups };
return \@safeCopy;
}
else {
my @groups = $self->session->db->buildArray("select groupId from groupings where userId=? $clause", [$self->userId]);
my $isInGroup = $self->session->stow->get("isInGroup");
foreach my $gid (@groups) {
$isInGroup->{$self->userId}{$gid} = 1;
}
$self->session->stow->set("isInGroup",$isInGroup);
$gotGroupsForUser->{$self->userId} = \@groups;
$self->session->stow->set("gotGroupsForUser",$gotGroupsForUser);
my @safeGroups = @groups;
return \@safeGroups;
}
}
#----------------------------------------------------------------------------
@ -357,7 +362,7 @@ sub getGroupIdsRecursive {
$groupIds{ $groupGroupingId } = 1;
}
}
return [ keys %groupIds ];
}