Bug 20568: Move value => client_id + secret
[koha.git] / members / 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   => 'members/apikeys.tt',
34         query           => $cgi,
35         type            => 'intranet',
36         authnotrequired => 0,
37         flagsrequired   => { borrowers => 1 },
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 ) {
48
49     # patron_id invalid -> exit
50     print $cgi->redirect("/cgi-bin/koha/errors/404.pl"); # escape early
51     exit;
52 }
53
54 my $op = $cgi->param('op');
55
56 if ($op) {
57     if ( $op eq 'generate' ) {
58         my $description = $cgi->param('description') // '';
59         my $api_key = Koha::ApiKey->new(
60             {   patron_id   => $patron_id,
61                 description => $description
62             }
63         );
64         $api_key->store;
65         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
66         exit;
67     }
68
69     if ( $op eq 'delete' ) {
70         my $api_key_id = $cgi->param('key');
71         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
72         if ($key) {
73             $key->delete;
74         }
75         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
76         exit;
77     }
78
79     if ( $op eq 'revoke' ) {
80         my $api_key_id = $cgi->param('key');
81         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
82         if ($key) {
83             $key->active(0);
84             $key->store;
85         }
86         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
87         exit;
88     }
89
90     if ( $op eq 'activate' ) {
91         my $api_key_id = $cgi->param('key');
92         my $key = Koha::ApiKeys->find({ patron_id => $patron_id, id => $api_key_id });
93         if ($key) {
94             $key->active(1);
95             $key->store;
96         }
97         print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
98         exit;
99     }
100 }
101
102 my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id });
103
104 $template->param(
105     api_keys => \@api_keys,
106     patron   => $patron
107 );
108
109 output_html_with_http_headers $cgi, $cookie, $template->output;