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