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

(-)a/Koha/Auth.pm (+151 lines)
Line 0 Link Here
1
package Koha::Auth;
2
3
use strict;
4
use warnings;
5
6
use C4::Auth qw//;
7
use C4::Context qw//;
8
9
=head2 authenticate
10
11
    my $user = Koha::Auth->authenticate({
12
        sessionID => $sessionID,
13
    });
14
15
Verifies that this user has an authenticated Koha session
16
17
=cut
18
19
#NOTE: This method requires that a Koha session exists
20
sub authenticate {
21
    my ($class,$args) = @_;
22
    my ($auth_user,$auth_session,$auth_cookie);
23
    my $sessionID = $args->{sessionID};
24
    if ($sessionID){
25
        my $session = C4::Auth::get_session($sessionID);
26
        if ($session){
27
            my $id = $session->param('id');
28
            if ( $id ){
29
                my $patrons = Koha::Patrons->search({ userid => $id });
30
                if ($patrons->count){
31
                    $auth_user = $patrons->next;
32
                    $auth_session = $session;
33
                    _set_userenv_compat({ session => $session });
34
                }
35
            }
36
        }
37
    }
38
    if ($auth_user){
39
        #FIXME: Check for timeout
40
    }
41
    #FIXME: Ideally, it would be nice to calculate patron authorizations at login time,
42
    #rather than on each page load (like we do currently)...
43
    return ($auth_user,$auth_session,$auth_cookie);
44
}
45
46
#NOTE: Needed since C4::Context->userenv is so ubiquitous
47
sub _set_userenv_compat {
48
    my ($args) = @_;
49
    my $session = $args->{session};
50
    if ($session){
51
        C4::Context->_new_userenv($session->param('id'));
52
        C4::Context->set_userenv(
53
            $session->param('number'),       $session->param('id'),
54
            $session->param('cardnumber'),   $session->param('firstname'),
55
            $session->param('surname'),      $session->param('branch'),
56
            $session->param('branchname'),   $session->param('flags'),
57
            $session->param('emailaddress'), $session->param('shibboleth'),
58
            $session->param('desk_id'),      $session->param('desk_name')
59
        );
60
    }
61
}
62
63
=head2 authorize
64
65
    my $flags = Koha::Auth->authorize({
66
        session => $session,
67
        flagsrequired => { self_check => 'self_checkout_module' },
68
    });
69
70
=cut
71
72
sub authorize {
73
    my ($class,$args) = @_;
74
    my $flags;
75
    my $session = $args->{session};
76
    my $flagsrequired = $args->{flagsrequired};
77
    if ($session && $flagsrequired){
78
        my $userid = $session->param('id');
79
        if ($userid){
80
            $flags = C4::Auth::haspermission($userid,$flagsrequired);
81
        }
82
    }
83
    return $flags;
84
}
85
86
sub get_authz_from_flags {
87
    my ($args) = @_;
88
    my $flags = $args->{flags};
89
    my $authz;
90
91
    #FIXME: This contains a lot of copy/paste from C4::Auth
92
    my $all_perms = C4::Auth::get_all_subpermissions();
93
94
    if ($flags){
95
        if ( $flags->{superlibrarian} == 1 ){
96
            #NOTE: These are similar but slightly different to @flatroots...
97
            my @perms = qw/
98
                circulate
99
                catalogue
100
                parameters
101
                borrowers
102
                permissions
103
                reserveforothers
104
                editcatalogue
105
                updatecharges
106
                acquisition
107
                tools
108
                editauthorities
109
                serials
110
                reports
111
                staffaccess
112
                plugins
113
                coursereserves
114
                clubs
115
                ill
116
                stockrotation
117
                problem_reports
118
            /;
119
            foreach my $perm (@perms){
120
                $authz->{ "CAN_user_${perm}" } = 1;
121
            }
122
            foreach my $module ( keys %$all_perms ) {
123
                foreach my $subperm ( keys %{ $all_perms->{$module} } ) {
124
                    $authz->{ "CAN_user_${module}_${subperm}" } = 1;
125
                }
126
            }
127
        }
128
        else {
129
            foreach my $module ( keys %$all_perms ) {
130
                if ( defined($flags->{$module}) && $flags->{$module} == 1 ) {
131
                    foreach my $subperm ( keys %{ $all_perms->{$module} } ) {
132
                        $authz->{ "CAN_user_${module}_${subperm}" } = 1;
133
                    }
134
                } elsif ( ref( $flags->{$module} ) ) {
135
                    foreach my $subperm ( keys %{ $flags->{$module} } ) {
136
                        $authz->{ "CAN_user_${module}_${subperm}" } = 1;
137
                    }
138
                }
139
            }
140
            foreach my $module ( keys %$flags ) {
141
                if ( $flags->{$module} == 1 or ref( $flags->{$module} ) ) {
142
                    $authz->{ "CAN_user_$module" } = 1;
143
                }
144
            }
145
        }
146
    }
147
148
    return $authz;
149
}
150
151
1;
(-)a/Koha/Staff.pm (+86 lines)
Line 0 Link Here
1
package Koha::Staff;
2
3
use Mojo::Base 'Mojolicious';
4
use Koha::Auth;
5
use Koha::Template;
6
use C4::Context;
7
8
sub startup {
9
    my $self = shift;
10
11
    #FIXME: Move into a plugin?
12
    $self->helper(
13
        staff_authorize => sub {
14
            my ($c,$args) = @_;
15
            my $session = $c->stash->{__koha_session__};
16
            my $flags = Koha::Auth->authorize({
17
                session => $session,
18
                flagsrequired => $args->{flagsrequired},
19
            });
20
            if($flags){
21
                $c->stash->{__koha_flags__} = $flags;
22
                $c->stash->{__koha_authz__} = Koha::Auth::get_authz_from_flags({ flags => $flags });
23
                return ($flags,$c->stash->{__koha__user__});
24
            }
25
            else {
26
                $c->render( text => 'Forbidden', status => 403 );
27
                return;
28
            }
29
        }
30
    );
31
    #FIXME: Move into a plugin?
32
    $self->helper(
33
        prepare_template => sub {
34
            my ($c,$args) = @_;
35
            my $template_filename = $args->{template_filename};
36
            my $interface = $args->{interface};
37
            my $template = Koha::Template::prepare_template({
38
                template_filename => $template_filename,
39
                interface => $interface,
40
                koha_session => $c->stash->{__koha_session__},
41
                koha_user => $c->stash->{__koha_user__},
42
                koha_authz => $c->stash->{__koha_authz__},
43
            });
44
            return $template;
45
        }
46
    );
47
48
    # Router
49
    my $r = $self->routes;
50
51
    #NOTE: The /login route is public and does not require authentication
52
    #FIXME: Implement a Mojolicious login route
53
    #$r->get('/login')->to( controller => 'login', action => 'index' );
54
55
    #NOTE: All other routes require authentication
56
    my $auth = $r->under('/' => sub {
57
        my $c = shift;
58
        my $sessionID = $c->cookie('CGISESSID');
59
        my ($user,$session) = Koha::Auth->authenticate({
60
            sessionID => $sessionID,
61
        });
62
        if ($user && $session){
63
            $c->stash->{__koha_user__} = $user;
64
            $c->stash->{__koha_session__} = $session;
65
            $c->cookie(
66
                'CGISESSID' => $session->id,
67
                {
68
                    httponly => 1,
69
                    secure => ( C4::Context->https_enabled() ? 1 : 0 ),
70
                    path => '/',
71
                }
72
            );
73
            return 1;
74
        }
75
        else {
76
            #FIXME: In future, redirect to a /login route
77
            $c->redirect_to('/index.html');
78
            return;
79
        }
80
    });
81
    my $tools = $auth->under('tools');
82
    $tools->any(['GET','POST'] => '/:controller/:action' => { action => 'index' })->to(namespace => 'Koha::Staff::Controller::Tools');
83
    #FIXME: handle 404 errors with catch all routes
84
}
85
86
1;
(-)a/Koha/Staff/Controller/Tools/Home.pm (+50 lines)
Line 0 Link Here
1
package Koha::Staff::Controller::Tools::Home;
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
21
use C4::Context;
22
use Koha::Plugins;
23
use Koha::Reviews;
24
use C4::Tags qw/get_count_by_tag_status/;
25
26
sub index {
27
    my $c = shift;
28
29
    my ($flags,$loggedinuser) = $c->staff_authorize({ flagsrequired => { 'tools' => '*' } });
30
    my $template = $c->prepare_template({
31
        template_filename => 'tools/tools-home.tt',
32
        interface => 'intranet',
33
    });
34
35
    my $pendingcomments = Koha::Reviews->search_limited({ approved => 0 })->count;
36
    $template->{VARS}->{pendingcomments} = $pendingcomments;
37
    my $pendingtags = get_count_by_tag_status(0);
38
    $template->{VARS}->{pendingtags} = $pendingtags;
39
40
    if ( C4::Context->config('enable_plugins') ) {
41
        my @tool_plugins = Koha::Plugins->new()->GetPlugins({
42
            method => 'tool',
43
        });
44
        $template->{VARS}->{tool_plugins} = \@tool_plugins if @tool_plugins;
45
    }
46
47
    $c->render( text => $template->output );
48
}
49
50
1;
(-)a/Koha/Template.pm (+41 lines)
Line 0 Link Here
1
package Koha::Template;
2
3
use strict;
4
use warnings;
5
6
use C4::Templates;
7
8
sub prepare_template {
9
    my ($args) = @_;
10
    my $template;
11
    my $session = $args->{koha_session};
12
    my $user = $args->{koha_user};
13
    my $authz = $args->{koha_authz};
14
    my $template_filename = $args->{template_filename};
15
    my $interface = $args->{interface};
16
    if ($template_filename && $interface){
17
        #FIXME: See Bug 27293 about refactoring C4::Templates::gettemplate for providing the "language" param and "KohaOpacLanguage" cookie
18
        $template = C4::Templates::gettemplate($template_filename,$interface);
19
        if ($template){
20
            if ($session){
21
                $template->{VARS}->{ sessionID } = $session->param('id');
22
            }
23
            if ($user){
24
                $template->{VARS}->{ logged_in_user } = $user;
25
                $template->{VARS}->{ loggedinusernumber } = $user->borrowernumber;
26
                $template->{VARS}->{ loggedinusername } = $user->userid;
27
            }
28
            if ($authz){
29
                foreach my $key ( keys %$authz ){
30
                    $template->{VARS}->{ $key } = $authz->{$key};
31
                }
32
            }
33
34
            #NOTE: You could include all the syspref stuff here that exists in C4::Auth,
35
            #but I rather avoid it, and have templates update to use the Koha.Preference mechanism instead.
36
        }
37
    }
38
    return $template;
39
}
40
41
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 28325