line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::LDAP; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21056
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
111034
|
|
|
1
|
|
|
|
|
7
|
|
4
|
1
|
|
|
1
|
|
1369
|
use Catmandu::Util qw(:is); |
|
1
|
|
|
|
|
44201
|
|
|
1
|
|
|
|
|
428
|
|
5
|
1
|
|
|
1
|
|
10
|
use Carp qw(confess); |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
48
|
|
6
|
1
|
|
|
1
|
|
806
|
use Net::LDAP; |
|
1
|
|
|
|
|
217648
|
|
|
1
|
|
|
|
|
10
|
|
7
|
1
|
|
|
1
|
|
103
|
use Moo; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
11
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has host => (is => 'ro', default => sub { 'ldap://127.0.0.1:389' }); |
12
|
|
|
|
|
|
|
has base => (is => 'ro', predicate => 1); |
13
|
|
|
|
|
|
|
has password => (is => 'ro', predicate => 1); |
14
|
|
|
|
|
|
|
has search_base => (is => 'ro', predicate => 1); |
15
|
|
|
|
|
|
|
has search_filter => (is => 'ro', predicate => 1); |
16
|
|
|
|
|
|
|
has ldap => (is => 'ro', lazy => 1, builder => '_build_ldap'); |
17
|
|
|
|
|
|
|
has attributes => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
coerce => sub { |
20
|
|
|
|
|
|
|
my $attrs = $_[0]; |
21
|
|
|
|
|
|
|
if (is_string $attrs) { |
22
|
|
|
|
|
|
|
return { map { $_ => {} } split ',', $attrs }; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
if (is_array_ref $attrs) { |
25
|
|
|
|
|
|
|
return { map { $_ => {} } @$attrs }; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
if ($attrs) { |
28
|
|
|
|
|
|
|
for my $attr (keys %$attrs) { |
29
|
|
|
|
|
|
|
$attrs->{$attr} = {} unless ref $attrs->{$attr}; |
30
|
|
|
|
|
|
|
}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
$attrs; |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _build_ldap { |
37
|
0
|
|
|
0
|
|
|
my $self = $_[0]; |
38
|
0
|
|
0
|
|
|
|
my $ldap = Net::LDAP->new($self->host, raw => qr/;binary/) || confess $@; |
39
|
0
|
0
|
|
|
|
|
my $bind = $self->has_base |
|
|
0
|
|
|
|
|
|
40
|
|
|
|
|
|
|
? $self->has_password |
41
|
|
|
|
|
|
|
? $ldap->bind($self->base, password => $self->password) |
42
|
|
|
|
|
|
|
: $ldap->bind($self->base) |
43
|
|
|
|
|
|
|
: $ldap->bind; |
44
|
0
|
0
|
|
|
|
|
$bind->code && confess $bind->error; |
45
|
0
|
|
|
|
|
|
$ldap; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub _new_search { |
49
|
0
|
|
|
0
|
|
|
my $self = $_[0]; |
50
|
0
|
|
|
|
|
|
my %args; |
51
|
0
|
0
|
|
|
|
|
$args{base} = $self->search_base if $self->has_search_base; |
52
|
0
|
0
|
|
|
|
|
$args{filter} = $self->search_filter if $self->has_search_filter; |
53
|
0
|
0
|
|
|
|
|
if (my $attrs = $self->attributes) { |
54
|
0
|
|
|
|
|
|
$args{attrs} = [keys %$attrs]; |
55
|
|
|
|
|
|
|
} |
56
|
0
|
|
|
|
|
|
my $search = $self->ldap->search(%args); |
57
|
0
|
0
|
|
|
|
|
$search->code && confess $search->error; |
58
|
0
|
|
|
|
|
|
$search; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub generator { |
62
|
|
|
|
|
|
|
my $self = $_[0]; |
63
|
|
|
|
|
|
|
sub { |
64
|
|
|
|
|
|
|
state $search = $self->_new_search; |
65
|
|
|
|
|
|
|
my $entry = $search->shift_entry // return; |
66
|
|
|
|
|
|
|
my $data = {}; |
67
|
|
|
|
|
|
|
if (my $attrs = $self->attributes) { |
68
|
|
|
|
|
|
|
for my $attr (keys %$attrs) { |
69
|
|
|
|
|
|
|
my $config = $attrs->{$attr}; |
70
|
|
|
|
|
|
|
my $val = $entry->get_value($attr, asref => $config->{array}) // next; |
71
|
|
|
|
|
|
|
$data->{$config->{as} // $attr} = $config->{array} ? [@$val] : $val; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
for my $attr ($entry->attributes) { |
75
|
|
|
|
|
|
|
my $val = $entry->get_value($attr, asref => 1); |
76
|
|
|
|
|
|
|
$data->{$attr} = [@$val]; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
$data; |
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 NAME |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Catmandu::Importer::LDAP - Package that imports LDAP directories |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 SYNOPSIS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Catmandu::Importer::LDAP; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $importer = Catmandu::Importer::LDAP->new( |
92
|
|
|
|
|
|
|
base => "...", |
93
|
|
|
|
|
|
|
password => "...", |
94
|
|
|
|
|
|
|
search_base => "...", |
95
|
|
|
|
|
|
|
search_filter => "(&(...)(...))", |
96
|
|
|
|
|
|
|
attributes => { |
97
|
|
|
|
|
|
|
name => 1, |
98
|
|
|
|
|
|
|
# or |
99
|
|
|
|
|
|
|
name => {as => "Name"}, |
100
|
|
|
|
|
|
|
# or |
101
|
|
|
|
|
|
|
name => {as => "Name", array => 1}, |
102
|
|
|
|
|
|
|
}, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
my $n = $importer->each(sub { |
106
|
|
|
|
|
|
|
my $hashref = $_[0]; |
107
|
|
|
|
|
|
|
# ... |
108
|
|
|
|
|
|
|
}); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |