View | Details | Raw Unified | Return to bug 31380
Collapse All | Expand All

(-)a/Koha/Auth.pm (+79 lines)
Line 0 Link Here
1
package Koha::Auth;
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 Modern::Perl;
19
20
use C4::Auth qw//;
21
use Koha::Patrons;
22
23
=head2 authenticate
24
25
    my $user = Koha::Auth->authenticate({
26
        sessionID => $sessionID,
27
    });
28
29
Verifies that this user has an authenticated Koha session
30
31
=cut
32
33
sub authenticate {
34
    my ($class,$args) = @_;
35
    my ($auth_user,$auth_session);
36
    my $sessionID = $args->{sessionID};
37
    if ($sessionID){
38
        my $flags;
39
        my ( $return, $session, $more_info) = C4::Auth::check_cookie_auth($sessionID,$flags,
40
            { remote_addr => $ENV{REMOTE_ADDR}, skip_version_check => 1 }
41
        );
42
        if ($return && $return eq 'ok' && $session){
43
            my $userid = $session->param('id');
44
            if ( $userid ){
45
                my $patron = Koha::Patrons->find({ userid => $userid });
46
                if ($patron){
47
                    $auth_user = $patron;
48
                    $auth_session = $session;
49
                }
50
            }
51
        }
52
    }
53
    return ($auth_user,$auth_session);
54
}
55
56
=head2 authorize
57
58
    my $flags = Koha::Auth->authorize({
59
        session => $session,
60
        flagsrequired => { self_check => 'self_checkout_module' },
61
    });
62
63
=cut
64
65
sub authorize {
66
    my ($class,$args) = @_;
67
    my $flags;
68
    my $session = $args->{session};
69
    my $flagsrequired = $args->{flagsrequired};
70
    if ($session){
71
        my $userid = $session->param('id');
72
        if ($userid){
73
            $flags = C4::Auth::haspermission($userid,$flagsrequired);
74
        }
75
    }
76
    return $flags;
77
}
78
79
1;
(-)a/Koha/Mojo/Plugins/Core.pm (+63 lines)
Line 0 Link Here
1
package Koha::Mojo::Plugins::Core;
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 Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Plugin';
21
22
sub register {
23
	my ($self, $app, $conf) = @_;
24
25
    $app->helper(
26
        staff_authorize => sub {
27
            my ($c,$args) = @_;
28
            my $session = $c->stash->{__koha_session__};
29
            my $flags = Koha::Auth->authorize({
30
                session => $session,
31
                flagsrequired => $args->{flagsrequired},
32
            });
33
            if($flags){
34
                $c->stash->{__koha_flags__} = $flags;
35
                $c->stash->{__koha_authz__} = Koha::Auth::Permissions->get_authz_from_flags({ flags => $flags });
36
                return ($flags,$c->stash->{__koha__user__});
37
            }
38
            else {
39
                $c->render( text => 'Forbidden', status => 403 );
40
                return;
41
            }
42
        }
43
    );
44
45
    $app->helper(
46
        prepare_template => sub {
47
            my ($c,$args) = @_;
48
            my $template_filename = $args->{template_filename};
49
            my $interface = $args->{interface};
50
            my $template = Koha::Template::prepare_template({
51
                template_filename => $template_filename,
52
                interface => $interface,
53
                koha_session => $c->stash->{__koha_session__},
54
                koha_user => $c->stash->{__koha_user__},
55
                koha_authz => $c->stash->{__koha_authz__},
56
            });
57
            return $template;
58
        }
59
    );
60
}
61
62
1;
63
(-)a/Koha/Staff.pm (+82 lines)
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;
(-)a/Koha/Staff/Controller/Plugins.pm (+40 lines)
Line 0 Link Here
1
package Koha::Staff::Controller::Plugins;
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::Controller';
19
20
use C4::Context;
21
use Koha::Plugins::Handler;
22
23
sub run {
24
    my $c = shift;
25
    my $class = $c->param('class');
26
    my $method = $c->param('method');
27
    my ($flags,$loggedinuser) = $c->staff_authorize({ flagsrequired => { 'plugins' => $method } });
28
    my $plugins_enabled = C4::Context->config("enable_plugins");
29
    if ( $plugins_enabled ) {
30
        my $plugin = Koha::Plugins::Handler->run( { class => $class, method => $method, cgi => $c } );
31
    } else {
32
        my $template = $c->prepare_template({
33
            template_filename => 'plugins/plugins-disabled.tt',
34
            interface => 'intranet',
35
        });
36
        $c->render( text => $template->output );
37
    }
38
}
39
40
1;
(-)a/Koha/Template.pm (+52 lines)
Line 0 Link Here
1
package Koha::Template;
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 Modern::Perl;
19
20
use C4::Templates;
21
22
sub prepare_template {
23
    my ($args) = @_;
24
    my $template;
25
    my $session = $args->{koha_session};
26
    my $user = $args->{koha_user};
27
    my $authz = $args->{koha_authz};
28
    my $template_filename = $args->{template_filename};
29
    my $interface = $args->{interface};
30
    if ($template_filename && $interface){
31
        $template = C4::Templates::gettemplate($template_filename,$interface);
32
        if ($template){
33
            if ($session){
34
                $template->{VARS}->{ sessionID } = $session->param('id');
35
            }
36
            if ($user){
37
                $template->{VARS}->{ logged_in_user } = $user;
38
                $template->{VARS}->{ loggedinusernumber } = $user->borrowernumber;
39
                $template->{VARS}->{ loggedinusername } = $user->userid;
40
            }
41
            if ($authz){
42
                foreach my $key ( keys %$authz ){
43
                    $template->{VARS}->{ $key } = $authz->{$key};
44
                }
45
            }
46
            #NOTE: Instead of including syspref code here like in C4::Auth, start switching to Koha.Preference in templates
47
        }
48
    }
49
    return $template;
50
}
51
52
1;
(-)a/debian/templates/plack.psgi (+32 lines)
Lines 66-71 my $apiv1 = builder { Link Here
66
    $server->to_psgi_app;
66
    $server->to_psgi_app;
67
};
67
};
68
68
69
my $staff_interface = builder {
70
    my $server = Mojo::Server::PSGI->new;
71
    $server->build_app('Koha::Staff');
72
    my $app = $server->app;
73
    $app->hook( before_dispatch => sub {
74
        my $c = shift;
75
        #NOTE: Rewrite the base path to strip off the mount prefix
76
        my $path = $c->req->url->base->path(Mojo::Path->new);
77
78
    });
79
    $server->to_psgi_app;
80
};
81
69
Koha::Logger->_init;
82
Koha::Logger->_init;
70
83
71
builder {
84
builder {
Lines 114-119 builder { Link Here
114
        }
127
        }
115
        $intranet;
128
        $intranet;
116
    };
129
    };
130
    mount '/intranet/staff' => builder {
131
        #NOTE: it is important that these are relative links
132
        enable 'ErrorDocument',
133
            400 => 'errors/400.pl',
134
            401 => 'errors/401.pl',
135
            402 => 'errors/402.pl',
136
            403 => 'errors/403.pl',
137
            404 => 'errors/404.pl',
138
            500 => 'errors/500.pl',
139
            subrequest => 1;
140
        #NOTE: Without this middleware to catch fatal errors, ErrorDocument won't be able to render a 500 document
141
        #NOTE: This middleware must be closer to the PSGI app than ErrorDocument
142
        enable "HTTPExceptions";
143
        if ( Log::Log4perl->get_logger('plack-intranet')->has_appenders ){
144
            enable 'Log4perl', category => 'plack-intranet';
145
            enable 'LogWarn';
146
        }
147
        $staff_interface;
148
    };
117
    mount '/api/v1/app.pl' => builder {
149
    mount '/api/v1/app.pl' => builder {
118
        if ( Log::Log4perl->get_logger('plack-api')->has_appenders ){
150
        if ( Log::Log4perl->get_logger('plack-api')->has_appenders ){
119
            enable 'Log4perl', category => 'plack-api';
151
            enable 'Log4perl', category => 'plack-api';
(-)a/staff (-1 / +21 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
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 Modern::Perl;
19
20
require Mojolicious::Commands;
21
Mojolicious::Commands->start_app('Koha::Staff');

Return to bug 31380