View | Details | Raw Unified | Return to bug 20568
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 (+52 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
22
use Carp;
23
24
use Koha::Database;
25
26
use Koha::Borrower;
27
28
use base qw(Koha::Objects);
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
1;
(-)a/Koha/Schema/Result/ApiKey.pm (+92 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 active
39
40
  data_type: 'integer'
41
  default_value: 1
42
  is_nullable: 1
43
44
=cut
45
46
__PACKAGE__->add_columns(
47
  "borrowernumber",
48
  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
49
  "api_key",
50
  { data_type => "varchar", is_nullable => 0, size => 255 },
51
  "active",
52
  { data_type => "integer", default_value => 1, is_nullable => 1 },
53
);
54
55
=head1 PRIMARY KEY
56
57
=over 4
58
59
=item * L</borrowernumber>
60
61
=item * L</api_key>
62
63
=back
64
65
=cut
66
67
__PACKAGE__->set_primary_key("borrowernumber", "api_key");
68
69
=head1 RELATIONS
70
71
=head2 borrowernumber
72
73
Type: belongs_to
74
75
Related object: L<Koha::Schema::Result::Borrower>
76
77
=cut
78
79
__PACKAGE__->belongs_to(
80
  "borrowernumber",
81
  "Koha::Schema::Result::Borrower",
82
  { borrowernumber => "borrowernumber" },
83
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
84
);
85
86
87
# Created by DBIx::Class::Schema::Loader v0.07025 @ 2015-03-24 07:35:30
88
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dvujXVM5Vfu3SA2UfiVPtw
89
90
91
# You can replace this text with custom code or comments, and it will be preserved on regeneration
92
1;
(-)a/installer/data/mysql/kohastructure.sql (+16 lines)
Lines 15-20 Link Here
15
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
15
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
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
--
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
    active int(1) DEFAULT 1, -- 0 means this API key is revoked
27
    PRIMARY KEY (borrowernumber, api_key),
28
    CONSTRAINT api_keys_fk_borrowernumber
29
      FOREIGN KEY (borrowernumber)
30
      REFERENCES borrowers (borrowernumber)
31
      ON DELETE CASCADE ON UPDATE CASCADE
32
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
33
18
--
34
--
19
-- Table structure for table `auth_header`
35
-- Table structure for table `auth_header`
20
--
36
--
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc (+9 lines)
Lines 55-64 Link Here
55
                [% ELSE %]
55
                [% ELSE %]
56
                    <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>
56
                    <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>
57
                [% END %]
57
                [% END %]
58
58
                [% IF CAN_user_borrowers_edit_borrowers && useDischarge %]
59
                [% IF CAN_user_borrowers_edit_borrowers && useDischarge %]
59
                    <li><a href="/cgi-bin/koha/members/discharge.pl?borrowernumber=[% patron.borrowernumber %]">Discharge</a></li>
60
                    <li><a href="/cgi-bin/koha/members/discharge.pl?borrowernumber=[% patron.borrowernumber %]">Discharge</a></li>
60
                [% END %]
61
                [% END %]
61
                [% IF CAN_user_borrowers_edit_borrowers %]
62
                [% IF CAN_user_borrowers_edit_borrowers %]
63
64
                [% IF ( CAN_user_borrowers ) %]
65
                    <li><a id="apikeys" href="/cgi-bin/koha/members/apikeys.pl?borrowernumber=[% borrowernumber %]">Manage API keys</a></li>
66
                [% ELSE %]
67
                    <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>
68
                [% END %]
69
70
                [% IF ( CAN_user_borrowers ) %]
62
                    [% IF ( NorwegianPatronDBEnable == 1 ) %]
71
                    [% IF ( NorwegianPatronDBEnable == 1 ) %]
63
                        <li><a id="deletepatronlocal" href="#">Delete local</a></li>
72
                        <li><a id="deletepatronlocal" href="#">Delete local</a></li>
64
                        <li><a id="deletepatronremote" href="#">Delete remote</a></li>
73
                        <li><a id="deletepatronremote" href="#">Delete remote</a></li>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/apikeys.tt (+76 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>Actions</th>
39
              </tr>
40
            </thead>
41
            <tbody>
42
              [% FOREACH key IN api_keys %]
43
                <tr>
44
                  <td>[% key.api_key %]</td>
45
                  <td>[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
46
                  <td>
47
                    <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
48
                      <input type="hidden" name="borrowernumber" value="[% borrowernumber %]">
49
                      <input type="hidden" name="key" value="[% key.api_key %]">
50
                      <input type="hidden" name="op" value="delete">
51
                      <input type="submit" value="Delete">
52
                    </form>
53
                    <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
54
                      <input type="hidden" name="borrowernumber" value="[% borrowernumber %]">
55
                      <input type="hidden" name="key" value="[% key.api_key %]">
56
                      [% IF key.active %]
57
                        <input type="hidden" name="op" value="revoke">
58
                        <input type="submit" value="Revoke">
59
                      [% ELSE %]
60
                        <input type="hidden" name="op" value="activate">
61
                        <input type="submit" value="Activate">
62
                      [% END %]
63
                    </form>
64
                  </td>
65
                </tr>
66
              [% END %]
67
            </tbody>
68
          </table>
69
        [% END %]
70
      </div>
71
    </div>
72
    <div class="yui-b">
73
      [% INCLUDE 'circ-menu.inc' %]
74
    </div>
75
  </div>
76
[% INCLUDE 'intranet-bottom.inc' %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/usermenu.inc (+9 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 113-118 Link Here
113
                [% END %]
114
                [% END %]
114
                <a href="/cgi-bin/koha/opac-illrequests.pl">your interlibrary loan requests</a></li>
115
                <a href="/cgi-bin/koha/opac-illrequests.pl">your interlibrary loan requests</a></li>
115
            [% END %]
116
            [% END %]
117
118
            [% IF apikeysview %]
119
              <li class="active">
120
            [% ELSE %]
121
              <li>
122
            [% END %]
123
              <a href="/cgi-bin/koha/opac-apikeys.pl">your API keys</a>
124
            </li>
116
        </ul>
125
        </ul>
117
    </div>
126
    </div>
118
[% END %]
127
[% END %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-apikeys.tt (+80 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>Actions</th>
44
                        </tr>
45
                      </thead>
46
                      <tbody>
47
                        [% FOREACH key IN api_keys %]
48
                          <tr>
49
                            <td>[% key.api_key %]</td>
50
                            <td>[% IF key.active %]Yes[% ELSE %]No[% END %]</td>
51
                            <td>
52
                              <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
53
                                <input type="hidden" name="key" value="[% key.api_key %]">
54
                                <input type="hidden" name="op" value="delete">
55
                                <input type="submit" value="Delete">
56
                              </form>
57
                              <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
58
                                <input type="hidden" name="key" value="[% key.api_key %]">
59
                                [% IF key.active %]
60
                                  <input type="hidden" name="op" value="revoke">
61
                                  <input type="submit" value="Revoke">
62
                                [% ELSE %]
63
                                  <input type="hidden" name="op" value="activate">
64
                                  <input type="submit" value="Activate">
65
                                [% END %]
66
                              </form>
67
                            </td>
68
                          </tr>
69
                        [% END %]
70
                      </tbody>
71
                    </table>
72
                  [% END %]
73
                </div> <!-- /#apikeys -->
74
            </div> <!-- /.span10 -->
75
        </div> <!-- /.row-fluid -->
76
    </div> <!-- /.container-fluid -->
77
</div> <!-- /#main -->
78
79
[% BLOCK jsinclude %][% END %]
80
[% INCLUDE 'opac-bottom.inc' %]
(-)a/members/apikeys.pl (+96 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
        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/members/apikeys.pl?borrowernumber=' . $borrowernumber);
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/members/apikeys.pl?borrowernumber=' . $borrowernumber);
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/members/apikeys.pl?borrowernumber=' . $borrowernumber);
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/members/apikeys.pl?borrowernumber=' . $borrowernumber);
84
        exit;
85
    }
86
}
87
88
my @api_keys = Koha::ApiKeys->search({borrowernumber => $borrowernumber});
89
90
$template->param(
91
    api_keys => \@api_keys,
92
    borrower => $borrower,
93
    borrowernumber => $borrowernumber,
94
);
95
96
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 20568