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

(-)a/Koha/ApiKey.pm (-23 / +9 lines)
Lines 70-88 sub store { Link Here
70
        );
70
        );
71
    }
71
    }
72
72
73
    my $result = $self->SUPER::store();
74
75
    # Log the action unless explicitly skipped
73
    # Log the action unless explicitly skipped
76
    if ( !$params->{skip_log} && C4::Context->preference('ApiKeyLog') ) {
74
    if ( !$params->{skip_log} && C4::Context->preference('ApiKeyLog') ) {
77
        if ($is_new) {
75
        if ($is_new) {
78
            logaction(
76
            logaction( 'APIKEYS', 'CREATE', $self->patron_id, $self, undef, $self->get_from_storage() );
79
                'APIKEYS', 'CREATE', $self->patron_id,
80
                sprintf( "Client ID: %s, Description: %s", $self->client_id, $self->description )
81
            );
82
        }
77
        }
83
    }
78
    }
84
79
85
    return $result;
80
    return $self->SUPER::store();
86
}
81
}
87
82
88
=head3 validate_secret
83
=head3 validate_secret
Lines 139-148 sub delete { Link Here
139
    my $result = $self->SUPER::delete();
134
    my $result = $self->SUPER::delete();
140
135
141
    if ( C4::Context->preference('ApiKeyLog') ) {
136
    if ( C4::Context->preference('ApiKeyLog') ) {
142
        logaction(
137
        logaction( 'APIKEYS', 'DELETE', $patron_id, undef, undef, $self );
143
            'APIKEYS', 'DELETE', $patron_id,
144
            sprintf( "Client ID: %s, Description: %s", $client_id, $description )
145
        );
146
    }
138
    }
147
139
148
    return $result;
140
    return $result;
Lines 163-178 sub revoke { Link Here
163
    Koha::Exceptions::ApiKey::AlreadyRevoked->throw
155
    Koha::Exceptions::ApiKey::AlreadyRevoked->throw
164
        if !$self->active;
156
        if !$self->active;
165
157
166
    $self->active(0)->store( { skip_log => 1 } );
158
    $self->active(0);
167
159
168
    if ( C4::Context->preference('ApiKeyLog') ) {
160
    if ( C4::Context->preference('ApiKeyLog') ) {
169
        logaction(
161
        logaction( 'APIKEYS', 'REVOKE', $self->patron_id, $self, undef, $self->get_from_storage() );
170
            'APIKEYS', 'REVOKE', $self->patron_id,
171
            sprintf( "Client ID: %s, Description: %s", $self->client_id, $self->description )
172
        );
173
    }
162
    }
174
163
175
    return $self;
164
    return $self->store( { skip_log => 1 } );
176
}
165
}
177
166
178
=head3 activate
167
=head3 activate
Lines 190-205 sub activate { Link Here
190
    Koha::Exceptions::ApiKey::AlreadyActive->throw
179
    Koha::Exceptions::ApiKey::AlreadyActive->throw
191
        if $self->active;
180
        if $self->active;
192
181
193
    $self->active(1)->store( { skip_log => 1 } );
182
    $self->active(1);
194
183
195
    if ( C4::Context->preference('ApiKeyLog') ) {
184
    if ( C4::Context->preference('ApiKeyLog') ) {
196
        logaction(
185
        logaction( 'APIKEYS', 'ACTIVATE', $self->patron_id, $self, undef, $self->get_from_storage() );
197
            'APIKEYS', 'ACTIVATE', $self->patron_id,
198
            sprintf( "Client ID: %s, Description: %s", $self->client_id, $self->description )
199
        );
200
    }
186
    }
201
187
202
    return $self;
188
    return $self->store( { skip_log => 1 } );
203
}
189
}
204
190
205
=head2 Internal methods
191
=head2 Internal methods
(-)a/t/db_dependent/Koha/ApiKey.t (-8 / +6 lines)
Lines 154-160 subtest 'validate_secret() tests' => sub { Link Here
154
    )->first;
154
    )->first;
155
    ok( $log_entry, 'CREATE log entry found' );
155
    ok( $log_entry, 'CREATE log entry found' );
156
    is( $log_entry->object, $test_patron->id, 'Log object is patron_id' );
156
    is( $log_entry->object, $test_patron->id, 'Log object is patron_id' );
157
    like( $log_entry->info, qr/Client ID: .+, Description: logged/, 'Log info contains client_id and description' );
157
    like( $log_entry->info, qr/logged/, 'Log info contains description' );
158
158
159
    # Test no CREATE logging when ApiKeyLog is disabled
159
    # Test no CREATE logging when ApiKeyLog is disabled
160
    t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 );
160
    t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 );
Lines 178-187 subtest 'validate_secret() tests' => sub { Link Here
178
    )->first;
178
    )->first;
179
    ok( $delete_log, 'DELETE log entry found' );
179
    ok( $delete_log, 'DELETE log entry found' );
180
    is( $delete_log->object, $test_patron->id, 'DELETE log object is patron_id' );
180
    is( $delete_log->object, $test_patron->id, 'DELETE log object is patron_id' );
181
    like(
181
182
        $delete_log->info, qr/Client ID: $client_id_to_delete, Description: logged/,
182
    # DELETE logs pass undef for info field, object is in diff field
183
        'DELETE log info contains client_id and description'
183
    is( $delete_log->info, undef, 'DELETE log info is undef as expected' );
184
    );
185
184
186
    # Test no DELETE logging when ApiKeyLog is disabled
185
    # Test no DELETE logging when ApiKeyLog is disabled
187
    t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 );
186
    t::lib::Mocks::mock_preference( 'ApiKeyLog', 0 );
Lines 247-253 subtest 'revoke() tests' => sub { Link Here
247
    ok( $revoke_log, 'REVOKE log entry found' );
246
    ok( $revoke_log, 'REVOKE log entry found' );
248
    is( $revoke_log->object, $patron->id, 'REVOKE log object is patron_id' );
247
    is( $revoke_log->object, $patron->id, 'REVOKE log object is patron_id' );
249
    like(
248
    like(
250
        $revoke_log->info, qr/Client ID: $client_id_to_revoke, Description: test2/,
249
        $revoke_log->info, qr/$client_id_to_revoke.*test2/s,
251
        'REVOKE log info contains client_id and description'
250
        'REVOKE log info contains client_id and description'
252
    );
251
    );
253
252
Lines 301-307 subtest 'activate() tests' => sub { Link Here
301
    ok( $activate_log, 'ACTIVATE log entry found' );
300
    ok( $activate_log, 'ACTIVATE log entry found' );
302
    is( $activate_log->object, $patron->id, 'ACTIVATE log object is patron_id' );
301
    is( $activate_log->object, $patron->id, 'ACTIVATE log object is patron_id' );
303
    like(
302
    like(
304
        $activate_log->info, qr/Client ID: $client_id_to_activate, Description: test2/,
303
        $activate_log->info, qr/$client_id_to_activate.*test2/s,
305
        'ACTIVATE log info contains client_id and description'
304
        'ACTIVATE log info contains client_id and description'
306
    );
305
    );
307
306
308
- 

Return to bug 20638