|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\perun\Auth\Process; |
| 6 | + |
| 7 | +use SimpleSAML\Auth\ProcessingFilter; |
| 8 | +use SimpleSAML\Configuration; |
| 9 | +use SimpleSAML\Logger; |
| 10 | +use SimpleSAML\Module\perun\PerunConstants; |
| 11 | + |
| 12 | +/** |
| 13 | + * Drop specified user attributes |
| 14 | + */ |
| 15 | +class DropUserAttributes extends ProcessingFilter |
| 16 | +{ |
| 17 | + public const STAGE = 'perun:DropUserAttributes'; |
| 18 | + public const DEBUG_PREFIX = self::STAGE . ' - '; |
| 19 | + |
| 20 | + public const ATTRIBUTE_NAMES = 'attribute_names'; |
| 21 | + |
| 22 | + private $attributeNames; |
| 23 | + private $filterConfig; |
| 24 | + |
| 25 | + public function __construct($config, $reserved) |
| 26 | + { |
| 27 | + parent::__construct($config, $reserved); |
| 28 | + $this->filterConfig = Configuration::loadFromArray($config); |
| 29 | + |
| 30 | + $this->attributeNames = $this->filterConfig->getArray(self::ATTRIBUTE_NAMES, []); |
| 31 | + if (empty($this->attributeNames)) { |
| 32 | + Logger::warning( |
| 33 | + self::DEBUG_PREFIX . 'Invalid configuration: no name of attributes to be dropped has ' |
| 34 | + . 'been configured. Use option \'' . self::ATTRIBUTE_NAMES . '\' to configure the name of the attribute.' |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function process(&$request) |
| 40 | + { |
| 41 | + assert(is_array($request)); |
| 42 | + if (empty($this->attributeNames)) { |
| 43 | + Logger::warning( |
| 44 | + self::DEBUG_PREFIX . 'List of attribute names which should be dropped is empty. Skip processing.' |
| 45 | + ); |
| 46 | + } elseif (empty($request[PerunConstants::ATTRIBUTES])) { |
| 47 | + Logger::warning(self::DEBUG_PREFIX . 'There are no attributes in the request. Skip processing.'); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + $attributes = &$request[PerunConstants::ATTRIBUTES]; |
| 53 | + foreach ($this->attributeNames as $attributeName) { |
| 54 | + if (isset($attributes[$attributeName])) { |
| 55 | + unset($attributes[$attributeName]); |
| 56 | + Logger::debug(self::DEBUG_PREFIX . 'Removed attribute \'' . $attributeName . '\'.'); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments