|
Line 0
Link Here
|
|
|
1 |
package Koha::ApiKeys; |
| 2 |
|
| 3 |
# Copyright BibLibre 2015 |
| 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 3 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 |
use Scalar::Util qw(blessed); |
| 22 |
|
| 23 |
use Koha::Borrowers; |
| 24 |
|
| 25 |
use base qw(Koha::Objects); |
| 26 |
|
| 27 |
use Koha::Exception::BadParameter; |
| 28 |
use Koha::Exception::UnknownObject; |
| 29 |
|
| 30 |
=head1 NAME |
| 31 |
|
| 32 |
Koha::ApiKeys - Koha API Keys Object class |
| 33 |
|
| 34 |
=head1 API |
| 35 |
|
| 36 |
=head2 Class Methods |
| 37 |
|
| 38 |
=cut |
| 39 |
|
| 40 |
=head3 type |
| 41 |
|
| 42 |
=cut |
| 43 |
|
| 44 |
sub type { |
| 45 |
return 'ApiKey'; |
| 46 |
} |
| 47 |
|
| 48 |
sub object_class { |
| 49 |
return 'Koha::ApiKey'; |
| 50 |
} |
| 51 |
|
| 52 |
=head cast |
| 53 |
|
| 54 |
my $borrower = Koha::ApiKeys->cast("02132ofnsajdvbi24jjabac9a2g36l32"); |
| 55 |
my $borrower = Koha::ApiKeys->cast($Koha::ApiKey); |
| 56 |
my $borrower = Koha::ApiKeys->cast($Koha::Schema::Result::ApiKey); |
| 57 |
|
| 58 |
@PARAM1 Scalar, or object. |
| 59 |
@RETURNS Koha::ApiKey, possibly already in DB or a completely new one if nothing was |
| 60 |
inferred from the DB. |
| 61 |
@THROWS Koha::Exception::BadParameter, if no idea what to do with the input. |
| 62 |
@THROWS Koha::Exception::UnknownObject, if we cannot find a Borrower with the given input. |
| 63 |
=cut |
| 64 |
|
| 65 |
sub cast { |
| 66 |
my ($self, $input) = @_; |
| 67 |
|
| 68 |
unless ($input) { |
| 69 |
Koha::Exception::BadParameter->throw(error => __PACKAGE__."::cast():> No parameter given!"); |
| 70 |
} |
| 71 |
if (blessed($input) && $input->isa('Koha::ApiKey')) { |
| 72 |
return $input; |
| 73 |
} |
| 74 |
if (blessed($input) && $input->isa('Koha::Schema::Result::ApiKey')) { |
| 75 |
return Koha::ApiKey->_new_from_dbic($input); |
| 76 |
} |
| 77 |
|
| 78 |
my $apiKey = Koha::ApiKeys->find({api_key => $input}); |
| 79 |
|
| 80 |
unless ($apiKey) { |
| 81 |
Koha::Exception::UnknownObject->throw(error => "Koha::ApiKeys->cast():> Cannot find an existing ApiKey from api_key '$input'."); |
| 82 |
} |
| 83 |
|
| 84 |
return $apiKey; |
| 85 |
} |
| 86 |
|
| 87 |
=head grant |
| 88 |
|
| 89 |
my $apiKey = Koha::ApiKey->grant({borrower => $borrower, |
| 90 |
apiKey => $apiKey |
| 91 |
}); |
| 92 |
|
| 93 |
Granting an ApiKey should be easy. This creates a new ApiKey for the given Borrower, |
| 94 |
or sets the owner of an existing key. |
| 95 |
$PARAM1 HASHRef of params, { |
| 96 |
borrower => MANDATORY, a Koha::Borrower or something castable to one. |
| 97 |
apiKey => OPTIONAL, an existing Koha::ApiKEy to give to somebody else. |
| 98 |
not sure why anybody would want to do that, but |
| 99 |
provided as a convenience for testing. |
| 100 |
} |
| 101 |
@THROWS Koha::Exception::BadParameter |
| 102 |
=cut |
| 103 |
|
| 104 |
sub grant { |
| 105 |
my ($self, $borrower, $apiKey) = @_; |
| 106 |
$borrower = Koha::Borrowers::castToBorrower($borrower); |
| 107 |
if ($apiKey) { |
| 108 |
$apiKey = Koha::ApiKeys->cast($apiKey); |
| 109 |
$apiKey->borrowernumber($borrower->borrowernumber); |
| 110 |
} |
| 111 |
else { |
| 112 |
$apiKey = new Koha::ApiKey; |
| 113 |
$apiKey->borrowernumber($borrower->borrowernumber); |
| 114 |
$apiKey->api_key(String::Random->new->randregex('[a-zA-Z0-9]{32}')); |
| 115 |
} |
| 116 |
$apiKey->store; |
| 117 |
return $apiKey; |
| 118 |
} |
| 119 |
|
| 120 |
sub delete { |
| 121 |
my ($self, $apiKey) = @_; |
| 122 |
$apiKey = Koha::ApiKeys->cast($apiKey); |
| 123 |
|
| 124 |
if ($apiKey) { |
| 125 |
$apiKey->delete; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
sub revoke { |
| 130 |
my ($self, $apiKey) = @_; |
| 131 |
$apiKey = Koha::ApiKeys->cast($apiKey); |
| 132 |
|
| 133 |
if ($apiKey) { |
| 134 |
$apiKey->active(0); |
| 135 |
$apiKey->store; |
| 136 |
} |
| 137 |
return $apiKey; |
| 138 |
} |
| 139 |
|
| 140 |
sub activate { |
| 141 |
my ($self, $apiKey) = @_; |
| 142 |
$apiKey = Koha::ApiKeys->cast($apiKey); |
| 143 |
|
| 144 |
if ($apiKey) { |
| 145 |
$apiKey->active(1); |
| 146 |
$apiKey->store; |
| 147 |
} |
| 148 |
return $apiKey; |
| 149 |
} |
| 150 |
|
| 151 |
1; |