| 1 | package Net::LDAP::SimpleServer::LDIFStore; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Data store to support Net::LDAP::SimpleServer | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 28 | our $VERSION = '0.0.19'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 28 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use UNIVERSAL::isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 28 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 28 | ▶ | use Net::LDAP::LDIF; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 28 | ▶ | use Net::LDAP::Util qw/canonical_dn/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 28 | ▶ | use Net::LDAP::SimpleServer::Constant; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||||||||||
| 21 | 17 | ▶ | my ( $class, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 17 | ▶ | croak 'Must pass parameter!' unless defined($param); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # empty defaults | ||||||||||||||||||||||||||||
| 25 | 17 | my $data = { | |||||||||||||||||||||||||||
| 26 | ldif_object => undef, | ||||||||||||||||||||||||||||
| 27 | tree => {}, | ||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 17 | my $self = bless( $data, $class ); | |||||||||||||||||||||||||||
| 31 | 17 | $self->load($param); | |||||||||||||||||||||||||||
| 32 | 15 | return $self; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub load { | ||||||||||||||||||||||||||||
| 36 | 17 | ▶ | my ( $self, $param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=14.8
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 17 | ▶ | croak 'Must pass parameter!' unless $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 16 | ▶ | if ( blessed($param) && $param->isa('Net::LDAP::LDIF') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($param) and $param->isa("Net::LDAP::LDIF")
MC/DC0%0/2
builtin::blessed($param) and $param->isa("Net::LDAP::LDIF")
builtin::blessed($param)
$param->isa("Net::LDAP::LDIF")
Truth table33%1/3
A and B
A= builtin::blessed($param)B= $param->isa("Net::LDAP::LDIF")
| |||||||||||||||||||||||||||||
| 41 | 0 | $self->{ldif_object} = $param; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 16 | $self->_open_ldif($param); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 15 | $self->_load_ldif(); | |||||||||||||||||||||||||||
| 47 | 15 | return; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub ldif { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
ldif: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | return $self->{ldif_object}; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||
| 56 | # opens a filename, a file-handle, or a Net::LDAP::LDIF object | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | sub _open_ldif { | ||||||||||||||||||||||||||||
| 59 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_open_ldif: called CC=7 SCAR=25.7
| |||||||||||||||||||||||||||||
| 60 | 16 | ▶ | my $param = shift // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 16 | ▶ | my $reftype = reftype($param) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
builtin::reftype($param) // ""
MC/DC0%0/1
builtin::reftype($param) // ""
builtin::reftype($param)
Truth table50%1/2
A
A= builtin::reftype($param)
| |||||||||||||||||||||||||||||
| 63 | 16 | ▶ | if ( $reftype eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | croak q{Hash parameter must contain a "ldif" parameter} | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless exists $param->{ldif}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | $self->{ldif_object} = Net::LDAP::LDIF->new( | ||||||||||||||||||||||||||||
| 68 | $param->{ldif}, | ||||||||||||||||||||||||||||
| 69 | 'r', | ||||||||||||||||||||||||||||
| 70 | ( | ||||||||||||||||||||||||||||
| 71 | exists $param->{ldif_options} | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | ? %{ $param->{ldif_options} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | : undef | ||||||||||||||||||||||||||||
| 74 | ) | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Then, it must be a filename | ||||||||||||||||||||||||||||
| 80 | 16 | ▶ | croak q{Cannot read file "} . $param . q{"} unless -r $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 15 | $self->{ldif_object} = Net::LDAP::LDIF->new($param); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _make_entry_path { | ||||||||||||||||||||||||||||
| 86 | 25 | ▶ | my $dn = shift; | ||||||||||||||||||||||||||
|
Subroutine
_make_entry_path: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 25 | ▶ | $dn = $dn->dn() if $dn->isa('Net::LDAP::Entry'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 25 | return [ reverse( split( ',', canonical_dn($dn) ) ) ]; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub _make_entry { | ||||||||||||||||||||||||||||
| 94 | 45 | ▶ | my ( $entry, $tree, $current_dn, @path ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_entry: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 45 | ▶ | $tree = {} unless defined($tree); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 45 | ▶ | if ( scalar(@path) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 15 | $tree->{_object} = $entry; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||||||||||
| 101 | 30 | my $next = $path[0]; | |||||||||||||||||||||||||||
| 102 | $tree->{_object} = Net::LDAP::Entry->new($current_dn) | ||||||||||||||||||||||||||||
| 103 | 30 | ▶ | unless exists $tree->{_object}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | $tree->{$next} = _make_entry( | ||||||||||||||||||||||||||||
| 105 | 30 | $entry, $tree->{$next}, | |||||||||||||||||||||||||||
| 106 | join( q{,}, $next, $current_dn ), | ||||||||||||||||||||||||||||
| 107 | @path[ 1 .. $#path ] | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 45 | return $tree; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub _add { | ||||||||||||||||||||||||||||
| 115 | 15 | ▶ | my ( $self, $entry ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_add: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 15 | my @path = @{ _make_entry_path($entry) }; | |||||||||||||||||||||||||||
| 118 | 15 | my $tree = $self->{tree}; | |||||||||||||||||||||||||||
| 119 | 15 | my $next = $path[0]; | |||||||||||||||||||||||||||
| 120 | 15 | $tree->{$next} = _make_entry( $entry, $tree->{$next}, @path ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # line above is equivalent to | ||||||||||||||||||||||||||||
| 123 | # _make_entry( $entry, $tree->{$next}, $next, @path[ 1 .. $#path ] ); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | # loads a LDIF file | ||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||
| 129 | sub _load_ldif { | ||||||||||||||||||||||||||||
| 130 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_load_ldif: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 131 | 15 | my $ldif = $self->{ldif_object}; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 15 | ▶ | while ( not $ldif->eof() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 15 | my $entry = $ldif->read_entry(); | |||||||||||||||||||||||||||
| 135 | 15 | ▶ | if ( $ldif->error() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | print STDERR "Error msg: ", $ldif->error(), "\n"; | |||||||||||||||||||||||||||
| 137 | 0 | print STDERR "Error lines:\n", $ldif->error_lines(), "\n"; | |||||||||||||||||||||||||||
| 138 | 0 | next; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 15 | $self->_add($entry); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 15 | $ldif->done(); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _find_subtree { | ||||||||||||||||||||||||||||
| 147 | 23 | ▶ | my ( $tree, $rdn, @path ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_subtree: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 23 | ▶ | return unless exists $tree->{$rdn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 22 | ▶ | return $tree->{$rdn} if scalar(@path) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 13 | return _find_subtree( $tree->{$rdn}, @path ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub find_tree { | ||||||||||||||||||||||||||||
| 155 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_tree: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 10 | my $dn = shift; | |||||||||||||||||||||||||||
| 157 | 10 | ▶ | $dn = $dn->dn() if $dn->isa('Net::LDAP::Entry'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 10 | return _find_subtree( $self->{tree}, @{ _make_entry_path($dn) } ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub exists_dn { | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | my ( $self, $dn ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
exists_dn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $tree = $self->find_tree($dn); | |||||||||||||||||||||||||||
| 166 | 0 | return defined($tree); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub find_entry { | ||||||||||||||||||||||||||||
| 170 | 3 | ▶ | my ( $self, $dn ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find_entry: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 3 | my $tree = $self->find_tree($dn); | |||||||||||||||||||||||||||
| 173 | 3 | ▶ | return $tree->{_object} if defined($tree); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | return; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub _list { | ||||||||||||||||||||||||||||
| 178 | 9 | ▶ | my $tree = shift; | ||||||||||||||||||||||||||
|
Subroutine
_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | my @children_trees = | ||||||||||||||||||||||||||||
| 181 | 9 | map { $tree->{$_} } ( grep { $_ ne '_object' } keys( %{$tree} ) ); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 9 | return ( $tree->{_object}, ( map { ( _list($_) ) } @children_trees ) ); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | sub list { | ||||||||||||||||||||||||||||
| 187 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
list: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 1 | ▶ | my $tree = shift // $self->{tree}->{ ( keys( %{ $self->{tree} } ) )[0] }; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() // $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
MC/DC0%0/2
shift() // $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
shift()
$self->{"tree"}{(keys %{$$self{"tree"};})[0]}
Truth table33%1/3
A // B
A= shift()B= $self->{"tree"}{(keys %{$$self{"tree"};})[0]}
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 1 | return [ _list($tree) ]; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | sub _list_baseobj { | ||||||||||||||||||||||||||||
| 194 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_list_baseobj: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 195 | 2 | my $dn = shift; | |||||||||||||||||||||||||||
| 196 | 2 | my $entry = $self->find_entry($dn); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 2 | ▶ | return unless defined($entry); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | return [$entry]; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | sub _list_onelevel { | ||||||||||||||||||||||||||||
| 204 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_list_onelevel: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 205 | 2 | my $dn = shift; | |||||||||||||||||||||||||||
| 206 | 2 | my $tree = $self->find_tree($dn); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | ▶ | return unless defined($tree); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | my @children = | ||||||||||||||||||||||||||||
| 210 | 1 | map { $tree->{$_}->{_object} } | |||||||||||||||||||||||||||
| 211 | 2 | ( grep { $_ ne '_object' } keys( %{$tree} ) ); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 2 | return [ $tree->{_object}, @children ]; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub _list_subtree { | ||||||||||||||||||||||||||||
| 217 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_list_subtree: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 218 | 4 | my $dn = shift; | |||||||||||||||||||||||||||
| 219 | 4 | my $tree = $self->find_tree($dn); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 4 | ▶ | return unless defined($tree); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 3 | return [ _list($tree) ]; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub list_with_dn_scope { | ||||||||||||||||||||||||||||
| 226 | 8 | ▶ | my ( $self, $dn, $scope ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
list_with_dn_scope: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 8 | my @funcs = ( \&_list_baseobj, \&_list_onelevel, \&_list_subtree ); | |||||||||||||||||||||||||||
| 229 | 8 | return $funcs[$scope]->( $self, $dn ); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 28 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||