Bug 20568: API keys management in interface
[koha.git] / opac / opac-apikeys.pl
bloba2008a90b77661e84c4b1a63587a84efd199f0b0
1 #!/usr/bin/env perl
3 # Copyright 2015 BibLibre
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use CGI;
23 use String::Random;
25 use C4::Auth;
26 use C4::Members;
27 use C4::Output;
28 use Koha::ApiKeys;
29 use Koha::ApiKey;
31 my $cgi = new CGI;
33 my ($template, $loggedinuser, $cookie) = get_template_and_user({
34 template_name => 'opac-apikeys.tt',
35 query => $cgi,
36 type => 'opac',
37 authnotrequired => 0,
38 flagsrequired => {borrow => 1},
39 });
41 my $borrowernumber = $loggedinuser;
42 my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
43 my $op = $cgi->param('op');
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/opac-apikeys.pl');
52 exit;
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;
61 print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
62 exit;
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;
72 print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
73 exit;
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;
83 print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
84 exit;
88 my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
90 $template->param(
91 apikeysview => 1,
92 api_keys => \@api_keys,
93 borrower => $borrower,
94 borrowernumber => $borrowernumber,
97 output_html_with_http_headers $cgi, $cookie, $template->output;