diff --git a/lib/AccountManager/WebRequest.pm b/lib/AccountManager/WebRequest.pm index 4f65d11b922260d908177f11f3d07042faa9afdf..7da9ed02dec23230a61cc5afba061bb9b7c54b0c 100644 --- a/lib/AccountManager/WebRequest.pm +++ b/lib/AccountManager/WebRequest.pm @@ -10,6 +10,10 @@ use Template; use Log::Any::Adapter; use List::MoreUtils qw(uniq); use HTTP::AcceptLanguage; +use HTTP::Message; +use HTTP::Response; +use IO::String; +use Text::CSV; use AccountManager::Account; use AccountManager::Account::Manager; @@ -127,7 +131,6 @@ sub run { sub respond { my ($self, $data) = @_; - $data->{app} = { name => $self->{configuration}->{app}->{name}, url => $self->{configuration}->{app}->{url}, @@ -154,8 +157,9 @@ sub respond { binmode(STDOUT, ":utf8"); print $self->{cgi}->header( + -nph => 1, -type => 'text/html', - -charset => '' + -charset => 'utf8' ); unless ($tt2->process($template, $data, \*STDOUT)) { @@ -499,13 +503,95 @@ sub req_create_accounts { $self->{in}->{token} ); - $self->respond({ + binmode(STDOUT, ":utf8"); + + my $response = HTTP::Response->new( + 200, 'OK', [ 'Content-Type' => 'multipart/x-mixed-replace' ] + ); + + $response->protocol('HTTP/1.1'); + $response->date(time); + $response->server($ENV{SERVER_SOFTWARE}); + + # HTML page + my $data = { + app => { + name => $self->{configuration}->{app}->{name}, + url => $self->{configuration}->{app}->{url}, + support_email => $self->{configuration}->{app}->{support_email}, + version => $self->{configuration}->{app}->{version}, + }, accounts => \@accounts, accounts_validity_period => $self->{configuration}->{service}->{account_validity_period}, idp_displayname => $self->{configuration}->{idp}->{displayname}, entityid => $self->{in}->{entityid}, action => 'create_accounts' + }; + + my $lang = HTTP::AcceptLanguage->new($ENV{HTTP_ACCEPT_LANGUAGE})->match(qw/en fr/) || 'en'; + + my $tt2 = Template->new({ + INCLUDE_PATH => $self->{configuration}->{_}->{templates_dir} . "/web/$lang" }); + + my $page_content; + $tt2->process('index.tt2.html', $data, \$page_content); + + my $page_response = HTTP::Message->new( + [ + 'Content-Type' => 'text/html; charset=utf-8', + 'Content-Disposition' => 'inline' + ], + $page_content + ); + $response->add_part($page_response); + + # CSV file + my $csv = Text::CSV->new({ binary => 1, eol => "\r\n", quote_space => 0 }); + my $file_content; + my $file_content_io = IO::String->new($file_content); + $csv->print($file_content_io, [ qw/ + username + password + profile + cn + displayName + givenName + mail + eduPersonAffiliation + eduPersonScopedAffiliation + eduPersonPrincipalName + schacHomeOrganization + schacHomeOrganizationType + / ]); + + foreach my $account (@accounts) { + $csv->print($file_content_io, [ + $account->internal_uid(), + $account->password(), + $account->profile(), + $account->cn(), + $account->displayName(), + $account->givenName(), + $account->mail(), + $account->eduPersonAffiliation(), + $account->eduPersonScopedAffiliation(), + $account->eduPersonPrincipalName(), + $account->schacHomeOrganization(), + $account->schacHomeOrganizationType(), + ]); + } + + my $file_response = HTTP::Message->new( + [ + 'Content-Type' => 'text/csv; charset=utf-8', + 'Content-Disposition' => 'attachment; filename="accounts.csv"' + ], + $file_content + ); + $response->add_part($file_response); + + print $response->as_string(); } ## Return the homepage of the service