3 # This file is part of Koha.
5 # Copyright 2015 BibLibre
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.
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.
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>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_and_exit output_html_with_http_headers );
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34 { template_name => 'members/apikeys.tt',
37 flagsrequired => { borrowers => 'edit_borrowers' },
42 my $patron_id = $cgi->param('patron_id') // '';
43 my $api_key = $cgi->param('key') // '';
45 $patron = Koha::Patrons->find($patron_id) if $patron_id;
47 if ( not defined $patron or
48 not C4::Context->preference('RESTOAuth2ClientCredentials') ) {
50 # patron_id invalid -> exit
51 print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
55 if( $patron_id != $loggedinuser && !C4::Context->IsSuperLibrarian() ) {
56 # not the owner of the account viewing/editing own API keys, nor superlibrarian -> exit
57 print $cgi->redirect("/cgi-bin/koha/errors/403.pl"); # escape early
61 my $op = $cgi->param('op') // '';
63 if ( $op eq 'generate' or
68 output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
69 unless Koha::Token->new->check_csrf({
70 session_id => scalar $cgi->cookie('CGISESSID'),
71 token => scalar $cgi->param('csrf_token'),
76 if ( $op eq 'generate' ) {
77 my $description = $cgi->param('description') // '';
78 my $api_key = Koha::ApiKey->new(
79 { patron_id => $patron_id,
80 description => $description
86 fresh_api_key => $api_key,
87 api_keys => Koha::ApiKeys->search({ patron_id => $patron_id }),
91 if ( $op eq 'delete' ) {
92 my $api_key_id = $cgi->param('key');
93 my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
97 print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
101 if ( $op eq 'revoke' ) {
102 my $api_key_id = $cgi->param('key');
103 my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
108 print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
112 if ( $op eq 'activate' ) {
113 my $api_key_id = $cgi->param('key');
114 my $key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $api_key_id });
119 print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
125 api_keys => Koha::ApiKeys->search({ patron_id => $patron_id }),
126 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $cgi->cookie('CGISESSID') }),
130 output_html_with_http_headers $cgi, $cookie, $template->output;