LDAP/Command/Add/User.pm

filter: Statementstmt 56.0% 28 / 50 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 6 / 12 Podpod 0.0% 0 / 4 totaltotal 40.9% 34 / 83 CC 8 SCAR 26.9
CC
8
Cov
43%
run
42.0
__ANON__
6.0
gid_of
3.0
CRAP 14.8
« LDAP/Command/Add/Group.pm ↑ Summary LDAP/Command/Import.pm »
1 package App::LDAP::Command::Add::User;
2  
3 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 with qw( App::LDAP::Role::Command
8 App::LDAP::Role::Bindable );
9  
10 1 has shell => (
11 is => "rw",
12 isa => "Str",
13 documentation => "the login shell. default /bin/bash.",
14 );
15  
16 1 has home => (
17 is => "rw",
18 isa => "Str",
19 documentation => 'the home directory. default /home/\$username',
20 );
21  
22 1 has group => (
23 is => "rw",
24 isa => "Str",
25 documentation => 'the group name. default $username',
26 );
27  
28 1 has base => (
29 is => "rw",
30 isa => "Str",
31 documentation => 'the organizational unit this user belongs to. default /etc/ldap/ldap.conf nss_base_passwd.',
32 );
33  
34 # inetOrgPerson
35  
36 has surname => (
37 is => "rw",
38 isa => "ArrayRef[Str]",
39 0 ▶ default => sub { ["NULL"] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 1 documentation => 'the surname. default $username',
41 );
42  
43 1 has mail => (
44 is => "rw",
45 isa => "ArrayRef",
46 required => 1,
47 documentation => "the email addresses. this option can be multiple values"
48 );
49  
50 1 ▶use App::LDAP::LDIF::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 0 ▶around prepare => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 my $orig = shift;
54 0 my $self = shift;
55  
56 0 ▶ $self->group($self->extra_argv->[2]) unless $self->group;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->group 0 0 0
57 # $self->group is the same as user name if undefined
58  
59 0 $self->$orig(@_);
60 1 };
61  
62 # {{{ sub run
63 0 ▶sub run {
Subroutine run: not called CC=6 SCAR=37.4
Pod undocumented
64 0 my ($self) = shift;
65  
66 0 my $uid = next_uid();
67  
68 0 ▶ my $username = $self->extra_argv->[2] or die "no username specified";
Branch: 1 branch
BranchTrueFalseTotal
my $username = $self->extra_argv->[2] or die "no username specified" 0 0 0
69  
70 die "user $username already exists" if App::LDAP::LDIF::User->search(
71 base => config()->{nss_base_passwd}->[0],
72 0 ▶ scope => config()->{nss_base_passwd}->[1],
Branch: 1 branch
BranchTrueFalseTotal
if "App::LDAP::LDIF::User"->search("base", config()->{"nss_base_passwd"}[0], "scope", config()->{"nss_base_passwd"}[1], "filter", "uid=$username") 0 0 0
73 filter => "uid=$username",
74 );
75  
76 my $user = App::LDAP::LDIF::User->new(
77 0 ▶ base => $self->base // config()->{nss_base_passwd}->[0],
Condition0%0/3
$self->base // config()->{"nss_base_passwd"}[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->base // config()->{"nss_base_passwd"}[0]
$self->base config()->{"nss_base_passwd"}[0]
Truth table0%0/3
A // B
A= $self->baseB= config()->{"nss_base_passwd"}[0]
ABresult
1X1
011
000
78 uid => $username,
79 userPassword => encrypt(new_password()),
80 uidNumber => $uid->get_value("uidNumber"),
81 gidNumber => $self->gid_of( $self->group ),
82 sn => $self->surname,
83 mail => $self->mail,
84 );
85  
86 0 ▶ $user->loginShell ( $self->shell ) if $self->shell;
Branch: 1 branch
BranchTrueFalseTotal
if $self->shell 0 0 0
87 0 ▶ $user->homeDirectory ( $self->home ) if $self->home;
Branch: 1 branch
BranchTrueFalseTotal
if $self->home 0 0 0
88  
89 0 $user->save;
90  
91 0 $uid->replace(uidNumber => $uid->get_value("uidNumber")+1)->update(ldap());
92  
93 0 $user;
94 }
95 # }}}
96  
97 0 ▶sub next_uid {
Subroutine next_uid: not called CC=1 SCAR=6.9
Pod undocumented
98 ldap()->search(
99 base => config()->{base},
100 0 filter => "(objectClass=uidnext)",
101 )->entry(0);
102 }
103  
104 0 ▶sub gid_of {
Subroutine gid_of: not called CC=2 SCAR=10.9
Pod undocumented
105 0 my ($self, $groupname) = @_;
106  
107 1 ▶ use App::LDAP::LDIF::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 my $group = App::LDAP::LDIF::Group->search(
109 base => config()->{nss_base_group}->[0],
110 0 scope => config()->{nss_base_group}->[1],
111 filter => "cn=$groupname",
112 );
113  
114 0 ▶ return $group ? $group->gidNumber : $self->create_group($groupname)->gidNumber;
Branch: 1 branch
BranchTrueFalseTotal
$group ? : 0 0 0
115 }
116  
117 0 ▶sub create_group {
Subroutine create_group: not called CC=1 SCAR=1.2
Pod undocumented
118 0 my ($self, $groupname) = @_;
119  
120 1 ▶ use App::LDAP::Command::Add::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 0 local *ARGV = ['add', 'group', $groupname];
122  
123 0 App::LDAP::Command::Add::Group->new_with_options->run;
124 }
125  
126 1 __PACKAGE__->meta->make_immutable;
127 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 1 1;
130  
131 =pod
132  
133 =head1 NAME
134  
135 App::LDAP::Command::Add::User - handler for adding users
136  
137 =head1 SYNOPSIS
138  
139 # ldap add user hello --mail hello@example.com
140  
141 # ldap add user mark --mail mark@facebook.com \
142 --surname Zuckerberg \
143 --group founder \
144 --shell zsh \
145 --home /home/developer/mark
146  
147  
148 =cut
« LDAP/Command/Add/Group.pm ↑ Summary LDAP/Command/Import.pm »