File Coverage

blib/lib/Weblogic/UserAdmin.pm
Criterion Covered Total %
statement 30 113 26.5
branch 0 16 0.0
condition 3 7 42.8
subroutine 8 17 47.0
pod 7 7 100.0
total 48 160 30.0


line stmt bran cond sub pod time code
1             package Weblogic::UserAdmin;
2              
3 2     2   142878 use WWW::Mechanize;
  2         472250  
  2         88  
4 2     2   26 use strict;
  2         3  
  2         112  
5 2     2   9 use warnings;
  2         8  
  2         71  
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             BEGIN {
34 2     2   9     use Exporter ();
  2         4  
  2         34  
35 2     2   8     use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  2         4  
  2         291  
36 2     2   5     $VERSION = '1.03';
37 2         36     @ISA = qw(Exporter);
38             #Give a hoot don't pollute, do not export more than needed by default
39 2         3     @EXPORT = qw();
40 2         4     @EXPORT_OK = qw();
41 2         2262     %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             {
63 1     1 1 19     my ($class, $parameters) = @_;
64              
65 1   33     12     my $self = bless ({}, ref ($class) || $class);
66            
67            
68              
69            
70            
71            
72            
73            
74 1         11 $self->{console} = $parameters->{console};
75 1         3 $self->{port} = $parameters->{port};
76 1   50     6     $self->{username} = $parameters->{username} || die "Must Specify Username";
77 1   50     5     $self->{password} = $parameters->{password} || die "Must Specify Password";
78            
79 1         11 $self->{browser} = WWW::Mechanize->new();
80                 
81 1         17584     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             {
92            
93 0     0 1 0 my $self = shift;
94            
95             # login to console
96 0         0 $self->_loginConsole($self);
97            
98            
99             # Logged in? Jump to Users Page
100 0         0     my $servernum = substr( $self->{console},length($self->{console})-1, 1 );
101                 
102 0         0     $self->_jumpUserPage();
103                       
104                   
105 0         0     my $list = $self->{browser}->text();
106                 
107                    
108 0         0     $list =~ s/^.*Users\.\.\.Users//;
109 0         0     $list =~ s/ //g;
110                 
111 0         0     my @users=split /\,/, $list;
112            
113 0         0 @{$self->{users}} = @users;
  0         0  
114            
115 0         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             {
126 0     0 1 0 my ($self, $user) = @_;
127            
128            
129            
130 0 0       0 if( !defined $self->{users}) {
131 0         0 $self->users();
132             }
133              
134              
135            
136 0         0 foreach( @{$self->{users}}) {
  0         0  
137              
138 0 0       0 if( $_ eq $user ) {
139            
140 0         0 return -1;
141             }
142             }
143 0         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             sub user_add {
154 0     0 1 0 my $self = shift;
155 0         0 my $parm = shift;
156            
157 0         0 $self->{user} = $parm->{user};
158 0 0       0 if(!defined $parm->{user}) {
159 0         0 die ("Must specify user\n");
160             }
161 0 0       0 if(!defined $parm->{password}) {
162 0         0 die ("Must specify password\n");
163             }
164            
165             # login to console and jump to user page
166 0         0 $self->_loginConsole($self);
167 0         0 $self->_jumpUserPage($self);
168            
169             # fill in form and submit
170 0         0 $self->{browser}->form_number(1);
171 0         0     $self->{browser}->field("Name", $parm->{user});
172 0         0     $self->{browser}->field("Password", $parm->{password});
173 0         0     $self->{browser}->field("ConfirmPassword", $parm->{password});
174                 
175 0         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             sub group_list {
187            
188 0     0 1 0 my $self = shift;
189 0         0 my $parm = shift;
190            
191             # login to console and jump to user page
192 0         0 $self->_loginConsole($self);
193            
194 0         0 $self->_jumpGroupPage($parm->{group});
195            
196 0         0 my $page = $self->{browser}->text();
197 0         0 $page =~ s/.*emove\)//g;
198 0         0 $page =~ s/Add.*//g;
199              
200 0         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             sub user_add_group {
210            
211 0     0 1 0 my $self = shift;
212 0         0 my $parm = shift;
213            
214 0         0 $self->{user} = $parm->{user};
215 0 0       0 if(!defined $parm->{user}) {
216 0         0 die ("Must specify user\n");
217             }
218 0 0       0 if(!defined $parm->{group}) {
219 0         0 die ("Must specify group\n");
220             }
221            
222             # login to console and jump to user page
223 0         0 $self->_loginConsole($self);
224            
225 0         0 $self->_jumpGroupPage($parm->{group});
226            
227            
228             # fill in form and submit
229 0         0 $self->{browser}->form_number(1);
230                
231 0         0     $self->{browser}->field("AddUsers", $parm->{user});
232 0         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             sub user_del {
243 0     0 1 0 my $self = shift;
244 0         0 my $parm = shift;
245            
246 0         0 $self->{user} = $parm->{user};
247 0 0       0 if(!defined $parm->{user}) {
248 0         0 die ("Must specify user\n");
249             }
250              
251             # login to console and jump to user page
252 0 0       0 if(!$self->{loggedin}) {
253 0         0 print "---------\n";
254 0         0 $self->_loginConsole($self);
255             }
256 0         0 $self->_jumpUserPage($self);
257              
258            
259             # fill in form and submit
260 0         0 $self->{browser}->form_number(1);
261 0         0     $self->{browser}->field("DeleteUsers", $parm->{user});
262                 
263 0         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             sub _jumpUserPage {
276            
277 0     0   0 my $self = shift;
278            
279 0         0 $self->{browser}->get($self->{console} . ":" . $self->{port} .
280             "/console/actions/realm/ListRealmEntitiesAction?type=weblogic.management.configuration.User&realm=" .
281                  $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             sub _jumpGroupPage {
291            
292 0     0   0 my $self = shift;
293 0         0 my $group = shift;
294            
295            
296 0         0 $self->{browser}->get($self->{console} . ":" . $self->{port} .
297             "/console/actions/realm/EditRealmEntityAction?type=weblogic.management.configuration.Group&realm=" .
298                  $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             {
311 0     0   0 my $self = shift;
312            
313             # tell it to get the main page
314 0         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         0     $self->{browser}->form_number(1);
319 0         0     $self->{browser}->field("j_username", $self->{username});
320 0         0     $self->{browser}->field("j_password", $self->{password});
321                 
322 0         0     $self->{browser}->submit();
323                 
324                 
325                 
326 0         0     my $page=$self->{browser}->content();
327            
328 0         0     $page =~ s/\l\n//g;
329 0         0     $page =~ s/%253AName.*//;
330 0         0     $page =~ s/.*MBean%3D//;
331                 
332 0         0     $self->{environment}= $page;
333 0         0 $self->{loggedin} = -1;
334                 
335 0         0 return $page;
336             }
337              
338              
339              
340              
341             sub DESTROY {
342 1     1   743 my $self = shift;
343 1         40 $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             1;
378             # The preceding line will help the module return a true value
379              
380