Make the user factory return the same object if the same user is requested multiple times. This means that if the currect user is changed on the fly, the changes actually take effect instead of there being a potential race condition between the change, the access of the currect user, and the objects going out of scope and updating the database.

This commit is contained in:
ian%hixie.ch 2002-05-04 01:09:27 +00:00
parent 565188440e
commit 50cd6af032

View File

@ -85,12 +85,20 @@ sub getUserByContactDetails {
sub getUserByID { sub getUserByID {
my $self = shift; my $self = shift;
my($app, $userID) = @_; my($app, $userID) = @_;
# do we already have that user?
my $user = $app->getObject("user.$userID");
if (defined($user)) {
# got a user created already, return that
return $user;
} else {
# that user isn't in our system yet, look it up
my(@data) = $app->getService('dataSource.user')->getUserByID($app, $userID); my(@data) = $app->getService('dataSource.user')->getUserByID($app, $userID);
if (@data) { if (@data) {
return $self->objectCreate($app, @data); return $self->objectCreate($app, @data);
} else { } else {
return undef; return undef;
} }
}
} }
sub getNewUser { sub getNewUser {
@ -100,11 +108,30 @@ sub getNewUser {
} }
sub objectProvides { sub objectProvides {
my $class = shift; my $self = shift;
my($service) = @_; my($service) = @_;
return ($service eq 'user' or $class->SUPER::objectProvides($service)); return ($service eq 'user' or
$service eq 'user.'.($self->userID) or
$self->SUPER::objectProvides($service));
} }
# We don't need this yet, so it's commented out as an optimisation.
# # Override the default constructor to check to see if the user already
# # has an object associated with them in the system, and if they do,
# # use that instead of creating a new one.
# sub objectCreate {
# my $class = shift;
# my($app, $userID, @data) @_;
# return $app->getObject("user.$userID") || $class->SUPER::objectCreate($app, $userID, @data);
# # more strictly:
# #
# # my $user = $app->getObject("user.$userID");
# # if (not defined($user)) {
# # $user = $class->SUPER::objectCreate($app, $userID, @data);
# # }
# # return $user;
# }
sub objectInit { sub objectInit {
my $self = shift; my $self = shift;
my($app, $userID, $mode, $password, $adminMessage, $fields, $groups, $rights) = @_; my($app, $userID, $mode, $password, $adminMessage, $fields, $groups, $rights) = @_;