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

(-)a/Koha/REST/V1/Patrons/Password/Expiration.pm (+65 lines)
Line 0 Link Here
1
package Koha::REST::V1::Patrons::Password::Expiration;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Patrons;
23
use Koha::DateUtils qw(dt_from_string);
24
25
use Scalar::Util qw( blessed );
26
use Try::Tiny qw( catch try );
27
28
=head1 NAME
29
30
Koha::REST::V1::Patrons::Password::Expiration
31
32
=head1 API
33
34
=head2 Methods
35
36
=head3 set
37
38
Controller method that sets a patron's password expiration
39
40
=cut
41
42
sub set {
43
44
    my $c = shift->openapi->valid_input or return;
45
46
    my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
47
    my $body   = $c->validation->param('body');
48
49
    unless ($patron) {
50
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
51
    }
52
53
    my $password_expiration_date   = $body->{expiration_date} // "";
54
55
    return try {
56
        my $pw_expiration_dt = dt_from_string($password_expiration_date);
57
        $patron->password_expiration_date( $pw_expiration_dt)->store();
58
        return $c->render( status => 200, openapi => "" );
59
    }
60
    catch {
61
        $c->unhandled_exception($_);
62
    };
63
}
64
65
1;
(-)a/api/v1/swagger/paths.yaml (+2 lines)
Lines 97-102 Link Here
97
  $ref: paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds
97
  $ref: paths/patrons_holds.yaml#/~1patrons~1{patron_id}~1holds
98
"/patrons/{patron_id}/password":
98
"/patrons/{patron_id}/password":
99
  $ref: paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password
99
  $ref: paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password
100
"/patrons/{patron_id}/password/expiration":
101
  $ref: paths/patrons_password_expiration.yaml#/~1patrons~1{patron_id}~1password~1expiration
100
/ill_backends:
102
/ill_backends:
101
  $ref: paths/ill_backends.yaml#/~1ill_backends
103
  $ref: paths/ill_backends.yaml#/~1ill_backends
102
"/ill_backends/{ill_backend_id}":
104
"/ill_backends/{ill_backend_id}":
(-)a/api/v1/swagger/paths/patrons_password_expiration.yaml (+57 lines)
Line 0 Link Here
1
---
2
"/patrons/{patron_id}/password/expiration":
3
  post:
4
    x-mojo-to: Patrons::Password::Expiration#set
5
    operationId: setPatronPasswordExpiration
6
    tags:
7
      - patrons
8
    summary: Set password expiration for a patron
9
    parameters:
10
      - $ref: ../parameters.yaml#/patron_id_pp
11
      - name: body
12
        in: body
13
        description: A JSON object containing password expiration date
14
        schema:
15
          type: object
16
          properties:
17
            expiration_date:
18
              description: Date to expire password
19
              type: string
20
          required:
21
            - expiration_date
22
          additionalProperties: false
23
    produces:
24
      - application/json
25
    responses:
26
      "200":
27
        description: Password expiration changed
28
      "400":
29
        description: Bad request
30
        schema:
31
          $ref: ../definitions.yaml#/error
32
      "401":
33
        description: Authentication required
34
        schema:
35
          $ref: ../definitions.yaml#/error
36
      "403":
37
        description: Access forbidden
38
        schema:
39
          $ref: ../definitions.yaml#/error
40
      "404":
41
        description: Patron not found
42
        schema:
43
          $ref: ../definitions.yaml#/error
44
      "500":
45
        description: |
46
          Internal server error. Possible `error_code` attribute values:
47
48
          * `internal_server_error`
49
        schema:
50
          $ref: ../definitions.yaml#/error
51
      "503":
52
        description: Under maintenance
53
        schema:
54
          $ref: ../definitions.yaml#/error
55
    x-koha-authorization:
56
      permissions:
57
        superlibrarian: "1"
(-)a/t/db_dependent/api/v1/patrons_password_expiration.t (-1 / +96 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Patrons;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
36
subtest 'basic tests' => sub {
37
38
    plan tests => 12;
39
40
    $schema->storage->txn_begin;
41
42
    my $privileged_patron = $builder->build_object(
43
        {
44
            class => 'Koha::Patrons',
45
            value => { flags => 1 }
46
        }
47
    );
48
    my $password = 'thePassword123';
49
    $privileged_patron->set_password(
50
        { password => $password, skip_validation => 1 } );
51
    my $userid = $privileged_patron->userid;
52
53
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
54
55
    t::lib::Mocks::mock_preference( 'dateformat','us' );
56
57
    my $new_password = 'abc';
58
59
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
60
          . $patron->id
61
          . "/password/expiration" => json =>
62
          { expiration_date => '2021-01-01' } )
63
      ->status_is(200)->json_is('');
64
65
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
66
          . $patron->id
67
          . "/password/expiration" => json =>
68
          { expiration_date => '01/13/2021' } )
69
      ->status_is(200)->json_is('');
70
71
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
72
          . $patron->id
73
          . "/password/expiration" => json =>
74
          { expiration_date => '13/01/2021' } )
75
      ->status_is(500)->json_is({
76
          error => 'Something went wrong, check Koha logs for details.',
77
          error_code => "internal_server_error"
78
      });
79
80
    $privileged_patron->flags(0)->store();
81
82
    $t->post_ok( "//$userid:$password@/api/v1/patrons/"
83
          . $patron->id
84
          . "/password/expiration" => json =>
85
          { expiration_date => '2021-01-01' } )
86
      ->status_is(403)->json_is({
87
           error => "Authorization failure. Missing required permission(s).",
88
           "required_permissions" => {
89
               "superlibrarian" => "1"
90
           }
91
      });
92
93
94
95
    $schema->storage->txn_rollback;
96
};

Return to bug 29926