Conditions | 26 |
Paths | 1624 |
Total Lines | 209 |
Code Lines | 115 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
128 | public function main(Request $request): Template |
||
129 | { |
||
130 | $authority = $this->moduleConfig->getValue('authority'); |
||
131 | |||
132 | $as = new $this->authSimple($authority); |
||
133 | |||
134 | // If request is a logout request |
||
135 | $logout = $request->get('logout'); |
||
136 | if ($logout !== null) { |
||
137 | $returnURL = $this->moduleConfig->getValue('returnURL'); |
||
138 | $as->logout($returnURL); |
||
139 | } |
||
140 | |||
141 | $hashAttributes = $this->moduleConfig->getValue('attributes.hash', false); |
||
142 | |||
143 | $excludeAttributes = $this->moduleConfig->getValue('attributes.exclude', []); |
||
144 | |||
145 | // Check if valid local session exists |
||
146 | $as->requireAuth(); |
||
147 | |||
148 | // Get released attributes |
||
149 | $attributes = $as->getAttributes(); |
||
150 | |||
151 | // Get metadata storage handler |
||
152 | $metadata = $this->metadataStorageHandler; |
||
153 | |||
154 | /* |
||
155 | * Get IdP id and metadata |
||
156 | */ |
||
157 | $idp_entityid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted'); |
||
158 | $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-hosted'); |
||
159 | |||
160 | // Calc correct source |
||
161 | if ($as->getAuthData('saml:sp:IdP') !== null) { |
||
162 | // from a remote idp (as bridge) |
||
163 | $source = 'saml20-idp-remote|' . $as->getAuthData('saml:sp:IdP'); |
||
164 | } else { |
||
165 | // from the local idp |
||
166 | $source = $idp_metadata['metadata-set'] . '|' . $idp_entityid; |
||
167 | } |
||
168 | |||
169 | // Get user ID |
||
170 | if (isset($idp_metadata['userid.attribute']) && is_string($idp_metadata['userid.attribute'])) { |
||
171 | $userid_attributename = $idp_metadata['userid.attribute']; |
||
172 | } else { |
||
173 | $userid_attributename = 'eduPersonPrincipalName'; |
||
174 | } |
||
175 | |||
176 | $userids = $attributes[$userid_attributename]; |
||
177 | |||
178 | if (empty($userids)) { |
||
179 | throw new Exception(sprintf( |
||
180 | 'Could not generate useridentifier for storing consent. Attribute [%s] was not available.', |
||
181 | $userid_attributename |
||
182 | )); |
||
183 | } |
||
184 | |||
185 | $userid = $userids[0]; |
||
186 | |||
187 | // Get all SP metadata |
||
188 | $all_sp_metadata = $metadata->getList('saml20-sp-remote'); |
||
189 | |||
190 | $sp_entityid = $request->get('cv');; |
||
191 | $action = $request->get('action'); |
||
192 | |||
193 | Logger::critical('consentAdmin: sp: ' . $sp_entityid . ' action: ' . $action); |
||
194 | |||
195 | // Remove services, whitch have consent disabled |
||
196 | if (isset($idp_metadata['consent.disable'])) { |
||
197 | foreach ($idp_metadata['consent.disable'] as $disable) { |
||
198 | if (array_key_exists($disable, $all_sp_metadata)) { |
||
199 | unset($all_sp_metadata[$disable]); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | Logger::info('consentAdmin: ' . $idp_entityid); |
||
205 | |||
206 | // Parse consent config |
||
207 | $consent_storage = $this->store::parseStoreConfig($this->moduleConfig->getValue('consentadmin')); |
||
208 | |||
209 | // Calc correct user ID hash |
||
210 | $hashed_user_id = $this->consent::getHashedUserID($userid, $source); |
||
211 | |||
212 | // If a checkbox have been clicked |
||
213 | if ($action !== null && $sp_entityid !== null) { |
||
214 | // init template to enable translation of status messages |
||
215 | $template = new Template( |
||
216 | $this->config, |
||
217 | 'consentAdmin:consentadminajax.twig', |
||
218 | 'consentAdmin:consentadmin' |
||
219 | ); |
||
220 | |||
221 | // Get SP metadata |
||
222 | $sp_metadata = $metadata->getMetaData($sp_entityid, 'saml20-sp-remote'); |
||
223 | |||
224 | // Run AuthProc filters |
||
225 | list($targeted_id, $attribute_hash, $attributes) = $this->driveProcessingChain( |
||
226 | $idp_metadata, |
||
227 | $source, |
||
228 | $sp_metadata, |
||
229 | $sp_entityid, |
||
230 | $attributes, |
||
231 | $userid, |
||
232 | $hashAttributes, |
||
233 | $excludeAttributes |
||
234 | ); |
||
235 | |||
236 | // Add a consent (or update if attributes have changed and old consent for SP and IdP exists) |
||
237 | if ($action == 'true') { |
||
238 | $isStored = $consent_storage->saveConsent($hashed_user_id, $targeted_id, $attribute_hash); |
||
239 | } else { |
||
240 | if ($action == 'false') { |
||
241 | // Got consent, so this is a request to remove it |
||
242 | $consent_storage->deleteConsent($hashed_user_id, $targeted_id); |
||
243 | $isStored = false; |
||
244 | } else { |
||
245 | Logger::info('consentAdmin: unknown action'); |
||
246 | $isStored = null; |
||
247 | } |
||
248 | } |
||
249 | $template->data['isStored'] = $isStored; |
||
250 | return $template; |
||
251 | } |
||
252 | |||
253 | // Get all consents for user |
||
254 | $user_consent_list = $consent_storage->getConsents($hashed_user_id); |
||
255 | |||
256 | // Parse list of consents |
||
257 | $user_consent = []; |
||
258 | foreach ($user_consent_list as $c) { |
||
259 | $user_consent[$c[0]] = $c[1]; |
||
260 | } |
||
261 | |||
262 | // Init template |
||
263 | $template = new Template($this->config, 'consentAdmin:consentadmin.twig', 'consentAdmin:consentadmin'); |
||
264 | $template->getLocalization()->addAttributeDomains(); |
||
265 | |||
266 | $sp_list = []; |
||
267 | |||
268 | // Process consents for all SP |
||
269 | foreach ($all_sp_metadata as $sp_entityid => $sp_values) { |
||
270 | // Get metadata for SP |
||
271 | $sp_metadata = $metadata->getMetaData($sp_entityid, 'saml20-sp-remote'); |
||
272 | |||
273 | // Run attribute filters |
||
274 | list($targeted_id, $attribute_hash, $attributes) = $this->driveProcessingChain( |
||
275 | $idp_metadata, |
||
276 | $source, |
||
277 | $sp_metadata, |
||
278 | $sp_entityid, |
||
279 | $attributes, |
||
280 | $userid, |
||
281 | $hashAttributes, |
||
282 | $excludeAttributes |
||
283 | ); |
||
284 | |||
285 | // Check if consent exists |
||
286 | if (array_key_exists($targeted_id, $user_consent)) { |
||
287 | $sp_status = "changed"; |
||
288 | Logger::info('consentAdmin: changed'); |
||
289 | // Check if consent is valid. (Possible that attributes has changed) |
||
290 | if ($user_consent[$targeted_id] == $attribute_hash) { |
||
291 | Logger::info('consentAdmin: ok'); |
||
292 | $sp_status = "ok"; |
||
293 | } |
||
294 | // Consent does not exist |
||
295 | } else { |
||
296 | Logger::info('consentAdmin: none'); |
||
297 | $sp_status = "none"; |
||
298 | } |
||
299 | |||
300 | // Set name of SP |
||
301 | if (isset($sp_values['name']) && is_array($sp_values['name'])) { |
||
302 | $sp_name = $sp_metadata['name']; |
||
303 | } elseif (isset($sp_values['name']) && is_string($sp_values['name'])) { |
||
304 | $sp_name = $sp_metadata['name']; |
||
305 | } elseif (isset($sp_values['OrganizationDisplayName']) && is_array($sp_values['OrganizationDisplayName'])) { |
||
306 | $sp_name = $sp_metadata['OrganizationDisplayName']; |
||
307 | } else { |
||
308 | $sp_name = $sp_entityid; |
||
309 | } |
||
310 | |||
311 | // Set description of SP |
||
312 | $sp_description = null; |
||
313 | if (!empty($sp_metadata['description']) && is_array($sp_metadata['description'])) { |
||
314 | $sp_description = $sp_metadata['description']; |
||
315 | } |
||
316 | |||
317 | // Add a URL to the service if present in metadata |
||
318 | $sp_service_url = isset($sp_metadata['ServiceURL']) ? $sp_metadata['ServiceURL'] : null; |
||
319 | |||
320 | // Fill out array for the template |
||
321 | $sp_list[$sp_entityid] = [ |
||
322 | 'spentityid' => $sp_entityid, |
||
323 | 'name' => $sp_name, |
||
324 | 'description' => $sp_description, |
||
325 | 'consentStatus' => $sp_status, |
||
326 | 'consentValue' => $sp_entityid, |
||
327 | 'attributes_by_sp' => $attributes, |
||
328 | 'serviceurl' => $sp_service_url, |
||
329 | ]; |
||
330 | } |
||
331 | |||
332 | $template->data['header'] = 'Consent Administration'; |
||
333 | $template->data['spList'] = $sp_list; |
||
334 | $template->data['showDescription'] = $this->moduleConfig->getValue('showDescription'); |
||
335 | |||
336 | return $template; |
||
337 | } |
||
419 |