From 005b9da1acb116067141c80c170ab7438e3f9bbc Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Thu, 1 Jul 2010 13:11:01 -0700 Subject: [PATCH] Move session->env->getIp into session->request->address --- docs/migration.txt | 15 ++++++++++--- lib/WebGUI/Asset/Post.pm | 4 ++-- lib/WebGUI/Asset/Wobject/Poll.pm | 4 ++-- lib/WebGUI/Asset/Wobject/Survey.pm | 4 ++-- lib/WebGUI/Asset/Wobject/Thingy.pm | 2 +- lib/WebGUI/AssetCollateral/DataForm/Entry.pm | 2 +- lib/WebGUI/Auth.pm | 2 +- lib/WebGUI/Form/Captcha.pm | 2 +- lib/WebGUI/Operation/Cron.pm | 2 +- lib/WebGUI/Operation/Spectre.pm | 4 ++-- lib/WebGUI/Operation/User.pm | 2 +- lib/WebGUI/Operation/Workflow.pm | 2 +- lib/WebGUI/Session.pm | 23 +++++++++++++------- lib/WebGUI/Session/Env.pm | 14 ------------ lib/WebGUI/Session/ErrorHandler.pm | 2 +- lib/WebGUI/Session/Var.pm | 4 ++-- lib/WebGUI/User.pm | 2 +- t/Session/Env.t | 2 +- t/Session/ErrorHandler.t | 2 +- t/VersionTag.t | 3 +-- 20 files changed, 49 insertions(+), 48 deletions(-) diff --git a/docs/migration.txt b/docs/migration.txt index dfbf12e8f..e8c4a3526 100644 --- a/docs/migration.txt +++ b/docs/migration.txt @@ -6,7 +6,7 @@ The information contained herein documents the API changes that have occurred in 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. my $config = WebGUI::Config->new($filename); @@ -14,10 +14,19 @@ my $config = WebGUI::Config->new($filename); 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. -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 diff --git a/lib/WebGUI/Asset/Post.pm b/lib/WebGUI/Asset/Post.pm index d7279effe..3f36b8446 100644 --- a/lib/WebGUI/Asset/Post.pm +++ b/lib/WebGUI/Asset/Post.pm @@ -830,7 +830,7 @@ sub hasRated { return 1 if $self->isPoster; my $flag = 0; 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 { ($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->getId, $self->session->user->userId, - $self->session->env->getIp, + $self->session->request->address, time(), $rating,] ); diff --git a/lib/WebGUI/Asset/Wobject/Poll.pm b/lib/WebGUI/Asset/Wobject/Poll.pm index b56132443..9f96e0deb 100644 --- a/lib/WebGUI/Asset/Wobject/Poll.pm +++ b/lib/WebGUI/Asset/Wobject/Poll.pm @@ -203,7 +203,7 @@ sub _hasVoted { my $self = shift; 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)." - 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; } @@ -545,7 +545,7 @@ sub www_vote { my $self = shift; my $u; 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")) { $self->session->user->karma($self->get("karmaPerVote"),"Poll (".$self->getId.")","Voted on this poll."); } diff --git a/lib/WebGUI/Asset/Wobject/Survey.pm b/lib/WebGUI/Asset/Wobject/Survey.pm index 3b2684268..704d9ffb7 100644 --- a/lib/WebGUI/Asset/Wobject/Survey.pm +++ b/lib/WebGUI/Asset/Wobject/Survey.pm @@ -2045,7 +2045,7 @@ sub responseId { my $ignoreRevisionDate = $opts{ignoreRevisionDate}; my $user = WebGUI::User->new( $self->session, $userId ); - my $ip = $self->session->env->getIp; + my $ip = $self->session->request->address; my $responseId = $self->{responseId}; return $responseId if $responseId; @@ -2178,7 +2178,7 @@ sub canTakeSurvey { } my $maxResponsesPerUser = $self->maxResponsesPerUser; - my $ip = $self->session->env->getIp; + my $ip = $self->session->request->address; my $userId = $self->session->user->userId(); my $takenCount = 0; diff --git a/lib/WebGUI/Asset/Wobject/Thingy.pm b/lib/WebGUI/Asset/Wobject/Thingy.pm index 2f7584b70..9fbe6e89c 100644 --- a/lib/WebGUI/Asset/Wobject/Thingy.pm +++ b/lib/WebGUI/Asset/Wobject/Thingy.pm @@ -565,7 +565,7 @@ sub editThingDataSave { if ($thingDataId eq "new"){ $thingData{dateCreated} = time(); $thingData{createdById} = $session->user->userId; - $thingData{ipAddress} = $session->env->getIp; + $thingData{ipAddress} = $session->request->address; } else { %thingData = $session->db->quickHash("select * from ".$session->db->dbh->quote_identifier("Thingy_".$thingId) diff --git a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm index 650a9f099..9b636586c 100644 --- a/lib/WebGUI/AssetCollateral/DataForm/Entry.pm +++ b/lib/WebGUI/AssetCollateral/DataForm/Entry.pm @@ -301,7 +301,7 @@ sub new { } else { $self->user($session->user); - $self->ipAddress($session->env->getIp); + $self->ipAddress($session->request->address); $self->submissionDate(WebGUI::DateTime->new($session, time)); $entryData{id $self} = {}; } diff --git a/lib/WebGUI/Auth.pm b/lib/WebGUI/Auth.pm index 780eac849..811b94db3 100644 --- a/lib/WebGUI/Auth.pm +++ b/lib/WebGUI/Auth.pm @@ -100,7 +100,7 @@ sub _logLogin { $_[0], $_[1], time(), - $self->session->env->getIp, + $self->session->request->address, $self->session->env->get("HTTP_USER_AGENT"), $self->session->getId, time(), diff --git a/lib/WebGUI/Form/Captcha.pm b/lib/WebGUI/Form/Captcha.pm index 21fab775a..a0130a126 100644 --- a/lib/WebGUI/Form/Captcha.pm +++ b/lib/WebGUI/Form/Captcha.pm @@ -111,7 +111,7 @@ sub getValue { my $ua = LWP::UserAgent->new; my $res = $ua->post('http://api-verify.recaptcha.net/verify', { privatekey => $privKey, - remoteip => $self->session->env->getIp, + remoteip => $self->session->request->env->{REMOTE_ADDR}, challenge => $challenge, response => $response, }); diff --git a/lib/WebGUI/Operation/Cron.pm b/lib/WebGUI/Operation/Cron.pm index ccfa81e49..712bc7f1a 100644 --- a/lib/WebGUI/Operation/Cron.pm +++ b/lib/WebGUI/Operation/Cron.pm @@ -271,7 +271,7 @@ sub www_runCronJob { my $session = shift; $session->http->setMimeType("text/plain"); $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")})."."); return "error"; } diff --git a/lib/WebGUI/Operation/Spectre.pm b/lib/WebGUI/Operation/Spectre.pm index fe6253eb2..c8c3d9cec 100644 --- a/lib/WebGUI/Operation/Spectre.pm +++ b/lib/WebGUI/Operation/Spectre.pm @@ -59,7 +59,7 @@ sub www_spectreGetSiteData { if (!defined $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 " .join(",",@{$subnets})."."); } @@ -181,7 +181,7 @@ sub www_spectreTest { $subnets = []; } - my $sessionIp = $session->env->getIp; + my $sessionIp = $session->request->address; unless (isInSubnet($sessionIp, $subnets)) { $session->errorHandler->security( sprintf "Tried to make a Spectre workflow runner request from %s, but we're only allowed to accept requests from %s", diff --git a/lib/WebGUI/Operation/User.pm b/lib/WebGUI/Operation/User.pm index 2014ee832..b7d0971bb 100644 --- a/lib/WebGUI/Operation/User.pm +++ b/lib/WebGUI/Operation/User.pm @@ -141,7 +141,7 @@ sub canUseService { my ( $session ) = @_; my $subnets = $session->config->get('serviceSubnets'); 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 } diff --git a/lib/WebGUI/Operation/Workflow.pm b/lib/WebGUI/Operation/Workflow.pm index 1c038cc89..2806fc2ca 100644 --- a/lib/WebGUI/Operation/Workflow.pm +++ b/lib/WebGUI/Operation/Workflow.pm @@ -482,7 +482,7 @@ sub www_runWorkflow { my $session = shift; $session->http->setMimeType("text/plain"); $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")})."."); return "error"; } diff --git a/lib/WebGUI/Session.pm b/lib/WebGUI/Session.pm index 496129b8e..1a153b6a3 100644 --- a/lib/WebGUI/Session.pm +++ b/lib/WebGUI/Session.pm @@ -20,6 +20,8 @@ use 5.010; use CHI; use File::Temp qw( tempdir ); use Scalar::Util qw( weaken ); +use HTTP::Message::PSGI; +use HTTP::Request::Common; use WebGUI::Config; use WebGUI::SQL; use WebGUI::User; @@ -480,15 +482,20 @@ sub open { my $self = { _config => $config }; bless $self, $class; - if ($env) { - 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}; + ##No env was passed, so construct one + if (! $env) { + my $url = 'http://' . $config->get('sitename')->[0]; + my $request = HTTP::Request::Common::GET($url); + $env = $request->to_psgi; } + + 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 (!$sessionId || !$self->id->valid($sessionId)) { diff --git a/lib/WebGUI/Session/Env.pm b/lib/WebGUI/Session/Env.pm index 2f2965c89..583d621ea 100644 --- a/lib/WebGUI/Session/Env.pm +++ b/lib/WebGUI/Session/Env.pm @@ -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 ( ) diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index 0519e2ab5..887cf0ecd 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -253,7 +253,7 @@ sub security { my $self = shift; my $message = shift; @_ = ($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'); } diff --git a/lib/WebGUI/Session/Var.pm b/lib/WebGUI/Session/Var.pm index 1dba94d22..6f611b1c0 100644 --- a/lib/WebGUI/Session/Var.pm +++ b/lib/WebGUI/Session/Var.pm @@ -182,7 +182,7 @@ sub new { my $time = time(); my $timeout = $session->setting->get("sessionTimeout"); $self->{_var}{lastPageView} = $time; - $self->{_var}{lastIP} = $session->env->getIp; + $self->{_var}{lastIP} = $session->request->address; $self->{_var}{expires} = $time + $timeout; if ($self->{_var}{nextCacheFlush} > 0 && $self->{_var}{nextCacheFlush} < $time) { delete $self->{_var}{nextCacheFlush}; @@ -247,7 +247,7 @@ sub start { $self->{_var} = { expires => $time + $timeout, lastPageView => $time, - lastIP => $session->env->getIp, + lastIP => $session->request->address, adminOn => 0, userId => $userId }; diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index 4871fe20d..65333072b 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -299,7 +299,7 @@ sub canUseAdminMode { my $pass = 1; my $subnets = $self->session->config->get("adminModeSubnets") || []; 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) diff --git a/t/Session/Env.t b/t/Session/Env.t index 45ea04ce2..850b56b80 100644 --- a/t/Session/Env.t +++ b/t/Session/Env.t @@ -24,5 +24,5 @@ cmp_ok($session->env->get("REMOTE_ADDR"), 'ne', "", "get() one valid entry"); my $env = $session->env; $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'); diff --git a/t/Session/ErrorHandler.t b/t/Session/ErrorHandler.t index 2b2eb5588..564effa21 100644 --- a/t/Session/ErrorHandler.t +++ b/t/Session/ErrorHandler.t @@ -47,7 +47,7 @@ WebGUI::Test->interceptLogging( sub { is($log_data->{warn}, "Second warning", "warn: Log4perl called again"); $eh->security('Shields up, red alert'); 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'); }); diff --git a/t/VersionTag.t b/t/VersionTag.t index fa7958837..d473a6409 100644 --- a/t/VersionTag.t +++ b/t/VersionTag.t @@ -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'); +$adminUserTag->rollback(); $admin_session->var()->end(); $admin_session->close(); @@ -314,7 +315,6 @@ isnt( $userTag->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. ## See bug #10689 (Version Tag Modes) @@ -329,7 +329,6 @@ $adminUserTag->rollback(); # create admin session my $admin_session = WebGUI::Test->newSession; - WebGUI::Test->addToCleanup($admin_session); $admin_session->user({'userId' => 3}); setUserVersionTagMode($admin_session->user(), q{autoCommit});