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 86-89 sub renew { Link Here
86
    return $c->$cb($checkout->unblessed, 200);
87
    return $c->$cb($checkout->unblessed, 200);
87
}
88
}
88
89
90
sub renewability {
91
    my ($c, $args, $cb) = @_;
92
93
    my $user = $c->stash('koha.user');
94
95
    my $checkout_id = $args->{checkout_id};
96
    my $checkout = Koha::Issues->find($checkout_id);
97
98
    if (!$checkout) {
99
        return $c->$cb({
100
            error => "Checkout doesn't exist"
101
        }, 404);
102
    }
103
104
    my $borrowernumber = $checkout->borrowernumber;
105
    my $itemnumber = $checkout->itemnumber;
106
107
    my $OpacRenewalAllowed;
108
    if ($user->borrowernumber == $borrowernumber) {
109
        $OpacRenewalAllowed = C4::Context->preference('OpacRenewalAllowed');
110
    }
111
112
    unless ($user && ($OpacRenewalAllowed
113
            || haspermission($user->userid, { circulate => "circulate_remaining_permissions" }))) {
114
        return $c->$cb({error => "You don't have the required permission"}, 403);
115
    }
116
117
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
118
        $borrowernumber, $itemnumber);
119
120
    return $c->$cb({}, 200) if $can_renew;
121
    return $c->$cb({error => "Renewal not authorized ($error)"}, 403);
122
}
123
89
1;
124
1;
(-)a/api/v1/swagger/paths.json (+3 lines)
Lines 11-16 Link Here
11
  "/checkouts/{checkout_id}": {
11
  "/checkouts/{checkout_id}": {
12
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}"
12
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}"
13
  },
13
  },
14
  "/checkouts/{checkout_id}/renewability": {
15
    "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1renewability"
16
  },
14
  "/patrons": {
17
  "/patrons": {
15
    "$ref": "paths/patrons.json#/~1patrons"
18
    "$ref": "paths/patrons.json#/~1patrons"
16
  },
19
  },
(-)a/api/v1/swagger/paths/checkouts.json (+30 lines)
Lines 93-97 Link Here
93
        }
93
        }
94
      }
94
      }
95
    }
95
    }
96
  },
97
  "/checkouts/{checkout_id}/renewability": {
98
    "get": {
99
      "operationId": "renewabilityCheckout",
100
      "tags": ["patrons", "checkouts"],
101
      "parameters": [{
102
          "name": "checkout_id",
103
          "in": "path",
104
          "description": "Internal checkout identifier",
105
          "required": true,
106
          "type": "integer"
107
      }],
108
      "produces": ["application/json"],
109
      "responses": {
110
        "200": {
111
          "description": "Checkout is renewable",
112
          "schema": {
113
            "type": "object"
114
          }
115
        },
116
        "403": {
117
          "description": "Checkout is not renewable",
118
          "schema": { "$ref": "../definitions.json#/error" }
119
        },
120
        "404": {
121
          "description": "Checkout not found",
122
          "schema": { "$ref": "../definitions.json#/error" }
123
        }
124
      }
125
    }
96
  }
126
  }
97
}
127
}
(-)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 189-195 $t->request_ok($tx) Link Here
189
  ->status_is(403)
189
  ->status_is(403)
190
  ->json_is({ error => "Opac Renewal not allowed"	});
190
  ->json_is({ error => "Opac Renewal not allowed"	});
191
191
192
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
193
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
194
$t->request_ok($tx)
195
  ->status_is(403)
196
  ->json_is({ error => "You don't have the required permission" });
197
192
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
198
t::lib::Mocks::mock_preference( "OpacRenewalAllowed", 1 );
199
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
200
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
201
$t->request_ok($tx)
202
  ->status_is(200)
203
  ->json_is({});
204
193
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
205
$tx = $t->ua->build_tx(PUT => "/api/v1/checkouts/" . $issue2->issue_id);
194
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
206
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
195
$t->request_ok($tx)
207
$t->request_ok($tx)
Lines 202-207 $t->request_ok($tx) Link Here
202
  ->status_is(403)
214
  ->status_is(403)
203
  ->json_is({ error => 'Renewal not authorized (too_many)' });
215
  ->json_is({ error => 'Renewal not authorized (too_many)' });
204
216
217
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/" . $issue2->issue_id . "/renewability");
218
$tx->req->cookies({name => 'CGISESSID', value => $patron_session->id});
219
$t->request_ok($tx)
220
  ->status_is(403)
221
  ->json_is({ error => 'Renewal not authorized (too_many)' });
222
205
sub create_biblio {
223
sub create_biblio {
206
    my ($title) = @_;
224
    my ($title) = @_;
207
225
208
- 

Return to bug 17003