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 (+148 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
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
}
118
119
sub delete {
120
    my ($self, $apiKey) = @_;
121
    $apiKey = Koha::ApiKeys->cast($apiKey);
122
123
    if ($apiKey) {
124
        $apiKey->delete;
125
    }
126
}
127
128
sub revoke {
129
    my ($self, $apiKey) = @_;
130
    $apiKey = Koha::ApiKeys->cast($apiKey);
131
132
    if ($apiKey) {
133
        $apiKey->active(0);
134
        $apiKey->store;
135
    }
136
}
137
138
sub activate {
139
    my ($self, $apiKey) = @_;
140
    $apiKey = Koha::ApiKeys->cast($apiKey);
141
142
    if ($apiKey) {
143
        $apiKey->active(1);
144
        $apiKey->store;
145
    }
146
}
147
148
1;
(-)a/Koha/Schema/Result/ApiKey.pm (+99 lines)
Line 0 Link Here
1
use utf8;
2
package Koha::Schema::Result::ApiKey;
3
4
# Created by DBIx::Class::Schema::Loader
5
# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7
=head1 NAME
8
9
Koha::Schema::Result::ApiKey
10
11
=cut
12
13
use strict;
14
use warnings;
15
16
use base 'DBIx::Class::Core';
17
18
=head1 TABLE: C<api_keys>
19
20
=cut
21
22
__PACKAGE__->table("api_keys");
23
24
=head1 ACCESSORS
25
26
=head2 borrowernumber
27
28
  data_type: 'integer'
29
  is_foreign_key: 1
30
  is_nullable: 0
31
32
=head2 api_key
33
34
  data_type: 'varchar'
35
  is_nullable: 0
36
  size: 255
37
38
=head2 last_request_time
39
40
  data_type: 'integer'
41
  is_nullable: 1
42
43
=head2 active
44
45
  data_type: 'integer'
46
  default_value: 1
47
  is_nullable: 1
48
49
=cut
50
51
__PACKAGE__->add_columns(
52
  "borrowernumber",
53
  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
54
  "api_key",
55
  { data_type => "varchar", is_nullable => 0, size => 255 },
56
  "last_request_time",
57
  { data_type => "integer", is_nullable => 1 },
58
  "active",
59
  { data_type => "integer", default_value => 1, is_nullable => 1 },
60
);
61
62
=head1 PRIMARY KEY
63
64
=over 4
65
66
=item * L</borrowernumber>
67
68
=item * L</api_key>
69
70
=back
71
72
=cut
73
74
__PACKAGE__->set_primary_key("borrowernumber", "api_key");
75
76
=head1 RELATIONS
77
78
=head2 borrowernumber
79
80
Type: belongs_to
81
82
Related object: L<Koha::Schema::Result::Borrower>
83
84
=cut
85
86
__PACKAGE__->belongs_to(
87
  "borrowernumber",
88
  "Koha::Schema::Result::Borrower",
89
  { borrowernumber => "borrowernumber" },
90
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
91
);
92
93
94
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2015-07-24 12:05:49
95
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:GkDFK7kq8kqzyR+Kej/DRQ
96
97
98
# You can replace this text with custom code or comments, and it will be preserved on regeneration
99
1;
(-)a/installer/data/mysql/kohastructure.sql (+17 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
    borrowernumber INT(11) NOT NULL, -- foreign key to the borrowers table
25
    api_key VARCHAR(255) NOT NULL, -- API key used for API authentication
26
    last_request_time INT(11) default NULL, -- UNIX timestamp of when was the last transaction for this API-key? Used for request replay control.
27
    active INT(1) DEFAULT 1, -- 0 means this API key is revoked
28
    PRIMARY KEY (borrowernumber, api_key),
29
    CONSTRAINT api_keys_fk_borrowernumber
30
      FOREIGN KEY (borrowernumber)
31
      REFERENCES borrowers (borrowernumber)
32
      ON DELETE CASCADE ON UPDATE CASCADE
33
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
34
35
--
19
-- Table structure for table `auth_header`
36
-- Table structure for table `auth_header`
20
--
37
--
21
38
(-)a/installer/data/mysql/updatedatabase.pl (-43 / +24 lines)
Lines 10695-10743 if ( CheckVersion($DBversion) ) { Link Here
10695
    SetVersion ($DBversion);
10695
    SetVersion ($DBversion);
10696
}
10696
}
10697
10697
10698
$DBversion = "XXX";
10699
if(CheckVersion($DBversion)) {
10700
    $dbh->do(q{
10701
        DROP TABLE IF EXISTS api_keys;
10702
    });
10703
    $dbh->do(q{
10704
        CREATE TABLE api_keys (
10705
            borrowernumber int(11) NOT NULL,
10706
            api_key VARCHAR(255) NOT NULL,
10707
            active int(1) DEFAULT 1,
10708
            PRIMARY KEY (borrowernumber, api_key),
10709
            CONSTRAINT api_keys_fk_borrowernumber
10710
              FOREIGN KEY (borrowernumber)
10711
              REFERENCES borrowers (borrowernumber)
10712
              ON DELETE CASCADE ON UPDATE CASCADE
10713
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
10714
    });
10715
10716
    print "Upgrade to $DBversion done (Bug 13920: Add API keys table)\n";
10717
    SetVersion($DBversion);
10718
}
10719
10720
$DBversion = "XXX";
10721
if(CheckVersion($DBversion)) {
10722
    $dbh->do(q{
10723
        DROP TABLE IF EXISTS api_timestamps;
10724
    });
10725
    $dbh->do(q{
10726
        CREATE TABLE api_timestamps (
10727
            borrowernumber int(11) NOT NULL,
10728
            timestamp bigint,
10729
            PRIMARY KEY (borrowernumber),
10730
            CONSTRAINT api_timestamps_fk_borrowernumber
10731
              FOREIGN KEY (borrowernumber)
10732
              REFERENCES borrowers (borrowernumber)
10733
              ON DELETE CASCADE ON UPDATE CASCADE
10734
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
10735
    });
10736
10737
    print "Upgrade to $DBversion done (Bug 13920: Add API timestamps table)\n";
10738
    SetVersion($DBversion);
10739
}
10740
10741
$DBversion = "3.21.00.008";
10698
$DBversion = "3.21.00.008";
10742
if ( CheckVersion($DBversion) ) {
10699
if ( CheckVersion($DBversion) ) {
10743
    $dbh->do(q{
10700
    $dbh->do(q{
Lines 10773-10778 if ( CheckVersion($DBversion) ) { Link Here
10773
    SetVersion ($DBversion);
10730
    SetVersion ($DBversion);
10774
}
10731
}
10775
10732
10733
$DBversion = "XXX";
10734
if(CheckVersion($DBversion)) {
10735
    $dbh->do(q{
10736
        DROP TABLE IF EXISTS api_keys;
10737
    });
10738
    $dbh->do(q{
10739
        CREATE TABLE api_keys (
10740
            borrowernumber int(11) NOT NULL,
10741
            api_key VARCHAR(255) NOT NULL,
10742
            last_request_time INT(11) default NULL,
10743
            active int(1) DEFAULT 1,
10744
            PRIMARY KEY (borrowernumber, api_key),
10745
            CONSTRAINT api_keys_fk_borrowernumber
10746
              FOREIGN KEY (borrowernumber)
10747
              REFERENCES borrowers (borrowernumber)
10748
              ON DELETE CASCADE ON UPDATE CASCADE
10749
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
10750
    });
10751
10752
    print "Upgrade to $DBversion done (Bug 13920: Add API keys table)\n";
10753
    SetVersion($DBversion);
10754
}
10755
10756
10776
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10757
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10777
# SEE bug 13068
10758
# SEE bug 13068
10778
# if there is anything in the atomicupdate, read and execute it.
10759
# 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 171-176 function searchToHold(){ Link Here
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 ) %]
173
                [% IF ( CAN_user_borrowers ) %]
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 %]
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>
176
                        <li><a id="deletepatronremote" href="#">Delete remote</a></li>
181
                        <li><a id="deletepatronremote" href="#">Delete remote</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 [% IF ( searching ) %]&rsaquo; API Keys[% END %]</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 type="submit" value="Generate new key">
30
          </form>
31
        </div>
32
        [% IF api_keys.size > 0 %]
33
          <table>
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>[% key.api_key %]</td>
46
                  <td>[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
47
                  <td>[% key.timestamp || '' %]</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 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 type="submit" value="Revoke">
61
                      [% ELSE %]
62
                        <input type="hidden" name="op" value="activate">
63
                        <input 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
[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog &rsaquo; Your library home
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 type="submit" value="Generate new key">
35
                    </form>
36
                  </div>
37
                  [% IF api_keys.size > 0 %]
38
                    <table 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>[% key.api_key %]</td>
51
                            <td>[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
52
                            <td>[% key.timestamp || '' %]</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 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 type="submit" value="Revoke">
64
                                [% ELSE %]
65
                                  <input type="hidden" name="op" value="activate">
66
                                  <input 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/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
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 => 1},
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 (-1 / +97 lines)
Line 0 Link Here
0
- 
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
use Koha::ApiKey;
30
31
my $cgi = new CGI;
32
33
my ($template, $loggedinuser, $cookie) = get_template_and_user({
34
    template_name => 'opac-apikeys.tt',
35
    query => $cgi,
36
    type => 'opac',
37
    authnotrequired => 0,
38
    flagsrequired => {borrow => 1},
39
});
40
41
my $borrowernumber = $loggedinuser;
42
my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
43
my $op = $cgi->param('op');
44
45
if ($op) {
46
    if ($op eq 'generate') {
47
        my $apikey = new Koha::ApiKey;
48
        $apikey->borrowernumber($borrowernumber);
49
        $apikey->api_key(String::Random->new->randregex('[a-zA-Z0-9]{32}'));
50
        $apikey->store;
51
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
52
        exit;
53
    }
54
55
    if ($op eq 'delete') {
56
        my $key = $cgi->param('key');
57
        my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
58
        if ($api_key) {
59
            $api_key->delete;
60
        }
61
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
62
        exit;
63
    }
64
65
    if ($op eq 'revoke') {
66
        my $key = $cgi->param('key');
67
        my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
68
        if ($api_key) {
69
            $api_key->active(0);
70
            $api_key->store;
71
        }
72
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
73
        exit;
74
    }
75
76
    if ($op eq 'activate') {
77
        my $key = $cgi->param('key');
78
        my $api_key = Koha::ApiKeys->find({borrowernumber => $borrowernumber, api_key => $key});
79
        if ($api_key) {
80
            $api_key->active(1);
81
            $api_key->store;
82
        }
83
        print $cgi->redirect('/cgi-bin/koha/opac-apikeys.pl');
84
        exit;
85
    }
86
}
87
88
my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
89
90
$template->param(
91
    apikeysview => 1,
92
    api_keys => \@api_keys,
93
    borrower => $borrower,
94
    borrowernumber => $borrowernumber,
95
);
96
97
output_html_with_http_headers $cgi, $cookie, $template->output;

Return to bug 13920