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 / +122 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
Returns a data structure representing the JSON-decoded value of field I<field_name>.
47
48
=cut
49
50
sub decode_json_field {
51
    my ( $self, $params ) = @_;
52
53
    Koha::Exceptions::MissingParameter->throw( parameter => 'field' )
54
      unless defined $params->{field};
55
56
    my $field = $params->{field};
57
58
    return try {
59
        $self->$field ? $self->_json->decode( $self->$field ) : undef;
60
    }
61
    catch {
62
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
63
    };
64
}
65
66
=head3 set_encoded_json_field
67
68
    $object->set_encoded_json_field(
69
        {   data  => $data,
70
            field => 'field_name',
71
        }
72
    );
73
74
Sets a JSON string encoded representation of I<$data> into the object's I<field_name>
75
attribute.
76
77
=cut
78
79
sub set_encoded_json_field {
80
    my ( $self, $params ) = @_;
81
82
    Koha::Exceptions::MissingParameter->throw( parameter => 'field' )
83
      unless defined $params->{field};
84
85
    Koha::Exceptions::MissingParameter->throw( parameter => 'data' )
86
      unless exists $params->{data};
87
88
    my $field = $params->{field};
89
    my $data  = $params->{data};
90
91
    return try {
92
        $self->$field( $data ? $self->_json->encode($data) : undef );
93
    }
94
    catch {
95
        Koha::Exceptions::Object::BadValue->throw("Error reading JSON data: $_");
96
    };
97
}
98
99
=head2 Internal methods
100
101
=head3 _json
102
103
    my $JSON_object = $self->_json;
104
105
Returns a JSON object with utf8 disabled. Encoding to UTF-8 should be
106
done later.
107
108
=cut
109
110
sub _json {
111
    my ($self) = @_;
112
    $self->{_json} //= JSON->new->utf8(0);
113
    return $self->{_json};
114
}
115
116
=head1 AUTHOR
117
118
Tomas Cohen Arazi, E<lt>tomascohen@theke.ioE<gt>
119
120
=cut
121
122
1;

Return to bug 32370