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

(-)a/debian/docs/koha-api-keys.xml (-12 / +14 lines)
Lines 29-47 Link Here
29
29
30
        <refsynopsisdiv>
30
        <refsynopsisdiv>
31
            <cmdsynopsis>
31
            <cmdsynopsis>
32
                <command>sudo koha-shell</command>
32
                <command>sudo koha-api-keys</command>
33
                <arg choice="req"><option>-c</option> "koha-api-keys <option>--generate</option> <option>--description</option> <replaceable>description</replaceable> <option>--borrowernumber</option> <replaceable>borrowernumber</replaceable>"</arg>
33
                <arg choice="req"><option>--generate</option></arg>
34
                <arg choice="req"><option>--description</option> <replaceable>description</replaceable></arg>
35
                <arg choice="req"><option>--borrowernumber</option> <replaceable>borrowernumber</replaceable></arg>
34
                <arg choice="req"><replaceable>instancename</replaceable></arg>
36
                <arg choice="req"><replaceable>instancename</replaceable></arg>
35
            </cmdsynopsis>
37
            </cmdsynopsis>
36
            <cmdsynopsis>
38
            <cmdsynopsis>
37
                <command>sudo koha-shell</command>
39
                <command>sudo koha-api-keys</command>
38
                <arg choice="req"><option>-c</option> "koha-api-keys <option>--delete</option> [<option>--client-id</option> <replaceable>client_id</replaceable>]"</arg>
40
                <arg choice="req"><option>--delete</option></arg>
41
                <arg choice="opt"><option>--client-id</option> <replaceable>client_id</replaceable></arg>
39
                <arg choice="req"><replaceable>instancename</replaceable></arg>
42
                <arg choice="req"><replaceable>instancename</replaceable></arg>
40
            </cmdsynopsis>
43
            </cmdsynopsis>
41
            <cmdsynopsis>
44
            <cmdsynopsis>
42
                <command>sudo koha-shell</command>
45
                <command>sudo koha-api-keys</command>
43
                <arg choice="req"><option>-c</option> "koha-api-keys <option>--help</option>"</arg>
46
                <arg choice="req"><option>--help</option></arg>
44
                <arg choice="req"><replaceable>instancename</replaceable></arg>
45
            </cmdsynopsis>
47
            </cmdsynopsis>
46
        </refsynopsisdiv>
48
        </refsynopsisdiv>
47
49
Lines 51-57 Link Here
51
                The <command>koha-api-keys</command> script allows administrators to generate or delete API keys associated with specific Koha patrons. These API keys can be used for authorized programmatic access to the Koha system.
53
                The <command>koha-api-keys</command> script allows administrators to generate or delete API keys associated with specific Koha patrons. These API keys can be used for authorized programmatic access to the Koha system.
52
            </para>
54
            </para>
53
            <para>
55
            <para>
54
                As this tool requires administrative privileges and access to the Koha environment, it must be run using <command>sudo</command> and the <command>koha-shell</command> wrapper as shown in the synopsis.
56
                The instance name must be provided as the last argument. The script will automatically set up the Koha environment and run as the instance user.
55
            </para>
57
            </para>
56
        </refsect1>
58
        </refsect1>
57
59
Lines 100-115 Link Here
100
        <refsect1>
102
        <refsect1>
101
            <title>Examples</title>
103
            <title>Examples</title>
102
            <para>Generate a new API key for patron '123' with the description 'Test API access':</para>
104
            <para>Generate a new API key for patron '123' with the description 'Test API access':</para>
103
            <screen>$ sudo koha-shell -c "koha-api-keys --generate --description 'Test API access' --borrowernumber 123" instancename</screen>
105
            <screen>$ sudo koha-api-keys --generate --description 'Test API access' --borrowernumber 123 instancename</screen>
104
106
105
            <para>Delete an existing API key (interactive):</para>
107
            <para>Delete an existing API key (interactive):</para>
106
            <screen>$ echo "CLIENT_ID" | sudo koha-shell -c "koha-api-keys --delete" instancename</screen>
108
            <screen>$ sudo koha-api-keys --delete instancename</screen>
107
109
108
            <para>Delete an existing API key (scripted):</para>
110
            <para>Delete an existing API key (scripted):</para>
109
            <screen>$ sudo koha-shell -c "koha-api-keys --delete --client-id a1b2c3d4-e5f6-7890-abcd-ef1234567890" instancename</screen>
111
            <screen>$ sudo koha-api-keys --delete --client-id a1b2c3d4-e5f6-7890-abcd-ef1234567890 instancename</screen>
110
112
111
            <para>Display help information:</para>
113
            <para>Display help information:</para>
112
            <screen>$ sudo koha-shell -c "koha-api-keys --help" instancename</screen>
114
            <screen>$ sudo koha-api-keys --help</screen>
113
        </refsect1>
115
        </refsect1>
114
    </refentry>
116
    </refentry>
115
</article>
117
</article>
(-)a/debian/koha-common.install (+1 lines)
Lines 7-12 debian/unavailable.html usr/share/koha/intranet/htdocs Link Here
7
debian/unavailable.html                     usr/share/koha/opac/htdocs
7
debian/unavailable.html                     usr/share/koha/opac/htdocs
8
debian/templates/*                          etc/koha
8
debian/templates/*                          etc/koha
9
debian/scripts/koha-functions.sh            usr/share/koha/bin
9
debian/scripts/koha-functions.sh            usr/share/koha/bin
10
debian/scripts/Koha                         usr/share/koha/bin
10
debian/scripts/koha-api-keys                usr/sbin
11
debian/scripts/koha-api-keys                usr/sbin
11
debian/scripts/koha-create                  usr/sbin
12
debian/scripts/koha-create                  usr/sbin
12
debian/scripts/koha-create-dirs             usr/sbin
13
debian/scripts/koha-create-dirs             usr/sbin
(-)a/debian/koha-core.install (+1 lines)
Lines 7-12 debian/unavailable.html usr/share/koha/intranet/htdocs Link Here
7
debian/unavailable.html                     usr/share/koha/opac/htdocs
7
debian/unavailable.html                     usr/share/koha/opac/htdocs
8
debian/templates/*                          etc/koha
8
debian/templates/*                          etc/koha
9
debian/scripts/koha-functions.sh            usr/share/koha/bin
9
debian/scripts/koha-functions.sh            usr/share/koha/bin
10
debian/scripts/Koha                         usr/share/koha/bin
10
debian/scripts/koha-create                  usr/sbin
11
debian/scripts/koha-create                  usr/sbin
11
debian/scripts/koha-create-dirs             usr/sbin
12
debian/scripts/koha-create-dirs             usr/sbin
12
debian/scripts/koha-disable                 usr/sbin
13
debian/scripts/koha-disable                 usr/sbin
(-)a/debian/scripts/Koha/Perl/Instance.pm (+197 lines)
Line 0 Link Here
1
package Koha::Perl::Instance;
2
3
# Koha::Perl::Instance - Shared Perl utilities for koha-* debian scripts
4
# Copyright 2026 LMSCloud GmbH
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
use Modern::Perl;
20
21
use Exporter qw( import );
22
our @EXPORT_OK = qw( in_koha_env get_instance_perl5lib reexec_in_koha_env );
23
24
=head1 NAME
25
26
Koha::Perl::Instance - Shared utilities for Koha debian scripts
27
28
=head1 SYNOPSIS
29
30
    use lib '/usr/share/koha/bin';
31
    use Koha::Perl::Instance qw( in_koha_env reexec_in_koha_env );
32
33
    # Re-exec with Koha environment if not already set up
34
    if ( !in_koha_env() ) {
35
        my $instance = pop @ARGV;
36
        reexec_in_koha_env({ instance => $instance, argv => \@ARGV });
37
    }
38
39
=head1 DESCRIPTION
40
41
This module provides shared utilities for Perl-based koha-* debian scripts
42
that need to run within a Koha instance's environment.
43
44
=head1 FUNCTIONS
45
46
=head2 in_koha_env
47
48
    if ( in_koha_env() ) { ... }
49
50
Returns true if the script is running within a Koha environment
51
(i.e., KOHA_CONF is set).
52
53
=cut
54
55
sub in_koha_env {
56
    return defined $ENV{KOHA_CONF} && $ENV{KOHA_CONF} ne q{};
57
}
58
59
=head2 get_instance_perl5lib
60
61
    my $perl5lib = get_instance_perl5lib($instance);
62
63
Returns the appropriate PERL5LIB path for the given Koha instance.
64
Handles both package installs and git/dev installs.
65
66
=cut
67
68
sub get_instance_perl5lib {
69
    my ($instance) = @_;
70
71
    my $koha_conf = "/etc/koha/sites/$instance/koha-conf.xml";
72
    my $perl5lib;
73
74
    # Check if this is a git install
75
    my $is_git = `bash -c '. /usr/share/koha/bin/koha-functions.sh; if is_git_install $instance; then echo 1; fi'`;
76
    chomp $is_git;
77
78
    if ($is_git) {
79
80
        # For git installs, get path from koha-conf.xml
81
        $perl5lib = `xmlstarlet sel -t -v 'yazgfs/config/intranetdir' $koha_conf 2>/dev/null`;
82
        chomp $perl5lib;
83
        $perl5lib = "$perl5lib:$perl5lib/lib" if $perl5lib;
84
    }
85
86
    if ( !$perl5lib ) {
87
88
        # Fall back to system default
89
        $perl5lib = `grep "^PERL5LIB=" /etc/default/koha-common 2>/dev/null`;
90
        chomp $perl5lib;
91
        $perl5lib =~ s/^PERL5LIB=\s*//;
92
        $perl5lib =~ s/^"//;
93
        $perl5lib =~ s/"$//;
94
    }
95
96
    return $perl5lib;
97
}
98
99
=head2 reexec_in_koha_env
100
101
    reexec_in_koha_env({
102
        instance => $instance,
103
        argv     => \@ARGV,
104
        script   => $0,
105
    });
106
107
Re-executes the current script with the proper Koha environment set up.
108
This function does not return on success.
109
110
Arguments are passed directly to exec() without shell interpolation,
111
avoiding quoting issues.
112
113
Parameters:
114
115
=over 4
116
117
=item instance
118
119
The Koha instance name (required).
120
121
=item argv
122
123
Array ref of arguments to pass to the re-executed script (default: current @ARGV).
124
125
=item script
126
127
Path to the script to execute (default: $0).
128
129
=back
130
131
Dies with an error message if:
132
133
=over 4
134
135
=item * instance is not provided
136
137
=item * instance does not exist
138
139
=item * exec fails
140
141
=back
142
143
=cut
144
145
sub reexec_in_koha_env {
146
    my ($args) = @_;
147
148
    my $instance = $args->{instance};
149
    my $argv     = $args->{argv}   // \@ARGV;
150
    my $script   = $args->{script} // $0;
151
152
    die "Usage: sudo $0 [options] instancename\n" unless $instance;
153
    die "Usage: sudo $0 [options] instancename\n" if $instance =~ /^-/;
154
    die "Error: Koha instance '$instance' does not exist.\n"
155
        unless -e "/etc/koha/sites/$instance";
156
157
    my $perl5lib  = get_instance_perl5lib($instance);
158
    my $koha_conf = "/etc/koha/sites/$instance/koha-conf.xml";
159
160
    exec(
161
        '/usr/bin/sudo', '-u',                 "$instance-koha", '-H',
162
        'env',           "PERL5LIB=$perl5lib", "KOHA_CONF=$koha_conf",
163
        '/usr/bin/perl', $script,              @{$argv}
164
    );
165
    die "Failed to exec: $!\n";
166
}
167
168
1;
169
170
__END__
171
172
=head1 AUTHOR
173
174
Paul Derscheid <paul.derscheid@lmscloud.de>
175
176
=head1 COPYRIGHT
177
178
Copyright LMSCloud GmbH
179
180
=head1 LICENSE
181
182
This file is part of Koha.
183
184
Koha is free software; you can redistribute it and/or modify it
185
under the terms of the GNU General Public License as published by
186
the Free Software Foundation; either version 3 of the License, or
187
(at your option) any later version.
188
189
Koha is distributed in the hope that it will be useful, but
190
WITHOUT ANY WARRANTY; without even the implied warranty of
191
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
192
GNU General Public License for more details.
193
194
You should have received a copy of the GNU General Public License
195
along with Koha; if not, see <http://www.gnu.org/licenses>.
196
197
=cut
(-)a/debian/scripts/koha-api-keys (-14 / +31 lines)
Lines 2-8 Link Here
2
2
3
# koha-api-keys: generate or delete API keys for a given patron and
3
# koha-api-keys: generate or delete API keys for a given patron and
4
#   Koha instance.
4
#   Koha instance.
5
# Copyright 2024 LMSCloud GmbH
5
# Copyright 2026 LMSCloud GmbH
6
#
6
#
7
# This program is free software: you can redistribute it and/or modify
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
8
# it under the terms of the GNU General Public License as published by
Lines 21-30 use Modern::Perl; Link Here
21
use Getopt::Long qw( GetOptions );
21
use Getopt::Long qw( GetOptions );
22
use Pod::Usage   qw( pod2usage );
22
use Pod::Usage   qw( pod2usage );
23
23
24
use Koha::Script;
24
use lib '/usr/share/koha/bin';
25
use Koha::ApiKeys;
25
use Koha::Perl::Instance qw( in_koha_env reexec_in_koha_env );
26
use Koha::Patrons;
26
27
use C4::Log qw( logaction );
27
# Handle --help before re-exec (doesn't need Koha environment)
28
if ( grep { $_ eq '--help' || $_ eq '-h' } @ARGV ) {
29
    pod2usage(1);
30
}
31
32
# If not in Koha environment, re-exec with proper environment
33
if ( !in_koha_env() ) {
34
    my $instance = pop @ARGV;
35
    reexec_in_koha_env( { instance => $instance, argv => \@ARGV } );
36
}
37
38
# Now in Koha environment - load modules at runtime
39
require Koha::Script;
40
Koha::Script->import();    # Set CLI interface for proper audit logging
41
require Koha::ApiKeys;
42
require Koha::Patrons;
43
require C4::Log;
44
C4::Log->import('logaction');
28
45
29
my $generate;
46
my $generate;
30
my $delete;
47
my $delete;
Lines 143-153 koha-api-keys - Generate or delete API keys for specified patrons Link Here
143
160
144
=head1 SYNOPSIS
161
=head1 SYNOPSIS
145
162
146
sudo koha-shell -c "koha-api-keys --generate --description DESCRIPTION --borrowernumber BORROWERNUMBER" instancename
163
sudo koha-api-keys --generate --description DESCRIPTION --borrowernumber BORROWERNUMBER instancename
147
164
148
sudo koha-shell -c "koha-api-keys --delete [--client-id CLIENT_ID]" instancename
165
sudo koha-api-keys --delete [--client-id CLIENT_ID] instancename
149
166
150
sudo koha-shell -c "koha-api-keys --help" instancename
167
sudo koha-api-keys --help
151
168
152
=head1 OPTIONS
169
=head1 OPTIONS
153
170
Lines 183-190 Display this help message. Link Here
183
200
184
This script lets you generate or delete API keys for specified patrons in Koha.
201
This script lets you generate or delete API keys for specified patrons in Koha.
185
202
186
As this tool requires administrative privileges and access to the Koha environment,
203
The instance name must be provided as the last argument. The script will
187
it must be run using sudo and koha-shell wrapper as shown in the synopsis.
204
automatically set up the Koha environment and run as the instance user.
188
205
189
For generating keys, you must provide both a description and a borrower number.
206
For generating keys, you must provide both a description and a borrower number.
190
For deleting keys, you can provide the client ID via --client-id or be prompted interactively.
207
For deleting keys, you can provide the client ID via --client-id or be prompted interactively.
Lines 192-208 For deleting keys, you can provide the client ID via --client-id or be prompted Link Here
192
=head1 EXAMPLES
209
=head1 EXAMPLES
193
210
194
Generate an API key:
211
Generate an API key:
195
    sudo koha-shell -c "koha-api-keys --generate --description 'Test API access' --borrowernumber 123" instancename
212
    sudo koha-api-keys --generate --description 'Test API access' --borrowernumber 123 instancename
196
213
197
Delete an API key (interactive):
214
Delete an API key (interactive):
198
    echo "CLIENT_ID" | sudo koha-shell -c "koha-api-keys --delete" instancename
215
    sudo koha-api-keys --delete instancename
199
216
200
Delete an API key (scripted):
217
Delete an API key (scripted):
201
    sudo koha-shell -c "koha-api-keys --delete --client-id a1b2c3d4-e5f6-7890-abcd-ef1234567890" instancename
218
    sudo koha-api-keys --delete --client-id a1b2c3d4-e5f6-7890-abcd-ef1234567890 instancename
202
219
203
=head1 AUTHOR
220
=head1 AUTHOR
204
221
205
Koha Development Team
222
Paul Derscheid <paul.derscheid@lmscloud.de>
206
223
207
=head1 COPYRIGHT
224
=head1 COPYRIGHT
208
225
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/action-logs.inc (+4 lines)
Lines 1-5 Link Here
1
[% BLOCK translate_log_module %]
1
[% BLOCK translate_log_module %]
2
    [% SWITCH module %]
2
    [% SWITCH module %]
3
    [% CASE 'APIKEY' %]
4
        <span>API keys</span>
3
    [% CASE 'AUTH' %]
5
    [% CASE 'AUTH' %]
4
        <span>Authentication</span>[% UNLESS Koha.Preference('AuthFailureLog') && Koha.Preference('AuthSuccessLog') %]
6
        <span>Authentication</span>[% UNLESS Koha.Preference('AuthFailureLog') && Koha.Preference('AuthSuccessLog') %]
5
            <i class="log-disabled fa-solid fa-triangle-exclamation" title="Log not enabled" data-log="AuthFailureLog|AuthSuccessLog"></i>
7
            <i class="log-disabled fa-solid fa-triangle-exclamation" title="Log not enabled" data-log="AuthFailureLog|AuthSuccessLog"></i>
Lines 61-66 Link Here
61
        <span>Return</span>
63
        <span>Return</span>
62
    [% CASE 'CREATE' %]
64
    [% CASE 'CREATE' %]
63
        <span>Create</span>
65
        <span>Create</span>
66
    [% CASE 'GENERATE' %]
67
        <span>Generate</span>
64
    [% CASE 'CANCEL' %]
68
    [% CASE 'CANCEL' %]
65
        <span>Cancel</span>
69
        <span>Cancel</span>
66
    [% CASE 'FILL' %]
70
    [% CASE 'FILL' %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/viewlog.tt (-2 / +1 lines)
Lines 123-129 Link Here
123
                            [% ELSE %]
123
                            [% ELSE %]
124
                                <label for="moduleALL" class="viewlog"><input type="checkbox" id="moduleALL" name="modules" value="" /> All</label>
124
                                <label for="moduleALL" class="viewlog"><input type="checkbox" id="moduleALL" name="modules" value="" /> All</label>
125
                            [% END %]
125
                            [% END %]
126
                            [% FOREACH modx IN [ 'AUTH' 'CATALOGUING' 'AUTHORITIES' 'MEMBERS' 'ACQUISITIONS' 'SERIAL' 'HOLDS' 'ILL' 'CIRCULATION' 'CLAIMS' 'FINES' 'SYSTEMPREFERENCE' 'CRONJOBS', 'REPORTS', 'SEARCHENGINE', 'NOTICES', 'NEWS', 'RECALLS', 'SUGGESTION', 'TRANSFERS' ] %]
126
                            [% FOREACH modx IN [ 'APIKEY', 'AUTH' 'CATALOGUING' 'AUTHORITIES' 'MEMBERS' 'ACQUISITIONS' 'SERIAL' 'HOLDS' 'ILL' 'CIRCULATION' 'CLAIMS' 'FINES' 'SYSTEMPREFERENCE' 'CRONJOBS', 'REPORTS', 'SEARCHENGINE', 'NOTICES', 'NEWS', 'RECALLS', 'SUGGESTION', 'TRANSFERS' ] %]
127
                                [% IF modules.grep(modx).size %]
127
                                [% IF modules.grep(modx).size %]
128
                                    <label for="module[% modx | html %]" class="viewlog"
128
                                    <label for="module[% modx | html %]" class="viewlog"
129
                                        ><input type="checkbox" id="module[% modx | html %]" name="modules" value="[% modx | html %]" checked="checked" /> [% PROCESS translate_log_module module=modx %]</label
129
                                        ><input type="checkbox" id="module[% modx | html %]" name="modules" value="[% modx | html %]" checked="checked" /> [% PROCESS translate_log_module module=modx %]</label
130
- 

Return to bug 37025