Lines 25-96
use String::Random;
Link Here
|
25 |
use C4::Auth; |
25 |
use C4::Auth; |
26 |
use C4::Members; |
26 |
use C4::Members; |
27 |
use C4::Output; |
27 |
use C4::Output; |
|
|
28 |
|
28 |
use Koha::ApiKeys; |
29 |
use Koha::ApiKeys; |
29 |
use Koha::ApiKey; |
30 |
use Koha::Patrons; |
30 |
|
31 |
|
31 |
my $cgi = new CGI; |
32 |
my $cgi = new CGI; |
32 |
|
33 |
|
33 |
my ($template, $loggedinuser, $cookie) = get_template_and_user({ |
34 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
34 |
template_name => 'members/apikeys.tt', |
35 |
{ template_name => 'members/apikeys.tt', |
35 |
query => $cgi, |
36 |
query => $cgi, |
36 |
type => 'intranet', |
37 |
type => 'intranet', |
37 |
authnotrequired => 0, |
38 |
authnotrequired => 0, |
38 |
flagsrequired => {borrowers => 1}, |
39 |
flagsrequired => { borrowers => 1 }, |
39 |
}); |
40 |
} |
|
|
41 |
); |
42 |
|
43 |
my $patron_id = $cgi->param('patron_id') // ''; |
44 |
my $api_key = $cgi->param('key') // ''; |
45 |
my $patron = Koha::Patrons->find($patron_id); |
40 |
|
46 |
|
41 |
my $borrowernumber = $cgi->param('borrowernumber'); |
|
|
42 |
my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber); |
43 |
my $op = $cgi->param('op'); |
47 |
my $op = $cgi->param('op'); |
44 |
|
48 |
|
45 |
if ($op) { |
49 |
if ($op) { |
46 |
if ($op eq 'generate') { |
50 |
if ( $op eq 'generate' ) { |
47 |
my $apikey = new Koha::ApiKey; |
51 |
my $description = $cgi->param('description') // ''; |
48 |
$apikey->borrowernumber($borrowernumber); |
52 |
my $api_key = Koha::ApiKey->new( |
49 |
$apikey->api_key(String::Random->new->randregex('[a-zA-Z0-9]{32}')); |
53 |
{ patron_id => $patron_id, |
50 |
$apikey->store; |
54 |
description => $description |
51 |
print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber); |
55 |
} |
|
|
56 |
); |
57 |
$api_key->store; |
58 |
print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id ); |
52 |
exit; |
59 |
exit; |
53 |
} |
60 |
} |
54 |
|
61 |
|
55 |
if ($op eq 'delete') { |
62 |
if ( $op eq 'delete' ) { |
56 |
my $key = $cgi->param('key'); |
63 |
my $api_key = $cgi->param('key'); |
57 |
my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key}); |
64 |
my $key = Koha::ApiKeys->find({ patron_id => $patron_id, value => $api_key }); |
58 |
if ($api_key) { |
65 |
if ($key) { |
59 |
$api_key->delete; |
66 |
$key->delete; |
60 |
} |
67 |
} |
61 |
print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber); |
68 |
print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id ); |
62 |
exit; |
69 |
exit; |
63 |
} |
70 |
} |
64 |
|
71 |
|
65 |
if ($op eq 'revoke') { |
72 |
if ( $op eq 'revoke' ) { |
66 |
my $key = $cgi->param('key'); |
73 |
my $api_key = $cgi->param('key'); |
67 |
my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key}); |
74 |
my $key = Koha::ApiKeys->find({ patron_id => $patron_id, value => $api_key }); |
68 |
if ($api_key) { |
75 |
if ($key) { |
69 |
$api_key->active(0); |
76 |
$key->active(0); |
70 |
$api_key->store; |
77 |
$key->store; |
71 |
} |
78 |
} |
72 |
print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber); |
79 |
print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id ); |
73 |
exit; |
80 |
exit; |
74 |
} |
81 |
} |
75 |
|
82 |
|
76 |
if ($op eq 'activate') { |
83 |
if ( $op eq 'activate' ) { |
77 |
my $key = $cgi->param('key'); |
84 |
my $api_key = $cgi->param('key'); |
78 |
my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key}); |
85 |
my $key = Koha::ApiKeys->find({ patron_id => $patron_id, value => $api_key }); |
79 |
if ($api_key) { |
86 |
if ($key) { |
80 |
$api_key->active(1); |
87 |
$key->active(1); |
81 |
$api_key->store; |
88 |
$key->store; |
82 |
} |
89 |
} |
83 |
print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber); |
90 |
print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id ); |
84 |
exit; |
91 |
exit; |
85 |
} |
92 |
} |
86 |
} |
93 |
} |
87 |
|
94 |
|
88 |
my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber}); |
95 |
my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id }); |
89 |
|
96 |
|
90 |
$template->param( |
97 |
$template->param( |
91 |
api_keys => \@api_keys, |
98 |
api_keys => \@api_keys, |
92 |
borrower => $borrower, |
99 |
patron => $patron |
93 |
borrowernumber => $borrowernumber, |
|
|
94 |
); |
100 |
); |
95 |
|
101 |
|
96 |
output_html_with_http_headers $cgi, $cookie, $template->output; |
102 |
output_html_with_http_headers $cgi, $cookie, $template->output; |
97 |
- |
|
|