]> mj.ucw.cz Git - pciids.git/commitdiff
Login gives a real page now
authorMichal Vaner <vorner@ucw.cz>
Sun, 31 Aug 2008 16:37:18 +0000 (18:37 +0200)
committerMichal Vaner <vorner@ucw.cz>
Sun, 31 Aug 2008 16:37:18 +0000 (18:37 +0200)
PciIds/Html/Changes.pm
PciIds/Html/Handler.pm
PciIds/Html/List.pm
PciIds/Html/Tables.pm
PciIds/Html/Users.pm
PciIds/Html/Util.pm

index e2899f65abcd0224335d5cb48d7b0d06dbeb8a25..433a91c9390563391d444eead4af37f8558ff8ba 100644 (file)
@@ -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 "<div class='clear'></div></div>\n";
        print "<div class='error'>$error</div>\n" if( defined $error );
-       print "<form name='newitem' id='newitem' method='POST' action=''>\n<table>";
+       print "<form name='newitem' id='newitem' method='POST' action='".( $args->{'full_links'} ? 'http://'.$req->hostname().$req->uri().buildExcept( 'action', $args ).'?action=newitem' : '' )."'>\n<table>";
        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 "<div class='clear'></div></div>\n";
        print "<div class='error'>$error</div>\n" if( defined $error );
-       print "<form name='newhistory' id='newhistory' method='POST' action=''>\n<table>";
+       print "<form name='newhistory' id='newhistory' method='POST' action='".( $args->{'full_links'} ? 'http://'.$req->hostname().$req->uri().buildExcept( 'action', $args ).'?action=newhistory' : '' )."'>\n<table>";
        genFormEx( [ [ 'textarea', 'Text:', undef, 'text', 'rows="5" cols="50"' ],
                [ 'input', 'Request deletion', 'checkbox', 'delete', 'value="delete"' ],
                [ 'input', 'Name:', 'text', 'name', 'maxlength="200"' ],
index 879086a26c4632175eb5127fc8eab119edbff926..0d82e3f4031d5ef2a7763eb7cb1cfbc7830c83b2 100644 (file)
@@ -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( $@ ) {
index f4a0b4de2bb0558bbe8e4ec5665109cdd3e72957..bcd55257226864e85c5eb69bd8de12c03f5c576f 100644 (file)
@@ -56,6 +56,7 @@ sub list( $$$$ ) {
                print "</div>\n";
        }
        print "</div>\n" if( $diss );
+       my $url_prefix = $args->{'full_links'} ? 'http://'.$req->hostname() : '';
        unless( $address->leaf() ) {
                print "<h2>Sub-items</h2>\n";
                my $restricts = $address->defaultRestrictList();
@@ -63,15 +64,15 @@ sub list( $$$$ ) {
                        print "<p>";
                        my $url = '/read/'.$address->get().buildExcept( 'restrict', $args ).'?restrict=';
                        foreach( @{$restricts} ) {
-                               print "<a href='".$url.$_->[0]."'>".$_->[1]."</a> ";
+                               print "<a href='".$url_prefix.$url.$_->[0]."'>".$_->[1]."</a> ";
                        }
                }
                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', [ '<a href="'.$url.'?sort='.$sort_id.'">Id</a>', '<a href="'.$url.'?sort='.$sort_name.'">Name</a>', 'Note' ], [ 'id-col', 'name-col', 'note-col' ] );
+               genTableHead( 'subnodes', [ '<a href="'.$url_prefix.$url.'?sort='.$sort_id.'">Id</a>', '<a href="'.$url_prefix.$url.'?sort='.$sort_name.'">Name</a>', '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();
index 4eab1b3a9c1ca04738ed5c6d72362eba355b7c73..e8a42d578ec912df5ae1fc6cbc7bf0d62a23383f 100644 (file)
@@ -10,17 +10,20 @@ sub new( $ ) {
        return bless PciIds::DBQ::new( $dbh );
 }
 
-sub formatLink( $ ) {
+sub formatLink( $$ ) {
        my $address = PciIds::Address::new( shift );
-       return '<a href="/read/'.$address->get().'">'.$address->tail().'</a>';
+       my $url_prefix = shift;
+       return '<a href="'.$url_prefix.'/read/'.$address->get().'">'.$address->tail().'</a>';
 }
 
-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' ).'"';
        } );
index 06d1ea6504d3054b63d2d445d72d1dc3f39ac546..d105bbe6efd615673a351c44c0a693fa320de07c 100644 (file)
@@ -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 "<div class='top'>\n";
-               print '<h1>Logged in</h1>';
-               genPath( $req, PciIds::Address::new( $req->uri() ), 1 );
-               print "<div class='clear'></div></div>\n";
-               print "<p>You are logged in" . ( defined $args->{'redirectaction'} ? ", continue with your <a href='$url'>action</a>.\n" : ".\n" );
-               print '<div class="lastlog"><p>'.encode( $last ).'</div>' 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'};
index ca5abea45bdead77d915340bbaf7af631a8bd9e1..073fa01101c21583f6d116fcf3fc5c5684014165 100644 (file)
@@ -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 );
 }