| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::LDAP::Class::MethodMaker; |
|
2
|
10
|
|
|
10
|
|
55
|
use strict; |
|
|
10
|
|
|
|
|
23
|
|
|
|
10
|
|
|
|
|
346
|
|
|
3
|
10
|
|
|
10
|
|
57
|
use warnings; |
|
|
10
|
|
|
|
|
21
|
|
|
|
10
|
|
|
|
|
315
|
|
|
4
|
10
|
|
|
10
|
|
59
|
use base qw( Rose::Object::MakeMethods::Generic ); |
|
|
10
|
|
|
|
|
97
|
|
|
|
10
|
|
|
|
|
196406
|
|
|
5
|
10
|
|
|
10
|
|
1199211
|
use Carp; |
|
|
10
|
|
|
|
|
88
|
|
|
|
10
|
|
|
|
|
826
|
|
|
6
|
10
|
|
|
10
|
|
77
|
use Data::Dump; |
|
|
10
|
|
|
|
|
23
|
|
|
|
10
|
|
|
|
|
14505
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.26'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Net::LDAP::Class::MethodMaker - create methods for Net::LDAP::Class classes |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
package MyUser; |
|
17
|
|
|
|
|
|
|
use base qw( Net::LDAP::Class::User ); |
|
18
|
|
|
|
|
|
|
use Net::LDAP::Class::MethodMaker ( |
|
19
|
|
|
|
|
|
|
'scalar --get_set_init' => [qw( foo )], |
|
20
|
|
|
|
|
|
|
'related_objects' => [qw( bars )], |
|
21
|
|
|
|
|
|
|
); |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
__PACKAGE__->metadata->setup( |
|
24
|
|
|
|
|
|
|
base_dn => 'dc=local', |
|
25
|
|
|
|
|
|
|
attributes => [qw( foo )], |
|
26
|
|
|
|
|
|
|
unique_attributes => [qw( foo )], |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# must define a fetch_bars method |
|
30
|
|
|
|
|
|
|
sub fetch_bars { |
|
31
|
|
|
|
|
|
|
my $user = shift; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# do something to get bar objects. |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# elsewhere |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $user = MyUser->new( foo => '1234' )->read or die; |
|
42
|
|
|
|
|
|
|
$user->foo; # == $user->ldap_entry->get_value('foo'); |
|
43
|
|
|
|
|
|
|
$user->foo(5678); # == $user->ldap_entry->replace( foo => 5678 ); |
|
44
|
|
|
|
|
|
|
$user->foo; # returns '5678' |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $bars = $user->bars; # == $user->fetch_bars; |
|
47
|
|
|
|
|
|
|
push(@$bars, 'new bar'); |
|
48
|
|
|
|
|
|
|
$user->bars($bars); |
|
49
|
|
|
|
|
|
|
my $newbars = $user->bars; # != $user->fetch_bars; |
|
50
|
|
|
|
|
|
|
$user->clear_bars; |
|
51
|
|
|
|
|
|
|
$newbars = $user->bars; # == $user->fetch_bars; |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Net::LDAP::Class::MethodMaker is a subclass of Rose::Object::MakeMethods::Generic. |
|
57
|
|
|
|
|
|
|
It extends the base class with two new method types: related_objects and ldap_entry. |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 METHODS |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head2 related_objects( I, I ) |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
The related_objects method type creates three methods for each |
|
64
|
|
|
|
|
|
|
I when using the 'get_set' (default) interface: |
|
65
|
|
|
|
|
|
|
C, C, and C. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The I method must be defined by your class. It should return |
|
68
|
|
|
|
|
|
|
values from the LDAP server. |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
The I method is a get/set method. If nothing is set, it calls |
|
71
|
|
|
|
|
|
|
through to I. Otherwise, if you have set something, it returns |
|
72
|
|
|
|
|
|
|
what you have set. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
The I method will delete any set value from the object and return it. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub related_objects { |
|
79
|
32
|
|
|
32
|
1
|
7748
|
my ( $class, $name, $args ) = @_; |
|
80
|
|
|
|
|
|
|
|
|
81
|
32
|
|
|
|
|
56
|
my %methods; |
|
82
|
|
|
|
|
|
|
|
|
83
|
32
|
|
33
|
|
|
498
|
my $key = $args->{'hash_key'} || $name; |
|
84
|
32
|
|
50
|
|
|
1428
|
my $interface = $args->{'interface'} || 'get_set'; |
|
85
|
|
|
|
|
|
|
|
|
86
|
32
|
50
|
|
|
|
144
|
if ( $interface eq 'get_set_init' ) { |
|
|
|
50
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
croak |
|
88
|
|
|
|
|
|
|
"get_set_init interface not supported for related_objects: $name"; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
elsif ( $interface eq 'get_set' ) { |
|
91
|
32
|
|
33
|
|
|
150
|
my $fetcher_method = $args->{'fetch_method'} || "fetch_$name"; |
|
92
|
|
|
|
|
|
|
$methods{$name} = sub { |
|
93
|
257
|
100
|
|
257
|
|
2246
|
if ( @_ > 1 ) { |
|
94
|
97
|
50
|
|
|
|
533
|
if ( !$_[0]->validate( $key, $_[1] ) ) { |
|
95
|
0
|
|
|
|
|
0
|
croak "validate failed for attribute $key: " |
|
96
|
|
|
|
|
|
|
. $_[0]->error; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
97
|
|
|
|
|
528
|
return $_[0]->{$key} = $_[1]; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
160
|
100
|
|
|
|
1624
|
return exists $_[0]->{$key} |
|
101
|
|
|
|
|
|
|
? $_[0]->{$key} |
|
102
|
|
|
|
|
|
|
: $_[0]->$fetcher_method; |
|
103
|
32
|
|
|
|
|
212
|
}; |
|
104
|
|
|
|
|
|
|
|
|
105
|
32
|
|
|
0
|
|
192
|
$methods{"clear_$name"} = sub { return delete $_[0]->{$key} }; |
|
|
0
|
|
|
|
|
0
|
|
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
else { |
|
108
|
0
|
|
|
|
|
0
|
croak "Unknown interface: $interface"; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
32
|
|
|
|
|
118
|
return \%methods; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 ldap_entry |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The ldap_entry method type supports the 'get_set' interface only. |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This method type negotiates the getting and setting of values |
|
119
|
|
|
|
|
|
|
in the delegate ldap_entry() object. |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# get/set attributes on the delegate ldap_entry |
|
124
|
|
|
|
|
|
|
sub ldap_entry { |
|
125
|
132
|
|
|
132
|
1
|
5512
|
my ( $class, $name, $args ) = @_; |
|
126
|
|
|
|
|
|
|
|
|
127
|
132
|
50
|
|
|
|
1788
|
if ( $class->can($name) ) { |
|
128
|
0
|
|
|
|
|
0
|
carp "class $class already has method for $name"; |
|
129
|
0
|
|
|
|
|
0
|
return; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
132
|
|
|
|
|
157
|
my %methods; |
|
133
|
|
|
|
|
|
|
|
|
134
|
132
|
|
33
|
|
|
537
|
my $attribute = $args->{'hash_key'} || $name; |
|
135
|
132
|
|
50
|
|
|
556
|
my $interface = $args->{'interface'} || 'get_set'; |
|
136
|
|
|
|
|
|
|
|
|
137
|
132
|
50
|
|
|
|
270
|
if ( $interface eq 'get_set' ) { |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$methods{$name} = sub { |
|
140
|
2628
|
|
|
2628
|
|
8492
|
my $self = shift; |
|
141
|
2628
|
|
|
|
|
8614
|
my @args = @_; |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# we do not support values of more than one arg |
|
144
|
2628
|
50
|
|
|
|
8541
|
if ( scalar @args > 1 ) { |
|
145
|
0
|
|
|
|
|
0
|
croak "cannot set more than one value at a time"; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# if we haven't yet loaded a Net::LDAP::Entry via read() |
|
149
|
|
|
|
|
|
|
# cache the values and set them when/if we read(). |
|
150
|
2628
|
100
|
|
|
|
9549
|
if ( !defined $self->ldap_entry ) { |
|
151
|
|
|
|
|
|
|
|
|
152
|
1392
|
100
|
|
|
|
4318
|
if ( scalar @args ) { |
|
153
|
210
|
|
|
|
|
1070
|
$self->{_not_yet_set}->{$attribute} = $args[0]; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
return |
|
156
|
1392
|
100
|
|
|
|
13140
|
exists $self->{_not_yet_set}->{$attribute} |
|
157
|
|
|
|
|
|
|
? $self->{_not_yet_set}->{$attribute} |
|
158
|
|
|
|
|
|
|
: undef; |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# otherwise, delegate to the ldap_entry |
|
163
|
|
|
|
|
|
|
#unless ( grep { $_ eq $attribute } @{ $self->attributes } ) { |
|
164
|
|
|
|
|
|
|
# croak |
|
165
|
|
|
|
|
|
|
# qq[no such attribute or method "$attribute" defined for package "] |
|
166
|
|
|
|
|
|
|
# . ref($self) |
|
167
|
|
|
|
|
|
|
# . qq[ -- do you need to add '$attribute' to your setup() call?"]; |
|
168
|
|
|
|
|
|
|
# } |
|
169
|
|
|
|
|
|
|
|
|
170
|
1236
|
100
|
|
|
|
17458
|
if ( scalar @args ) { |
|
171
|
|
|
|
|
|
|
|
|
172
|
8
|
50
|
|
|
|
74
|
if ( !$self->validate( $attribute, $args[0] ) ) { |
|
173
|
0
|
|
|
|
|
0
|
croak "validate failed for attribute $attribute: " |
|
174
|
|
|
|
|
|
|
. $self->error; |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
#warn "AUTOLOAD set $attribute -> $args[0]"; |
|
178
|
8
|
|
|
|
|
170
|
my @old = $self->ldap_entry->get_value($attribute); |
|
179
|
8
|
|
|
|
|
208
|
$self->ldap_entry->replace( $attribute, $args[0] ); |
|
180
|
8
|
|
|
|
|
477
|
$self->{_was_set}->{$attribute}->{new} = $args[0]; |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# do not overwrite an existing 'old' value, since we might need to know |
|
183
|
|
|
|
|
|
|
# what was originally in the ldap_entry in order to replace it. |
|
184
|
8
|
50
|
|
|
|
47
|
unless ( exists $self->{_was_set}->{$attribute}->{old} ) { |
|
185
|
8
|
100
|
|
|
|
778
|
$self->{_was_set}->{$attribute}->{old} |
|
186
|
|
|
|
|
|
|
= @old > 1 ? \@old : $old[0]; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
1236
|
|
|
|
|
5091
|
my (@ret) = ( $self->ldap_entry->get_value($attribute) ); |
|
191
|
1236
|
100
|
|
|
|
7528052
|
if (wantarray) { |
|
192
|
88
|
|
|
|
|
812
|
return @ret; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
else { |
|
195
|
1148
|
100
|
|
|
|
9566
|
return @ret > 1 ? \@ret : $ret[0]; |
|
196
|
|
|
|
|
|
|
} |
|
197
|
132
|
|
|
|
|
2331592
|
}; |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
else { |
|
201
|
0
|
|
|
|
|
0
|
croak "Unknown interface: $interface"; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
|
|
|
|
|
|
|
|
204
|
132
|
|
|
|
|
467
|
return \%methods; |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 object_or_class_meta |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Similar to the 'scalar --get-set-init' method type but may be called as a class method, |
|
210
|
|
|
|
|
|
|
in which case it will call through to the class metadata() object. |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub object_or_class_meta { |
|
215
|
30
|
|
|
30
|
1
|
12251
|
my ( $class, $name, $args ) = @_; |
|
216
|
|
|
|
|
|
|
|
|
217
|
30
|
|
|
|
|
48
|
my %methods; |
|
218
|
30
|
|
33
|
|
|
175
|
my $key = $args->{'hash_key'} || $name; |
|
219
|
30
|
|
33
|
|
|
163
|
my $init_method = $args->{'init_method'} || "init_$name"; |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
$methods{$name} = sub { |
|
222
|
2040
|
100
|
|
2040
|
|
10488
|
if ( ref( $_[0] ) ) { |
|
223
|
1999
|
50
|
|
|
|
7080
|
return $_[0]->{$key} = $_[1] if ( @_ > 1 ); |
|
224
|
|
|
|
|
|
|
|
|
225
|
1999
|
50
|
|
|
|
13583
|
if ( $_[0]->can($init_method) ) { |
|
226
|
0
|
0
|
|
|
|
0
|
return defined $_[0]->{$key} |
|
227
|
|
|
|
|
|
|
? $_[0]->{$key} |
|
228
|
|
|
|
|
|
|
: ( $_[0]->{$key} = $_[0]->$init_method() ); |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
else { |
|
231
|
1999
|
100
|
|
|
|
17332
|
return defined $_[0]->{$key} |
|
232
|
|
|
|
|
|
|
? $_[0]->{$key} |
|
233
|
|
|
|
|
|
|
: ( $_[0]->{$key} = $_[0]->metadata->$key ); |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
} |
|
236
|
|
|
|
|
|
|
else { |
|
237
|
41
|
|
|
|
|
260
|
return $_[0]->metadata->$key; |
|
238
|
|
|
|
|
|
|
} |
|
239
|
30
|
|
|
|
|
172
|
}; |
|
240
|
|
|
|
|
|
|
|
|
241
|
30
|
|
|
|
|
92
|
return \%methods; |
|
242
|
|
|
|
|
|
|
} |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
1; |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
__END__ |