blib/lib/Net/LDAP/posixAccount.pm

filter: Statementstmt 18.1% 20 / 110 Branchbran 0.0% 0 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 6 / 9 Podpod 100% 3 / 3 totaltotal 16.4% 29 / 176 CC 28 SCAR 61.2
CC
28
Cov
12%
create
650.0
new
20.0
errorBlank
2.0
CRAP 453.8
↑ Summary
1 package Net::LDAP::posixAccount;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Net::LDAP::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Sys::User::UIDhelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Sys::Group::GIDhelper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::LDAP::posixAccount - Creates new Net::LDAP::Entry objects for a posixAccount entry.
13  
14 =head1 VERSION
15  
16 Version 1.0.0
17  
18 =cut
19  
20 2 our $VERSION = '1.0.0';
21  
22 =head1 SYNOPSIS
23  
24 use Net::LDAP::posixAccount;
25  
26 # Initiates the module with a base DN of 'ou=users,dc=foo'.
27 my $foo = Net::LDAP::posixAccount->new(baseDN=>'ou=user,dc=foo');
28  
29 # create the user vvelox with a gid of 404 and a uid of 404
30 my $entry = $foo->create(name=>'vvelox', gid=>'404', uid=>'404');
31  
32 # add it using $ldap, a previously created Net::LDAP object
33 $entry->update($ldap);
34  
35 =head1 METHODS
36  
37 =head2 new
38  
39 This initiates the module. It accepts one arguement, a hash. Please See below
40 for accepted values.
41  
42 - baseDN :: This is a required value and is the base that the entry will
43 be created under.
44  
45 - topless :: This is a perl boolean value. If this is set to true, the
46 objectClass top is not present.
47  
48 =cut
49  
50 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
51 0 my ( $blank, %args ) = @_;
52  
53 #returns undef if the baseDN is not set
54 0 my $self = {
55 perror => undef,
56 error => undef,
57 errorLine => undef,
58 errorFilename => undef,
59 errorString => "",
60 errorExtra => {
61 all_errors_fatal => 1,
62 flags => {
63 1 => 'missing_name',
64 2 => 'missing_uid',
65 3 => 'missing_gid',
66 4 => 'invalid_value',
67 5 => 'missing_baseDN',
68 6 => 'invalid_baseDN',
69 },
70 fatal_flags => {},
71 perror_not_fatal => 0,
72 },
73 baseDN => undef,
74 topless => undef,
75 };
76 0 bless $self;
77  
78 #if it is defined it sets the topless setting to what ever it is
79 0 ▶ if ( defined( $args{topless} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"topless"}) 0 0 0
80 0 $self->{topless} = $args{topless};
81 }
82  
83 0 ▶ if ( !defined( $args{baseDN} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"baseDN"}) 0 0 0
84 0 $self->{error} = 5;
85 0 $self->{errorString} = 'baseDN is not defined';
86 0 $self->{perror} = 1;
87 0 $self->warn;
88 0 return $self;
89 }
90  
91 # check to see if the base DN looks legit
92 0 ▶ if ( $args{baseDN} !~ /^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"baseDN"} =~ /^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$/) 0 0 0
93 0 $self->{error} = 6;
94 0 $self->{errorString} = 'baseDN, "' . $args{baseDN} . '", does not appear to be a valid DN';
95 0 $self->{perror} = 1;
96 0 $self->warn;
97 0 return $self;
98 }
99  
100 0 $self->{baseDN} = $args{baseDN};
101  
102 0 return $self;
103 } ## end sub new
104  
105 =head2 create
106  
107 Creates a new Net::LDAP::Entry object.
108  
109 - name :: The name of the user.
110  
111 - cn :: What the common name should be for a user. This defaults to the username if it is not defined.
112  
113 - uid ::This is the UID number of a user. If set to 'AUTO', Sys::User::UIDhelper will be used.
114  
115 - gid :: This is GID number of a user. If set to 'AUTO', Sys::Group::GIDhelper will be used.
116  
117 - gecos :: This is the GECOS field for a user. If it is not defined, the name is used.
118  
119 - loginShell This is the login shell for the user.
120 - default :: /sbin/nologin
121  
122 - home ::This is the home directory of a user.
123 - default :: /home/$name
124  
125 - primary :: This is the attribute that will be used for when creating the entry.
126 'uid', 'uidNumber', or 'cn' are the accepted value. The default is 'uid'.
127  
128 - description :: This is the LDAP description field. If it is not defined, it is set to gecos.
129  
130 - minUID :: This is the min UID that will be used if 'uid' is set to 'AUTO'.
131 - default :: 1001
132  
133 - maxUID This is the max UID that will be used if 'uid' is set to 'AUTO'.
134 - default :: 64000
135  
136 - minGID :: This is the min GID that will be used if 'gid' is set to 'AUTO'.
137 - default :: 1001
138  
139 - maxGID :: This is the max GID that will be used if 'gid' is set to 'AUTO'.
140 - default :: 64000
141  
142 =cut
143  
144 0 ▶sub create {
Subroutine create: not called CC=25 SCAR=64.8
Pod documented
145 0 my ( $self, %args ) = @_;
146  
147 0 $self->errorblank;
148  
149 #error if name is not defined
150 0 ▶ if ( !defined( $args{name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"name"}) 0 0 0
151 0 $self->{error} = 1;
152 0 $self->{errorString} = 'name not defined';
153 0 $self->warn;
154 }
155  
156 #set CN to name if it is not defined
157 0 ▶ if ( !defined( $args{cn} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"cn"}) 0 0 0
158 0 $args{cn} = $args{name};
159 }
160  
161 #error if uid is not defined
162 0 ▶ if ( !defined( $args{uid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"uid"}) 0 0 0
163 0 $self->{error} = 2;
164 0 $self->{errorString} = 'uid not defined';
165 0 $self->warn;
166 }
167  
168 #handles choosing the UID if it is set to AUTO
169 0 ▶ if ( $args{uid} eq 'AUTO' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"uid"} eq "AUTO") 0 0 0
170 #sets the minUID if it is not defined
171 0 ▶ if ( !defined( $args{minUID} eq '1001' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($args{"minUID"} eq 1001)) 0 0 0
172 0 $args{uid} = '1001';
173 }
174  
175 #sets the maxUID if it is not defined
176 0 ▶ if ( !defined( $args{minUID} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"minUID"}) 0 0 0
177 0 $args{uid} = '64000';
178 }
179  
180 #creates it
181 my $uidhelper = Sys::User::UIDhelper->new(
182 min => $args{minUID},
183 max => $args{maxUID}
184 0 );
185 #gets the first free one
186 0 $args{uid} = $uidhelper->firstfree();
187 } ## end if ( $args{uid} eq 'AUTO' )
188  
189 #error if gid is not defined
190 0 ▶ if ( !defined( $args{gid} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"gid"}) 0 0 0
191 0 $self->{error} = 3;
192 0 $self->{errorString} = 'gid not defined';
193 0 $self->warn;
194 }
195  
196 #handles choosing the GID if it is set to AUTO
197 0 ▶ if ( $args{gid} eq 'AUTO' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"gid"} eq "AUTO") 0 0 0
198 #sets the minUID if it is not defined
199 0 ▶ if ( !defined( $args{minGID} eq '1001' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($args{"minGID"} eq 1001)) 0 0 0
200 0 $args{uid} = '1001';
201 }
202  
203 #sets the maxUID if it is not defined
204 0 ▶ if ( !defined( $args{minGID} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"minGID"}) 0 0 0
205 0 $args{uid} = '64000';
206 }
207  
208 #creates it
209 my $gidhelper = Sys::Group::GIDhelper->new(
210 min => $args{minGID},
211 max => $args{maxGID}
212 0 );
213 #gets the first free one
214 0 $args{gid} = $gidhelper->firstfree();
215 } ## end if ( $args{gid} eq 'AUTO' )
216  
217 #set gecos to name if it is not defined
218 0 ▶ if ( !defined( $args{gecos} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"gecos"}) 0 0 0
219 0 ▶ if ( defined( $args{description} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'description'}) { } 0 0 0
220 0 $args{gecos} = $args{description};
221 } else {
222 0 $args{gecos} = $args{name};
223 }
224 }
225  
226 #sets the description field
227 0 ▶ if ( !defined( $args{description} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"description"}) 0 0 0
228 0 ▶ if ( defined( $args{gecos} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"gecos"}) 0 0 0
229 0 $args{description} = $args{gecos};
230 }
231 }
232  
233 #sets the loginShell to '/sbin/nologin' if it is not defined
234 0 ▶ if ( !defined( $args{loginShell} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"loginShell"}) 0 0 0
235 0 $args{loginShell} = '/sbin/nologin';
236 }
237  
238 #sets the home if it is not specified
239 0 ▶ if ( !defined( $args{home} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"home"}) 0 0 0
240 0 $args{loginShell} = '/home/' . $args{name};
241 }
242  
243 #set primary if it is not defined
244 0 ▶ if ( !defined( $args{primary} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"primary"}) 0 0 0
245 0 $args{primary} = 'uid';
246 }
247  
248 #
249 0 my @primary = ( 'uid', 'cn', 'uidNumber' );
250 0 my $dn = undef;
251 0 my $primaryInt = 0;
252 0 ▶ while ( defined( $primary[$primaryInt] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $primary[$primaryInt]) 0 0 0
253 #when a match is found, use it to begin forming the the DN
254 0 ▶ if ( $args{primary} eq $primary[$primaryInt] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"primary"} eq $primary[$primaryInt]) 0 0 0
255 0 $dn = $args{primary} . '=';
256 }
257 0 $primaryInt++;
258 }
259  
260 #error if none is matched
261 0 ▶ if ( !defined($dn) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dn) 0 0 0
262 0 $self->{error} = 4;
263 0 $self->{errorString} = 'primary is a invalid value';
264 0 $self->warn;
265 }
266  
267 #forms the DN if it is using the UID
268 0 ▶ if ( $args{primary} eq 'uid' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"primary"} eq "uid") 0 0 0
269 0 $dn = $dn . $args{name};
270 }
271  
272 #forms the DN if it is using the uidNumber
273 0 ▶ if ( $args{primary} eq 'uidNumber' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"primary"} eq "uidNumber") 0 0 0
274 0 $dn = $dn . $args{uid};
275 }
276  
277 #forms the DN if it is using the CN
278 0 ▶ if ( $args{primary} eq 'cn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"primary"} eq "cn") 0 0 0
279 0 $dn = $dn . $args{cn};
280 }
281  
282 #full forms the DN
283 0 $dn = $dn . ',' . $self->{baseDN};
284  
285 #creates a new object
286 0 my $entry = Net::LDAP::Entry->new;
287  
288 #sets the dn
289 0 $entry->dn($dn);
290  
291 #adds top if it is not topless
292 0 ▶ if ( !$args{topless} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"topless"}) 0 0 0
293 0 $entry->add( objectClass => ['top'] );
294 }
295  
296 #adds the various attributes
297 $entry->add(
298 objectClass => [ 'account', 'posixAccount' ],
299 uidNumber => [ $args{uid} ],
300 gidNumber => [ $args{gid} ],
301 uid => [ $args{name} ],
302 homeDirectory => [ $args{home} ],
303 gecos => [ $args{gecos} ],
304 loginShell => [ $args{loginShell} ],
305 cn => [ $args{cn} ],
306 0 description => [ $args{description} ]
307 );
308  
309 0 return $entry;
310 } ## end sub create
311  
312 =head2 errorBlank
313  
314 A internal function user for clearing an error.
315  
316 =cut
317  
318 #blanks the error flags
319 0 ▶sub errorBlank {
Subroutine errorBlank: not called CC=1 SCAR=6.9
Pod documented
320 0 my $self = $_[0];
321  
322 #error handling
323 0 $self->{error} = undef;
324 0 $self->{errorString} = "";
325  
326 0 return 1;
327 }
328  
329 =head1 Error Codes/Flags
330  
331 L<Error::Helper> is used and all errors are considered fatal.
332  
333 =head2 1/missing_name
334  
335 'name' not defined.
336  
337 =head2 2/missing_uid
338  
339 'uid' not defined.
340  
341 =head2 3/missing_gid
342  
343 'gid' not defined.
344  
345 =head2 4/invalid_value
346  
347 The primary value is a invalid value.
348  
349 =head2 5/missing_baseDN
350  
351 Missing baseDN.
352  
353 =head2 6/invalid_baseDN
354  
355 The specified base DN does does not appear to be a DN.
356  
357 Checked via the regex below.
358  
359 ^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$
360  
361 =head1 AUTHOR
362  
363 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
364  
365 =head1 BUGS
366  
367 Please report any bugs or feature requests to C<bug-net-ldap-posixaccount at rt.cpan.org>, or through
368 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-LDAP-posixAccount>. I will be notified, and then you'll
369 automatically be notified of progress on your bug as I make changes.
370  
371  
372  
373  
374 =head1 SUPPORT
375  
376 You can find documentation for this module with the perldoc command.
377  
378 perldoc Net::LDAP::posixAccount
379  
380  
381 You can also look for information at:
382  
383 =over 4
384  
385 =item * RT: CPAN's request tracker
386  
387 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-LDAP-posixAccount>
388  
389 =item * AnnoCPAN: Annotated CPAN documentation
390  
391 L<http://annocpan.org/dist/Net-LDAP-posixAccount>
392  
393 =item * CPAN Ratings
394  
395 L<http://cpanratings.perl.org/d/Net-LDAP-posixAccount>
396  
397 =item * Search CPAN
398  
399 L<http://search.cpan.org/dist/Net-LDAP-posixAccount>
400  
401 =back
402  
403  
404 =head1 ACKNOWLEDGEMENTS
405  
406  
407 =head1 COPYRIGHT & LICENSE
408  
409 Copyright 2008 Zane C. Bowers, all rights reserved.
410  
411 This program is free software; you can redistribute it and/or modify it
412 under the same terms as Perl itself.
413  
414  
415 =cut
416  
417 2 1; # End of Net::LDAP::posixAccount
↑ Summary