Bug 20568: API keys management in interface
[koha.git] / members / apikeys.pl
1 #!/usr/bin/env perl
2
3 # Copyright 2015 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use CGI;
23 use String::Random;
24
25 use C4::Auth;
26 use C4::Members;
27 use C4::Output;
28 use Koha::ApiKeys;
29 use Koha::ApiKey;
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 => 1},
39 });
40
41 my $borrowernumber = $cgi->param('borrowernumber');
42 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
43 my $op = $cgi->param('op');
44
45 if ($op) {
46     if ($op eq 'generate') {
47         my $apikey = new Koha::ApiKey;
48         $apikey->borrowernumber($borrowernumber);
49         $apikey->api_key(String::Random->new->randregex('[a-zA-Z0-9]{32}'));
50         $apikey->store;
51         print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
52         exit;
53     }
54
55     if ($op eq 'delete') {
56         my $key = $cgi->param('key');
57         my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
58         if ($api_key) {
59             $api_key->delete;
60         }
61         print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
62         exit;
63     }
64
65     if ($op eq 'revoke') {
66         my $key = $cgi->param('key');
67         my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
68         if ($api_key) {
69             $api_key->active(0);
70             $api_key->store;
71         }
72         print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
73         exit;
74     }
75
76     if ($op eq 'activate') {
77         my $key = $cgi->param('key');
78         my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
79         if ($api_key) {
80             $api_key->active(1);
81             $api_key->store;
82         }
83         print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
84         exit;
85     }
86 }
87
88 my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
89
90 $template->param(
91     api_keys => \@api_keys,
92     borrower => $borrower,
93     borrowernumber => $borrowernumber,
94 );
95
96 output_html_with_http_headers $cgi, $cookie, $template->output;