-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAggregator.php
More file actions
121 lines (101 loc) · 3.33 KB
/
Aggregator.php
File metadata and controls
121 lines (101 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\aggregator2\Controller;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\Module\aggregator2\Aggregator as AttributeAggregator;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\{Request, Response};
use function array_keys;
use function explode;
use function in_array;
use function strlen;
use function strval;
/**
* Controller class for the aggregator2 module.
*
* This class serves the different views available in the module.
*
* @package simplesamlphp/simplesamlphp-module-aggregator2
*/
class Aggregator
{
/** @var \SimpleSAML\Configuration */
protected Configuration $moduleConfig;
/** @var string[] */
private static array $allowedMimeTypes = [
'text/plain',
'application/samlmetadata-xml',
'application/xml',
];
/**
* Controller constructor.
*
* It initializes the global configuration and session for the controllers implemented here.
*
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
* @param \SimpleSAML\Session $session The session to use by the controllers.
*
* @throws \Exception
*/
public function __construct(
protected Configuration $config,
protected Session $session,
) {
$this->moduleConfig = Configuration::getConfig('module_aggregator2.php');
}
/**
* @return \SimpleSAML\XHTML\Template
*/
public function main(): Template
{
$t = new Template($this->config, 'aggregator2:list.twig');
$t->data['names'] = array_keys($this->moduleConfig->toArray());
return $t;
}
/**
* @param \Symfony\Component\HttpFoundation\Request $request The current request.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function get(Request $request): Response
{
$id = $request->query->get('id');
if ($id === null) {
throw new Error\BadRequest('Missing required parameter "id".');
}
$id = strval($id);
$sets = [];
$set = $request->query->get('set');
if ($set !== null) {
$sets = explode(',', $set);
}
$excluded_entities = [];
$exclude = $request->query->get('exclude');
if ($exclude !== null) {
$excluded_entities = explode(',', $exclude);
}
$aggregator = AttributeAggregator::getAggregator($id);
$aggregator->setFilters($sets);
$aggregator->excludeEntities($excluded_entities);
$xml = $aggregator->getMetadata();
$mimeType = $request->query->get('mimetype');
if (in_array($mimeType, self::$allowedMimeTypes)) {
$mime = $mimeType;
if ($mime === 'text/plain') {
$xmlUtils = new Utils\XML();
$xml = $xmlUtils->formatXMLString($xml);
}
} else {
$mime = 'application/samlmetadata+xml';
}
$response = new Response();
$response->headers->set('Content-Type', $mime);
$response->headers->set('Content-Length', strval(strlen($xml)));
$response->headers->set('Content-Disposition', 'filename=' . $id . '.xml');
$response->setContent($xml);
return $response;
}
}