View | Details | Raw Unified | Return to bug 13920
Collapse All | Expand All

(-)a/Koha/ApiKey.pm (+46 lines)
Line 0 Link Here
1
package Koha::ApiKey;
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
22
use Carp;
23
24
use Koha::Database;
25
26
use base qw(Koha::Object);
27
28
=head1 NAME
29
30
Koha::ApiKey - Koha API Key Object class
31
32
=head1 API
33
34
=head2 Class Methods
35
36
=cut
37
38
=head3 type
39
40
=cut
41
42
sub type {
43
    return 'ApiKey';
44
}
45
46
1;
(-)a/Koha/ApiKeys.pm (+121 lines)
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
use Koha::ApiKey;
25
26
use base qw(Koha::Objects);
27
28
use Koha::Exception::BadParameter;
29
use Koha::Exception::UnknownObject;
30
31
=head1 NAME
32
33
Koha::ApiKeys - Koha API Keys Object class
34
35
=head1 API
36
37
=head2 Class Methods
38
39
=cut
40
41
=head3 type
42
43
=cut
44
45
sub type {
46
    return 'ApiKey';
47
}
48
49
sub object_class {
50
    return 'Koha::ApiKey';
51
}
52
53
sub _get_castable_unique_columns {
54
    return ['api_key_id', 'api_key'];
55
}
56
57
=head grant
58
59
    my $apiKey = Koha::ApiKey->grant({borrower => $borrower,
60
                                    apiKey => $apiKey
61
                                });
62
63
Granting an ApiKey should be easy. This creates a new ApiKey for the given Borrower,
64
or sets the owner of an existing key.
65
$PARAM1 HASHRef of params, {
66
            borrower => MANDATORY, a Koha::Borrower or something castable to one.
67
            apiKey   => OPTIONAL, an existing Koha::ApiKEy to give to somebody else.
68
                                not sure why anybody would want to do that, but
69
                                provided as a convenience for testing.
70
}
71
@THROWS Koha::Exception::BadParameter
72
=cut
73
74
sub grant {
75
    my ($self, $borrower, $apiKey) = @_;
76
    $borrower = Koha::Borrowers->cast($borrower);
77
    if ($apiKey) {
78
        $apiKey = Koha::ApiKeys->cast($apiKey);
79
        $apiKey->borrowernumber($borrower->borrowernumber);
80
    }
81
    else {
82
        $apiKey = new Koha::ApiKey;
83
        $apiKey->borrowernumber($borrower->borrowernumber);
84
        $apiKey->api_key(String::Random->new->randregex('[a-zA-Z0-9]{32}'));
85
    }
86
    $apiKey->store;
87
    return $apiKey;
88
}
89
90
sub delete {
91
    my ($self, $apiKey) = @_;
92
    $apiKey = Koha::ApiKeys->cast($apiKey);
93
94
    if ($apiKey) {
95
        $apiKey->delete;
96
    }
97
}
98
99
sub revoke {
100
    my ($self, $apiKey) = @_;
101
    $apiKey = Koha::ApiKeys->cast($apiKey);
102
103
    if ($apiKey) {
104
        $apiKey->active(0);
105
        $apiKey->store;
106
    }
107
    return $apiKey;
108
}
109
110
sub activate {
111
    my ($self, $apiKey) = @_;
112
    $apiKey = Koha::ApiKeys->cast($apiKey);
113
114
    if ($apiKey) {
115
        $apiKey->active(1);
116
        $apiKey->store;
117
    }
118
    return $apiKey;
119
}
120
121
1;
(-)a/Koha/Borrower.pm (+32 lines)
Lines 43-48 sub type { Link Here
43
    return 'Borrower';
43
    return 'Borrower';
44
}
44
}
45
45
46
=head getApiKeys
47
48
    my @apiKeys = $borrower->getApiKeys( $activeOnly );
49
50
=cut
51
52
sub getApiKeys {
53
    my ($self, $activeOnly) = @_;
54
55
    my @dbix_objects = $self->_result()->api_keys({active => 1});
56
    for (my $i=0 ; $i<scalar(@dbix_objects) ; $i++) {
57
        $dbix_objects[$i] = Koha::ApiKey->_new_from_dbic($dbix_objects[$i]);
58
    }
59
60
    return \@dbix_objects;
61
}
62
63
=head getApiKey
64
65
    my $apiKey = $borrower->getApiKeys( $activeOnly );
66
67
=cut
68
69
sub getApiKey {
70
    my ($self, $activeOnly) = @_;
71
72
    my $dbix_object = $self->_result()->api_keys({active => 1})->next();
73
    my $object = Koha::ApiKey->_new_from_dbic($dbix_object);
74
75
    return $object;
76
}
77
46
=head1 AUTHOR
78
=head1 AUTHOR
47
79
48
Kyle M Hall <kyle@bywatersolutions.com>
80
Kyle M Hall <kyle@bywatersolutions.com>
(-)a/installer/data/mysql/kohastructure.sql (+19 lines)
Lines 16-21 Link Here
16
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
16
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
17
17
18
--
18
--
19
-- Table structure for table api_keys
20
--
21
22
DROP TABLE IF EXISTS api_keys;
23
CREATE TABLE api_keys (
24
    api_key_id INT(11) NOT NULL auto_increment,
25
    borrowernumber INT(11) NOT NULL, -- foreign key to the borrowers table
26
    api_key VARCHAR(255) NOT NULL, -- API key used for API authentication
27
    last_request_time INT(11) default 0, -- UNIX timestamp of when was the last transaction for this API-key? Used for request replay control.
28
    active INT(1) DEFAULT 1, -- 0 means this API key is revoked
29
    PRIMARY KEY (api_key_id),
30
    UNIQUE KEY apk_bornumkey_idx (borrowernumber, api_key),
31
    CONSTRAINT api_keys_fk_borrowernumber
32
      FOREIGN KEY (borrowernumber)
33
      REFERENCES borrowers (borrowernumber)
34
      ON DELETE CASCADE ON UPDATE CASCADE
35
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
36
37
--
19
-- Table structure for table `auth_header`
38
-- Table structure for table `auth_header`
20
--
39
--
21
40
(-)a/installer/data/mysql/updatedatabase.pl (+27 lines)
Lines 10776-10781 if ( CheckVersion($DBversion) ) { Link Here
10776
    SetVersion ($DBversion);
10776
    SetVersion ($DBversion);
10777
}
10777
}
10778
10778
10779
$DBversion = "XXX";
10780
if(CheckVersion($DBversion)) {
10781
    $dbh->do(q{
10782
        CREATE TABLE api_keys (
10783
            api_key_id INT(11) NOT NULL auto_increment,
10784
            borrowernumber INT(11) NOT NULL, -- foreign key to the borrowers table
10785
            api_key VARCHAR(255) NOT NULL, -- API key used for API authentication
10786
            last_request_time INT(11) default 0, -- UNIX timestamp of when was the last transaction for this API-key? Used for request replay control.
10787
            active INT(1) DEFAULT 1, -- 0 means this API key is revoked
10788
            PRIMARY KEY (api_key_id),
10789
            UNIQUE KEY apk_bornumkey_idx (borrowernumber, api_key),
10790
            CONSTRAINT api_keys_fk_borrowernumber
10791
              FOREIGN KEY (borrowernumber)
10792
              REFERENCES borrowers (borrowernumber)
10793
              ON DELETE CASCADE ON UPDATE CASCADE
10794
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
10795
    });
10796
10797
    use Koha::Auth::PermissionManager;
10798
    my $pm = Koha::Auth::PermissionManager->new();
10799
    $pm->addPermission({module => 'borrowers', code => 'manage_api_keys', description => "Manage Borrowers' REST API keys"});
10800
10801
    print "Upgrade to $DBversion done (Bug 13920: Add API keys table)\n";
10802
    SetVersion($DBversion);
10803
}
10804
10805
10779
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10806
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10780
# SEE bug 13068
10807
# SEE bug 13068
10781
# if there is anything in the atomicupdate, read and execute it.
10808
# if there is anything in the atomicupdate, read and execute it.
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc (+5 lines)
Lines 170-175 function searchToHold(){ Link Here
170
                [% ELSE %]
170
                [% ELSE %]
171
                    <li class="disabled"><a data-toggle="tooltip" data-placement="left" title="You are not authorized to set permissions" id="patronflags" href="#">Set permissions</a></li>
171
                    <li class="disabled"><a data-toggle="tooltip" data-placement="left" title="You are not authorized to set permissions" id="patronflags" href="#">Set permissions</a></li>
172
                [% END %]
172
                [% END %]
173
                [% IF ( CAN_user_borrowers_manage_api_keys ) %]
174
                    <li><a id="apikeys" href="/cgi-bin/koha/members/apikeys.pl?borrowernumber=[% borrowernumber %]">Manage API keys</a></li>
175
                [% ELSE %]
176
                    <li class="disabled"><a data-toggle="tooltip" data-placement="left" title="You are not authorized to manage API keys" id="apikeys" href="#">Manage API keys</a></li>
177
                [% END %]
173
                [% IF ( CAN_user_borrowers ) %]
178
                [% IF ( CAN_user_borrowers ) %]
174
                    [% IF ( NorwegianPatronDBEnable == 1 ) %]
179
                    [% IF ( NorwegianPatronDBEnable == 1 ) %]
175
                        <li><a id="deletepatronlocal" href="#">Delete local</a></li>
180
                        <li><a id="deletepatronlocal" href="#">Delete local</a></li>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/apikeys.tt (+78 lines)
Line 0 Link Here
1
[% USE Koha %]
2
[% INCLUDE 'doc-head-open.inc' %]
3
<title>Koha &rsaquo; Patrons &rsaquo; API Keys</title>
4
[% INCLUDE 'doc-head-close.inc' %]
5
</head>
6
<body id="pat_apikeys" class="pat">
7
[% INCLUDE 'header.inc' %]
8
[% INCLUDE 'patron-search.inc' %]
9
10
<div id="breadcrumbs">
11
  <a href="/cgi-bin/koha/mainpage.pl">Home</a>
12
  &rsaquo;
13
  <a href="/cgi-bin/koha/members/members-home.pl">Patrons</a>
14
  &rsaquo;
15
  API Keys for [% INCLUDE 'patron-title.inc' %]
16
</div>
17
18
<div id="doc3" class="yui-t2">
19
  <div id="bd">
20
    <div id="yui-main">
21
      <div class="yui-b">
22
        [% INCLUDE 'members-toolbar.inc' %]
23
24
        <h1>API keys for [% INCLUDE 'patron-title.inc' %]</h1>
25
        <div>
26
          <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
27
            <input type="hidden" name="borrowernumber" value="[% borrowernumber %]">
28
            <input type="hidden" name="op" value="generate">
29
            <input id="generatenewkey" type="submit" value="Generate new key">
30
          </form>
31
        </div>
32
        [% IF api_keys.size > 0 %]
33
          <table id="apikeystable">
34
            <thead>
35
              <tr>
36
                <th>Key</th>
37
                <th>Active</th>
38
                <th>Last transaction</th>
39
                <th>Actions</th>
40
              </tr>
41
            </thead>
42
            <tbody>
43
              [% FOREACH key IN api_keys %]
44
                <tr>
45
                  <td class="apikeykey">[% key.api_key %]</td>
46
                  <td class="apikeyactive">[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
47
                  <td class="apikeylastransaction">[% key.last_request_time || '' %]</td>
48
                  <td>
49
                    <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
50
                      <input type="hidden" name="borrowernumber" value="[% borrowernumber %]">
51
                      <input type="hidden" name="key" value="[% key.api_key %]">
52
                      <input type="hidden" name="op" value="delete">
53
                      <input class="apikeydelete" type="submit" value="Delete">
54
                    </form>
55
                    <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
56
                      <input type="hidden" name="borrowernumber" value="[% borrowernumber %]">
57
                      <input type="hidden" name="key" value="[% key.api_key %]">
58
                      [% IF key.active %]
59
                        <input type="hidden" name="op" value="revoke">
60
                        <input class="apikeyrevoke" type="submit" value="Revoke">
61
                      [% ELSE %]
62
                        <input type="hidden" name="op" value="activate">
63
                        <input class="apikeyactivate" type="submit" value="Activate">
64
                      [% END %]
65
                    </form>
66
                  </td>
67
                </tr>
68
              [% END %]
69
            </tbody>
70
          </table>
71
        [% END %]
72
      </div>
73
    </div>
74
    <div class="yui-b">
75
      [% INCLUDE 'circ-menu.inc' %]
76
    </div>
77
  </div>
78
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/usermenu.inc (+8 lines)
Lines 1-3 Link Here
1
[% USE Koha %]
1
[% IF ( ( Koha.Preference( 'opacuserlogin' ) == 1 ) && loggedinusername ) %]
2
[% IF ( ( Koha.Preference( 'opacuserlogin' ) == 1 ) && loggedinusername ) %]
2
    <div id="menu">
3
    <div id="menu">
3
        <h4><a href="#" class="menu-collapse-toggle">Your account menu</a></h4>
4
        <h4><a href="#" class="menu-collapse-toggle">Your account menu</a></h4>
Lines 102-107 Link Here
102
                [% END %]
103
                [% END %]
103
                <a href="/cgi-bin/koha/opac-discharge.pl">ask for a discharge</a></li>
104
                <a href="/cgi-bin/koha/opac-discharge.pl">ask for a discharge</a></li>
104
            [% END %]
105
            [% END %]
106
107
            [% IF apikeysview %]
108
              <li class="active">
109
            [% ELSE %]
110
              <li>
111
            [% END %]
112
              <a href="/cgi-bin/koha/opac-apikeys.pl">your API keys</a></li>
105
        </ul>
113
        </ul>
106
    </div>
114
    </div>
107
[% END %]
115
[% END %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-apikeys.tt (+82 lines)
Line 0 Link Here
1
[% INCLUDE 'doc-head-open.inc' %]
2
<title>[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog &rsaquo; Your library home &rsaquo; Your API keys</title>
3
[% INCLUDE 'doc-head-close.inc' %]
4
[% BLOCK cssinclude %][% END %]
5
</head>
6
[% INCLUDE 'bodytag.inc' bodyid='opac-user' bodyclass='scrollto' %]
7
[% INCLUDE 'masthead.inc' %]
8
9
<div class="main">
10
    <ul class="breadcrumb">
11
        <li><a href="/cgi-bin/koha/opac-main.pl">Home</a> <span class="divider">&rsaquo;</span></li>
12
        <li>
13
          <a href="/cgi-bin/koha/opac-user.pl">
14
            [% INCLUDE 'patron-title.inc' category_type = borrower.category_type firstname = borrower.firstname surname = borrower.surname othernames = borrower.othernames %]
15
          </a>
16
          <span class="divider">&rsaquo;</span>
17
        </li>
18
        <li><a href="/cgi-bin/koha/opac-apikeys.pl">Your API keys</a></li>
19
    </ul>
20
21
    <div class="container-fluid">
22
        <div class="row-fluid">
23
            <div class="span2">
24
                <div id="navigation">
25
                    [% INCLUDE 'navigation.inc' IsPatronPage = 1 %]
26
                </div>
27
            </div>
28
            <div class="span10">
29
                <div id="apikeys" class="maincontent">
30
                  <h1>Your API keys</h1>
31
                  <div>
32
                    <form action="/cgi-bin/koha/opac-apikeys.pl" method="post">
33
                      <input type="hidden" name="op" value="generate">
34
                      <input id="generatenewkey" type="submit" value="Generate new key">
35
                    </form>
36
                  </div>
37
                  [% IF api_keys.size > 0 %]
38
                    <table id="apikeystable" class="table table-bordered table-striped">
39
                      <thead>
40
                        <tr>
41
                          <th>Key</th>
42
                          <th>Active</th>
43
                          <th>Last transaction</th>
44
                          <th>Actions</th>
45
                        </tr>
46
                      </thead>
47
                      <tbody>
48
                        [% FOREACH key IN api_keys %]
49
                          <tr>
50
                            <td class="apikeykey">[% key.api_key %]</td>
51
                            <td class="apikeyactive">[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
52
                            <td class="apikeylastransaction">[% key.last_request_time || '' %]</td>
53
                            <td>
54
                              <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
55
                                <input type="hidden" name="key" value="[% key.api_key %]">
56
                                <input type="hidden" name="op" value="delete">
57
                                <input class="apikeydelete" type="submit" value="Delete">
58
                              </form>
59
                              <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
60
                                <input type="hidden" name="key" value="[% key.api_key %]">
61
                                [% IF key.active %]
62
                                  <input type="hidden" name="op" value="revoke">
63
                                  <input class="apikeyrevoke" type="submit" value="Revoke">
64
                                [% ELSE %]
65
                                  <input type="hidden" name="op" value="activate">
66
                                  <input class="apikeyactivate" type="submit" value="Activate">
67
                                [% END %]
68
                              </form>
69
                            </td>
70
                          </tr>
71
                        [% END %]
72
                      </tbody>
73
                    </table>
74
                  [% END %]
75
                </div> <!-- /#apikeys -->
76
            </div> <!-- /.span10 -->
77
        </div> <!-- /.row-fluid -->
78
    </div> <!-- /.container-fluid -->
79
</div> <!-- /#main -->
80
81
[% BLOCK jsinclude %][% END %]
82
[% INCLUDE 'opac-bottom.inc' %]
(-)a/members/apikeys.pl (+82 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2015 BibLibre
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 2 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
22
use CGI;
23
use String::Random;
24
25
use C4::Auth;
26
use C4::Members;
27
use C4::Output;
28
use Koha::ApiKeys;
29
use Koha::ApiKey;
30
31
my $cgi = new CGI;
32
33
my ($template, $loggedinuser, $cookie) = get_template_and_user({
34
    template_name => 'members/apikeys.tt',
35
    query => $cgi,
36
    type => 'intranet',
37
    authnotrequired => 0,
38
    flagsrequired => {borrowers => 'manage_api_keys'},
39
});
40
41
my $borrowernumber = $cgi->param('borrowernumber');
42
my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
43
my $op = $cgi->param('op');
44
45
if ($op) {
46
    if ($op eq 'generate') {
47
        Koha::ApiKeys->grant($borrower);
48
        print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
49
        exit;
50
    }
51
52
    if ($op eq 'delete') {
53
        my $key = $cgi->param('key');
54
        Koha::ApiKeys->delete($key);
55
        print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
56
        exit;
57
    }
58
59
    if ($op eq 'revoke') {
60
        my $key = $cgi->param('key');
61
        Koha::ApiKeys->revoke($key);
62
        print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
63
        exit;
64
    }
65
66
    if ($op eq 'activate') {
67
        my $key = $cgi->param('key');
68
        Koha::ApiKeys->activate($key);
69
        print $cgi->redirect('/cgi-bin/koha/members/apikeys.pl?borrowernumber=' . $borrowernumber);
70
        exit;
71
    }
72
}
73
74
my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
75
76
$template->param(
77
    api_keys => \@api_keys,
78
    borrower => $borrower,
79
    borrowernumber => $borrowernumber,
80
);
81
82
output_html_with_http_headers $cgi, $cookie, $template->output;
(-)a/opac/opac-apikeys.pl (+81 lines)
Line 0 Link Here
1
#!/usr/bin/env perl
2
3
# Copyright 2015 BibLibre
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 2 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
22
use CGI;
23
use String::Random;
24
25
use C4::Auth;
26
use C4::Members;
27
use C4::Output;
28
use Koha::ApiKeys;
29
30
my $cgi = new CGI;
31
32
my ($template, $loggedinuser, $cookie) = get_template_and_user({
33
    template_name => 'opac-apikeys.tt',
34
    query => $cgi,
35
    type => 'opac',
36
    authnotrequired => 0,
37
});
38
39
my $borrowernumber = $loggedinuser;
40
my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
41
my $op = $cgi->param('op');
42
43
if ($op) {
44
    if ($op eq 'generate') {
45
        Koha::ApiKeys->grant($borrower);
46
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
47
        exit;
48
    }
49
50
    if ($op eq 'delete') {
51
        my $key = $cgi->param('key');
52
        Koha::ApiKeys->delete($key);
53
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
54
        exit;
55
    }
56
57
    if ($op eq 'revoke') {
58
        my $key = $cgi->param('key');
59
        Koha::ApiKeys->revoke($key);
60
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
61
        exit;
62
    }
63
64
    if ($op eq 'activate') {
65
        my $key = $cgi->param('key');
66
        Koha::ApiKeys->activate($key);
67
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
68
        exit;
69
    }
70
}
71
72
my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
73
74
$template->param(
75
    apikeysview => 1,
76
    api_keys => \@api_keys,
77
    borrower => $borrower,
78
    borrowernumber => $borrowernumber,
79
);
80
81
output_html_with_http_headers $cgi, $cookie, $template->output;
(-)a/t/db_dependent/Koha/ApiKeys.t (-1 / +124 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2015 Vaara-kirjastot
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
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
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
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
20
use Modern::Perl;
21
use Test::More;
22
use Try::Tiny;
23
use Scalar::Util qw(blessed);
24
25
use Koha::Auth::PermissionManager;
26
use Koha::ApiKeys;
27
28
use t::lib::TestObjects::ObjectFactory;
29
use t::lib::TestObjects::BorrowerFactory;
30
use t::lib::Page::Members::Moremember;
31
use t::lib::Page::Opac::OpacMain;
32
33
##Setting up the test context
34
my $testContext = {};
35
36
my $password = '1234';
37
my $borrowerFactory = t::lib::TestObjects::BorrowerFactory->new();
38
my $borrowers = $borrowerFactory->createTestGroup([
39
            {firstname  => 'Olli-Antti',
40
             surname    => 'Kivi',
41
             cardnumber => '1A01',
42
             branchcode => 'CPL',
43
             password   => $password,
44
            },
45
            {firstname  => 'Alli-Ontti',
46
             surname    => 'Ivik',
47
             cardnumber => '1A02',
48
             branchcode => 'CPL',
49
             password   => $password,
50
            },
51
        ], undef, $testContext);
52
my $borrowerKivi = $borrowers->{'1A01'};
53
my $borrowerIvik = $borrowers->{'1A02'};
54
my $permissionManager = Koha::Auth::PermissionManager->new();
55
$permissionManager->grantPermission($borrowerKivi, 'borrowers', 'manage_api_keys');
56
57
58
##Test context set, starting testing:
59
eval { #run in a eval-block so we don't die without tearing down the test context
60
subtest "ApiKeys API Unit tests" => sub {
61
    my $borrowerKivi = $borrowers->{'1A01'};
62
    my $borrowerIvik = $borrowers->{'1A02'};
63
64
65
    my $apiKey = Koha::ApiKeys->grant($borrowerKivi);
66
    is($apiKey->borrowernumber, $borrowerKivi->borrowernumber, "ApiKey granted");
67
68
    Koha::ApiKeys->revoke($apiKey);
69
    is($apiKey->active, 0, "ApiKey revoked");
70
71
    Koha::ApiKeys->activate($apiKey);
72
    is($apiKey->active, 1, "ApiKey activated");
73
74
    Koha::ApiKeys->grant($borrowerIvik, $apiKey);
75
    is($apiKey->borrowernumber, $borrowerIvik->borrowernumber, "ApiKey granted to another Borrower");
76
77
    Koha::ApiKeys->delete($apiKey);
78
    $apiKey = Koha::ApiKeys->find({api_key_id => $apiKey->api_key_id});
79
    ok(not($apiKey), "ApiKey deleted");
80
}
81
};
82
if ($@) { #Catch all leaking errors and gracefully terminate.
83
    warn $@;
84
    tearDown();
85
    exit 1;
86
}
87
88
eval {
89
subtest "ApiKeys Intra Integration tests" => sub {
90
    my $agent = t::lib::Page::Members::Moremember->new({borrowernumber => $borrowerKivi->borrowernumber});
91
    $agent->doPasswordLogin($borrowerKivi->userid, $password)->navigateManageApiKeys()->generateNewApiKey();
92
    my @apiKeys = Koha::ApiKeys->search({borrowernumber => $borrowerKivi->borrowernumber});
93
    $agent->revokeApiKey($apiKeys[0]->api_key)->deleteApiKey($apiKeys[0]->api_key)
94
               ->quit();
95
}
96
};
97
if ($@) { #Catch all leaking errors and gracefully terminate.
98
    warn $@;
99
    tearDown();
100
    exit 1;
101
}
102
103
eval {
104
subtest "ApiKeys OPAC Integration tests" => sub {
105
    my $agent = t::lib::Page::Opac::OpacMain->new();
106
    $agent->doPasswordLogin($borrowerKivi->userid, $password)->navigateYourAPIKeys()->generateNewApiKey();
107
    my @apiKeys = Koha::ApiKeys->search({borrowernumber => $borrowerKivi->borrowernumber});
108
    $agent->revokeApiKey($apiKeys[0]->api_key)->deleteApiKey($apiKeys[0]->api_key)
109
               ->quit();
110
}
111
};
112
if ($@) { #Catch all leaking errors and gracefully terminate.
113
    warn $@;
114
    tearDown();
115
    exit 1;
116
}
117
118
##All tests done, tear down test context
119
tearDown();
120
done_testing;
121
122
sub tearDown {
123
    t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext);
124
}

Return to bug 13920