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

(-)a/Koha/AuthorisedValue.pm (+18 lines)
Lines 46-51 sub opac_description { Link Here
46
    return $self->lib_opac() || $self->lib();
46
    return $self->lib_opac() || $self->lib();
47
}
47
}
48
48
49
=head3 to_api_mapping
50
51
This method returns the mapping for representing a Koha::AuthorisedValue object
52
on the API.
53
54
=cut
55
56
sub to_api_mapping {
57
    return {
58
        id               => 'authorised_value_id',
59
        category         => 'category_name',
60
        authorised_value => 'value',
61
        lib              => 'description',
62
        lib_opac         => 'opac_description',
63
        imageurl         => 'image_url',
64
    };
65
}
66
49
=head2 Internal methods
67
=head2 Internal methods
50
68
51
=head3 _type
69
=head3 _type
(-)a/Koha/REST/V1/AuthorisedValues.pm (+59 lines)
Line 0 Link Here
1
package Koha::REST::V1::AuthorisedValues;
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::AuthorisedValues;
23
use Koha::AuthorisedValueCategories;
24
25
use Try::Tiny;
26
27
=head1 API
28
29
=head2 Methods
30
31
=head3 list
32
33
=cut
34
35
sub list_av_from_category {
36
    my $c = shift->openapi->valid_input or return;
37
38
    my $category_name = $c->validation->param('authorised_value_category_name');
39
40
    my $category = Koha::AuthorisedValueCategories->find($category_name);
41
42
    unless ($category) {
43
        return $c->render(
44
            status  => 404,
45
            openapi => { error => "Category not found" }
46
        );
47
    }
48
49
    return try {
50
        my $av_set = Koha::AuthorisedValues->search( { category => $category_name } )->search_with_library_limits;
51
        my $avs    = $c->objects->search($av_set);
52
        return $c->render( status => 200, openapi => $avs );
53
    } catch {
54
        $c->unhandled_exception($_);
55
    };
56
57
}
58
59
1;
(-)a/api/v1/swagger/definitions/authorised_value.yaml (+32 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  authorised_value_id:
5
    type: integer
6
    description: internally assigned authorised value identifier
7
    readOnly: true
8
  category_name:
9
    description: the category of this authorised value
10
    type: string
11
  value:
12
    description: The code for this authorised value
13
    type: string
14
  description:
15
    description: The staff interface description for this authorised value
16
    type: string
17
  opac_description:
18
    description: The public interface description of this authorised value, if set"
19
    type:
20
      - string
21
      - "null"
22
  image_url:
23
    description: The url of the image associated with this authorised value, if any
24
    type:
25
      - string
26
      - "null"
27
28
additionalProperties: false
29
required:
30
  - category_name
31
  - value
32
  - description
(-)a/api/v1/swagger/paths/authorised_values.yaml (+85 lines)
Line 0 Link Here
1
---
2
"/authorised_value_categories/{authorised_value_category_name}/values":
3
  get:
4
    x-mojo-to: AuthorisedValues#list_av_from_category
5
    operationId: listAuthorisedValues
6
    tags:
7
      - authorised_values
8
    summary: List authorised values for a given category
9
    produces:
10
      - application/json
11
    parameters:
12
      - description: category name
13
        in: path
14
        name: authorised_value_category_name
15
        required: true
16
        type: string
17
      - description: Case insensitive search on authorised value id
18
        in: query
19
        name: authorised_value_id
20
        required: false
21
        type: integer
22
      - description: Case insensitive search on authorised value category name
23
        in: query
24
        name: category_name
25
        required: false
26
        type: string
27
      - description: Case insensitive search on value
28
        in: query
29
        name: value
30
        required: false
31
        type: string
32
      - description: Case insensitive search on description
33
        in: query
34
        name: description
35
        required: false
36
        type: string
37
      - description: Case insensitive search on opac description
38
        in: query
39
        name: opac_description
40
        required: false
41
        type: string
42
      - description: Case insensitive search on image url
43
        in: query
44
        name: image_url
45
        required: false
46
        type: string
47
      - $ref: "../swagger.yaml#/parameters/match"
48
      - $ref: "../swagger.yaml#/parameters/order_by"
49
      - $ref: "../swagger.yaml#/parameters/page"
50
      - $ref: "../swagger.yaml#/parameters/per_page"
51
      - $ref: "../swagger.yaml#/parameters/q_param"
52
      - $ref: "../swagger.yaml#/parameters/q_body"
53
      - $ref: "../swagger.yaml#/parameters/q_header"
54
    responses:
55
      200:
56
        description: A list of authorised values
57
        schema:
58
          items:
59
            $ref: "../swagger.yaml#/definitions/authorised_value"
60
          type: array
61
      400:
62
        description: Bad request
63
        schema:
64
          $ref: "../swagger.yaml#/definitions/error"
65
      403:
66
        description: Access forbidden
67
        schema:
68
          $ref: "../swagger.yaml#/definitions/error"
69
      404:
70
        description: Ressource not found
71
        schema:
72
          $ref: "../swagger.yaml#/definitions/error"
73
      500:
74
        description: |-
75
          Internal server error. Possible `error_code` attribute values:
76
          * `internal_server_error`
77
        schema:
78
          $ref: "../swagger.yaml#/definitions/error"
79
      503:
80
        description: Under maintenance
81
        schema:
82
          $ref: "../swagger.yaml#/definitions/error"
83
    x-koha-authorization:
84
      permissions:
85
        catalogue: 1
(-)a/api/v1/swagger/swagger.yaml (+10 lines)
Lines 8-13 definitions: Link Here
8
    $ref: ./definitions/advancededitormacro.yaml
8
    $ref: ./definitions/advancededitormacro.yaml
9
  allows_renewal:
9
  allows_renewal:
10
    $ref: ./definitions/allows_renewal.yaml
10
    $ref: ./definitions/allows_renewal.yaml
11
  authorised_value:
12
    $ref: ./definitions/authorised_value.yaml
11
  identity_provider:
13
  identity_provider:
12
    "$ref": ./definitions/identity_provider.yaml
14
    "$ref": ./definitions/identity_provider.yaml
13
  identity_provider_domain:
15
  identity_provider_domain:
Lines 139-144 paths: Link Here
139
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
141
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains
140
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
142
  "/auth/identity_providers/{identity_provider_id}/domains/{identity_provider_domain_id}":
141
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
143
    $ref: paths/auth.yaml#/~1auth~1identity_providers~1{identity_provider_id}~1domains~1{identity_provider_domain_id}
144
  "/authorised_value_categories/{authorised_value_category_name}/values":
145
    $ref: "./paths/authorised_values.yaml#/~1authorised_value_categories~1{authorised_value_category_name}~1values"
142
  "/biblios/{biblio_id}":
146
  "/biblios/{biblio_id}":
143
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
147
    $ref: "./paths/biblios.yaml#/~1biblios~1{biblio_id}"
144
  "/biblios/{biblio_id}/checkouts":
148
  "/biblios/{biblio_id}/checkouts":
Lines 338-343 parameters: Link Here
338
    name: agreement_period_id
342
    name: agreement_period_id
339
    required: true
343
    required: true
340
    type: integer
344
    type: integer
345
  authorised_value_id_pp:
346
    description: Authorised value internal identifier
347
    in: path
348
    name: authorised_value_id
349
    required: true
350
    type: integer
341
  identity_provider_id_pp:
351
  identity_provider_id_pp:
342
    description: Authentication provider internal identifier
352
    description: Authentication provider internal identifier
343
    in: path
353
    in: path
(-)a/t/db_dependent/api/v1/authorised_values.t (-1 / +87 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env 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
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::AuthorisedValues;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list_av_from_category() tests' => sub {
36
37
    plan tests => 11;
38
39
    $schema->storage->txn_begin;
40
41
    my $librarian = $builder->build_object(
42
        {
43
            class => 'Koha::Patrons',
44
            value => { flags => 2 ** 2 } # catalogue flag = 2
45
        }
46
    );
47
    my $password = 'thePassword123';
48
    $librarian->set_password( { password => $password, skip_validation => 1 } );
49
    my $userid = $librarian->userid;
50
51
    my $patron = $builder->build_object(
52
        {
53
            class => 'Koha::Patrons',
54
            value => { flags => 0 }
55
        }
56
    );
57
58
    $patron->set_password( { password => $password, skip_validation => 1 } );
59
    my $unauth_userid = $patron->userid;
60
61
    ## Authorized user tests
62
    # No category, 404 expected
63
    $t->get_ok("//$userid:$password@/api/v1/authorised_value_categories/NON_EXISTS/values")
64
      ->status_is(404)
65
      ->json_is( '/error' => 'Category not found' );
66
67
    my $av_cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' })->category_name;
68
69
    # No AVs, so empty array should be returned
70
    $t->get_ok("//$userid:$password@/api/v1/authorised_value_categories/$av_cat/values")
71
      ->status_is(200)
72
      ->json_is( [] );
73
74
    my $av = $builder->build_object(
75
        { class => 'Koha::AuthorisedValues', value => { category => $av_cat } } );
76
77
    # One av created, should get returned
78
    $t->get_ok("//$userid:$password@/api/v1/authorised_value_categories/$av_cat/values")
79
      ->status_is(200)
80
      ->json_is( [$av->to_api] );
81
82
    # Unauthorized access
83
    $t->get_ok("//$unauth_userid:$password@/api/v1/authorised_value_categories/$av_cat/values")
84
      ->status_is(403);
85
86
    $schema->storage->txn_rollback;
87
};

Return to bug 32981