Apache-Sling
view release on metacpan or search on metacpan
lib/Apache/Sling/GroupMember.pm view on Meta::CPAN
#{{{sub new
sub new {
my ( $class, $authn, $verbose, $log ) = @_;
if ( !defined $authn ) { croak 'no authn provided!'; }
my $response;
$verbose = ( defined $verbose ? $verbose : 0 );
my $group = {
BaseURL => ${$authn}->{'BaseURL'},
Authn => $authn,
Message => q{},
Response => \$response,
Verbose => $verbose,
Log => $log
};
bless $group, $class;
return $group;
}
#}}}
#{{{sub set_results
sub set_results {
my ( $group, $message, $response ) = @_;
$group->{'Message'} = $message;
$group->{'Response'} = $response;
return 1;
}
#}}}
#{{{sub add
sub add {
my ( $group, $act_on_group, $add_member ) = @_;
my $res = Apache::Sling::Request::request(
\$group,
Apache::Sling::GroupMemberUtil::add_setup(
$group->{'BaseURL'}, $act_on_group, $add_member
)
);
my $success = Apache::Sling::GroupMemberUtil::add_eval($res);
my $message = "\"$add_member\" ";
$message .= ( $success ? 'added' : 'was not added' );
$message .= " to group \"$act_on_group\"!";
$group->set_results( "$message", $res );
return $success;
}
#}}}
#{{{sub add_from_file
sub add_from_file {
my ( $group, $file, $fork_id, $number_of_forks ) = @_;
$fork_id = defined $fork_id ? $fork_id : 0;
$number_of_forks = defined $number_of_forks ? $number_of_forks : 1;
my $csv = Text::CSV->new();
my $count = 0;
my $number_of_columns = 0;
my @column_headings;
if ( !defined $file ) {
croak 'File to upload from not defined';
}
if ( open my ($input), '<', $file ) {
while (<$input>) {
if ( $count++ == 0 ) {
# Parse file column headings first to determine field names:
if ( $csv->parse($_) ) {
@column_headings = $csv->fields();
# First field must be group:
if ( $column_headings[0] !~ /^[Gg][Rr][Oo][Uu][Pp]$/msx ) {
croak 'First CSV column must be the group ID, '
. 'column heading must be "group". '
. 'Found: "'
. $column_headings[0] . "\".\n";
}
# Second field must be user:
if ( $column_headings[1] !~ /^[Uu][Ss][Ee][Rr]$/msx ) {
croak 'Second CSV column must be the user ID, '
. 'column heading must be "user". '
. 'Found: "'
. $column_headings[1] . "\".\n";
}
$number_of_columns = @column_headings;
}
else {
croak 'CSV broken, failed to parse line: '
. $csv->error_input;
}
}
elsif ( $fork_id == ( $count++ % $number_of_forks ) ) {
if ( $csv->parse($_) ) {
my @columns = $csv->fields();
my $columns_size = @columns;
# Check row has same number of columns as there were column headings:
if ( $columns_size != $number_of_columns ) {
croak
"Found \"$columns_size\" columns. There should have been \"$number_of_columns\".\n"
. "Row contents was: $_";
}
my $act_on_group = $columns[0];
my $add_member = $columns[1];
$group->add( $act_on_group, $add_member );
Apache::Sling::Print::print_result($group);
}
else {
croak 'CSV broken, failed to parse line: '
. $csv->error_input;
}
}
}
close $input or croak q{Problem closing input!};
}
else {
croak "Problem opening file: '$file'";
}
return 1;
}
( run in 0.969 second using v1.01-cache-2.11-cpan-b16cb0d3907 )