Move session->env->getIp into session->request->address

This commit is contained in:
Colin Kuskie 2010-07-01 13:11:01 -07:00
parent 565cf955d7
commit 005b9da1ac
20 changed files with 49 additions and 48 deletions

View file

@ -6,7 +6,7 @@ The information contained herein documents the API changes that have occurred in
WebGUI::Config WebGUI::Config
============= ==============
WebGUI::Config->new has a new API. Its WebGUI root parameter has been eliminated. It now only accepts a config file as either an absolute path, or a path relative to WebGUI's etc directory. WebGUI::Config->new has a new API. Its WebGUI root parameter has been eliminated. It now only accepts a config file as either an absolute path, or a path relative to WebGUI's etc directory.
my $config = WebGUI::Config->new($filename); my $config = WebGUI::Config->new($filename);
@ -14,10 +14,19 @@ my $config = WebGUI::Config->new($filename);
WebGUI::Session WebGUI::Session
============= ===============
WebGUI::Session->open has a new API. Its WebGUI root parameter has been eliminated. The config file it is given can be either an absolute path, or a path relative to WebGUI's etc directory. WebGUI::Session->open has a new API. Its WebGUI root parameter has been eliminated. The config file it is given can be either an absolute path, or a path relative to WebGUI's etc directory.
my $session = WebGUI::Session->open($configFile, $request, $server); my $session = WebGUI::Session->open($configFile);
perldoc WebGUI::Session for more details about the arguments.
WebGUI::Session::Env
====================
WebGUI::Session::Env has been moved into WebGUI::Session::Request. A listing of replacements and equivalents follows:
$session->env->getIp => $session->request->address

View file

@ -830,7 +830,7 @@ sub hasRated {
return 1 if $self->isPoster; return 1 if $self->isPoster;
my $flag = 0; my $flag = 0;
if ($self->session->user->isVisitor) { if ($self->session->user->isVisitor) {
($flag) = $self->session->db->quickArray("select count(*) from Post_rating where assetId=? and ipAddress=?",[$self->getId, $self->session->env->getIp]); ($flag) = $self->session->db->quickArray("select count(*) from Post_rating where assetId=? and ipAddress=?",[$self->getId, $self->session->request->address]);
} else { } else {
($flag) = $self->session->db->quickArray("select count(*) from Post_rating where assetId=? and userId=?",[$self->getId, $self->session->user->userId]); ($flag) = $self->session->db->quickArray("select count(*) from Post_rating where assetId=? and userId=?",[$self->getId, $self->session->user->userId]);
} }
@ -895,7 +895,7 @@ sub insertUserPostRating {
$self->session->db->write("insert into Post_rating (assetId,userId,ipAddress,dateOfRating,rating) values (?,?,?,?,?)", $self->session->db->write("insert into Post_rating (assetId,userId,ipAddress,dateOfRating,rating) values (?,?,?,?,?)",
[$self->getId, [$self->getId,
$self->session->user->userId, $self->session->user->userId,
$self->session->env->getIp, $self->session->request->address,
time(), time(),
$rating,] $rating,]
); );

View file

@ -203,7 +203,7 @@ sub _hasVoted {
my $self = shift; my $self = shift;
my ($hasVoted) = $self->session->db->quickArray("select count(*) from Poll_answer my ($hasVoted) = $self->session->db->quickArray("select count(*) from Poll_answer
where assetId=".$self->session->db->quote($self->getId)." and ((userId=".$self->session->db->quote($self->session->user->userId)." where assetId=".$self->session->db->quote($self->getId)." and ((userId=".$self->session->db->quote($self->session->user->userId)."
and userId<>'1') or (userId=".$self->session->db->quote($self->session->user->userId)." and ipAddress='".$self->session->env->getIp."'))"); and userId<>'1') or (userId=".$self->session->db->quote($self->session->user->userId)." and ipAddress='".$self->session->request->address."'))");
return $hasVoted; return $hasVoted;
} }
@ -545,7 +545,7 @@ sub www_vote {
my $self = shift; my $self = shift;
my $u; my $u;
if ($self->session->form->process("answer") ne "" && $self->session->user->isInGroup($self->get("voteGroup")) && !($self->_hasVoted())) { if ($self->session->form->process("answer") ne "" && $self->session->user->isInGroup($self->get("voteGroup")) && !($self->_hasVoted())) {
$self->setVote($self->session->form->process("answer"),$self->session->user->userId,$self->session->env->getIp); $self->setVote($self->session->form->process("answer"),$self->session->user->userId,$self->session->request->address);
if ($self->session->setting->get("useKarma")) { if ($self->session->setting->get("useKarma")) {
$self->session->user->karma($self->get("karmaPerVote"),"Poll (".$self->getId.")","Voted on this poll."); $self->session->user->karma($self->get("karmaPerVote"),"Poll (".$self->getId.")","Voted on this poll.");
} }

View file

@ -2045,7 +2045,7 @@ sub responseId {
my $ignoreRevisionDate = $opts{ignoreRevisionDate}; my $ignoreRevisionDate = $opts{ignoreRevisionDate};
my $user = WebGUI::User->new( $self->session, $userId ); my $user = WebGUI::User->new( $self->session, $userId );
my $ip = $self->session->env->getIp; my $ip = $self->session->request->address;
my $responseId = $self->{responseId}; my $responseId = $self->{responseId};
return $responseId if $responseId; return $responseId if $responseId;
@ -2178,7 +2178,7 @@ sub canTakeSurvey {
} }
my $maxResponsesPerUser = $self->maxResponsesPerUser; my $maxResponsesPerUser = $self->maxResponsesPerUser;
my $ip = $self->session->env->getIp; my $ip = $self->session->request->address;
my $userId = $self->session->user->userId(); my $userId = $self->session->user->userId();
my $takenCount = 0; my $takenCount = 0;

View file

@ -565,7 +565,7 @@ sub editThingDataSave {
if ($thingDataId eq "new"){ if ($thingDataId eq "new"){
$thingData{dateCreated} = time(); $thingData{dateCreated} = time();
$thingData{createdById} = $session->user->userId; $thingData{createdById} = $session->user->userId;
$thingData{ipAddress} = $session->env->getIp; $thingData{ipAddress} = $session->request->address;
} }
else { else {
%thingData = $session->db->quickHash("select * from ".$session->db->dbh->quote_identifier("Thingy_".$thingId) %thingData = $session->db->quickHash("select * from ".$session->db->dbh->quote_identifier("Thingy_".$thingId)

View file

@ -301,7 +301,7 @@ sub new {
} }
else { else {
$self->user($session->user); $self->user($session->user);
$self->ipAddress($session->env->getIp); $self->ipAddress($session->request->address);
$self->submissionDate(WebGUI::DateTime->new($session, time)); $self->submissionDate(WebGUI::DateTime->new($session, time));
$entryData{id $self} = {}; $entryData{id $self} = {};
} }

View file

@ -100,7 +100,7 @@ sub _logLogin {
$_[0], $_[0],
$_[1], $_[1],
time(), time(),
$self->session->env->getIp, $self->session->request->address,
$self->session->env->get("HTTP_USER_AGENT"), $self->session->env->get("HTTP_USER_AGENT"),
$self->session->getId, $self->session->getId,
time(), time(),

View file

@ -111,7 +111,7 @@ sub getValue {
my $ua = LWP::UserAgent->new; my $ua = LWP::UserAgent->new;
my $res = $ua->post('http://api-verify.recaptcha.net/verify', { my $res = $ua->post('http://api-verify.recaptcha.net/verify', {
privatekey => $privKey, privatekey => $privKey,
remoteip => $self->session->env->getIp, remoteip => $self->session->request->env->{REMOTE_ADDR},
challenge => $challenge, challenge => $challenge,
response => $response, response => $response,
}); });

View file

@ -271,7 +271,7 @@ sub www_runCronJob {
my $session = shift; my $session = shift;
$session->http->setMimeType("text/plain"); $session->http->setMimeType("text/plain");
$session->http->setCacheControl("none"); $session->http->setCacheControl("none");
unless (isInSubnet($session->env->getIp, $session->config->get("spectreSubnets")) || canView($session)) { unless (isInSubnet($session->request->address, $session->config->get("spectreSubnets")) || canView($session)) {
$session->errorHandler->security("make a Spectre cron job runner request, but we're only allowed to accept requests from ".join(",",@{$session->config->get("spectreSubnets")})."."); $session->errorHandler->security("make a Spectre cron job runner request, but we're only allowed to accept requests from ".join(",",@{$session->config->get("spectreSubnets")}).".");
return "error"; return "error";
} }

View file

@ -59,7 +59,7 @@ sub www_spectreGetSiteData {
if (!defined $subnets) { if (!defined $subnets) {
$subnets = []; $subnets = [];
} }
if (!isInSubnet($session->env->getIp, $subnets)) { if (!isInSubnet($session->request->address, $subnets)) {
$session->errorHandler->security("Tried to make a Spectre workflow data load request, but we're only allowed to accept requests from " $session->errorHandler->security("Tried to make a Spectre workflow data load request, but we're only allowed to accept requests from "
.join(",",@{$subnets})."."); .join(",",@{$subnets}).".");
} }
@ -181,7 +181,7 @@ sub www_spectreTest {
$subnets = []; $subnets = [];
} }
my $sessionIp = $session->env->getIp; my $sessionIp = $session->request->address;
unless (isInSubnet($sessionIp, $subnets)) { unless (isInSubnet($sessionIp, $subnets)) {
$session->errorHandler->security( $session->errorHandler->security(
sprintf "Tried to make a Spectre workflow runner request from %s, but we're only allowed to accept requests from %s", sprintf "Tried to make a Spectre workflow runner request from %s, but we're only allowed to accept requests from %s",

View file

@ -141,7 +141,7 @@ sub canUseService {
my ( $session ) = @_; my ( $session ) = @_;
my $subnets = $session->config->get('serviceSubnets'); my $subnets = $session->config->get('serviceSubnets');
return 1 if !$subnets || !@{$subnets}; return 1 if !$subnets || !@{$subnets};
return 1 if WebGUI::Utility::isInSubnet( $session->env->getIp, $subnets ); return 1 if WebGUI::Utility::isInSubnet( $session->request->address, $subnets );
return 0; # Don't go away mad, just go away return 0; # Don't go away mad, just go away
} }

View file

@ -482,7 +482,7 @@ sub www_runWorkflow {
my $session = shift; my $session = shift;
$session->http->setMimeType("text/plain"); $session->http->setMimeType("text/plain");
$session->http->setCacheControl("none"); $session->http->setCacheControl("none");
unless (isInSubnet($session->env->getIp, $session->config->get("spectreSubnets")) || canRunWorkflow($session)) { unless (isInSubnet($session->request->address, $session->config->get("spectreSubnets")) || canRunWorkflow($session)) {
$session->errorHandler->security("make a Spectre workflow runner request, but we're only allowed to accept requests from ".join(",",@{$session->config->get("spectreSubnets")})."."); $session->errorHandler->security("make a Spectre workflow runner request, but we're only allowed to accept requests from ".join(",",@{$session->config->get("spectreSubnets")}).".");
return "error"; return "error";
} }

View file

@ -20,6 +20,8 @@ use 5.010;
use CHI; use CHI;
use File::Temp qw( tempdir ); use File::Temp qw( tempdir );
use Scalar::Util qw( weaken ); use Scalar::Util qw( weaken );
use HTTP::Message::PSGI;
use HTTP::Request::Common;
use WebGUI::Config; use WebGUI::Config;
use WebGUI::SQL; use WebGUI::SQL;
use WebGUI::User; use WebGUI::User;
@ -480,16 +482,21 @@ sub open {
my $self = { _config => $config }; my $self = { _config => $config };
bless $self, $class; bless $self, $class;
if ($env) { ##No env was passed, so construct one
my $request = WebGUI::Session::Request->new($env); if (! $env) {
$self->{_request} = $request; my $url = 'http://' . $config->get('sitename')->[0];
$self->{_response} = $request->new_response( 200 ); my $request = HTTP::Request::Common::GET($url);
$env = $request->to_psgi;
# Use the WebGUI::Session::Request object to look up the sessionId from cookies, if it
# wasn't given explicitly
$sessionId ||= $request->cookies->{$config->getCookieName};
} }
my $request = WebGUI::Session::Request->new($env);
$self->{_request} = $request;
$self->{_response} = $request->new_response( 200 );
# Use the WebGUI::Session::Request object to look up the sessionId from cookies, if it
# wasn't given explicitly
$sessionId ||= $request->cookies->{$config->getCookieName};
# If the sessionId is still unset or is invalid, generate a new one # If the sessionId is still unset or is invalid, generate a new one
if (!$sessionId || !$self->id->valid($sessionId)) { if (!$sessionId || !$self->id->valid($sessionId)) {
$sessionId = $self->id->generate; $sessionId = $self->id->generate;

View file

@ -118,20 +118,6 @@ sub get {
} }
#-------------------------------------------------------------------
=head2 getIp ( )
Returns the user's IP address.
=cut
sub getIp {
my $self = shift;
return $self->get('REMOTE_ADDR');
}
#------------------------------------------------------------------- #-------------------------------------------------------------------
=head2 new ( ) =head2 new ( )

View file

@ -253,7 +253,7 @@ sub security {
my $self = shift; my $self = shift;
my $message = shift; my $message = shift;
@_ = ($self, $self->session->user->username." (".$self->session->user->userId.") connecting from " @_ = ($self, $self->session->user->username." (".$self->session->user->userId.") connecting from "
.$self->session->env->getIp." attempted to ".$message); .$self->session->request->address." attempted to ".$message);
goto $self->can('warn'); goto $self->can('warn');
} }

View file

@ -182,7 +182,7 @@ sub new {
my $time = time(); my $time = time();
my $timeout = $session->setting->get("sessionTimeout"); my $timeout = $session->setting->get("sessionTimeout");
$self->{_var}{lastPageView} = $time; $self->{_var}{lastPageView} = $time;
$self->{_var}{lastIP} = $session->env->getIp; $self->{_var}{lastIP} = $session->request->address;
$self->{_var}{expires} = $time + $timeout; $self->{_var}{expires} = $time + $timeout;
if ($self->{_var}{nextCacheFlush} > 0 && $self->{_var}{nextCacheFlush} < $time) { if ($self->{_var}{nextCacheFlush} > 0 && $self->{_var}{nextCacheFlush} < $time) {
delete $self->{_var}{nextCacheFlush}; delete $self->{_var}{nextCacheFlush};
@ -247,7 +247,7 @@ sub start {
$self->{_var} = { $self->{_var} = {
expires => $time + $timeout, expires => $time + $timeout,
lastPageView => $time, lastPageView => $time,
lastIP => $session->env->getIp, lastIP => $session->request->address,
adminOn => 0, adminOn => 0,
userId => $userId userId => $userId
}; };

View file

@ -299,7 +299,7 @@ sub canUseAdminMode {
my $pass = 1; my $pass = 1;
my $subnets = $self->session->config->get("adminModeSubnets") || []; my $subnets = $self->session->config->get("adminModeSubnets") || [];
if (scalar(@$subnets)) { if (scalar(@$subnets)) {
$pass = WebGUI::Utility::isInSubnet($self->session->env->getIp, $subnets); $pass = WebGUI::Utility::isInSubnet($self->session->request->address, $subnets);
} }
return $pass && $self->isInGroup(12) return $pass && $self->isInGroup(12)

View file

@ -24,5 +24,5 @@ cmp_ok($session->env->get("REMOTE_ADDR"), 'ne', "", "get() one valid entry");
my $env = $session->env; my $env = $session->env;
$session->request->env->{REMOTE_ADDR} = '192.168.0.2'; $session->request->env->{REMOTE_ADDR} = '192.168.0.2';
is ($env->getIp, '192.168.0.2', 'getIp'); #is ($env->getIp, '192.168.0.2', 'getIp');

View file

@ -47,7 +47,7 @@ WebGUI::Test->interceptLogging( sub {
is($log_data->{warn}, "Second warning", "warn: Log4perl called again"); is($log_data->{warn}, "Second warning", "warn: Log4perl called again");
$eh->security('Shields up, red alert'); $eh->security('Shields up, red alert');
my $security = sprintf '%s (%d) connecting from %s attempted to %s', my $security = sprintf '%s (%d) connecting from %s attempted to %s',
$session->user->username, $session->user->userId, $session->env->getIp, 'Shields up, red alert'; $session->user->username, $session->user->userId, $session->request->address, 'Shields up, red alert';
is($log_data->{warn}, $security, 'security: calls warn with username, userId and IP address'); is($log_data->{warn}, $security, 'security: calls warn with username, userId and IP address');
}); });

View file

@ -289,6 +289,7 @@ ok($adminSiteWideTag->get(q{isSiteWide}), 'versionTagMode siteWide + admin inher
ok($adminSiteWideTag->getId() eq $siteWideTagId, 'versionTagMode siteWide + admin inherited: empty has same ID as site wide'); ok($adminSiteWideTag->getId() eq $siteWideTagId, 'versionTagMode siteWide + admin inherited: empty has same ID as site wide');
$adminUserTag->rollback();
$admin_session->var()->end(); $admin_session->var()->end();
$admin_session->close(); $admin_session->close();
@ -314,7 +315,6 @@ isnt(
$userTag->rollback(); $userTag->rollback();
$siteWideTag->rollback(); $siteWideTag->rollback();
$adminUserTag->rollback();
## Additional VersionTagMode to make sure that auto commit happens only when user is tag creator and tag is not site wide. ## Additional VersionTagMode to make sure that auto commit happens only when user is tag creator and tag is not site wide.
## See bug #10689 (Version Tag Modes) ## See bug #10689 (Version Tag Modes)
@ -329,7 +329,6 @@ $adminUserTag->rollback();
# create admin session # create admin session
my $admin_session = WebGUI::Test->newSession; my $admin_session = WebGUI::Test->newSession;
WebGUI::Test->addToCleanup($admin_session);
$admin_session->user({'userId' => 3}); $admin_session->user({'userId' => 3});
setUserVersionTagMode($admin_session->user(), q{autoCommit}); setUserVersionTagMode($admin_session->user(), q{autoCommit});