Apache-Sling
view release on metacpan or search on metacpan
lib/Apache/Sling/User.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 $user = {
BaseURL => ${$authn}->{'BaseURL'},
Authn => $authn,
Message => q{},
Response => \$response,
Verbose => $verbose,
Log => $log
};
bless $user, $class;
return $user;
}
#}}}
#{{{sub set_results
sub set_results {
my ( $user, $message, $response ) = @_;
$user->{'Message'} = $message;
$user->{'Response'} = $response;
return 1;
}
#}}}
#{{{sub add
sub add {
my ( $user, $act_on_user, $act_on_pass, $properties ) = @_;
my $res = Apache::Sling::Request::request(
\$user,
Apache::Sling::UserUtil::add_setup(
$user->{'BaseURL'}, $act_on_user, $act_on_pass, $properties
)
);
my $success = Apache::Sling::UserUtil::add_eval($res);
my $message = "User: \"$act_on_user\" ";
$message .= ( $success ? 'added!' : 'was not added!' );
$user->set_results( "$message", $res );
return $success;
}
#}}}
#{{{sub add_from_file
sub add_from_file {
my ( $user, $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 site:
if ( $column_headings[0] !~ /^[Uu][Ss][Ee][Rr]$/msx ) {
croak
'First CSV column must be the user ID, column heading must be "user". Found: "'
. $column_headings[0] . "\".\n";
}
if ( $column_headings[1] !~
/^[Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]$/msx )
{
croak
'Second CSV column must be the user password, column heading must be "password". 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 ) ) {
my @properties;
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\".\nRow contents was: $_";
}
my $id = $columns[0];
my $password = $columns[1];
for ( my $i = 2 ; $i < $number_of_columns ; $i++ ) {
my $heading = $column_headings[$i];
my $data = $columns[$i];
my $value = "$heading=$data";
push @properties, $value;
}
$user->add( $id, $password, \@properties );
Apache::Sling::Print::print_result($user);
}
else {
croak q{CSV broken, failed to parse line: }
. $csv->error_input;
}
}
}
close $input or croak q{Problem closing input};
}
else {
( run in 1.171 second using v1.01-cache-2.11-cpan-b16cb0d3907 )