blib/lib/Weblogic/UserAdmin.pm

filter: Statementstmt 27.1% 31 / 114 Branchbran 0.0% 0 / 16 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 47.0% 8 / 17 Podpod 100% 7 / 7 totaltotal 29.7% 49 / 165 CC 13 SCAR 20.4
CC
13
Cov
25%
user_exist
20.0
user_add
12.0
user_add_group
12.0
CRAP 7.7
↑ Summary
1 package Weblogic::UserAdmin;
2  
3 2 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 =head1 NAME
9  
10 Weblogic::UserAdmin - Administration Functions For Weblogic 8.1 Automated
11  
12 =head1 SYNOPSIS
13  
14 use Weblogic::UserAdmin;
15
16 my $Weblogic = Weblogic::UserAdmin->new({
17 console=>"http://$server",
18 port => $port,
19 username => "system",
20 password => "leper",
21 });
22
23 if($Weblogic->user_exist($user)) {
24 print "User Already Exists\n";
25 exit 1;
26 };
27  
28 $Weblogic->user_add({user=>$user, password=>$password});
29 =cut
30  
31  
32  
33 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 $VERSION = '1.03';
37 2 @ISA = qw(Exporter);
38 #Give a hoot don't pollute, do not export more than needed by default
39 2 @EXPORT = qw();
40 2 @EXPORT_OK = qw();
41 2 %EXPORT_TAGS = ();
42 }
43  
44  
45  
46  
47 =head1 DESCRIPTION
48
49  
50 =head2 my $Weblogic = Weblogic::UserAdmin->new({
51 console=>"http://$server",
52 port => $port,
53 username => "system",
54 password => "leper",
55 });
56  
57 Create and login to server object specifying host port system username
58 and password. Returns Weblogic::UserAdmin object.
59 =cut
60  
61 sub new
62 1 ▶{
Subroutine new: called CC=4 SCAR=14.6
Pod documented
63 1 my ($class, $parameters) = @_;
64  
65 1 ▶ my $self = bless ({}, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
66
67
68  
69
70
71
72
73
74 1 $self->{console} = $parameters->{console};
75 1 $self->{port} = $parameters->{port};
76 1 ▶ $self->{username} = $parameters->{username} || die "Must Specify Username";
Condition50%1/2
$parameters->{'username'} || die('Must Specify Username')
l!l
10
MC/DC0%0/1
$parameters->{'username'} || die('Must Specify Username')
$parameters->{'username'}
Truth table50%1/2
A
A= $parameters->{'username'}
Aresult
11
00
77 1 ▶ $self->{password} = $parameters->{password} || die "Must Specify Password";
Condition50%1/2
$parameters->{'password'} || die('Must Specify Password')
l!l
10
MC/DC0%0/1
$parameters->{'password'} || die('Must Specify Password')
$parameters->{'password'}
Truth table50%1/2
A
A= $parameters->{'password'}
Aresult
11
00
78
79 1 $self->{browser} = WWW::Mechanize->new();
80
81 1 return $self;
82 }
83  
84  
85 =head2 users
86
87 Return an array of all usernames from the server.
88 =cut
89  
90 sub users
91 0 ▶{
Subroutine users: not called CC=1 SCAR=6.9
Pod documented
92
93 0 my $self = shift;
94
95 # login to console
96 0 $self->_loginConsole($self);
97
98
99 # Logged in? Jump to Users Page
100 0 my $servernum = substr( $self->{console},length($self->{console})-1, 1 );
101
102 0 $self->_jumpUserPage();
103
104
105 0 my $list = $self->{browser}->text();
106
107
108 0 $list =~ s/^.*Users\.\.\.Users//;
109 0 $list =~ s/ //g;
110
111 0 my @users=split /\,/, $list;
112
113 0 @{$self->{users}} = @users;
114
115 0 return @users;
116
117 }
118  
119 =head2 user_exist('username')
120  
121 Checks if a user exists
122  
123 =cut
124 sub user_exist
125 0 ▶{
Subroutine user_exist: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($self, $user) = @_;
127
128
129
130 0 ▶ if( !defined $self->{users}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"users"}) 0 0 0
131 0 $self->users();
132 }
133  
134  
135
136 0 foreach( @{$self->{users}}) {
137  
138 0 ▶ if( $_ eq $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $user) 0 0 0
139
140 0 return -1;
141 }
142 }
143 0 return 0;
144
145 }
146  
147 =head2 user_add({user=>$user, password=>$userpassword});
148  
149 Add user specifying username and password.
150  
151 =cut
152  
153 0 ▶sub user_add {
Subroutine user_add: not called CC=3 SCAR=24.8
Pod documented
154 0 my $self = shift;
155 0 my $parm = shift;
156
157 0 $self->{user} = $parm->{user};
158 0 ▶ if(!defined $parm->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parm->{"user"}) 0 0 0
159 0 die ("Must specify user\n");
160 }
161 0 ▶ if(!defined $parm->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parm->{"password"}) 0 0 0
162 0 die ("Must specify password\n");
163 }
164
165 # login to console and jump to user page
166 0 $self->_loginConsole($self);
167 0 $self->_jumpUserPage($self);
168
169 # fill in form and submit
170 0 $self->{browser}->form_number(1);
171 0 $self->{browser}->field("Name", $parm->{user});
172 0 $self->{browser}->field("Password", $parm->{password});
173 0 $self->{browser}->field("ConfirmPassword", $parm->{password});
174
175 0 $self->{browser}->click("create");
176
177
178 }
179  
180 =head2 group_list
181  
182 Lists all groups - returned as an array
183  
184 =cut
185  
186 0 ▶sub group_list {
Subroutine group_list: not called CC=1 SCAR=6.9
Pod documented
187
188 0 my $self = shift;
189 0 my $parm = shift;
190
191 # login to console and jump to user page
192 0 $self->_loginConsole($self);
193
194 0 $self->_jumpGroupPage($parm->{group});
195
196 0 my $page = $self->{browser}->text();
197 0 $page =~ s/.*emove\)//g;
198 0 $page =~ s/Add.*//g;
199  
200 0 return split /\s/, $page
201 }
202  
203 =head2 user_add_group({user=>$user, group=>$groupname})
204
205 Add the specified user to the specified group.
206  
207 =cut
208  
209 0 ▶sub user_add_group {
Subroutine user_add_group: not called CC=3 SCAR=24.8
Pod documented
210
211 0 my $self = shift;
212 0 my $parm = shift;
213
214 0 $self->{user} = $parm->{user};
215 0 ▶ if(!defined $parm->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parm->{"user"}) 0 0 0
216 0 die ("Must specify user\n");
217 }
218 0 ▶ if(!defined $parm->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parm->{"group"}) 0 0 0
219 0 die ("Must specify group\n");
220 }
221
222 # login to console and jump to user page
223 0 $self->_loginConsole($self);
224
225 0 $self->_jumpGroupPage($parm->{group});
226
227
228 # fill in form and submit
229 0 $self->{browser}->form_number(1);
230
231 0 $self->{browser}->field("AddUsers", $parm->{user});
232 0 $self->{browser}->submit();
233
234 }
235  
236 =head2 user_del({user=>$user})
237
238 Delete user. USer is automagically removed from group.
239 =cut
240
241  
242 0 ▶sub user_del {
Subroutine user_del: not called CC=3 SCAR=24.8
Pod documented
243 0 my $self = shift;
244 0 my $parm = shift;
245
246 0 $self->{user} = $parm->{user};
247 0 ▶ if(!defined $parm->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $parm->{"user"}) 0 0 0
248 0 die ("Must specify user\n");
249 }
250  
251 # login to console and jump to user page
252 0 ▶ if(!$self->{loggedin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"loggedin"}) 0 0 0
253 0 print "---------\n";
254 0 $self->_loginConsole($self);
255 }
256 0 $self->_jumpUserPage($self);
257  
258
259 # fill in form and submit
260 0 $self->{browser}->form_number(1);
261 0 $self->{browser}->field("DeleteUsers", $parm->{user});
262
263 0 $self->{browser}->click("delete");
264
265
266 }
267  
268  
269  
270  
271 ##
272 ## Jump to the page of users
273 ## used internally
274 ##
275 0 ▶sub _jumpUserPage {
Subroutine _jumpUserPage: not called CC=1 SCAR=6.9
276
277 0 my $self = shift;
278
279 $self->{browser}->get($self->{console} . ":" . $self->{port} .
280 "/console/actions/realm/ListRealmEntitiesAction?type=weblogic.management.configuration.User&realm=" .
281 0 $self->{environment} . "%3AName%3Dwl_default_realm%2CType%3DRealm");
282
283
284 }
285  
286 ##
287 ## Jump to the page of groups
288 ## used internally
289 ##
290 0 ▶sub _jumpGroupPage {
Subroutine _jumpGroupPage: not called CC=1 SCAR=6.9
291
292 0 my $self = shift;
293 0 my $group = shift;
294
295
296 $self->{browser}->get($self->{console} . ":" . $self->{port} .
297 "/console/actions/realm/EditRealmEntityAction?type=weblogic.management.configuration.Group&realm=" .
298 0 $self->{environment} . "%3AName%3Dwl_default_realm%2CType%3DRealm&name=" . $group);
299
300
301
302 }
303  
304  
305 ##
306 ## Login to the console server
307 ## used internally
308 ##
309 sub _loginConsole
310 0 ▶{
Subroutine _loginConsole: not called CC=1 SCAR=6.9
311 0 my $self = shift;
312
313 # tell it to get the main page
314 0 $self->{browser}->get($self->{console} .":".$self->{port}. "/console/login/LoginForm.jsp");
315  
316 # okay, fill in the box with the name of the
317 # module we want to look up
318 0 $self->{browser}->form_number(1);
319 0 $self->{browser}->field("j_username", $self->{username});
320 0 $self->{browser}->field("j_password", $self->{password});
321
322 0 $self->{browser}->submit();
323
324
325
326 0 my $page=$self->{browser}->content();
327
328 0 $page =~ s/\l\n//g;
329 0 $page =~ s/%253AName.*//;
330 0 $page =~ s/.*MBean%3D//;
331
332 0 $self->{environment}= $page;
333 0 $self->{loggedin} = -1;
334
335 0 return $page;
336 }
337  
338  
339  
340  
341 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
342 1 my $self = shift;
343 1 $self->{browser} = undef;
344 }
345  
346  
347  
348  
349  
350  
351
352  
353 =head1 AUTHOR
354  
355 David Peters
356 CPAN ID: DAVIDP
357 David.Peters@EssentialEnergy.com.au
358  
359 =head1 COPYRIGHT
360  
361 This program is free software; you can redistribute
362 it and/or modify it under the same terms as Perl itself.
363  
364 The full text of the license can be found in the
365 LICENSE file included with this module.
366  
367  
368 =head1 SEE ALSO
369  
370 perl(1).
371  
372 =cut
373  
374 #################### main pod documentation end ###################
375  
376  
377 2 1;
378 # The preceding line will help the module return a true value
379  
↑ Summary