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

(-)a/Koha/Auth/Identity/Provider.pm (-34 / +5 lines)
Lines 19-32 package Koha::Auth::Identity::Provider; Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use base qw(Koha::Object);
22
use base qw(Koha::Object Koha::Object::JSONFields);
23
24
use JSON qw( decode_json encode_json );
25
use Try::Tiny;
26
23
27
use Koha::Auth::Identity::Provider::Domains;
24
use Koha::Auth::Identity::Provider::Domains;
28
use Koha::Exceptions;
25
use Koha::Exceptions;
29
use Koha::Exceptions::Object;
30
26
31
=head1 NAME
27
=head1 NAME
32
28
Lines 61-72 Returns a I<hashref> containing the configuration parameters for the provider. Link Here
61
sub get_config {
57
sub get_config {
62
    my ($self) = @_;
58
    my ($self) = @_;
63
59
64
    return try {
60
    return $self->decode_json_field({ field => 'config' });
65
        return decode_json( $self->config );
66
    }
67
    catch {
68
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
69
    };
70
}
61
}
71
62
72
=head3 set_config
63
=head3 set_config
Lines 105-118 sub set_config { Link Here
105
        }
96
        }
106
    }
97
    }
107
98
108
    try {
99
    return $self->set_encoded_json_field({ data => $config, field => 'config' });
109
        my $encoded_config = encode_json($config);
110
        $self->config($encoded_config);
111
    } catch {
112
        Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
113
    };
114
115
    return $self;
116
}
100
}
117
101
118
=head3 get_mapping
102
=head3 get_mapping
Lines 126-137 Returns a I<hashref> containing the attribute mapping for the provider. Link Here
126
sub get_mapping {
110
sub get_mapping {
127
    my ($self) = @_;
111
    my ($self) = @_;
128
112
129
    return try {
113
    return $self->decode_json_field({ field => 'mapping' });
130
        return decode_json( $self->mapping );
131
    }
132
    catch {
133
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
134
    };
135
}
114
}
136
115
137
=head3 set_mapping
116
=head3 set_mapping
Lines 145-159 This method stores the passed mappings in JSON format. Link Here
145
sub set_mapping {
124
sub set_mapping {
146
    my ($self, $mapping) = @_;
125
    my ($self, $mapping) = @_;
147
126
148
    try {
127
    return $self->set_encoded_json_field({ data => $mapping, field => 'mapping' });
149
        my $encoded_mapping = encode_json( $mapping );
150
        $self->mapping( $encoded_mapping );
151
    }
152
    catch {
153
        Koha::Exceptions::Object::BadValue->throw("Error serializing data into JSON: $_");
154
    };
155
156
    return $self;
157
}
128
}
158
129
159
=head3 upgrade_class
130
=head3 upgrade_class
(-)a/Koha/Object/JSONFields.pm (-1 / +111 lines)
Line 0 Link Here
0
- 
1
package Koha::Object::JSONFields;
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 JSON;
21
use Try::Tiny;
22
23
use Koha::Exceptions;
24
use Koha::Exceptions::Object;
25
26
=head1 NAME
27
28
Koha::Object::JSONFields - Class that adds JSON field manipulation helper methods
29
for Koha::Object-derived classes
30
31
=head1 SYNOPSIS
32
33
    use base qw(Koha::Object Koha::Object::JSONFields);
34
    my $object = Koha::Object->new({ property1 => $property1, property2 => $property2, etc... } );
35
    my $field_name_hashref = $object->decode_json_field({ field => 'field_name' });
36
    $object->encode_json_field({ field => 'field_name', data => $data });
37
38
=head1 API
39
40
=head2 Class methods
41
42
=head3 decode_json_field
43
44
    my $hashref = $object->decode_json_field({ field => 'field_name' });
45
46
=cut
47
48
sub decode_json_field {
49
    my ( $self, $params ) = @_;
50
51
    Koha::Exceptions::MissingParameter->throw( parameter => 'field' )
52
      unless defined $params->{field};
53
54
    my $field = $params->{field};
55
56
    return try {
57
        $self->$field ? $self->_json->decode( $self->$field ) : undef;
58
    }
59
    catch {
60
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
61
    };
62
}
63
64
=head3 set_encoded_json_field
65
66
=cut
67
68
sub set_encoded_json_field {
69
    my ( $self, $params ) = @_;
70
71
    Koha::Exceptions::MissingParameter->throw( parameter => 'field' )
72
      unless defined $params->{field};
73
74
    Koha::Exceptions::MissingParameter->throw( parameter => 'data' )
75
      unless exists $params->{data};
76
77
    my $field = $params->{field};
78
    my $data  = $params->{data};
79
80
    return try {
81
        $self->$field( $data ? $self->_json->encode($data) : undef );
82
    }
83
    catch {
84
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
85
    };
86
}
87
88
=head2 Internal methods
89
90
=head3 _json
91
92
    my $JSON_object = $self->_json;
93
94
Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be
95
done later.
96
97
=cut
98
99
sub _json {
100
    my ($self) = @_;
101
    $self->{_json} //= JSON->new->utf8(0);
102
    return $self->{_json};
103
}
104
105
=head1 AUTHOR
106
107
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
108
109
=cut
110
111
1;

Return to bug 32370