Line 0
Link Here
|
|
|
1 |
package Koha::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 |
use C4::Context; |
20 |
|
21 |
use Koha::Auth; |
22 |
use Koha::Auth::Permissions; |
23 |
use Koha::Template; |
24 |
|
25 |
sub startup { |
26 |
my $self = shift; |
27 |
|
28 |
#NOTE: Load plugin which sets up core helper methods needed for any Koha app |
29 |
$self->plugin('Koha::Mojo::Plugins::Core'); |
30 |
|
31 |
# Router |
32 |
my $r = $self->routes; |
33 |
|
34 |
#NOTE: The /login route is public and does not require authentication |
35 |
#FIXME: Implement a Mojolicious login route |
36 |
#$r->get('/login')->to( controller => 'login', action => 'index' ); |
37 |
|
38 |
#NOTE: All other routes require authentication |
39 |
my $auth = $r->under('/' => sub { |
40 |
my $c = shift; |
41 |
my $sessionID = $c->cookie('CGISESSID'); |
42 |
my ($user,$session) = Koha::Auth->authenticate({ |
43 |
sessionID => $sessionID, |
44 |
}); |
45 |
if ($user && $session){ |
46 |
$c->stash->{__koha_user__} = $user; |
47 |
$c->stash->{__koha_session__} = $session; |
48 |
$c->cookie( |
49 |
'CGISESSID' => $session->id, |
50 |
{ |
51 |
httponly => 1, |
52 |
secure => ( C4::Context->https_enabled() ? 1 : 0 ), |
53 |
path => '/', |
54 |
} |
55 |
); |
56 |
return 1; |
57 |
} |
58 |
else { |
59 |
#FIXME: In future, redirect to a /login route, or prompt for login here |
60 |
$c->redirect_to('/index.html'); |
61 |
return; |
62 |
} |
63 |
}); |
64 |
my $plugins = $auth->under('plugins'); |
65 |
#NOTE: Mojolicious 9 seems to require explicitly defining all routes (rather than getting :controller and :action from the path... |
66 |
$plugins->any(['GET','POST'] => '/run')->to(controller => 'Plugins', action => 'run'); |
67 |
|
68 |
#NOTE: Catch-all route to redirect to CGI 404 handler for any unmatched routes |
69 |
$auth->any('/*' => sub { |
70 |
my $c = shift; |
71 |
my ($flags,$loggedinuser) = $c->staff_authorize(); |
72 |
my $template = $c->prepare_template({ |
73 |
template_filename => 'errors/errorpage.tt', |
74 |
interface => 'intranet', |
75 |
}); |
76 |
$template->{VARS}->{errno} = 404; |
77 |
$template->{VARS}->{admin} = C4::Context->preference('KohaAdminEmailAddress'); |
78 |
$c->render( text => $template->output(), status => 404 ); |
79 |
}); |
80 |
} |
81 |
|
82 |
1; |