File Coverage

blib/lib/Net/LDAP/posixGroup.pm
Criterion Covered Total %
statement 12 74 16.2
branch 0 24 0.0
condition n/a
subroutine 4 6 66.6
pod 2 2 100.0
total 18 106 16.9


line stmt bran cond sub pod time code
1             package Net::LDAP::posixGroup;
2              
3 2     2   356127 use warnings;
  2         4  
  2         120  
4 2     2   9 use strict;
  2         4  
  2         76  
5 2     2   950 use Net::LDAP::Entry;
  2         312482  
  2         95  
6 2     2   25 use base 'Error::Helper';
  2         5  
  2         1493  
7              
8             =head1 NAME
9              
10             Net::LDAP::posixGroup - Creates new Net::LDAP::Entry objects for a posixGroup entry.
11              
12             =head1 VERSION
13              
14             Version 1.0.0
15              
16             =cut
17              
18             our $VERSION = '1.0.0';
19              
20             =head1 SYNOPSIS
21              
22             use Net::LDAP::posixGroup;
23              
24             my $foo = Net::LDAP::posixGroup->new(baseDN=>'ou=group,dc=foo');
25              
26             # creates a new for the group newGroup with a GID of 404 and members of user1 and user2.
27             my $entry = $foo->create(name=>'newGroup', gid=>'404', members=>['user1', 'user2']);
28              
29             print $entry->ldif;
30              
31             =head1 FUNCTIONS
32              
33             =head2 new
34              
35             This initiates the object.
36              
37             - baseDN :: This is a required value and is the base that the entry will
38             be created under.
39              
40             - topless :: This is a perl boolean value. If this is set to true, the
41             objectClass top is not present.
42              
43             =cut
44              
45             sub new {
46 0     0 1   my ( $blank, %args ) = @_;
47              
48             # returns undef if the baseDN is not set
49 0 0         if ( !defined( $args{baseDN} ) ) {
50 0           warn('Net-LDAP-postixGroup new:0: "baseDN" is not defined');
51 0           return undef;
52             }
53              
54             #returns undef if the baseDN is not set
55 0           my $self = {
56             perror => undef,
57             error => undef,
58             errorLine => undef,
59             errorFilename => undef,
60             errorString => "",
61             errorExtra => {
62             all_errors_fatal => 1,
63             flags => {
64             1 => 'noGroupName',
65             2 => 'noGID',
66             3 => 'invalidPrimary',
67             4 => 'noBaseDN',
68             },
69             fatal_flags => {},
70             perror_not_fatal => 0,
71             },
72             baseDN => undef,
73             topless => undef,
74             };
75 0           bless $self;
76              
77             # if it is defined it sets the topless setting to what ever it is
78 0 0         if ( defined( $args{topless} ) ) {
79 0           $self->{topless} = $args{topless};
80             } else {
81 0           $self->{topless} = undef;
82             }
83              
84             # check to see if the base DN looks legit
85 0 0         if ( $args{baseDN} !~ /^(?:(?:[A-Za-z0-9]+=[^,]+),\s*)*(?:[A-Za-z0-9]+=[^,]+)$/ ) {
86 0           $self->{error} = 6;
87 0           $self->{errorString} = 'baseDN, "' . $args{baseDN} . '", does not appear to be a valid DN';
88 0           $self->{perror} = 1;
89 0           $self->warn;
90 0           return $self;
91             }
92              
93 0           $self->{baseDN} = $args{baseDN};
94              
95 0           return $self;
96             } ## end sub new
97              
98             =head2 create
99              
100             Creates a new Net::LDAP::Entry object.
101              
102             The following args are required.
103              
104             - name :: The group name.
105              
106             - gid :: The numeric GID of a group.
107              
108             The following are optional.
109              
110             - description :: A optional LDAP desciption.
111              
112             - primary :: The accepted values are 'cn' and 'gidNumber'.
113             - default :: cn
114              
115             =cut
116              
117             sub create {
118 0     0 1   my ( $self, %args ) = @_;
119              
120 0           $self->errorblank;
121              
122 0           my @members;
123 0 0         if ( defined( $args{members} ) ) {
124 0           @members = @{ $args{members} };
  0            
125             }
126              
127             # error if name is not defined
128 0 0         if ( !defined( $args{name} ) ) {
129 0           $self->{error} = 1;
130 0           $self->{errorString} = 'name not defined';
131 0           $self->warn;
132 0           return undef;
133             }
134              
135             # error if name is not defined
136 0 0         if ( !defined( $args{name} ) ) {
137 0           $self->{error} = 2;
138 0           $self->{errorString} = 'gid not defined';
139 0           $self->warn;
140 0           return undef;
141             }
142              
143             # sets the primary if it is not defined
144 0 0         if ( !defined( $args{primary} ) ) {
145 0           $args{primary} = 'cn';
146             }
147              
148             # verifies the primary
149 0           my @primary = ( 'gid', 'cn' );
150 0           my $dn = undef;
151 0           my $primaryInt = 0;
152 0           while ( defined( $primary[$primaryInt] ) ) {
153             #when a match is found, use it to begin forming the the DN
154 0 0         if ( $args{primary} eq $primary[$primaryInt] ) {
155 0           $dn = $args{primary} . '=';
156             }
157 0           $primaryInt++;
158             }
159              
160             #error if none is matched
161 0 0         if ( !defined($dn) ) {
162 0           $self->{error} = 3;
163 0           $self->{errorString} = 'primary is a invalid value';
164 0           $self->warn;
165 0           return undef;
166             }
167              
168             #forms the DN if it is using the gidNumber
169 0 0         if ( $args{primary} eq 'gidNumber' ) {
170 0           $dn = $dn . $args{uid};
171             }
172              
173             #forms the DN if it is using the CN
174 0 0         if ( $args{primary} eq 'cn' ) {
175 0           $dn = $dn . $args{name};
176             }
177              
178             #full forms the DN
179 0           $dn = $dn . ',' . $self->{baseDN};
180              
181             #creates a new object
182 0           my $entry = Net::LDAP::Entry->new;
183              
184             #sets the dn
185 0           $entry->dn($dn);
186              
187             #adds the various attributes
188             $entry->add(
189             objectClass => [ 'posixGroup', 'top' ],
190             gidNumber => [ $args{gid} ],
191 0           cn => [ $args{name} ]
192             );
193              
194             #adds the description if needed
195 0 0         if ( defined( $args{description} ) ) {
196 0           $entry->add( description => [ $args{description} ] );
197             }
198              
199 0           my $membersInt = 0;
200 0           while ( defined( $members[$membersInt] ) ) {
201 0           $entry->add( memberUid => [ $members[$membersInt] ] );
202              
203 0           $membersInt++;
204             }
205              
206 0           return $entry;
207             } ## end sub create
208              
209             =head1 Error Codes
210              
211             All error codes are considered fatal, allowing for easy cheacking via eval.
212              
213             =head2 1, noGroupName
214              
215             No group name specified.
216              
217             =head2 2, noGID
218              
219             No GID specified.
220              
221             =head2 3, invalidPrimary
222              
223             The primary is a invalid value.
224              
225             =head2 4, noBaseDN
226              
227             Missing baseDN.
228              
229             =head1 AUTHOR
230              
231             Zane C. Bowers, C<< >>
232              
233             =head1 BUGS
234              
235             Please report any bugs or feature requests to C, or through
236             the web interface at L. I will be notified, and then you'll
237             automatically be notified of progress on your bug as I make changes.
238              
239              
240              
241              
242             =head1 SUPPORT
243              
244             You can find documentation for this module with the perldoc command.
245              
246             perldoc Net::LDAP::posixGroup
247              
248              
249             You can also look for information at:
250              
251             =over 4
252              
253             =item * RT: CPAN's request tracker
254              
255             L
256              
257             =item * Search CPAN
258              
259             L
260              
261             =back
262              
263             =head1 ACKNOWLEDGEMENTS
264              
265              
266             =head1 COPYRIGHT & LICENSE
267              
268             Copyright 2023 Zane C. Bowers-Hadley, all rights reserved.
269              
270             This program is free software; you can redistribute it and/or modify it
271             under the same terms as Perl itself.
272              
273              
274             =cut
275              
276             1; # End of Net::LDAP::posixGroup