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

(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/apikeys.tt (+20 lines)
Lines 36-41 Link Here
36
36
37
    <h1>API keys for [% INCLUDE 'patron-title.inc' %]</h1>
37
    <h1>API keys for [% INCLUDE 'patron-title.inc' %]</h1>
38
38
39
    [% FOREACH m IN messages %]
40
        <div class="alert alert-[% m.type | html %]">
41
            [% SWITCH m.code %]
42
            [% CASE 'already_revoked' %]
43
                <span>This API key is already revoked.</span>
44
            [% CASE 'already_active' %]
45
                <span>This API key is already active.</span>
46
            [% CASE 'key_not_found' %]
47
                <span>The specified API key was not found.</span>
48
            [% CASE 'unhandled_exception' %]
49
                <span>An unexpected error occurred while processing your request.</span>
50
                [% IF m.error_string %]
51
                    <p><strong>Error details:</strong> [% m.error_string | html %]</p>
52
                [% END %]
53
            [% CASE %]
54
                <span>[% m.code | html %]</span>
55
            [% END %]
56
        </div>
57
    [% END %]
58
39
    [% IF fresh_api_key %]
59
    [% IF fresh_api_key %]
40
        [%# A fresh api key was generated, display the secret, only once %]
60
        [%# A fresh api key was generated, display the secret, only once %]
41
        <div class="alert alert-info">Make sure to copy your API secret now. You won’t be able to see it again!</div>
61
        <div class="alert alert-info">Make sure to copy your API secret now. You won’t be able to see it again!</div>
(-)a/members/apikeys.pl (-28 / +44 lines)
Lines 20-25 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI;
22
use CGI;
23
use Try::Tiny    qw( catch try );
24
use Scalar::Util qw( blessed );
23
25
24
use C4::Auth   qw( get_template_and_user );
26
use C4::Auth   qw( get_template_and_user );
25
use C4::Output qw( output_and_exit output_html_with_http_headers );
27
use C4::Output qw( output_and_exit output_html_with_http_headers );
Lines 28-33 use Koha::ApiKeys; Link Here
28
use Koha::Patrons;
30
use Koha::Patrons;
29
31
30
my $cgi = CGI->new;
32
my $cgi = CGI->new;
33
my @messages;
31
34
32
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33
    {
36
    {
Lines 73-120 if ($op) { Link Here
73
        );
76
        );
74
        $api_key->store;
77
        $api_key->store;
75
78
76
        $template->param(
79
        $template->param( fresh_api_key => $api_key, );
77
            fresh_api_key => $api_key,
80
    } elsif ( $op eq 'cud-delete' ) {
78
            api_keys      => Koha::ApiKeys->search( { patron_id => $patron_id } ),
79
        );
80
    }
81
82
    if ( $op eq 'cud-delete' ) {
83
        my $api_key_id = $cgi->param('key');
81
        my $api_key_id = $cgi->param('key');
84
        my $key        = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
82
83
        my $key = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
84
85
        if ($key) {
85
        if ($key) {
86
            $key->delete;
86
            $key->delete;
87
        } else {
88
            push @messages, { type => 'alert', code => 'key_not_found' };
87
        }
89
        }
88
        print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
90
        $template->param(
89
        exit;
91
            key_deleted => 1,
90
    }
92
        );
91
93
    } elsif ( $op eq 'cud-revoke' ) {
92
    if ( $op eq 'cud-revoke' ) {
93
        my $api_key_id = $cgi->param('key');
94
        my $api_key_id = $cgi->param('key');
94
        my $key        = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
95
96
        my $key = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
97
95
        if ($key) {
98
        if ($key) {
96
            $key->active(0);
99
            try {
97
            $key->store;
100
                $key->revoke();
101
            } catch {
102
                if ( blessed $_ && $_->isa('Koha::Exceptions::ApiKey::AlreadyRevoked') ) {
103
                    push @messages, { type => 'alert', code => 'already_revoked' };
104
                } else {
105
                    push @messages, { type => 'alert', code => 'unhandled_exception', error_string => $_ };
106
                }
107
            };
108
        } else {
109
            push @messages, { type => 'alert', code => 'key_not_found' };
98
        }
110
        }
99
        print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
111
    } elsif ( $op eq 'cud-activate' ) {
100
        exit;
101
    }
102
103
    if ( $op eq 'cud-activate' ) {
104
        my $api_key_id = $cgi->param('key');
112
        my $api_key_id = $cgi->param('key');
105
        my $key        = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
113
114
        my $key = Koha::ApiKeys->find( { patron_id => $patron_id, client_id => $api_key_id } );
106
        if ($key) {
115
        if ($key) {
107
            $key->active(1);
116
            try {
108
            $key->store;
117
                $key->activate();
118
            } catch {
119
                if ( blessed $_ && $_->isa('Koha::Exceptions::ApiKey::AlreadyActive') ) {
120
                    push @messages, { type => 'alert', code => 'already_active' };
121
                } else {
122
                    push @messages, { type => 'alert', code => 'unhandled_exception', error_string => $_ };
123
                }
124
            };
125
        } else {
126
            push @messages, { type => 'alert', code => 'key_not_found' };
109
        }
127
        }
110
        print $cgi->redirect( '/cgi-bin/koha/members/apikeys.pl?patron_id=' . $patron_id );
111
        exit;
112
    }
128
    }
113
}
129
}
114
130
115
$template->param(
131
$template->param(
116
    api_keys => Koha::ApiKeys->search( { patron_id => $patron_id } ),
132
    api_keys => Koha::ApiKeys->search( { patron_id => $patron_id } ),
117
    patron   => $patron
133
    patron   => $patron,
134
    messages => \@messages
118
);
135
);
119
136
120
output_html_with_http_headers $cgi, $cookie, $template->output;
137
output_html_with_http_headers $cgi, $cookie, $template->output;
121
- 

Return to bug 20638