Skip to content
Snippets Groups Projects
Commit 893001bf authored by Guillaume ROUSSE's avatar Guillaume ROUSSE
Browse files

use multipart response to download accounts in CSV format

parent f85b05b1
Branches
Tags
No related merge requests found
...@@ -10,6 +10,10 @@ use Template; ...@@ -10,6 +10,10 @@ use Template;
use Log::Any::Adapter; use Log::Any::Adapter;
use List::MoreUtils qw(uniq); use List::MoreUtils qw(uniq);
use HTTP::AcceptLanguage; use HTTP::AcceptLanguage;
use HTTP::Message;
use HTTP::Response;
use IO::String;
use Text::CSV;
use AccountManager::Account; use AccountManager::Account;
use AccountManager::Account::Manager; use AccountManager::Account::Manager;
...@@ -127,7 +131,6 @@ sub run { ...@@ -127,7 +131,6 @@ sub run {
sub respond { sub respond {
my ($self, $data) = @_; my ($self, $data) = @_;
$data->{app} = { $data->{app} = {
name => $self->{configuration}->{app}->{name}, name => $self->{configuration}->{app}->{name},
url => $self->{configuration}->{app}->{url}, url => $self->{configuration}->{app}->{url},
...@@ -154,8 +157,9 @@ sub respond { ...@@ -154,8 +157,9 @@ sub respond {
binmode(STDOUT, ":utf8"); binmode(STDOUT, ":utf8");
print $self->{cgi}->header( print $self->{cgi}->header(
-nph => 1,
-type => 'text/html', -type => 'text/html',
-charset => '' -charset => 'utf8'
); );
unless ($tt2->process($template, $data, \*STDOUT)) { unless ($tt2->process($template, $data, \*STDOUT)) {
...@@ -499,13 +503,95 @@ sub req_create_accounts { ...@@ -499,13 +503,95 @@ sub req_create_accounts {
$self->{in}->{token} $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 => \@accounts,
accounts_validity_period => $self->{configuration}->{service}->{account_validity_period}, accounts_validity_period => $self->{configuration}->{service}->{account_validity_period},
idp_displayname => $self->{configuration}->{idp}->{displayname}, idp_displayname => $self->{configuration}->{idp}->{displayname},
entityid => $self->{in}->{entityid}, entityid => $self->{in}->{entityid},
action => 'create_accounts' 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 ## Return the homepage of the service
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment