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

(-)a/Koha/RecordSource.pm (+93 lines)
Line 0 Link Here
1
package Koha::RecordSource;
2
3
# This file is part of Koha.
4
#
5
# Copyright 2020 Koha Development Team
6
#
7
# Koha is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as
9
# published by the Free Software Foundation; either version 3
10
# of the License, or (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General
18
# Public License along with Koha; if not, see
19
# <http://www.gnu.org/licenses>
20
21
use Modern::Perl;
22
23
use base qw(Koha::Object);
24
25
use Koha::Patrons;
26
use Koha::Token;
27
28
=head1 NAME
29
30
Koha::RecordSource - Koha RecordSource Object class
31
32
=head1 API
33
34
=head2 Class methods
35
36
=head3 store
37
38
    my $record_source = Koha::RecordSource->new( { name => $name, patron_id => $patron->id } )->store;
39
40
Overloaded I<store> method that takes care of generating the assigned token.
41
42
=cut
43
44
sub store {
45
    my ($self) = @_;
46
47
    my $needs_new_token = 1;
48
49
    if ( $self->in_storage ) {    # update
50
        if ( Koha::Token->new->check_jwt( { id => $self->id, token => $self->api_token } ) ) {
51
            $needs_new_token = 0;
52
        }
53
    } else {                      # fresh
54
                                  # store so we get a fresh id
55
        $self->SUPER::store;
56
    }
57
58
    if ($needs_new_token) {       # new
59
        my $token = Koha::Token->new->generate_jwt( { id => $self->id } );
60
        $self->api_token($token);
61
    }
62
63
    return $self->SUPER::store;
64
}
65
66
=head3 patron
67
68
    my $patron = $record_source->patron
69
70
    Return the patron for this record source
71
72
=cut
73
74
sub patron {
75
    my ($self) = @_;
76
77
    my $patron_rs = $self->_result->patron;
78
    return unless $patron_rs;
79
80
    return Koha::Patron->_new_from_dbic($patron_rs);
81
}
82
83
=head2 Internal methods
84
85
=head3 _type
86
87
=cut
88
89
sub _type {
90
    return 'RecordSource';
91
}
92
93
1;
(-)a/Koha/RecordSources.pm (+51 lines)
Line 0 Link Here
1
package Koha::RecordSources;
2
3
# This file is part of Koha.
4
#
5
# Copyright 2020 Koha Development Team
6
#
7
# Koha is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as
9
# published by the Free Software Foundation; either version 3
10
# of the License, or (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General
18
# Public License along with Koha; if not, see
19
# <http://www.gnu.org/licenses>
20
21
use Modern::Perl;
22
23
use base qw(Koha::Objects);
24
25
use Koha::RecordSource;
26
27
=head1 NAME
28
29
Koha::RecordSources - Koha RecordSources Object class
30
31
=head1 API
32
33
=head2 Internal methods
34
35
=head3 _type
36
37
=cut
38
39
sub _type {
40
    return 'RecordSource';
41
}
42
43
=head3 object_class
44
45
=cut
46
47
sub object_class {
48
    return 'Koha::RecordSource';
49
}
50
51
1;
(-)a/Koha/Schema/Result/RecordSource.pm (+115 lines)
Line 0 Link Here
1
use utf8;
2
package Koha::Schema::Result::RecordSource;
3
4
# Created by DBIx::Class::Schema::Loader
5
# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
7
=head1 NAME
8
9
Koha::Schema::Result::RecordSource
10
11
=cut
12
13
use strict;
14
use warnings;
15
16
use base 'DBIx::Class::Core';
17
18
=head1 TABLE: C<record_sources>
19
20
=cut
21
22
__PACKAGE__->table("record_sources");
23
24
=head1 ACCESSORS
25
26
=head2 record_source_id
27
28
  data_type: 'integer'
29
  is_auto_increment: 1
30
  is_nullable: 0
31
32
Primary key for the `record_sources` table
33
34
=head2 name
35
36
  data_type: 'text'
37
  is_nullable: 0
38
39
User defined name for the record source
40
41
=head2 patron_id
42
43
  data_type: 'integer'
44
  is_foreign_key: 1
45
  is_nullable: 1
46
47
Internal patron identifier
48
49
=head2 api_token
50
51
  data_type: 'varchar'
52
  is_nullable: 1
53
  size: 191
54
55
Generated token for identifying the record source on API interactions
56
57
=cut
58
59
__PACKAGE__->add_columns(
60
  "record_source_id",
61
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
62
  "name",
63
  { data_type => "text", is_nullable => 0 },
64
  "patron_id",
65
  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
66
  "api_token",
67
  { data_type => "varchar", is_nullable => 1, size => 191 },
68
);
69
70
=head1 PRIMARY KEY
71
72
=over 4
73
74
=item * L</record_source_id>
75
76
=back
77
78
=cut
79
80
__PACKAGE__->set_primary_key("record_source_id");
81
82
=head1 RELATIONS
83
84
=head2 patron
85
86
Type: belongs_to
87
88
Related object: L<Koha::Schema::Result::Borrower>
89
90
=cut
91
92
__PACKAGE__->belongs_to(
93
  "patron",
94
  "Koha::Schema::Result::Borrower",
95
  { borrowernumber => "patron_id" },
96
  {
97
    is_deferrable => 1,
98
    join_type     => "LEFT",
99
    on_delete     => "SET NULL",
100
    on_update     => "CASCADE",
101
  },
102
);
103
104
105
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2023-09-21 12:34:54
106
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:YtRIZEX2Hg088ls6sw2U4Q
107
108
sub koha_objects_class {
109
    'Koha::RecordSources';
110
}
111
sub koha_object_class {
112
    'Koha::RecordSource';
113
}
114
115
1;
(-)a/t/db_dependent/Koha/RecordSource.t (-1 / +82 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 Theke Solutions
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 2;
23
24
use Koha::RecordSources;
25
use Koha::Token;
26
27
use t::lib::TestBuilder;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
subtest 'patron() tests' => sub {
33
34
    plan tests => 3;
35
36
    $schema->storage->txn_begin;
37
38
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
39
40
    my $source = $builder->build_object(
41
        {
42
            class => 'Koha::RecordSources',
43
            value => { patron_id => $patron->id }
44
        }
45
    );
46
47
    my $fetched_patron = $source->patron;
48
49
    is( ref($fetched_patron), 'Koha::Patron' );
50
    is( $fetched_patron->id,  $patron->id );
51
52
    $patron->delete;
53
    $source->discard_changes;
54
55
    is( $source->patron, undef, 'Method returns undef if no linked patron' );
56
57
    $schema->storage->txn_rollback;
58
};
59
60
subtest 'store() tests' => sub {
61
62
    plan tests => 5;
63
64
    $schema->storage->txn_begin;
65
66
    my $source = Koha::RecordSource->new( { name => 'Sample' } )->store;
67
    isnt( $source->api_token, undef, "'api_token' set on creation" );
68
    ok( Koha::Token->new->check_jwt( { id => $source->id, token => $source->api_token } ), 'Token is valid' );
69
70
    my $api_token = $source->api_token;
71
    $source->name('Sample2')->store->discard_changes;
72
73
    is( $api_token, $source->api_token, 'Token unchanged on name change' );
74
75
    # change the id, token validation is based on id so should be regenerated
76
    $source->set( { record_source_id => $source->record_source_id + 1 } )->store->discard_changes;
77
78
    isnt( $api_token, $source->api_token, 'Token changed on id change' );
79
    ok( Koha::Token->new->check_jwt( { id => $source->id, token => $source->api_token } ), 'New token is valid' );
80
81
    $schema->storage->txn_rollback;
82
};

Return to bug 32607