|
Line 0
Link Here
|
|
|
1 |
package Koha::Mojo::Staff; |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Mojo::Base 'Mojolicious'; |
| 19 |
|
| 20 |
use C4::Context; |
| 21 |
use Koha::Auth; |
| 22 |
|
| 23 |
=head1 NAME |
| 24 |
|
| 25 |
Koha::Mojo::Staff - Mojolicious application for Koha staff interface |
| 26 |
|
| 27 |
=head1 SYNOPSIS |
| 28 |
|
| 29 |
=head2 METHODS |
| 30 |
|
| 31 |
=head3 startup |
| 32 |
|
| 33 |
=cut |
| 34 |
|
| 35 |
sub startup { |
| 36 |
my $self = shift; |
| 37 |
|
| 38 |
#NOTE: Load plugin which sets up core helper methods needed for any Koha app |
| 39 |
$self->plugin('Koha::Mojo::Plugins::Core'); |
| 40 |
|
| 41 |
# Router |
| 42 |
my $r = $self->routes; |
| 43 |
|
| 44 |
#NOTE: The /login route is public and does not require authentication |
| 45 |
#FIXME: Implement a Mojolicious login route |
| 46 |
#$r->get('/login')->to( controller => 'login', action => 'index' ); |
| 47 |
|
| 48 |
#NOTE: All other routes require authentication |
| 49 |
my $auth = $r->under('/' => sub { |
| 50 |
my $c = shift; |
| 51 |
my $sessionID = $c->cookie('CGISESSID'); |
| 52 |
my ($user,$session) = Koha::Auth->authenticate({ |
| 53 |
sessionID => $sessionID, |
| 54 |
}); |
| 55 |
if ($user && $session){ |
| 56 |
$c->stash->{__koha_user__} = $user; |
| 57 |
$c->stash->{__koha_session__} = $session; |
| 58 |
$c->cookie( |
| 59 |
'CGISESSID' => $session->id, |
| 60 |
{ |
| 61 |
httponly => 1, |
| 62 |
secure => ( C4::Context->https_enabled() ? 1 : 0 ), |
| 63 |
path => '/', |
| 64 |
} |
| 65 |
); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
else { |
| 69 |
#FIXME: In future, redirect to a /login route, or prompt for login here |
| 70 |
$c->redirect_to('/index.html'); |
| 71 |
return; |
| 72 |
} |
| 73 |
}); |
| 74 |
my $plugins = $auth->under('plugins'); |
| 75 |
#NOTE: Mojolicious 9 seems to require explicitly defining all routes (rather than getting :controller and :action from the path... |
| 76 |
$plugins->any(['GET','POST'] => '/run')->to(controller => 'Plugins', action => 'run'); |
| 77 |
|
| 78 |
#NOTE: Catch-all route to redirect to CGI 404 handler for any unmatched routes |
| 79 |
$auth->any('/*' => sub { |
| 80 |
my $c = shift; |
| 81 |
my ($flags,$loggedinuser) = $c->staff_authorize(); |
| 82 |
my $template = $c->prepare_template({ |
| 83 |
template_filename => 'errors/errorpage.tt', |
| 84 |
interface => 'intranet', |
| 85 |
}); |
| 86 |
$template->{VARS}->{errno} = 404; |
| 87 |
$template->{VARS}->{admin} = C4::Context->preference('KohaAdminEmailAddress'); |
| 88 |
my $status = 404; |
| 89 |
$c->render( text => $template->output(), status => $status ); |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
1; |