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

(-)a/Koha/REST/V1/Patrons.pm (+86 lines)
Lines 296-301 sub delete { Link Here
296
    };
296
    };
297
}
297
}
298
298
299
=head3 guarantors_can_see_charges
300
301
Method for setting whether guarantors can see the patron's charges.
302
303
=cut
304
305
sub guarantors_can_see_charges {
306
    my $c = shift->openapi->valid_input or return;
307
308
    return try {
309
        if ( C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor') ) {
310
            my $patron = $c->stash( 'koha.user' );
311
            my $privacy_setting = ($c->req->json->{allowed}) ? 1 : 0;
312
313
            $patron->privacy_guarantor_fines( $privacy_setting )->store;
314
315
            return $c->render(
316
                status  => 200,
317
                openapi => {}
318
            );
319
        }
320
        else {
321
            return $c->render(
322
                status  => 403,
323
                openapi => {
324
                    error =>
325
                      'The current configuration doesn\'t allow the requested action.'
326
                }
327
            );
328
        }
329
    }
330
    catch {
331
        return $c->render(
332
            status  => 500,
333
            openapi => {
334
                error =>
335
                  "Something went wrong, check Koha logs for details. $_"
336
            }
337
        );
338
    };
339
}
340
341
=head3 guarantors_can_see_checkouts
342
343
Method for setting whether guarantors can see the patron's checkouts.
344
345
=cut
346
347
sub guarantors_can_see_checkouts {
348
    my $c = shift->openapi->valid_input or return;
349
350
    return try {
351
        if ( C4::Context->preference('AllowPatronToSetCheckoutsVisibilityForGuarantor') ) {
352
            my $patron = $c->stash( 'koha.user' );
353
            my $privacy_setting = ( $c->req->json->{allowed} ) ? 1 : 0;
354
355
            $patron->privacy_guarantor_checkouts( $privacy_setting )->store;
356
357
            return $c->render(
358
                status  => 200,
359
                openapi => {}
360
            );
361
        }
362
        else {
363
            return $c->render(
364
                status  => 403,
365
                openapi => {
366
                    error =>
367
                      'The current configuration doesn\'t allow the requested action.'
368
                }
369
            );
370
        }
371
    }
372
    catch {
373
        return $c->render(
374
            status  => 500,
375
            openapi => {
376
                error =>
377
                  "Something went wrong, check Koha logs for details. $_"
378
            }
379
        );
380
    };
381
}
382
383
=head2 Internal methods
384
299
=head3 _to_api
385
=head3 _to_api
300
386
301
Helper function that maps unblessed Koha::Patron objects into REST api
387
Helper function that maps unblessed Koha::Patron objects into REST api
(-)a/t/db_dependent/api/v1/patrons.t (-2 / +77 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 5;
20
use Test::More tests => 7;
21
use Test::Mojo;
21
use Test::Mojo;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 342-347 subtest 'delete() tests' => sub { Link Here
342
    };
342
    };
343
};
343
};
344
344
345
subtest 'guarantors_can_see_charges() tests' => sub {
346
347
    plan tests => 11;
348
349
    t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
350
    t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
351
352
    $schema->storage->txn_begin;
353
354
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { privacy_guarantor_fines => 0 } });
355
    my $password = 'thePassword123';
356
    $patron->set_password({ password => $password, skip_validation => 1 });
357
    my $userid = $patron->userid;
358
    my $patron_id = $patron->borrowernumber;
359
360
    t::lib::Mocks::mock_preference( 'AllowPatronToSetFinesVisibilityForGuarantor', 0 );
361
362
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_charges" => json => { allowed => Mojo::JSON->true } )
363
      ->status_is( 403 )
364
      ->json_is( '/error', 'The current configuration doesn\'t allow the requested action.' );
365
366
    t::lib::Mocks::mock_preference( 'AllowPatronToSetFinesVisibilityForGuarantor', 1 );
367
368
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_charges" => json => { allowed => Mojo::JSON->true } )
369
      ->status_is( 200 )
370
      ->json_is( {} );
371
372
    ok( $patron->discard_changes->privacy_guarantor_fines, 'privacy_guarantor_fines has been set correctly' );
373
374
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_charges" => json => { allowed => Mojo::JSON->false } )
375
      ->status_is( 200 )
376
      ->json_is( {} );
377
378
    ok( !$patron->discard_changes->privacy_guarantor_fines, 'privacy_guarantor_fines has been set correctly' );
379
380
    $schema->storage->txn_rollback;
381
};
382
383
subtest 'guarantors_can_see_checkouts() tests' => sub {
384
385
    plan tests => 11;
386
387
    t::lib::Mocks::mock_preference( 'RESTPublicAPI', 1 );
388
    t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
389
390
    $schema->storage->txn_begin;
391
392
    my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { privacy_guarantor_checkouts => 0 } });
393
    my $password = 'thePassword123';
394
    $patron->set_password({ password => $password, skip_validation => 1 });
395
    my $userid = $patron->userid;
396
    my $patron_id = $patron->borrowernumber;
397
398
    t::lib::Mocks::mock_preference( 'AllowPatronToSetCheckoutsVisibilityForGuarantor', 0 );
399
400
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_checkouts" => json => { allowed => Mojo::JSON->true } )
401
      ->status_is( 403 )
402
      ->json_is( '/error', 'The current configuration doesn\'t allow the requested action.' );
403
404
    t::lib::Mocks::mock_preference( 'AllowPatronToSetCheckoutsVisibilityForGuarantor', 1 );
405
406
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_checkouts" => json => { allowed => Mojo::JSON->true } )
407
      ->status_is( 200 )
408
      ->json_is( {} );
409
410
    ok( $patron->discard_changes->privacy_guarantor_checkouts, 'privacy_guarantor_checkouts has been set correctly' );
411
412
    $t->put_ok( "//$userid:$password@/api/v1/public/patrons/$patron_id/guarantors/can_see_checkouts" => json => { allowed => Mojo::JSON->false } )
413
      ->status_is( 200 )
414
      ->json_is( {} );
415
416
    ok( !$patron->discard_changes->privacy_guarantor_checkouts, 'privacy_guarantor_checkouts has been set correctly' );
417
418
    $schema->storage->txn_rollback;
419
};
420
345
# Centralized tests for 401s and 403s assuming the endpoint requires
421
# Centralized tests for 401s and 403s assuming the endpoint requires
346
# borrowers flag for access
422
# borrowers flag for access
347
sub unauthorized_access_tests {
423
sub unauthorized_access_tests {
348
- 

Return to bug 23584