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 && $flagsrequired){
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/Staff.pm (+104 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
    #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;
(-)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