From 6173b0f3acbdd6691a729d75e9d2ebfa664c1473 Mon Sep 17 00:00:00 2001 From: Michal Vaner Date: Sat, 30 Aug 2008 11:06:27 +0200 Subject: [PATCH] Writing who is logged in in log out link --- PciIds/Html/Users.pm | 20 ++++++++++---------- PciIds/Html/Util.pm | 16 ++++++++++------ PciIds/Users.pm | 12 ++++++------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/PciIds/Html/Users.pm b/PciIds/Html/Users.pm index d665596..317702f 100644 --- a/PciIds/Html/Users.pm +++ b/PciIds/Html/Users.pm @@ -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 ? '' : '

'.$error.'

'; - 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 "

User profile

\n"; - genLocMenu( $req, $args, [ [ 'Log out', 'logout' ], [ 'Notifications', 'notifications' ] ] ); + genLocMenu( $req, $args, [ logItem( $auth ), [ 'Notifications', 'notifications' ] ] ); print '

'.$error.'

' if defined $error; print "

$info

\n" if defined $info; print '
'; @@ -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; diff --git a/PciIds/Html/Util.pm b/PciIds/Html/Util.pm index 1c4b339..85d03de 100644 --- a/PciIds/Html/Util.pm +++ b/PciIds/Html/Util.pm @@ -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 "\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' ) ); diff --git a/PciIds/Users.pm b/PciIds/Users.pm index 1bb26f9..d6b1367 100644 --- a/PciIds/Users.pm +++ b/PciIds/Users.pm @@ -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( $$ ) { -- 2.39.2