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:
parent
f6d2ca31ba
commit
28fe739f46
@ -5,7 +5,7 @@ use Mojo::Base 'Mojolicious';
|
||||
sub startup {
|
||||
my $self = shift;
|
||||
|
||||
$self->sessions->cookie_name('explain');
|
||||
$self->sessions->cookie_name( 'explain' );
|
||||
$self->sessions->default_expiration( 60 * 60 * 24 * 365 );
|
||||
|
||||
# register Explain plugins namespace
|
||||
|
@ -10,158 +10,151 @@ use Email::Valid;
|
||||
|
||||
sub logout {
|
||||
my $self = shift;
|
||||
delete $self->session->{'user'};
|
||||
delete $self->session->{ 'user' };
|
||||
$self->redirect_to( 'new-explain' );
|
||||
}
|
||||
|
||||
sub user_history {
|
||||
my $self = shift;
|
||||
$self->redirect_to( 'history' ) unless $self->session->{'user'};
|
||||
$self->redirect_to( 'history' ) unless $self->session->{ 'user' };
|
||||
|
||||
my @args = ( $self->session->{'user'} );
|
||||
if (
|
||||
( $self->param('direction') ) &&
|
||||
( $self->param('direction') =~ m{\A(?:before|after)\z} ) &&
|
||||
( $self->param('key') )
|
||||
) {
|
||||
push @args, $self->param('direction') eq 'before' ? 'DESC' : 'ASC';
|
||||
push @args, $self->param('key');
|
||||
my @args = ( $self->session->{ 'user' } );
|
||||
if ( ( $self->param( 'direction' ) )
|
||||
&& ( $self->param( 'direction' ) =~ m{\A(?:before|after)\z} )
|
||||
&& ( $self->param( 'key' ) ) )
|
||||
{
|
||||
push @args, $self->param( 'direction' ) eq 'before' ? 'DESC' : 'ASC';
|
||||
push @args, $self->param( 'key' );
|
||||
}
|
||||
my $data = $self->database->get_user_history( @args );
|
||||
$self->stash->{'plans'} = $data;
|
||||
$self->stash->{ 'plans' } = $data;
|
||||
return $self->render();
|
||||
}
|
||||
|
||||
sub user {
|
||||
my $self = shift;
|
||||
|
||||
my $old = $self->req->param('old-pw');
|
||||
my $new = $self->req->param('new-pw');
|
||||
my $new2 = $self->req->param('new-pw2');
|
||||
my $old = $self->req->param( 'old-pw' );
|
||||
my $new = $self->req->param( 'new-pw' );
|
||||
my $new2 = $self->req->param( 'new-pw2' );
|
||||
|
||||
return $self->render unless defined $old;
|
||||
|
||||
if (
|
||||
( !defined $new ) ||
|
||||
( !defined $new2) ||
|
||||
( $new ne $new2 )
|
||||
) {
|
||||
$self->stash->{'message'} = 'You have to provide two identical copies of new password!';
|
||||
|
||||
if ( ( !defined $new )
|
||||
|| ( !defined $new2 )
|
||||
|| ( $new ne $new2 ) )
|
||||
{
|
||||
$self->stash->{ 'message' } = 'You have to provide two identical copies of new password!';
|
||||
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 ) {
|
||||
$self->flash('message' => 'Password changed.');
|
||||
$self->flash( 'message' => 'Password changed.' );
|
||||
$self->redirect_to( 'new-explain' );
|
||||
}
|
||||
$self->stash->{'message'} = 'Changing the password failed.';
|
||||
$self->stash->{ 'message' } = 'Changing the password failed.';
|
||||
}
|
||||
|
||||
sub plan_change {
|
||||
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->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') );
|
||||
if (
|
||||
( ! defined $plan->{'added_by'} ) ||
|
||||
( $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'});
|
||||
my $plan = $self->database->get_plan_data( $self->param( 'id' ) );
|
||||
if ( ( !defined $plan->{ 'added_by' } )
|
||||
|| ( $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->redirect_to( 'logout' );
|
||||
}
|
||||
|
||||
# All looks fine. Current plan data are in $plan.
|
||||
if (
|
||||
( $self->req->param('delete') ) &&
|
||||
( $self->req->param('delete') eq 'yes' )
|
||||
) {
|
||||
$self->database->delete_plan( $plan->{'id'}, $plan->{'delete_key'} );
|
||||
return $self->redirect_to( $self->req->param('return') );
|
||||
if ( ( $self->req->param( 'delete' ) )
|
||||
&& ( $self->req->param( 'delete' ) eq 'yes' ) )
|
||||
{
|
||||
$self->database->delete_plan( $plan->{ 'id' }, $plan->{ 'delete_key' } );
|
||||
return $self->redirect_to( $self->req->param( 'return' ) );
|
||||
}
|
||||
|
||||
my %changes = ();
|
||||
if ( $plan->{'title'} ne ( $self->req->param('title') // '' ) ) {
|
||||
$changes{'title'} = ( $self->req->param('title') // '' );
|
||||
if ( $plan->{ 'title' } ne ( $self->req->param( 'title' ) // '' ) ) {
|
||||
$changes{ 'title' } = ( $self->req->param( 'title' ) // '' );
|
||||
}
|
||||
if (
|
||||
( $plan->{'is_public'} ) &&
|
||||
( ! $self->req->param('is_public') )
|
||||
) {
|
||||
if ( ( $plan->{ 'is_public' } )
|
||||
&& ( !$self->req->param( 'is_public' ) ) )
|
||||
{
|
||||
$changes{ 'is_public' } = 0;
|
||||
} elsif (
|
||||
( ! $plan->{'is_public'} ) &&
|
||||
( $self->req->param('is_public') )
|
||||
) {
|
||||
}
|
||||
elsif (( !$plan->{ 'is_public' } )
|
||||
&& ( $self->req->param( 'is_public' ) ) )
|
||||
{
|
||||
$changes{ 'is_public' } = 1;
|
||||
}
|
||||
|
||||
if (
|
||||
( ! $plan->{'is_anonymized'}) &&
|
||||
( $self->req->param('is_anonymized') )
|
||||
) {
|
||||
my $explain = Pg::Explain->new( source => $plan->{'plan'} );
|
||||
if ( ( !$plan->{ 'is_anonymized' } )
|
||||
&& ( $self->req->param( 'is_anonymized' ) ) )
|
||||
{
|
||||
my $explain = Pg::Explain->new( source => $plan->{ 'plan' } );
|
||||
$explain->anonymize();
|
||||
$changes{'plan'} = $explain->as_text();
|
||||
$changes{ 'plan' } = $explain->as_text();
|
||||
$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 {
|
||||
my $self = shift;
|
||||
|
||||
# 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;
|
||||
|
||||
if ( 30 < length( $username ) ) {
|
||||
$self->stash->{'message'} = 'Username cannot be longer than 30 characters. Really?!';
|
||||
return;
|
||||
$self->stash->{ 'message' } = 'Username cannot be longer than 30 characters. Really?!';
|
||||
return;
|
||||
}
|
||||
|
||||
my $password = $self->req->param('password');
|
||||
my $password2 = $self->req->param('password2');
|
||||
my $password = $self->req->param( 'password' );
|
||||
my $password2 = $self->req->param( 'password2' );
|
||||
|
||||
if ( ( ! defined $password ) || ( '' eq $password ) ) {
|
||||
$self->stash->{'message'} = 'There has to be some password!';
|
||||
if ( ( !defined $password ) || ( '' eq $password ) ) {
|
||||
$self->stash->{ 'message' } = 'There has to be some password!';
|
||||
return;
|
||||
}
|
||||
|
||||
# Registration
|
||||
if ( $self->req->param('is_registration') ) {
|
||||
if (
|
||||
( ! defined $password2 ) ||
|
||||
( $password2 ne $password )
|
||||
) {
|
||||
$self->stash->{'message'} = 'You have to repeat password correctly!';
|
||||
if ( $self->req->param( 'is_registration' ) ) {
|
||||
if ( ( !defined $password2 )
|
||||
|| ( $password2 ne $password ) )
|
||||
{
|
||||
$self->stash->{ 'message' } = 'You have to repeat password correctly!';
|
||||
return;
|
||||
}
|
||||
|
||||
my $status = $self->database->user_register( $username, $password );
|
||||
if ( $status ) {
|
||||
$self->flash('message' => 'User registered.');
|
||||
$self->flash( 'message' => 'User registered.' );
|
||||
$self->session( 'user' => $username );
|
||||
$self->redirect_to( 'new-explain' );
|
||||
}
|
||||
$self->stash->{'message'} = 'Registration failed.';
|
||||
$self->stash->{ 'message' } = 'Registration failed.';
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $self->database->user_login( $username, $password ) ) {
|
||||
$self->flash('message' => 'User logged in.');
|
||||
$self->flash( 'message' => 'User logged in.' );
|
||||
$self->session( 'user' => $username );
|
||||
$self->redirect_to( 'new-explain' );
|
||||
}
|
||||
$self->stash->{'message'} = 'Bad username or password.';
|
||||
$self->stash->{ 'message' } = 'Bad username or password.';
|
||||
return;
|
||||
}
|
||||
|
||||
@ -218,7 +211,7 @@ sub index {
|
||||
}
|
||||
|
||||
# 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
|
||||
$self->flash( delete_key => $delete_key );
|
||||
|
@ -9,9 +9,9 @@ use DBI;
|
||||
use Date::Simple;
|
||||
use English qw( -no_match_vars );
|
||||
|
||||
has dbh => undef;
|
||||
has dbh => undef;
|
||||
has connection_args => sub { [] };
|
||||
has log => undef;
|
||||
has log => undef;
|
||||
|
||||
sub register {
|
||||
my ( $self, $app, $config ) = @_;
|
||||
@ -80,15 +80,15 @@ sub user_login {
|
||||
$username,
|
||||
);
|
||||
return if 0 == scalar @row;
|
||||
my $crypted = crypt( $password, $row[0] );
|
||||
|
||||
return if $crypted ne $row[0];
|
||||
my $crypted = crypt( $password, $row[ 0 ] );
|
||||
|
||||
return if $crypted ne $row[ 0 ];
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub user_change_password {
|
||||
my $self = shift;
|
||||
my ($username, $old, $new) = @_;
|
||||
my ( $username, $old, $new ) = @_;
|
||||
|
||||
my @row = $self->dbh->selectrow_array(
|
||||
'SELECT password FROM users where username = ?',
|
||||
@ -96,10 +96,10 @@ sub user_change_password {
|
||||
$username,
|
||||
);
|
||||
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() );
|
||||
|
||||
|
||||
@row = $self->dbh->selectrow_array(
|
||||
'UPDATE users SET password = ? WHERE ( username, password ) = ( ?, ? ) returning username',
|
||||
undef,
|
||||
@ -114,10 +114,10 @@ sub get_user_history {
|
||||
my ( $user, $direction, $marker ) = @_;
|
||||
|
||||
my $limit = 100;
|
||||
|
||||
|
||||
$direction = 'DESC' if ( $direction // '' ) ne 'ASC';
|
||||
my $query = '';
|
||||
my @args = ();
|
||||
my @args = ();
|
||||
|
||||
if ( defined $marker ) {
|
||||
my $comparison = $direction eq 'DESC' ? '<' : '>';
|
||||
@ -134,7 +134,8 @@ sub get_user_history {
|
||||
";
|
||||
@args = ( $user, $marker );
|
||||
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$query = "
|
||||
SELECT p.id, p.entered_on::date, p.is_public, p.is_anonymized, p.title
|
||||
FROM plans p
|
||||
@ -149,32 +150,32 @@ sub get_user_history {
|
||||
$plans = [ reverse @{ $plans } ] if $direction eq 'ASC';
|
||||
|
||||
return {
|
||||
'list' => [],
|
||||
'list' => [],
|
||||
'earlier' => 0,
|
||||
'later' => 0,
|
||||
'later' => 0,
|
||||
} if 0 == scalar @{ $plans };
|
||||
|
||||
|
||||
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',
|
||||
undef,
|
||||
$user, $plans->[0]->{'id'},
|
||||
$user, $plans->[ 0 ]->{ 'id' },
|
||||
);
|
||||
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',
|
||||
undef,
|
||||
$user, $plans->[-1]->{'id'},
|
||||
$user, $plans->[ -1 ]->{ 'id' },
|
||||
);
|
||||
return {
|
||||
'list' => $plans,
|
||||
'later' => scalar @later,
|
||||
'list' => $plans,
|
||||
'later' => scalar @later,
|
||||
'earlier' => scalar @earlier,
|
||||
};
|
||||
}
|
||||
|
||||
sub get_pw_salt {
|
||||
my $self = shift;
|
||||
my @salt_chars = ( 'a'..'z', 'A'..'Z', 0..9, '.', '/' );
|
||||
my $salt = sprintf '$6$%s$', join( '', map { $salt_chars[ rand @salt_chars ] } 1..16 );
|
||||
my $self = shift;
|
||||
my @salt_chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9, '.', '/' );
|
||||
my $salt = sprintf '$6$%s$', join( '', map { $salt_chars[ rand @salt_chars ] } 1 .. 16 );
|
||||
return $salt;
|
||||
}
|
||||
|
||||
@ -184,9 +185,7 @@ sub user_register {
|
||||
|
||||
my $crypted = crypt( $password, $self->get_pw_salt() );
|
||||
|
||||
eval {
|
||||
$self->dbh->do( 'INSERT INTO users (username, password, registered) values (?, ?, now())', undef, $username, $crypted, );
|
||||
};
|
||||
eval { $self->dbh->do( 'INSERT INTO users (username, password, registered) values (?, ?, now())', undef, $username, $crypted, ); };
|
||||
return 1 unless $EVAL_ERROR;
|
||||
$self->log->error( "user_register( $username ) => " . $EVAL_ERROR );
|
||||
return;
|
||||
@ -194,13 +193,13 @@ sub user_register {
|
||||
|
||||
sub update_plan {
|
||||
my $self = shift;
|
||||
my ($id, $changes) = @_;
|
||||
my ( $id, $changes ) = @_;
|
||||
my @columns = keys %{ $changes };
|
||||
my @values = values %{ $changes };
|
||||
|
||||
eval {
|
||||
$self->dbh->do(
|
||||
'UPDATE plans SET ' . join(', ', map { "$_ = ?" } @columns) . ' WHERE id = ?',
|
||||
'UPDATE plans SET ' . join( ', ', map { "$_ = ?" } @columns ) . ' WHERE id = ?',
|
||||
undef,
|
||||
@values, $id
|
||||
);
|
||||
@ -235,7 +234,7 @@ sub get_plan_data {
|
||||
);
|
||||
return unless defined $rows;
|
||||
return if 0 == scalar @{ $rows };
|
||||
return $rows->[0];
|
||||
return $rows->[ 0 ];
|
||||
}
|
||||
|
||||
sub get_plan {
|
||||
|
@ -32,18 +32,18 @@ sub register {
|
||||
|
||||
# set mail charset and content type
|
||||
$mail->{ charset } = 'utf-8';
|
||||
$mail->{ ctype } = 'text/plain';
|
||||
$mail->{ ctype } = 'text/plain';
|
||||
|
||||
# log debug message
|
||||
$controller->app->log->debug(
|
||||
sprintf "Sending mail:\n%s", $controller->dumper( $mail )
|
||||
);
|
||||
$controller->app->log->debug( sprintf "Sending mail:\n%s", $controller->dumper( $mail ) );
|
||||
|
||||
# create Mail::Sender instance
|
||||
my $sender = Mail::Sender->new( {
|
||||
smtp => delete $mail->{ smtp },
|
||||
from => delete $mail->{ from }
|
||||
} );
|
||||
my $sender = Mail::Sender->new(
|
||||
{
|
||||
smtp => delete $mail->{ smtp },
|
||||
from => delete $mail->{ from }
|
||||
}
|
||||
);
|
||||
|
||||
# unable to create instance
|
||||
unless ( ref $sender ) {
|
||||
|
Loading…
Reference in New Issue
Block a user