|
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 |
#FIXME: Move into a plugin? |
| 29 |
$self->helper( |
| 30 |
staff_authorize => sub { |
| 31 |
my ($c,$args) = @_; |
| 32 |
my $session = $c->stash->{__koha_session__}; |
| 33 |
my $flags = Koha::Auth->authorize({ |
| 34 |
session => $session, |
| 35 |
flagsrequired => $args->{flagsrequired}, |
| 36 |
}); |
| 37 |
if($flags){ |
| 38 |
$c->stash->{__koha_flags__} = $flags; |
| 39 |
$c->stash->{__koha_authz__} = Koha::Auth::Permissions->get_authz_from_flags({ flags => $flags }); |
| 40 |
return ($flags,$c->stash->{__koha__user__}); |
| 41 |
} |
| 42 |
else { |
| 43 |
$c->render( text => 'Forbidden', status => 403 ); |
| 44 |
return; |
| 45 |
} |
| 46 |
} |
| 47 |
); |
| 48 |
#FIXME: Move into a plugin? |
| 49 |
$self->helper( |
| 50 |
prepare_template => sub { |
| 51 |
my ($c,$args) = @_; |
| 52 |
my $template_filename = $args->{template_filename}; |
| 53 |
my $interface = $args->{interface}; |
| 54 |
my $template = Koha::Template::prepare_template({ |
| 55 |
template_filename => $template_filename, |
| 56 |
interface => $interface, |
| 57 |
koha_session => $c->stash->{__koha_session__}, |
| 58 |
koha_user => $c->stash->{__koha_user__}, |
| 59 |
koha_authz => $c->stash->{__koha_authz__}, |
| 60 |
}); |
| 61 |
return $template; |
| 62 |
} |
| 63 |
); |
| 64 |
|
| 65 |
# Router |
| 66 |
my $r = $self->routes; |
| 67 |
|
| 68 |
#NOTE: The /login route is public and does not require authentication |
| 69 |
#FIXME: Implement a Mojolicious login route |
| 70 |
#$r->get('/login')->to( controller => 'login', action => 'index' ); |
| 71 |
|
| 72 |
#NOTE: All other routes require authentication |
| 73 |
my $auth = $r->under('/' => sub { |
| 74 |
my $c = shift; |
| 75 |
my $sessionID = $c->cookie('CGISESSID'); |
| 76 |
my ($user,$session) = Koha::Auth->authenticate({ |
| 77 |
sessionID => $sessionID, |
| 78 |
}); |
| 79 |
if ($user && $session){ |
| 80 |
$c->stash->{__koha_user__} = $user; |
| 81 |
$c->stash->{__koha_session__} = $session; |
| 82 |
$c->cookie( |
| 83 |
'CGISESSID' => $session->id, |
| 84 |
{ |
| 85 |
httponly => 1, |
| 86 |
secure => ( C4::Context->https_enabled() ? 1 : 0 ), |
| 87 |
path => '/', |
| 88 |
} |
| 89 |
); |
| 90 |
return 1; |
| 91 |
} |
| 92 |
else { |
| 93 |
#FIXME: In future, redirect to a /login route |
| 94 |
$c->redirect_to('/index.html'); |
| 95 |
return; |
| 96 |
} |
| 97 |
}); |
| 98 |
my $plugins = $auth->under('plugins'); |
| 99 |
#NOTE: Mojolicious 9 seems to require explicitly defining all routes (rather than getting :controller and :action from the path... |
| 100 |
$plugins->any(['GET','POST'] => '/run')->to(controller => 'Plugins', action => 'run'); |
| 101 |
#FIXME: handle 404 errors with catch all routes |
| 102 |
} |
| 103 |
|
| 104 |
1; |