Sling/Group.pm

filter: Statementstmt 57.1% 108 / 189 Branchbran 38.7% 24 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.0% 23 / 25 Podpod 69.2% 9 / 13 totaltotal 56.7% 164 / 289 CC 34 SCAR 33.3
CC
34
Cov
53%
run
61.9
add_from_file
49.3
add
3.9
CRAP 27.9
« Sling/Content.pm ↑ Summary Sling/Request.pm »
1 #!/usr/bin/perl -w
2  
3 package Apache::Sling::Group;
4  
5 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Apache::Sling;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Apache::Sling::GroupUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Apache::Sling::Print;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Apache::Sling::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 require Exporter;
18  
19 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our @EXPORT_OK = qw(command_line);
22  
23 1 our $VERSION = '0.27';
24  
25 #{{{sub new
26  
27 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
28 2 my ( $class, $authn, $verbose, $log ) = @_;
29 2 ▶ if ( !defined $authn ) { croak 'no authn provided!'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authn) 1 1 2
30 1 my $response;
31 1 ▶ $verbose = ( defined $verbose ? $verbose : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $verbose ? : 1 0 1
32 my $group = {
33 1 BaseURL => ${$authn}->{'BaseURL'},
34 Authn => $authn,
35 Message => q{},
36 Response => \$response,
37 Verbose => $verbose,
38 Log => $log
39 };
40 1 bless $group, $class;
41 1 return $group;
42 }
43  
44 #}}}
45  
46 #{{{sub set_results
47 1 ▶sub set_results {
Subroutine set_results: called CC=1 SCAR=0.0
Pod documented
48 1 my ( $group, $message, $response ) = @_;
49 1 $group->{'Message'} = $message;
50 1 $group->{'Response'} = $response;
51 1 return 1;
52 }
53  
54 #}}}
55  
56 #{{{sub add
57 1 ▶sub add {
Subroutine add: called CC=2 SCAR=13.6
Pod documented
58 1 my ( $group, $act_on_group, $properties ) = @_;
59 my $res = Apache::Sling::Request::request(
60 \$group,
61 Apache::Sling::GroupUtil::add_setup(
62 1 $group->{'BaseURL'}, $act_on_group, $properties
63 )
64 );
65 0 my $success = Apache::Sling::GroupUtil::add_eval($res);
66 0 my $message = "Group: \"$act_on_group\" ";
67 0 ▶ $message .= ( $success ? 'added!' : 'was not added!' );
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
68 0 $group->set_results( "$message", $res );
69 0 return $success;
70 }
71  
72 #}}}
73  
74 #{{{sub add_from_file
75 3 ▶sub add_from_file {
Subroutine add_from_file: called CC=14 SCAR=39.0
Pod documented
76 3 my ( $group, $file, $fork_id, $number_of_forks ) = @_;
77 3 ▶ $fork_id = defined $fork_id ? $fork_id : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $fork_id ? : 0 3 3
78 3 ▶ $number_of_forks = defined $number_of_forks ? $number_of_forks : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $number_of_forks ? : 0 3 3
79 3 my $csv = Text::CSV->new();
80 3 my $count = 0;
81 3 my $number_of_columns = 0;
82 3 my @column_headings;
83 3 ▶ if ( !defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 2 3
84 1 croak 'File to upload from not defined';
85 }
86 2 ▶ if ( open my ($input), '<', $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $input, '<', $file) { } 1 1 2
87 1 ▶ while (<$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $input)) 1 0 1
88 1 ▶ if ( $count++ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count++ == 0) { } 1 0 1
elsif ($fork_id == $count++ % $number_of_forks) { } 0 0 0
89  
90 # Parse file column headings first to determine field names:
91 1 ▶ if ( $csv->parse($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($_)) { } 1 0 1
92 1 @column_headings = $csv->fields();
93  
94 # First field must be group:
95 1 ▶ if ( $column_headings[0] !~ /^[Gg][Rr][Oo][Uu][Pp]$/msx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column_headings[0] =~ /^[Gg][Rr][Oo][Uu][Pp]$/msx) 1 0 1
96 1 croak 'First CSV column must be the group ID, '
97 . 'column heading must be "group". '
98 . 'Found: "'
99 . $column_headings[0] . "\".\n";
100 }
101 0 $number_of_columns = @column_headings;
102 }
103 else {
104 0 croak 'CSV broken, failed to parse line: '
105 . $csv->error_input;
106 }
107 }
108 elsif ( $fork_id == ( $count++ % $number_of_forks ) ) {
109 0 my @properties;
110 0 ▶ if ( $csv->parse($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($_)) { } 0 0 0
111 0 my @columns = $csv->fields();
112 0 my $columns_size = @columns;
113  
114 # Check row has same number of columns as there were column headings:
115 0 ▶ if ( $columns_size != $number_of_columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($columns_size != $number_of_columns) 0 0 0
116 0 croak
117 "Found \"$columns_size\" columns. There should have been \"$number_of_columns\".\n"
118 . "Row contents was: $_";
119 }
120 0 my $id = $columns[0];
121 0 ▶ for ( my $i = 1 ; $i < $number_of_columns ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $number_of_columns) 0 0 0
122 0 my $heading = $column_headings[$i];
123 0 my $data = $columns[$i];
124 0 my $value = "$heading=$data";
125 0 push @properties, $value;
126 }
127 0 $group->add( $id, \@properties );
128 0 Apache::Sling::Print::print_result($group);
129 }
130 else {
131 0 croak 'CSV broken, failed to parse line: '
132 . $csv->error_input;
133 }
134 }
135 }
136 0 ▶ close $input or croak q{Problem closing input!};
Branch: 1 branch
BranchTrueFalseTotal
close $input or croak("Problem closing input!") 0 0 0
137 }
138 else {
139 1 croak "Problem opening file: '$file'";
140 }
141 0 return 1;
142 }
143  
144 #}}}
145  
146 #{{{sub check_exists
147 1 ▶sub check_exists {
Subroutine check_exists: called CC=2 SCAR=13.6
Pod documented
148 1 my ( $group, $act_on_group ) = @_;
149 my $res = Apache::Sling::Request::request(
150 \$group,
151 Apache::Sling::GroupUtil::exists_setup(
152 1 $group->{'BaseURL'}, $act_on_group
153 )
154 );
155 0 my $success = Apache::Sling::GroupUtil::exists_eval($res);
156 0 my $message = "Group \"$act_on_group\" ";
157 0 ▶ $message .= ( $success ? 'exists!' : 'does not exist!' );
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
158 0 $group->set_results( "$message", $res );
159 0 return $success;
160 }
161  
162 #}}}
163  
164 #{{{ sub command_line
165 0 ▶sub command_line {
Subroutine command_line: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my ( $group, @ARGV ) = @_;
167 0 my $sling = Apache::Sling->new;
168 0 my $config = $group->config( $sling, @ARGV );
169 0 return $group->run( $sling, $config );
170 }
171  
172 #}}}
173  
174 #{{{sub config
175  
176 1 ▶sub config {
Subroutine config: called CC=2 SCAR=7.0
Pod documented
177 1 my ( $group, $sling, @ARGV ) = @_;
178 1 my $group_config = $group->config_hash( $sling, @ARGV );
179  
180 1 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions($group_config, "auth=s", "help|?", "log|L=s", "man|M", "pass|p=s", "threads|t=s", "url|U=s", "user|u=s", "verbose|v+", "add|a=s", "additions|A=s", "delete|d=s", "exists|e=s", "property|P=s", "view|V=s") or $group->help 1 0 1
181 $group_config, 'auth=s', 'help|?', 'log|L=s',
182 'man|M', 'pass|p=s', 'threads|t=s', 'url|U=s',
183 'user|u=s', 'verbose|v+', 'add|a=s', 'additions|A=s',
184 'delete|d=s', 'exists|e=s', 'property|P=s', 'view|V=s'
185 ) or $group->help();
186  
187 1 return $group_config;
188 }
189  
190 #}}}
191  
192 #{{{sub config_hash
193  
194 1 ▶sub config_hash {
Subroutine config_hash: called CC=1 SCAR=0.0
Pod undocumented
195 1 my ( $group, $sling, @ARGV ) = @_;
196 1 my $additions;
197 my $add;
198 1 my $delete;
199 1 my $exists;
200 1 my @property;
201 1 my $view;
202  
203 my %group_config = (
204 'auth' => \$sling->{'Auth'},
205 'help' => \$sling->{'Help'},
206 'log' => \$sling->{'Log'},
207 'man' => \$sling->{'Man'},
208 'pass' => \$sling->{'Pass'},
209 'threads' => \$sling->{'Threads'},
210 'url' => \$sling->{'URL'},
211 'user' => \$sling->{'User'},
212 1 'verbose' => \$sling->{'Verbose'},
213 'add' => \$add,
214 'additions' => \$additions,
215 'delete' => \$delete,
216 'exists' => \$exists,
217 'property' => \@property,
218 'view' => \$view
219 );
220  
221 1 return \%group_config;
222 }
223  
224 #}}}
225  
226 #{{{sub del
227 1 ▶sub del {
Subroutine del: called CC=2 SCAR=13.6
Pod documented
228 1 my ( $group, $act_on_group ) = @_;
229 my $res = Apache::Sling::Request::request(
230 \$group,
231 Apache::Sling::GroupUtil::delete_setup(
232 1 $group->{'BaseURL'}, $act_on_group
233 )
234 );
235 0 my $success = Apache::Sling::GroupUtil::delete_eval($res);
236 0 my $message = "Group: \"$act_on_group\" ";
237 0 ▶ $message .= ( $success ? 'deleted!' : 'was not deleted!' );
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
238 0 $group->set_results( "$message", $res );
239 0 return $success;
240 }
241  
242 #}}}
243  
244 #{{{ sub help
245 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod undocumented
246  
247 1 print <<"EOF";
248 Usage: perl $0 [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
249 The following options are accepted:
250  
251 --additions or -A (file) - file containing list of groups to be added.
252 --add or -a (actOnGroup) - add specified group.
253 --auth (type) - Specify auth type. If ommitted, default is used.
254 --delete or -d (actOnGroup) - delete specified group.
255 --exists or -e (actOnGroup) - check whether specified group exists.
256 --help or -? - view the script synopsis and options.
257 --log or -L (log) - Log script output to specified log file.
258 --man or -M - view the full script documentation.
259 --pass or -p (password) - Password of user performing actions.
260 --property or -P (property=value) - Specify property to set on group.
261 --threads or -t (threads) - Used with -A, defines number of parallel
262 processes to have running through file.
263 --url or -U (URL) - URL for system being tested against.
264 --user or -u (username) - Name of user to perform any actions as.
265 --verbose or -v or -vv or -vvv - Increase verbosity of output.
266 --view or -V (actOnGroup) - view details for specified group in json format.
267  
268 Options may be merged together. -- stops processing of options.
269 Space is not required between options and their arguments.
270 For full details run: perl $0 --man
271 EOF
272  
273 1 return 1;
274 }
275  
276 #}}}
277  
278 #{{{ sub man
279 0 ▶sub man {
Subroutine man: not called CC=1 SCAR=6.9
Pod undocumented
280  
281 0 my ($group) = @_;
282  
283 0 print <<'EOF';
284 group perl script. Provides a means of managing groups in sling from the
285 command line. The script also acts as a reference implementation for the Group
286 perl library.
287  
288 EOF
289  
290 0 $group->help();
291  
292 0 print <<"EOF";
293 Example Usage
294  
295 * Authenticate and add a group with id g-test:
296  
297 perl group.pl -U http://localhost:8080 -u admin -p admin -a g-test
298  
299 * Authenticate and check whether group with id g-test exists:
300  
301 perl group.pl -U http://localhost:8080 -u admin -p admin -a g-test
302  
303 * Authenticate and view details for group with id g-test:
304  
305 perl group.pl -U http://localhost:8080 -u admin -p admin -V g-test
306  
307 * Authenticate and delete group with id g-test:
308  
309 perl group.pl -U http://localhost:8080 -u admin -p admin -d g-test
310  
311 * Authenticate and add a group with id g-test and property p1=v1:
312  
313 perl group.pl -U http://localhost:8080 -u admin -p admin -a g-test -P p1=v1
314  
315 EOF
316  
317 0 return 1;
318 }
319  
320 #}}}
321  
322 #{{{sub run
323 2 ▶sub run {
Subroutine run: called CC=14 SCAR=41.2
Pod documented
324 2 my ( $group, $sling, $config ) = @_;
325 2 ▶ if ( !defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 1 1 2
326 1 croak 'No group config supplied!';
327 }
328 1 $sling->check_forks;
329 my $authn =
330 defined $sling->{'Authn'}
331 1 ▶ ? ${ $sling->{'Authn'} }
Branch: 1 branch
BranchTrueFalseTotal
defined $sling->{'Authn'} ? : 1 0 1
332 : new Apache::Sling::Authn( \$sling );
333  
334 1 my $success = 1;
335  
336 0 ▶ if ( $sling->{'Help'} ) { $group->help(); }
Branch: 3 branches
BranchTrueFalseTotal
if ($sling->{'Help'}) { } 0 1 1
elsif ($sling->{'Man'}) { } 0 1 1
elsif (defined ${$$config{'additions'};}) { } 0 1 1
337 0 elsif ( $sling->{'Man'} ) { $group->man(); }
338 1 elsif ( defined ${ $config->{'additions'} } ) {
339 my $message =
340 0 "Adding groups from file \"" . ${ $config->{'additions'} } . "\":\n";
341 0 Apache::Sling::Print::print_with_lock( "$message", $sling->{'Log'} );
342 0 my @childs = ();
343 0 for my $i ( 0 .. $sling->{'Threads'} ) {
344 0 my $pid = fork;
345 0 ▶ if ($pid) { push @childs, $pid; } # parent
Branch: 2 branches
BranchTrueFalseTotal
if ($pid) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
346 elsif ( $pid == 0 ) { # child
347 # Create a new separate user agent per fork in order to
348 # ensure cookie stores are separate, then log the user in:
349 0 $authn->{'LWP'} = $authn->user_agent( $sling->{'Referer'} );
350 0 $authn->login_user();
351 my $group =
352 new Apache::Sling::Group( \$authn, $sling->{'Verbose'},
353 0 $sling->{'Log'} );
354 0 $group->add_from_file( ${ $config->{'additions'} },
355 0 $i, $sling->{'Threads'} );
356 0 exit 0;
357 }
358 else {
359 0 croak "Could not fork $i!";
360 }
361 }
362 0 foreach (@childs) { waitpid $_, 0; }
363 }
364 else {
365 1 $authn->login_user();
366 1 ▶ if ( defined ${ $config->{'exists'} } ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined ${$$config{'exists'};}) { } 0 1 1
elsif (defined ${$$config{'add'};}) { } 0 1 1
elsif (defined ${$$config{'delete'};}) { } 0 1 1
elsif (defined ${$$config{'view'};}) { } 0 1 1
367 $group =
368 new Apache::Sling::Group( \$authn, $sling->{'Verbose'},
369 0 $sling->{'Log'} );
370 0 $success = $group->check_exists( ${ $config->{'exists'} } );
371 }
372 1 elsif ( defined ${ $config->{'add'} } ) {
373 $group =
374 new Apache::Sling::Group( \$authn, $sling->{'Verbose'},
375 0 $sling->{'Log'} );
376 $success =
377 0 $group->add( ${ $config->{'add'} }, $config->{'property'} );
378 }
379 1 elsif ( defined ${ $config->{'delete'} } ) {
380 $group =
381 new Apache::Sling::Group( \$authn, $sling->{'Verbose'},
382 0 $sling->{'Log'} );
383 0 $success = $group->del( ${ $config->{'delete'} } );
384 }
385 1 elsif ( defined ${ $config->{'view'} } ) {
386 $group =
387 new Apache::Sling::Group( \$authn, $sling->{'Verbose'},
388 0 $sling->{'Log'} );
389 0 $success = $group->view( ${ $config->{'view'} } );
390 }
391 else {
392 1 $group->help();
393 1 return 1;
394 }
395 0 Apache::Sling::Print::print_result($group);
396 }
397 0 return $success;
398 }
399  
400 #}}}
401  
402 #{{{sub view
403 1 ▶sub view {
Subroutine view: called CC=2 SCAR=13.6
Pod documented
404 1 my ( $group, $act_on_group ) = @_;
405 my $res = Apache::Sling::Request::request(
406 \$group,
407 Apache::Sling::GroupUtil::view_setup(
408 1 $group->{'BaseURL'}, $act_on_group
409 )
410 );
411 0 my $success = Apache::Sling::GroupUtil::view_eval($res);
412 my $message = (
413 $success
414 0 ▶ ? ${$res}->content
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
415 : "Problem viewing group: \"$act_on_group\""
416 );
417 0 $group->set_results( "$message", $res );
418 0 return $success;
419 }
420  
421 #}}}
422  
423 1 1;
424  
425 __END__
« Sling/Content.pm ↑ Summary Sling/Request.pm »