Sling/GroupMember.pm

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