1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\consentsimpleadmin\Controller; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use SimpleSAML\Auth; |
9
|
|
|
use SimpleSAML\Configuration; |
10
|
|
|
use SimpleSAML\Logger; |
11
|
|
|
use SimpleSAML\Module\consent\Auth\Process\Consent; |
12
|
|
|
use SimpleSAML\Session; |
13
|
|
|
use SimpleSAML\Metadata\MetaDataStorageHandler; |
14
|
|
|
use SimpleSAML\Module\consent\Store; |
15
|
|
|
use SimpleSAML\XHTML\Template; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Controller class for the consentsimpleadmin module. |
20
|
|
|
* |
21
|
|
|
* This class serves the different views available in the module. |
22
|
|
|
* |
23
|
|
|
* @package simplesamlphp/simplesamlphp-module-consentsimpleadmin |
24
|
|
|
*/ |
25
|
|
|
class Admin |
26
|
|
|
{ |
27
|
|
|
/** @var \SimpleSAML\Configuration */ |
28
|
|
|
protected Configuration $config; |
29
|
|
|
|
30
|
|
|
/** @var \SimpleSAML\Session */ |
31
|
|
|
protected Session $session; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Controller constructor. |
36
|
|
|
* |
37
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
38
|
|
|
* |
39
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
40
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
41
|
|
|
* |
42
|
|
|
* @throws \Exception |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
Configuration $config, |
46
|
|
|
Session $session |
47
|
|
|
) { |
48
|
|
|
$this->config = $config; |
49
|
|
|
$this->session = $session; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
56
|
|
|
* |
57
|
|
|
* @return \SimpleSAML\XHTML\Template |
58
|
|
|
*/ |
59
|
|
|
public function admin(Request $request): Template |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$consentconfig = Configuration::getConfig('module_consentSimpleAdmin.php'); |
62
|
|
|
|
63
|
|
|
$as = $consentconfig->getValue('auth'); |
64
|
|
|
$as = new Auth\Simple($as); |
|
|
|
|
65
|
|
|
$as->requireAuth(); |
66
|
|
|
|
67
|
|
|
// Get all attributes |
68
|
|
|
$attributes = $as->getAttributes(); |
69
|
|
|
|
70
|
|
|
// Get user ID |
71
|
|
|
$userid_attributename = $consentconfig->getValue('userid', 'eduPersonPrincipalName'); |
72
|
|
|
|
73
|
|
|
if (empty($attributes[$userid_attributename])) { |
74
|
|
|
throw new Exception(sprintf( |
75
|
|
|
'Could not generate useridentifier for storing consent. Attribute [%s] was not available.', |
76
|
|
|
$userid_attributename |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$userid = $attributes[$userid_attributename][0]; |
81
|
|
|
|
82
|
|
|
// Get metadata storage handler |
83
|
|
|
$metadata = MetaDataStorageHandler::getMetadataHandler(); |
84
|
|
|
|
85
|
|
|
// Get IdP id and metadata |
86
|
|
|
$idp_entityid = $as->getAuthData('saml:sp:IdP'); |
87
|
|
|
if ($idp_entityid !== null) { |
88
|
|
|
// From a remote idp (as bridge) |
89
|
|
|
$idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-remote'); |
90
|
|
|
} else { |
91
|
|
|
// from the local idp |
92
|
|
|
$idp_entityid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted'); |
93
|
|
|
$idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-hosted'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
Logger::debug('consentAdmin: IdP is [' . $idp_entityid . ']'); |
97
|
|
|
|
98
|
|
|
$source = $idp_metadata['metadata-set'] . '|' . $idp_entityid; |
99
|
|
|
|
100
|
|
|
// Parse consent config |
101
|
|
|
$consent_storage = Store::parseStoreConfig($consentconfig->getValue('store')); |
102
|
|
|
|
103
|
|
|
// Calc correct user ID hash |
104
|
|
|
$hashed_user_id = Consent::getHashedUserID($userid, $source); |
105
|
|
|
|
106
|
|
|
// Check if button with withdraw all consent was clicked |
107
|
|
|
if (array_key_exists('withdraw', $_REQUEST)) { |
108
|
|
|
Logger::info( |
109
|
|
|
'consentAdmin: UserID [' . $hashed_user_id . '] has requested to withdraw all consents given...' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$consent_storage->deleteAllConsents($hashed_user_id); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Get all consents for user |
116
|
|
|
$user_consent_list = $consent_storage->getConsents($hashed_user_id); |
117
|
|
|
|
118
|
|
|
$consentServices = []; |
119
|
|
|
foreach ($user_consent_list as $c) { |
120
|
|
|
$consentServices[$c[1]] = 1; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
Logger::debug( |
124
|
|
|
'consentAdmin: no of consents [' . count($user_consent_list) . '] no of services [' . count($consentServices) . ']' |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// Init template |
128
|
|
|
$t = new Template($this->config, 'consentSimpleAdmin:consentadmin.twig'); |
129
|
|
|
$translator = $t->getTranslator(); |
130
|
|
|
|
131
|
|
|
$t->data['consentServices'] = count($consentServices); |
132
|
|
|
$t->data['consents'] = count($user_consent_list); |
133
|
|
|
$t->data['granted'] = $translator->t('{consentSimpleAdmin:consentsimpleadmin:granted}', [ |
|
|
|
|
134
|
|
|
'%NO%' => (string)$this->data['consents'], |
135
|
|
|
'%OF%' => (string)$this->data['consentServices'], |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
return $t; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.