Created
May 27, 2014 10:33
-
-
Save nikita-d/f44cf89bede662a412b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package RTRu::Admin::Auth; | |
use Modern::Perl qw|2013|; | |
use Mojo::Base qw|Mojolicious::Controller|; | |
use Data::Printer (use_prototypes => 0); | |
use Hash::Merge::Simple qw|merge|; | |
sub login { | |
my $c = shift; | |
# return $c->redirect_to($c->url_for('page_index')) | |
# if $c->is_user_authenticated; | |
return $c->render('login'); | |
} | |
sub logoff { | |
my $c = shift; | |
# $c->logout; | |
return $c->redirect_to($c->url_for('page_login')); | |
} | |
sub auth { | |
my $c = shift->render_later; | |
my $delay = Mojo::IOLoop->delay( | |
sub { | |
$c->authenticate( | |
$c->param('login'), | |
$c->param('password'), | |
shift->begin | |
); | |
}, | |
sub { | |
my ($delay, $session_id, $expire, $user) = @_; | |
$c->app->log->debug(sprintf("authentication successfull: %s, %d", $session_id, $expire)); | |
$c->redirect_to($c->url_for('page_login')) and return 1 | |
unless defined $session_id && $session_id; | |
$c->stash('current_user' => $user); | |
$c->session( | |
sid => $session_id, | |
expire => $expire | |
)->redirect_to($c->url_for('page_index'), { current_user => $user }); | |
1; | |
}); | |
$delay->wait unless $delay->ioloop->is_running; | |
} | |
sub auth_as { | |
my $c = shift; | |
return my $auth_as = | |
defined $ENV{RT_AUTH_AS} && $ENV{RT_AUTH_AS} ? $ENV{RT_AUTH_AS} : 'admin'; | |
} | |
sub check { | |
my $c = shift; | |
my $sid = $c->session('sid'); | |
$c->redirect_to($c->url_for('page_login')) | |
unless (defined $sid && $sid); | |
my $delay = Mojo::IOLoop->delay( | |
sub { | |
$c->check_authentication($sid => shift->begin(0)); | |
}, | |
sub { | |
my ($delay, $c, $session_id, $expires, $data) = @_; | |
$c->redirect_to($c->url_for('page_login')) and return 1 | |
unless defined $session_id && $session_id; | |
return 1; | |
}); | |
$delay->wait unless $delay->ioloop->is_running; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment