1
0
mirror of https://gitlab.com/depesz/explain.depesz.com.git synced 2024-11-24 08:42:27 +02:00

perltidy run

This commit is contained in:
Hubert depesz Lubaczewski 2013-10-30 12:47:28 +01:00
parent f6d2ca31ba
commit 28fe739f46
4 changed files with 103 additions and 111 deletions

View File

@ -5,7 +5,7 @@ use Mojo::Base 'Mojolicious';
sub startup { sub startup {
my $self = shift; my $self = shift;
$self->sessions->cookie_name('explain'); $self->sessions->cookie_name( 'explain' );
$self->sessions->default_expiration( 60 * 60 * 24 * 365 ); $self->sessions->default_expiration( 60 * 60 * 24 * 365 );
# register Explain plugins namespace # register Explain plugins namespace

View File

@ -10,158 +10,151 @@ use Email::Valid;
sub logout { sub logout {
my $self = shift; my $self = shift;
delete $self->session->{'user'}; delete $self->session->{ 'user' };
$self->redirect_to( 'new-explain' ); $self->redirect_to( 'new-explain' );
} }
sub user_history { sub user_history {
my $self = shift; my $self = shift;
$self->redirect_to( 'history' ) unless $self->session->{'user'}; $self->redirect_to( 'history' ) unless $self->session->{ 'user' };
my @args = ( $self->session->{'user'} ); my @args = ( $self->session->{ 'user' } );
if ( if ( ( $self->param( 'direction' ) )
( $self->param('direction') ) && && ( $self->param( 'direction' ) =~ m{\A(?:before|after)\z} )
( $self->param('direction') =~ m{\A(?:before|after)\z} ) && && ( $self->param( 'key' ) ) )
( $self->param('key') ) {
) { push @args, $self->param( 'direction' ) eq 'before' ? 'DESC' : 'ASC';
push @args, $self->param('direction') eq 'before' ? 'DESC' : 'ASC'; push @args, $self->param( 'key' );
push @args, $self->param('key');
} }
my $data = $self->database->get_user_history( @args ); my $data = $self->database->get_user_history( @args );
$self->stash->{'plans'} = $data; $self->stash->{ 'plans' } = $data;
return $self->render(); return $self->render();
} }
sub user { sub user {
my $self = shift; my $self = shift;
my $old = $self->req->param('old-pw'); my $old = $self->req->param( 'old-pw' );
my $new = $self->req->param('new-pw'); my $new = $self->req->param( 'new-pw' );
my $new2 = $self->req->param('new-pw2'); my $new2 = $self->req->param( 'new-pw2' );
return $self->render unless defined $old; return $self->render unless defined $old;
if ( if ( ( !defined $new )
( !defined $new ) || || ( !defined $new2 )
( !defined $new2) || || ( $new ne $new2 ) )
( $new ne $new2 ) {
) { $self->stash->{ 'message' } = 'You have to provide two identical copies of new password!';
$self->stash->{'message'} = 'You have to provide two identical copies of new password!';
return; return;
} }
my $status = $self->database->user_change_password( $self->session->{'user'}, $old, $new ); my $status = $self->database->user_change_password( $self->session->{ 'user' }, $old, $new );
if ( $status ) { if ( $status ) {
$self->flash('message' => 'Password changed.'); $self->flash( 'message' => 'Password changed.' );
$self->redirect_to( 'new-explain' ); $self->redirect_to( 'new-explain' );
} }
$self->stash->{'message'} = 'Changing the password failed.'; $self->stash->{ 'message' } = 'Changing the password failed.';
} }
sub plan_change { sub plan_change {
my $self = shift; my $self = shift;
unless ( $self->session->{'user'} ) { unless ( $self->session->{ 'user' } ) {
$self->app->log->error( 'User tried to access plan change without being logged!' ); $self->app->log->error( 'User tried to access plan change without being logged!' );
$self->redirect_to( 'new-explain' ); $self->redirect_to( 'new-explain' );
} }
$self->redirect_to( 'new-explain' ) unless $self->req->param('return'); $self->redirect_to( 'new-explain' ) unless $self->req->param( 'return' );
my $plan = $self->database->get_plan_data( $self->param('id') ); my $plan = $self->database->get_plan_data( $self->param( 'id' ) );
if ( if ( ( !defined $plan->{ 'added_by' } )
( ! defined $plan->{'added_by'} ) || || ( $plan->{ 'added_by' } ne $self->session->{ 'user' } ) )
( $plan->{'added_by'} ne $self->session->{'user'} ) {
) { $self->app->log->error( 'User tried to access plan change for plan [' . $plan->{ 'id' } . '] of another user: ' . $self->session->{ 'user' } );
$self->app->log->error( 'User tried to access plan change for plan [' . $plan->{'id'} . '] of another user: ' . $self->session->{'user'});
$self->redirect_to( 'logout' ); $self->redirect_to( 'logout' );
} }
# All looks fine. Current plan data are in $plan. # All looks fine. Current plan data are in $plan.
if ( if ( ( $self->req->param( 'delete' ) )
( $self->req->param('delete') ) && && ( $self->req->param( 'delete' ) eq 'yes' ) )
( $self->req->param('delete') eq 'yes' ) {
) { $self->database->delete_plan( $plan->{ 'id' }, $plan->{ 'delete_key' } );
$self->database->delete_plan( $plan->{'id'}, $plan->{'delete_key'} ); return $self->redirect_to( $self->req->param( 'return' ) );
return $self->redirect_to( $self->req->param('return') );
} }
my %changes = (); my %changes = ();
if ( $plan->{'title'} ne ( $self->req->param('title') // '' ) ) { if ( $plan->{ 'title' } ne ( $self->req->param( 'title' ) // '' ) ) {
$changes{'title'} = ( $self->req->param('title') // '' ); $changes{ 'title' } = ( $self->req->param( 'title' ) // '' );
} }
if ( if ( ( $plan->{ 'is_public' } )
( $plan->{'is_public'} ) && && ( !$self->req->param( 'is_public' ) ) )
( ! $self->req->param('is_public') ) {
) {
$changes{ 'is_public' } = 0; $changes{ 'is_public' } = 0;
} elsif ( }
( ! $plan->{'is_public'} ) && elsif (( !$plan->{ 'is_public' } )
( $self->req->param('is_public') ) && ( $self->req->param( 'is_public' ) ) )
) { {
$changes{ 'is_public' } = 1; $changes{ 'is_public' } = 1;
} }
if ( if ( ( !$plan->{ 'is_anonymized' } )
( ! $plan->{'is_anonymized'}) && && ( $self->req->param( 'is_anonymized' ) ) )
( $self->req->param('is_anonymized') ) {
) { my $explain = Pg::Explain->new( source => $plan->{ 'plan' } );
my $explain = Pg::Explain->new( source => $plan->{'plan'} );
$explain->anonymize(); $explain->anonymize();
$changes{'plan'} = $explain->as_text(); $changes{ 'plan' } = $explain->as_text();
$changes{ 'is_anonymized' } = 1; $changes{ 'is_anonymized' } = 1;
} }
return $self->redirect_to( $self->req->param('return') ) if 0 == scalar keys %changes; return $self->redirect_to( $self->req->param( 'return' ) ) if 0 == scalar keys %changes;
$self->database->update_plan( $plan->{'id'}, \%changes ); $self->database->update_plan( $plan->{ 'id' }, \%changes );
return $self->redirect_to( $self->req->param('return') ); return $self->redirect_to( $self->req->param( 'return' ) );
} }
sub login { sub login {
my $self = shift; my $self = shift;
# If there is no username - there is nothing to do # If there is no username - there is nothing to do
my $username = $self->req->param('username'); my $username = $self->req->param( 'username' );
return $self->render unless defined $username; return $self->render unless defined $username;
if ( 30 < length( $username ) ) { if ( 30 < length( $username ) ) {
$self->stash->{'message'} = 'Username cannot be longer than 30 characters. Really?!'; $self->stash->{ 'message' } = 'Username cannot be longer than 30 characters. Really?!';
return; return;
} }
my $password = $self->req->param('password'); my $password = $self->req->param( 'password' );
my $password2 = $self->req->param('password2'); my $password2 = $self->req->param( 'password2' );
if ( ( ! defined $password ) || ( '' eq $password ) ) { if ( ( !defined $password ) || ( '' eq $password ) ) {
$self->stash->{'message'} = 'There has to be some password!'; $self->stash->{ 'message' } = 'There has to be some password!';
return; return;
} }
# Registration # Registration
if ( $self->req->param('is_registration') ) { if ( $self->req->param( 'is_registration' ) ) {
if ( if ( ( !defined $password2 )
( ! defined $password2 ) || || ( $password2 ne $password ) )
( $password2 ne $password ) {
) { $self->stash->{ 'message' } = 'You have to repeat password correctly!';
$self->stash->{'message'} = 'You have to repeat password correctly!';
return; return;
} }
my $status = $self->database->user_register( $username, $password ); my $status = $self->database->user_register( $username, $password );
if ( $status ) { if ( $status ) {
$self->flash('message' => 'User registered.'); $self->flash( 'message' => 'User registered.' );
$self->session( 'user' => $username ); $self->session( 'user' => $username );
$self->redirect_to( 'new-explain' ); $self->redirect_to( 'new-explain' );
} }
$self->stash->{'message'} = 'Registration failed.'; $self->stash->{ 'message' } = 'Registration failed.';
return; return;
} }
if ( $self->database->user_login( $username, $password ) ) { if ( $self->database->user_login( $username, $password ) ) {
$self->flash('message' => 'User logged in.'); $self->flash( 'message' => 'User logged in.' );
$self->session( 'user' => $username ); $self->session( 'user' => $username );
$self->redirect_to( 'new-explain' ); $self->redirect_to( 'new-explain' );
} }
$self->stash->{'message'} = 'Bad username or password.'; $self->stash->{ 'message' } = 'Bad username or password.';
return; return;
} }
@ -218,7 +211,7 @@ sub index {
} }
# save to database # save to database
my ( $id, $delete_key ) = $self->database->save_with_random_name( $title, $plan, $is_public, $is_anon, $self->session->{'user'} ); my ( $id, $delete_key ) = $self->database->save_with_random_name( $title, $plan, $is_public, $is_anon, $self->session->{ 'user' } );
# redirect to /show/:id # redirect to /show/:id
$self->flash( delete_key => $delete_key ); $self->flash( delete_key => $delete_key );

View File

@ -9,9 +9,9 @@ use DBI;
use Date::Simple; use Date::Simple;
use English qw( -no_match_vars ); use English qw( -no_match_vars );
has dbh => undef; has dbh => undef;
has connection_args => sub { [] }; has connection_args => sub { [] };
has log => undef; has log => undef;
sub register { sub register {
my ( $self, $app, $config ) = @_; my ( $self, $app, $config ) = @_;
@ -80,15 +80,15 @@ sub user_login {
$username, $username,
); );
return if 0 == scalar @row; return if 0 == scalar @row;
my $crypted = crypt( $password, $row[0] ); my $crypted = crypt( $password, $row[ 0 ] );
return if $crypted ne $row[0]; return if $crypted ne $row[ 0 ];
return 1; return 1;
} }
sub user_change_password { sub user_change_password {
my $self = shift; my $self = shift;
my ($username, $old, $new) = @_; my ( $username, $old, $new ) = @_;
my @row = $self->dbh->selectrow_array( my @row = $self->dbh->selectrow_array(
'SELECT password FROM users where username = ?', 'SELECT password FROM users where username = ?',
@ -96,10 +96,10 @@ sub user_change_password {
$username, $username,
); );
return if 0 == scalar @row; return if 0 == scalar @row;
my $crypted_old = crypt( $old, $row[0] ); my $crypted_old = crypt( $old, $row[ 0 ] );
my $crypted_new = crypt( $new, $self->get_pw_salt() ); my $crypted_new = crypt( $new, $self->get_pw_salt() );
@row = $self->dbh->selectrow_array( @row = $self->dbh->selectrow_array(
'UPDATE users SET password = ? WHERE ( username, password ) = ( ?, ? ) returning username', 'UPDATE users SET password = ? WHERE ( username, password ) = ( ?, ? ) returning username',
undef, undef,
@ -114,10 +114,10 @@ sub get_user_history {
my ( $user, $direction, $marker ) = @_; my ( $user, $direction, $marker ) = @_;
my $limit = 100; my $limit = 100;
$direction = 'DESC' if ( $direction // '' ) ne 'ASC'; $direction = 'DESC' if ( $direction // '' ) ne 'ASC';
my $query = ''; my $query = '';
my @args = (); my @args = ();
if ( defined $marker ) { if ( defined $marker ) {
my $comparison = $direction eq 'DESC' ? '<' : '>'; my $comparison = $direction eq 'DESC' ? '<' : '>';
@ -134,7 +134,8 @@ sub get_user_history {
"; ";
@args = ( $user, $marker ); @args = ( $user, $marker );
} else { }
else {
$query = " $query = "
SELECT p.id, p.entered_on::date, p.is_public, p.is_anonymized, p.title SELECT p.id, p.entered_on::date, p.is_public, p.is_anonymized, p.title
FROM plans p FROM plans p
@ -149,32 +150,32 @@ sub get_user_history {
$plans = [ reverse @{ $plans } ] if $direction eq 'ASC'; $plans = [ reverse @{ $plans } ] if $direction eq 'ASC';
return { return {
'list' => [], 'list' => [],
'earlier' => 0, 'earlier' => 0,
'later' => 0, 'later' => 0,
} if 0 == scalar @{ $plans }; } if 0 == scalar @{ $plans };
my @later = $self->dbh->selectrow_array( my @later = $self->dbh->selectrow_array(
'SELECT p.id FROM plans p where p.added_by = ? and not is_deleted and ( p.entered_on, p.id ) > ( select x.entered_on, x.id from plans x where x.id = ? ) limit 1', 'SELECT p.id FROM plans p where p.added_by = ? and not is_deleted and ( p.entered_on, p.id ) > ( select x.entered_on, x.id from plans x where x.id = ? ) limit 1',
undef, undef,
$user, $plans->[0]->{'id'}, $user, $plans->[ 0 ]->{ 'id' },
); );
my @earlier = $self->dbh->selectrow_array( my @earlier = $self->dbh->selectrow_array(
'SELECT p.id FROM plans p where p.added_by = ? and not is_deleted and ( p.entered_on, p.id ) < ( select x.entered_on, x.id from plans x where x.id = ? ) limit 1', 'SELECT p.id FROM plans p where p.added_by = ? and not is_deleted and ( p.entered_on, p.id ) < ( select x.entered_on, x.id from plans x where x.id = ? ) limit 1',
undef, undef,
$user, $plans->[-1]->{'id'}, $user, $plans->[ -1 ]->{ 'id' },
); );
return { return {
'list' => $plans, 'list' => $plans,
'later' => scalar @later, 'later' => scalar @later,
'earlier' => scalar @earlier, 'earlier' => scalar @earlier,
}; };
} }
sub get_pw_salt { sub get_pw_salt {
my $self = shift; my $self = shift;
my @salt_chars = ( 'a'..'z', 'A'..'Z', 0..9, '.', '/' ); my @salt_chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9, '.', '/' );
my $salt = sprintf '$6$%s$', join( '', map { $salt_chars[ rand @salt_chars ] } 1..16 ); my $salt = sprintf '$6$%s$', join( '', map { $salt_chars[ rand @salt_chars ] } 1 .. 16 );
return $salt; return $salt;
} }
@ -184,9 +185,7 @@ sub user_register {
my $crypted = crypt( $password, $self->get_pw_salt() ); my $crypted = crypt( $password, $self->get_pw_salt() );
eval { eval { $self->dbh->do( 'INSERT INTO users (username, password, registered) values (?, ?, now())', undef, $username, $crypted, ); };
$self->dbh->do( 'INSERT INTO users (username, password, registered) values (?, ?, now())', undef, $username, $crypted, );
};
return 1 unless $EVAL_ERROR; return 1 unless $EVAL_ERROR;
$self->log->error( "user_register( $username ) => " . $EVAL_ERROR ); $self->log->error( "user_register( $username ) => " . $EVAL_ERROR );
return; return;
@ -194,13 +193,13 @@ sub user_register {
sub update_plan { sub update_plan {
my $self = shift; my $self = shift;
my ($id, $changes) = @_; my ( $id, $changes ) = @_;
my @columns = keys %{ $changes }; my @columns = keys %{ $changes };
my @values = values %{ $changes }; my @values = values %{ $changes };
eval { eval {
$self->dbh->do( $self->dbh->do(
'UPDATE plans SET ' . join(', ', map { "$_ = ?" } @columns) . ' WHERE id = ?', 'UPDATE plans SET ' . join( ', ', map { "$_ = ?" } @columns ) . ' WHERE id = ?',
undef, undef,
@values, $id @values, $id
); );
@ -235,7 +234,7 @@ sub get_plan_data {
); );
return unless defined $rows; return unless defined $rows;
return if 0 == scalar @{ $rows }; return if 0 == scalar @{ $rows };
return $rows->[0]; return $rows->[ 0 ];
} }
sub get_plan { sub get_plan {

View File

@ -32,18 +32,18 @@ sub register {
# set mail charset and content type # set mail charset and content type
$mail->{ charset } = 'utf-8'; $mail->{ charset } = 'utf-8';
$mail->{ ctype } = 'text/plain'; $mail->{ ctype } = 'text/plain';
# log debug message # log debug message
$controller->app->log->debug( $controller->app->log->debug( sprintf "Sending mail:\n%s", $controller->dumper( $mail ) );
sprintf "Sending mail:\n%s", $controller->dumper( $mail )
);
# create Mail::Sender instance # create Mail::Sender instance
my $sender = Mail::Sender->new( { my $sender = Mail::Sender->new(
smtp => delete $mail->{ smtp }, {
from => delete $mail->{ from } smtp => delete $mail->{ smtp },
} ); from => delete $mail->{ from }
}
);
# unable to create instance # unable to create instance
unless ( ref $sender ) { unless ( ref $sender ) {