$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 );
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( $$$ ) {
}
}
-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>';
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( $$ ) {
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;
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, "\"'&<>" );
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' ) );
$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 );
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;
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( $$ ) {