From d72df165002c8f76430b8676884e240d869697c5 Mon Sep 17 00:00:00 2001 From: Michal Vaner Date: Sun, 31 Aug 2008 18:37:18 +0200 Subject: [PATCH] Login gives a real page now --- PciIds/Html/Changes.pm | 4 ++-- PciIds/Html/Handler.pm | 26 +++++++++++++++++--------- PciIds/Html/List.pm | 7 ++++--- PciIds/Html/Tables.pm | 13 ++++++++----- PciIds/Html/Users.pm | 33 +++++++++++++++++---------------- PciIds/Html/Util.pm | 1 + 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/PciIds/Html/Changes.pm b/PciIds/Html/Changes.pm index e2899f6..433a91c 100644 --- a/PciIds/Html/Changes.pm +++ b/PciIds/Html/Changes.pm @@ -20,7 +20,7 @@ sub genNewItemForm( $$$$$$ ) { genLocMenu( $req, $args, [ logItem( $auth ), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newitem' ], [ 'ID syntax', 'help', $address->helpName() ] ] ); print "
\n"; print "
$error
\n" if( defined $error ); - print "
\n"; + print "hostname().$req->uri().buildExcept( 'action', $args ).'?action=newitem' : '' )."'>\n
"; genFormEx( [ [ 'input', 'Id:', 'text', 'id', 'maxlength="50"' ], [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ], [ 'input', 'Note*:', 'text', 'note', 'maxlength="1024"' ], @@ -97,7 +97,7 @@ sub genNewHistoryForm( $$$$$$ ) { genLocMenu( $req, $args, [ logItem( $auth ), $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), [ 'Notifications', 'notifications' ], [ 'Help', 'help', 'newhistory' ] ] ); print "
\n"; print "
$error
\n" if( defined $error ); - print "\n
"; + print "hostname().$req->uri().buildExcept( 'action', $args ).'?action=newhistory' : '' )."'>\n
"; genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ], [ 'input', 'Request deletion', 'checkbox', 'delete', 'value="delete"' ], [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ], diff --git a/PciIds/Html/Handler.pm b/PciIds/Html/Handler.pm index 879086a..0d82e3f 100644 --- a/PciIds/Html/Handler.pm +++ b/PciIds/Html/Handler.pm @@ -12,6 +12,9 @@ use PciIds::Html::Admin; use PciIds::Html::Notifications; use PciIds::Html::Help; use Apache2::Const qw(:common :http); +use base 'Exporter'; + +our @EXPORT = qw(&callHandler); $ENV{'PATH'} = ''; my $dbh = connectDb(); @@ -56,22 +59,27 @@ my %handlers = ( } ); -sub handler( $$ ) { - my( $req, $hasSSL ) = @_; - my $args = parseArgs( $req->args() ); - return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) ); - return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ ); +sub callHandler( $$$$$$ ) { + my( $req, $args, $tables, $auth, $hasSSL, $meth ) = @_; my $action = $args->{'action'}; $action = '' unless( defined $action ); - my $method = $handlers{$req->method()}; + my $method = $handlers{$meth}; return HTTP_METHOD_NOT_ALLOWED unless( defined $method );#Can't handle this method my $sub = $method->{$action}; return HTTP_BAD_REQUEST unless( defined $sub );#I do not know this action for given method - my $auth = checkLogin( $req, $tables );#Check if logged in + $auth = checkLogin( $req, $tables ) unless defined $auth;#Check if logged in $auth->{'ssl'} = $hasSSL; - my( $result ); + return &{$sub}( $req, $args, $tables, $auth );#Just do the right thing +} + +sub handler( $$ ) { + my( $req, $hasSSL ) = @_; + my $args = parseArgs( $req->args() ); + return HTTPRedirect( $req, $req->uri()."index.html" ) if( $req->uri() eq '/' && ( !defined $args->{'action'} || $args->{'action'} ne 'help' ) ); + return DECLINED if( $req->uri() =~ /^\/((static)\/|robots.txt|index.html)/ ); + my $result; eval { - $result = &{$sub}( $req, $args, $tables, $auth );#Just do the right thing + $result = callHandler( $req, $args, $tables, undef, $hasSSL, $req->method() ); $tables->commit(); }; if( $@ ) { diff --git a/PciIds/Html/List.pm b/PciIds/Html/List.pm index f4a0b4d..bcd5525 100644 --- a/PciIds/Html/List.pm +++ b/PciIds/Html/List.pm @@ -56,6 +56,7 @@ sub list( $$$$ ) { print "\n"; } print "\n" if( $diss ); + my $url_prefix = $args->{'full_links'} ? 'http://'.$req->hostname() : ''; unless( $address->leaf() ) { print "

Sub-items

\n"; my $restricts = $address->defaultRestrictList(); @@ -63,15 +64,15 @@ sub list( $$$$ ) { print "

"; my $url = '/read/'.$address->get().buildExcept( 'restrict', $args ).'?restrict='; foreach( @{$restricts} ) { - print "".$_->[1]." "; + print "".$_->[1]." "; } } my $url = '/read/'.$address->get().buildExcept( 'sort', $args ); my $sort = ( $args->{'sort'} or 'id' ); my( $sort_id, $sort_name ) = ( ( $sort eq 'id' ? 'rid' : 'id' ), ( $sort eq 'name' ? 'rname' : 'name' ) ); - genTableHead( 'subnodes', [ 'Id', 'Name', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] ); + genTableHead( 'subnodes', [ 'Id', 'Name', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] ); $args->{'restrict'} = $address->defaultRestrict() unless( defined( $args->{'restrict'} ) ); - $tables->nodes( $address->get(), $args ); + $tables->nodes( $address->get(), $args, $url_prefix ); genTableTail(); } genHtmlTail(); diff --git a/PciIds/Html/Tables.pm b/PciIds/Html/Tables.pm index 4eab1b3..e8a42d5 100644 --- a/PciIds/Html/Tables.pm +++ b/PciIds/Html/Tables.pm @@ -10,17 +10,20 @@ sub new( $ ) { return bless PciIds::DBQ::new( $dbh ); } -sub formatLink( $ ) { +sub formatLink( $$ ) { my $address = PciIds::Address::new( shift ); - return ''.$address->tail().''; + my $url_prefix = shift; + return ''.$address->tail().''; } -sub nodes( $$$ ) { - my( $self, $parent, $args ) = @_; +sub nodes( $$$$ ) { + my( $self, $parent, $args, $url_prefix ) = @_; my $restrict = $args->{'restrict'}; $restrict = '' unless( defined $restrict ); $restrict = PciIds::Address::new( $parent )->restrictRex( $restrict );#How do I know if the restrict is OK? - htmlFormatTable( PciIds::DBQ::nodes( $self, $parent, $args, $restrict ), 3, [], [ \&formatLink ], sub { 1; }, sub { + htmlFormatTable( PciIds::DBQ::nodes( $self, $parent, $args, $restrict ), 3, [], [ sub { + return formatLink( shift, $url_prefix ); + }, ], sub { 1; }, sub { my $name = shift->[ 1 ]; return ' class="'.( defined $name && $name ne '' ? 'item' : 'unnamedItem' ).'"'; } ); diff --git a/PciIds/Html/Users.pm b/PciIds/Html/Users.pm index 06d1ea6..d105bbe 100644 --- a/PciIds/Html/Users.pm +++ b/PciIds/Html/Users.pm @@ -6,6 +6,7 @@ use PciIds::Html::Forms; use PciIds::Email; use PciIds::Users; use PciIds::Address; +use PciIds::Html::Handler; use CGI; use CGI::Cookie; use Apache2::Const qw(:common); @@ -221,19 +222,13 @@ sub loginSubmit( $$$ ) { $logged = $salted eq $passwd; } if( $logged ) { - $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ) ); + my $cookie = new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ); + $req->headers_out->add( 'Set-Cookie' => $cookie ); $args->{'action'} = ( defined $args->{'redirectaction'} ) ? $args->{'redirectaction'} : 'list'; - my $prefix = ( !defined( $args->{'action'} ) or ( $args->{'action'} eq '' ) or ( $args->{'action'} eq 'list' ) ) ? 'read' : 'mods'; - my $url = "http://".$req->hostname().setAddrPrefix( $req->uri(), $prefix ).buildExcept( 'redirectaction', $args ); - genHtmlHead( $req, 'Logged in', undef ); - print "

\n"; - print '

Logged in

'; - genPath( $req, PciIds::Address::new( $req->uri() ), 1 ); - print "
\n"; - print "

You are logged in" . ( defined $args->{'redirectaction'} ? ", continue with your action.\n" : ".\n" ); - print '

'.encode( $last ).'

' if( defined( $last ) ); - genHtmlTail(); - return OK; + delete $args->{'redirectaction'}; + $args->{'full_links'} = 1; + my $auth = checkLoginInternal( $req, $tables, $cookie ); + return PciIds::Html::Handler::callHandler( $req, $args, $tables, $auth, 1, 'GET' ); } else { return genLoginForm( $req, $args, 'Invalid login credetials', $data ); } @@ -245,10 +240,9 @@ sub logout( $$ ) { return PciIds::Html::List::list( $req, $args, $tables, {} ); } -sub checkLogin( $$ ) { - my( $req, $tables ) = @_; - my $cookies = fetch CGI::Cookie; - my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef ); +sub checkLoginInternal( $$$ ) { + my( $req, $tables, $cookie ) = @_; + my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookie ) ? $cookie->value : undef ); if( $regen ) { $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights, $name ) ) ); } @@ -256,6 +250,13 @@ sub checkLogin( $$ ) { return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror, 'name' => $authed ? $name : undef }; } +sub checkLogin( $$ ) { + my( $req, $tables ) = @_; + my $cookies = fetch CGI::Cookie; + my $cookie = $cookies->{'auth'}; + return checkLoginInternal( $req, $tables, $cookie ); +} + sub notLoggedComplaint( $$$ ) { my( $req, $args, $auth ) = @_; return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'}; diff --git a/PciIds/Html/Util.pm b/PciIds/Html/Util.pm index ca5abea..073fa01 100644 --- a/PciIds/Html/Util.pm +++ b/PciIds/Html/Util.pm @@ -119,6 +119,7 @@ sub buildExcept( $$ ) { my( $except, $args ) = @_; my %backup = %{$args}; delete $backup{$except}; + delete $backup{'full_links'};#This one is internal return buildArgs( \%backup ); } -- 2.39.2