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