Bug 20568: (QA follow-up) Get rid of the id column
[koha.git] / opac / opac-apikeys.pl
1 #!/usr/bin/env 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
30 my $cgi = new CGI;
31
32 my ($template, $loggedinuser, $cookie) = get_template_and_user({
33     template_name   => 'opac-apikeys.tt',
34     query           => $cgi,
35     type            => 'opac',
36     authnotrequired => 0
37 });
38
39 my $patron_id = $loggedinuser;
40 my $patron = Koha::Patrons->find( $patron_id );
41
42 if ( not defined $patron
43     or !C4::Context->preference('AllowPatronsManageAPIKeysInOPAC') )
44 {
45     # patron_id invalid -> exit
46     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");    # escape early
47     exit;
48 }
49
50 my $op = $cgi->param('op');
51
52 if ($op) {
53     if ($op eq 'generate') {
54         my $description = $cgi->param('description') // '';
55         my $apikey = Koha::ApiKey->new({
56             patron_id   => $patron_id,
57             description => $description
58         });
59         $apikey->store;
60         print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
61         exit;
62     }
63
64     if ($op eq 'delete') {
65         my $key_id  = $cgi->param('key');
66         my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
67         if ($api_key) {
68             $api_key->delete;
69         }
70         print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
71         exit;
72     }
73
74     if ($op eq 'revoke') {
75         my $key_id  = $cgi->param('key');
76         my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
77         if ($api_key) {
78             $api_key->active(0);
79             $api_key->store;
80         }
81         print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
82         exit;
83     }
84
85     if ($op eq 'activate') {
86         my $key_id  = $cgi->param('key');
87         my $api_key = Koha::ApiKeys->find({ patron_id => $patron_id, client_id => $key_id });
88         if ($api_key) {
89             $api_key->active(1);
90             $api_key->store;
91         }
92         print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
93         exit;
94     }
95 }
96
97 my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id });
98
99 $template->param(
100     api_keys    => \@api_keys,
101     apikeysview => 1,
102     patron      => $patron
103 );
104
105 output_html_with_http_headers $cgi, $cookie, $template->output;