]> mj.ucw.cz Git - pciids.git/commitdiff
Writing who is logged in in log out link
authorMichal Vaner <vorner@ucw.cz>
Sat, 30 Aug 2008 09:06:27 +0000 (11:06 +0200)
committerMichal Vaner <vorner@ucw.cz>
Sat, 30 Aug 2008 09:06:27 +0000 (11:06 +0200)
PciIds/Html/Users.pm
PciIds/Html/Util.pm
PciIds/Users.pm

index d665596686c0d2de6699c3c1a5f54ddedf15f3f8..317702fcc2aedc38c710690f93f5ec72d7cffc61 100644 (file)
@@ -216,7 +216,7 @@ 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 ) ) );
+               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, undef, $email ) ) );
                $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 );
@@ -241,12 +241,12 @@ sub logout( $$ ) {
 sub checkLogin( $$ ) {
        my( $req, $tables ) = @_;
        my $cookies = fetch CGI::Cookie;
-       my( $authed, $id, $regen, $rights, $error ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef );
+       my( $authed, $id, $regen, $rights, $error, $name ) = checkAuthToken( $tables, $req, defined( $cookies->{'auth'} ) ? $cookies->{'auth'}->value : undef );
        if( $regen ) {
-               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights ) ) );
+               $req->headers_out->add( 'Set-Cookie' => new CGI::Cookie( -name => 'auth', -value => genAuthToken( $tables, $id, $req, $rights, $name ) ) );
        }
        my $hterror = $authed ? '' : '<div class="error"><p>'.$error.'</div>';
-       return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror };
+       return { 'authid' => $authed ? $id : undef, 'accrights' => $rights, 'logerror' => $hterror, 'name' => $authed ? $name : undef };
 }
 
 sub notLoggedComplaint( $$$ ) {
@@ -374,14 +374,14 @@ sub resetPasswdConfirmFormSubmit( $$$ ) {
        }
 }
 
-sub genProfileForm( $$$$$ ) {
-       my( $req, $args, $error, $data, $info ) = @_;
+sub genProfileForm( $$$$$$ ) {
+       my( $req, $args, $auth, $error, $data, $info ) = @_;
        genHtmlHead( $req, 'User profile', undef );
        delete $data->{'current_password'};
        delete $data->{'confirm_password'};
        delete $data->{'password'};
        print "<h1>User profile</h1>\n";
-       genLocMenu( $req, $args, [ [ 'Log out', 'logout' ], [ 'Notifications', 'notifications' ] ] );
+       genLocMenu( $req, $args, [ logItem( $auth ), [ 'Notifications', 'notifications' ] ] );
        print '<div class="error"><p>'.$error.'</div>' if defined $error;
        print "<div class='info'><p>$info</div>\n" if defined $info;
        print '<form name="profile" id="profile" method="POST" action=""><table>';
@@ -403,7 +403,7 @@ sub profileForm( $$$$ ) {
        my( $req, $args, $tables, $auth ) = @_;
        return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
        return HTTPRedirect( $req, 'https://'.$req->hostname().$req->uri().buildArgs( $args ) ) unless $auth->{'ssl'};
-       return genProfileForm( $req, $args, undef, $tables->profileData( $auth->{'authid'} ), undef );
+       return genProfileForm( $req, $args, $auth, undef, $tables->profileData( $auth->{'authid'} ), undef );
 }
 
 sub checkNum( $$ ) {
@@ -470,9 +470,9 @@ sub profileFormSubmit( $$$$ ) {
                return "You need to provide correct current password to change email, login or password" unless $logged;
                return undef;
        } ] );
-       return genProfileForm( $req, $args, $error, $data, undef ) if defined $error;
+       return genProfileForm( $req, $args, $auth, $error, $data, undef ) if defined $error;
        pushProfile( $tables, $auth->{'authid'}, $oldData, $data );
-       return genProfileForm( $req, $args, undef, $data, "Profile updated." );
+       return genProfileForm( $req, $args, $auth, undef, $data, "Profile updated." );
 }
 
 1;
index 1c4b339c6cde2208df9ec09503a3a79ba28fe604..85d03de320f9bf6bd11b4917834cd08dd0a1815e 100644 (file)
@@ -7,7 +7,7 @@ use PciIds::Users;
 use Apache2::Const qw(:common :http);
 use APR::Table;
 
-our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath);
+our @EXPORT = qw(&genHtmlHead &htmlDiv &genHtmlTail &genTableHead &genTableTail &parseArgs &buildExcept &buildArgs &genMenu &genCustomMenu &encode &setAddrPrefix &HTTPRedirect &genPath &logItem);
 
 sub encode( $ ) {
        return encode_entities( shift, "\"'&<>" );
@@ -52,14 +52,18 @@ sub genCustomMenu( $$$$ ) {
        print "</ul></div>\n";
 }
 
-sub genMenu( $$$$ ) {
-       my( $req, $address, $args, $auth ) = @_;
-       my @list;
+sub logItem( $ ) {
+       my( $auth ) = @_;
        if( defined( $auth->{'authid'} ) ) {
-               push @list, [ 'Log out', 'logout' ];
+               return [ 'Log out ('.encode( $auth->{'name'} ).')', 'logout' ];
        } else {
-               push @list, [ 'Log in', 'login' ];
+               return [ 'Log in', 'login' ];
        }
+}
+
+sub genMenu( $$$$ ) {
+       my( $req, $address, $args, $auth ) = @_;
+       my @list = ( logItem( $auth ) );
        push @list, [ 'Add item', 'newitem' ] if( $address->canAddItem() );
        push @list, [ 'Discuss', 'newhistory' ] if( $address->canDiscuss() );
        push @list, [ 'Administrate', 'admin' ] if( hasRight( $auth->{'accrights'}, 'validate' ) );
index 1bb26f9530cb86f405ecd8a7e588d31a87372021..d6b136714613d2aff7a6008d6610c0515b52eec7 100644 (file)
@@ -64,8 +64,8 @@ sub changePasswd( $$$$ ) {
        $tables->changePasswd( $id, $salted );
 }
 
-sub genAuthToken( $$$$ ) {
-       my( $tables, $id, $req, $rights ) = @_;
+sub genAuthToken( $$$$$ ) {
+       my( $tables, $id, $req, $rights, $name ) = @_;
        unless( defined $rights ) {#Just logged in
                my $from = $req->connection()->remote_ip();
                $tables->setLastLog( $id, $from );
@@ -74,13 +74,13 @@ sub genAuthToken( $$$$ ) {
        my $haveRights = scalar @{$rights};
        my $time = time;
        my $ip = $req->connection()->remote_ip();
-       return "$id:$haveRights:$time:".md5_hex( "$id:$time:$ip:".$config{'authsalt'} );
+       return "$id:$haveRights:$time:".md5_hex( "$id:$time:$ip:".$config{'authsalt'} ).":$name";
 }
 
 sub checkAuthToken( $$$ ) {
        my( $tables, $req, $token ) = @_;
-       my( $id, $haveRights, $time, $hex ) = defined( $token ) ? split( /:/, $token ) : ();
-       return ( 0, 0, 0, [], "Not logged in" ) unless( defined $hex );
+       my( $id, $haveRights, $time, $hex, $name ) = defined( $token ) ? split( /:/, $token ) : ();
+       return ( 0, 0, 0, [], "Not logged in", undef ) unless( defined $hex );
        my $ip = $req->connection()->remote_ip();
        my $expected = md5_hex( "$id:$time:$ip:".$config{'authsalt'} );
        my $actTime = time;
@@ -96,7 +96,7 @@ sub checkAuthToken( $$$ ) {
                        push @{$rights}, \%r;
                }
        }
-       return ( $authed, $id, $regen, $rights, $authed ? undef : ( $tokOk ? "Login timed out" : "Not logged in x" ) );
+       return ( $authed, $id, $regen, $rights, $authed ? undef : ( $tokOk ? "Login timed out" : "Not logged in" ), $name );
 }
 
 sub hasRight( $$ ) {