blib/lib/Net/LDAP/posixAccount.pm

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