Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha.git] / members / apikeys.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2015 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI;
23
24 use C4::Auth;
25 use C4::Output;
26
27 use Koha::ApiKeys;
28 use Koha::Patrons;
29 use Koha::Token;
30
31 my $cgi = new CGI;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {   template_name   => 'members/apikeys.tt',
35         query           => $cgi,
36         type            => 'intranet',
37         authnotrequired => 0,
38         flagsrequired   => { borrowers => 'edit_borrowers' },
39     }
40 );
41
42 my $patron;
43 my $patron_id = $cgi->param('patron_id') // '';
44 my $api_key   = $cgi->param('key')       // '';
45
46 $patron = Koha::Patrons->find($patron_id) if $patron_id;
47
48 if ( not defined $patron or
49      not C4::Context->preference('RESTOAuth2ClientCredentials') ) {
50
51     # patron_id invalid -> exit
52     print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
53     exit;
54 }
55
56 my $op = $cgi->param('op') // '';
57
58 if ( $op eq 'generate' or
59      $op eq 'delete' or
60      $op eq 'revoke' or
61      $op eq 'activate' ) {
62
63     output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
64         unless Koha::Token->new->check_csrf({
65             session_id => scalar $cgi->cookie('CGISESSID'),
66             token      => scalar $cgi->param('csrf_token'),
67         });
68 }
69
70 if ($op) {
71     if ( $op eq 'generate' ) {
72         my $description = $cgi->param('description') // '';
73         my $api_key = Koha::ApiKey->new(
74             {   patron_id   => $patron_id,
75                 description => $description
76             }
77         );
78         $api_key->store;
79         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
80         exit;
81     }
82
83     if ( $op eq 'delete' ) {
84         my $api_key_id = $cgi->param('key');
85         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
86         if ($key) {
87             $key->delete;
88         }
89         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
90         exit;
91     }
92
93     if ( $op eq 'revoke' ) {
94         my $api_key_id = $cgi->param('key');
95         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
96         if ($key) {
97             $key->active(0);
98             $key->store;
99         }
100         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
101         exit;
102     }
103
104     if ( $op eq 'activate' ) {
105         my $api_key_id = $cgi->param('key');
106         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
107         if ($key) {
108             $key->active(1);
109             $key->store;
110         }
111         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
112         exit;
113     }
114 }
115
116 my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id });
117
118 $template->param(
119     api_keys   => \@api_keys,
120     csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $cgi->cookie('CGISESSID') }),
121     patron     => $patron
122 );
123
124 output_html_with_http_headers $cgi, $cookie, $template->output;