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

(-)a/Koha/REST/V1/Checkout.pm (+35 lines)
Lines 18-23 package Koha::REST::V1::Checkout; Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
use Mojo::JSON;
21
22
22
use C4::Auth qw( haspermission );
23
use C4::Auth qw( haspermission );
23
use C4::Context;
24
use C4::Context;
Lines 105-108 sub renew { Link Here
105
    return $c->$cb($checkout->unblessed, 200);
106
    return $c->$cb($checkout->unblessed, 200);
106
}
107
}
107
108
109
sub renewability {
110
    my ($c, $args, $cb) = @_;
111
112
    my $user = $c->stash('koha.user');
113
114
    my $checkout_id = $args->{checkout_id};
115
    my $checkout = Koha::Issues->find($checkout_id);
116
117
    if (!$checkout) {
118
        return $c->$cb({
119
            error => "Checkout doesn't exist"
120
        }, 404);
121
    }
122
123
    my $borrowernumber = $checkout->borrowernumber;
124
    my $itemnumber = $checkout->itemnumber;
125
126
    my $OpacRenewalAllowed;
127
    if ($user->borrowernumber == $borrowernumber) {
128
        $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
129
    }
130
131
    unless ($user && ($OpacRenewalAllowed
132
            || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
133
        return $c->$cb({error => "You don't have the required permission"}, 403);
134
    }
135
136
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
137
        $borrowernumber, $itemnumber);
138
139
    return $c->$cb({}, 200) if $can_renew;
140
    return $c->$cb({error => "Renewal not authorized ($error)"}, 403);
141
}
142
108
1;
143
1;
(-)a/api/v1/swagger.json (+32 lines)
Lines 426-431 Link Here
426
          }
426
          }
427
        }
427
        }
428
      }
428
      }
429
    },
430
    "/checkouts/{checkout_id}/renewability": {
431
      "get": {
432
        "operationId": "renewabilityCheckout",
433
        "tags": ["patrons", "checkouts"],
434
        "parameters": [
435
          {
436
            "name": "checkout_id",
437
            "in": "path",
438
            "description": "Internal checkout identifier",
439
            "required": true,
440
            "type": "integer"
441
          }
442
        ],
443
        "produces": ["application/json"],
444
        "responses": {
445
          "200": {
446
            "description": "Checkout is renewable",
447
            "schema": {
448
                "type": "object"
449
            }
450
          },
451
          "403": {
452
            "description": "Checkout is not renewable",
453
            "schema": { "$ref": "#/definitions/error" }
454
          },
455
          "404": {
456
            "description": "Checkout not found",
457
            "schema": { "$ref": "#/definitions/error" }
458
          }
459
        }
460
      }
429
    }
461
    }
430
  },
462
  },
431
  "definitions": {
463
  "definitions": {
(-)a/t/db_dependent/api/v1/checkouts.t (-2 / +19 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 57;
20
use Test::More tests => 66;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Mojo;
22
use Test::Mojo;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 183-189 $t->request_ok($tx) Link Here
183
  ->status_is(403)
183
  ->status_is(403)
184
  ->json_is({ error => "You don't have the required permission" });
184
  ->json_is({ error => "You don't have the required permission" });
185
185
186
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
187
$tx->req->cookies({name => 'CGISESSID', value => $borrower_session->id});
188
$t->request_ok($tx)
189
  ->status_is(403)
190
  ->json_is({ error => "You don't have the required permission" });
191
  
186
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
192
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
193
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
194
$tx->req->cookies({name => 'CGISESSID', value => $borrower_session->id});
195
$t->request_ok($tx)
196
  ->status_is(200)
197
  ->json_is({});
198
187
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
199
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
188
$tx->req->cookies({name => 'CGISESSID', value => $borrower_session->id});
200
$tx->req->cookies({name => 'CGISESSID', value => $borrower_session->id});
189
$t->request_ok($tx)
201
$t->request_ok($tx)
Lines 196-201 $t->request_ok($tx) Link Here
196
  ->status_is(403)
208
  ->status_is(403)
197
  ->json_is({ error => 'Renewal not authorized (too_many)' });
209
  ->json_is({ error => 'Renewal not authorized (too_many)' });
198
210
211
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
212
$tx->req->cookies({name => 'CGISESSID', value => $borrower_session->id});
213
$t->request_ok($tx)
214
  ->status_is(403)
215
  ->json_is({ error => 'Renewal not authorized (too_many)' });
216
199
sub create_biblio {
217
sub create_biblio {
200
    my ($title) = @_;
218
    my ($title) = @_;
201
219
202
- 

Return to bug 17003