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
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment