Bug 30477: Add new UNIMARC installer translation files
[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 qw( get_template_and_user );
25 use C4::Output qw( output_and_exit output_html_with_http_headers );
26
27 use Koha::ApiKeys;
28 use Koha::Patrons;
29 use Koha::Token;
30
31 my $cgi = CGI->new;
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {   template_name   => 'members/apikeys.tt',
35         query           => $cgi,
36         type            => 'intranet',
37         flagsrequired   => { borrowers => 'edit_borrowers' },
38     }
39 );
40
41 my $patron;
42 my $patron_id = $cgi->param('patron_id') // '';
43 my $api_key   = $cgi->param('key')       // '';
44
45 $patron = Koha::Patrons->find($patron_id) if $patron_id;
46
47 if ( not defined $patron or
48      not C4::Context->preference('RESTOAuth2ClientCredentials') ) {
49
50     # patron_id invalid -> exit
51     print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
52     exit;
53 }
54
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
58     exit;
59 }
60
61 my $op = $cgi->param('op') // '';
62
63 if ( $op eq 'generate' or
64      $op eq 'delete' or
65      $op eq 'revoke' or
66      $op eq 'activate' ) {
67
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'),
72         });
73 }
74
75 if ($op) {
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
81             }
82         );
83         $api_key->store;
84
85         $template->param(
86             fresh_api_key => $api_key,
87             api_keys      => Koha::ApiKeys->search({ patron_id => $patron_id }),
88         );
89     }
90
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 });
94         if ($key) {
95             $key->delete;
96         }
97         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
98         exit;
99     }
100
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 });
104         if ($key) {
105             $key->active(0);
106             $key->store;
107         }
108         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
109         exit;
110     }
111
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 });
115         if ($key) {
116             $key->active(1);
117             $key->store;
118         }
119         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
120         exit;
121     }
122 }
123
124 $template->param(
125     api_keys   => Koha::ApiKeys->search({ patron_id => $patron_id }),
126     csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $cgi->cookie('CGISESSID') }),
127     patron     => $patron
128 );
129
130 output_html_with_http_headers $cgi, $cookie, $template->output;