line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Ado::Command::adduser; |
2
|
1
|
|
|
1
|
|
865
|
use Mojo::Base 'Ado::Command'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
11
|
|
3
|
1
|
|
|
1
|
|
199
|
use Getopt::Long qw(GetOptionsFromArray); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
4
|
1
|
|
|
1
|
|
788
|
use Time::Piece qw(); |
|
1
|
|
|
|
|
7330
|
|
|
1
|
|
|
|
|
37
|
|
5
|
1
|
|
|
1
|
|
8
|
use Time::Seconds; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
898
|
|
6
|
|
|
|
|
|
|
has description => 'Add and edit users'; |
7
|
|
|
|
|
|
|
has usage => sub { shift->extract_usage }; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
#define some defaults |
10
|
|
|
|
|
|
|
has args => sub { |
11
|
|
|
|
|
|
|
my $t = time; |
12
|
|
|
|
|
|
|
{ changed_by => 1, |
13
|
|
|
|
|
|
|
created_by => 1, |
14
|
|
|
|
|
|
|
disabled => 1, |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#TODO: add funcionality for notifying users on account expiration |
17
|
|
|
|
|
|
|
#TODO: document this |
18
|
|
|
|
|
|
|
stop_date => $t + ONE_YEAR, #account expires after one year |
19
|
|
|
|
|
|
|
start_date => $t, |
20
|
|
|
|
|
|
|
login_password => rand($t) . $$ . {} . $t, |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub init { |
25
|
0
|
|
|
0
|
1
|
|
my ($self, @args) = @_; |
26
|
0
|
|
|
|
|
|
$self->SUPER::init(); |
27
|
0
|
0
|
|
|
|
|
unless (@args) { Carp::croak($self->usage); } |
|
0
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $args = $self->args; |
29
|
|
|
|
|
|
|
my $ret = GetOptionsFromArray( |
30
|
|
|
|
|
|
|
\@args, |
31
|
|
|
|
|
|
|
'u|login_name=s' => \$args->{login_name}, |
32
|
|
|
|
|
|
|
'p|login_password=s' => \$args->{login_password}, |
33
|
|
|
|
|
|
|
'e|email=s' => \$args->{email}, |
34
|
|
|
|
|
|
|
'g|ingroup=s' => \$args->{ingroup}, |
35
|
|
|
|
|
|
|
'd|disabled:i' => \$args->{disabled}, |
36
|
|
|
|
|
|
|
'f|first_name=s' => \$args->{first_name}, |
37
|
|
|
|
|
|
|
'l|last_name=s' => \$args->{last_name}, |
38
|
|
|
|
|
|
|
'start_date=s' => sub { |
39
|
|
|
|
|
|
|
$args->{start_date} = |
40
|
0
|
0
|
|
0
|
|
|
$_[1] ? Time::Piece->strptime('%Y-%m-%d', $_[1])->epoch : time; |
41
|
|
|
|
|
|
|
}, |
42
|
0
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Assume an UTF-8 terminal. TODO: make this more clever |
45
|
|
|
|
|
|
|
utf8::decode($args->{login_name}) |
46
|
0
|
0
|
0
|
|
|
|
if ($args->{login_name} && !utf8::is_utf8($args->{login_name})); |
47
|
|
|
|
|
|
|
utf8::decode($args->{first_name}) |
48
|
0
|
0
|
0
|
|
|
|
if ($args->{first_name} && !utf8::is_utf8($args->{first_name})); |
49
|
|
|
|
|
|
|
utf8::decode($args->{last_name}) |
50
|
0
|
0
|
0
|
|
|
|
if ($args->{last_name} && !utf8::is_utf8($args->{last_name})); |
51
|
0
|
|
|
|
|
|
$args->{login_password} = Mojo::Util::sha1_hex($args->{login_name} . $args->{login_password}); |
52
|
0
|
0
|
|
|
|
|
unless ($args->{ingroup}) { |
53
|
|
|
|
|
|
|
say($self->usage) |
54
|
|
|
|
|
|
|
unless ($args->{first_name} |
55
|
|
|
|
|
|
|
and $args->{last_name} |
56
|
|
|
|
|
|
|
and $args->{login_name} |
57
|
0
|
0
|
0
|
|
|
|
and $args->{email}); |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
$self->app->log->debug('$self->args: ' . $self->app->dumper($self->args)); |
60
|
0
|
|
|
|
|
|
return $ret; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#default action |
65
|
|
|
|
|
|
|
sub adduser { |
66
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
67
|
0
|
|
|
|
|
|
my $args = $self->args; |
68
|
0
|
|
|
|
|
|
my ($group, $user, $ingroup); |
69
|
0
|
0
|
|
|
|
|
if (($group = Ado::Model::Groups->by_name($args->{login_name}))->id) { |
70
|
0
|
|
|
|
|
|
$self->app->log->debug('$group:', $self->app->dumper($group)); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#if we have such group, we have the user or we do not want to give a user |
73
|
|
|
|
|
|
|
#the privileges of a shared group |
74
|
0
|
|
|
|
|
|
say "'$args->{login_name}' is already taken!"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
0
|
0
|
|
|
|
|
$user = Ado::Model::Users->add($args) unless $group->id; |
78
|
0
|
0
|
|
|
|
|
return unless $user; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
0
|
|
|
|
|
if ($user) { |
81
|
0
|
|
|
|
|
|
say "User '$args->{login_name}' was created with primary group '$args->{login_name}'."; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
else { |
84
|
0
|
|
|
|
|
|
$user = Ado::Model::Users->by_login_name($args->{login_name}); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
return unless $args->{ingroup}; |
88
|
0
|
0
|
|
|
|
|
if (not $user->ingroup($args->{ingroup})) { |
89
|
0
|
0
|
|
|
|
|
if ($ingroup = $user->add_to_group($args)) { |
90
|
0
|
|
|
|
|
|
say "User '$args->{login_name}' was added to group '$args->{ingroup}'."; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
|
say "User '$args->{login_name}' is already in group '$args->{ingroup}'."; |
95
|
|
|
|
|
|
|
} |
96
|
0
|
|
|
|
|
|
return 1; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=pod |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=encoding utf8 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 NAME |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Ado::Command::adduser - adduser command |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 SYNOPSIS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
USAGE |
113
|
|
|
|
|
|
|
# On the command line |
114
|
|
|
|
|
|
|
# Minimal required options to add a user |
115
|
|
|
|
|
|
|
ado adduser --login_name USERNAME --email user\@example.com \ |
116
|
|
|
|
|
|
|
--first_name John --last_name Smith |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
# Add a user to an additional group |
119
|
|
|
|
|
|
|
ado adduser --login_name USERNAME --ingroup GROUPNAME |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# Change password / disable a user |
122
|
|
|
|
|
|
|
ado adduser --login_name USERNAME --login_password N3W$36RE7P1$5W |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Disable a user |
125
|
|
|
|
|
|
|
ado adduser --login_name USERNAME --disabled |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# Programatically |
128
|
|
|
|
|
|
|
use Ado::Command::adduser; |
129
|
|
|
|
|
|
|
Ado::Command::adduser->run('--login_name'=>'test1',...); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 DESCRIPTION |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L adds a user to an L application. |
135
|
|
|
|
|
|
|
It is a facade for L. |
136
|
|
|
|
|
|
|
This is a core L command, that means it is always available and its code |
137
|
|
|
|
|
|
|
a good example for learning to build new L commands, you're welcome to fork it. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L inherits all attributes from |
142
|
|
|
|
|
|
|
L and implements the following new ones. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 args |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
$self->args(login_name=>'peter','ingroup'=>'facebook'); |
147
|
|
|
|
|
|
|
my $args = $self->args; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Default arguments for creating a user. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 description |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $description = $a->description; |
155
|
|
|
|
|
|
|
$a = $a->description('Foo!'); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Short description of this command, used for the command list. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head2 usage |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my $usage = $a->usage; |
162
|
|
|
|
|
|
|
$a = $a->usage('Foo!'); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Usage information for this command, used for the help screen. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 OPTIONS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
On the commandline C accepts the following options: |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
'u|login_name=s' #username (mandatory) |
171
|
|
|
|
|
|
|
'p|login_password=s' #the user password (optional, random is generated) |
172
|
|
|
|
|
|
|
'e|email=s' #user email (mandatory) |
173
|
|
|
|
|
|
|
'g|ingroup=s' #existing users can be added to other groups too |
174
|
|
|
|
|
|
|
'd|disabled:i' #is user disabled? (1 by default) |
175
|
|
|
|
|
|
|
'f|first_name=s' #user's first name (mandatory) |
176
|
|
|
|
|
|
|
'l|last_name=s' #user's last name (mandatory) |
177
|
|
|
|
|
|
|
'start_date=s' #format: %Y-%m-%d (optional, today by default) |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 METHODS |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
L inherits all methods from |
182
|
|
|
|
|
|
|
L and implements the following new ones. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 init |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Calls the default parent L and parses the arguments |
187
|
|
|
|
|
|
|
passed on the command-line. Returns true on success. |
188
|
|
|
|
|
|
|
Croaks with L message on failure. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 adduser |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
The default and only action this command implements. |
194
|
|
|
|
|
|
|
Makes logical checks for existing user and group and calls |
195
|
|
|
|
|
|
|
L and L |
196
|
|
|
|
|
|
|
depending on parsed arguments. |
197
|
|
|
|
|
|
|
See L. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 SEE ALSO |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
L, |
202
|
|
|
|
|
|
|
L L, L, |
203
|
|
|
|
|
|
|
L, L. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=cut |
206
|
|
|
|
|
|
|
|