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