|
| 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\Error\Exception; |
| 10 | +use SimpleSAML\Logger; |
| 11 | +use SimpleSAML\Module\perun\Adapter; |
| 12 | +use SimpleSAML\Module\perun\PerunConstants; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class tries to find user in Perun using the extLogin and extSourceName (in case of RPC adapter). |
| 16 | + * |
| 17 | + * If the user cannot be found, it redirects user to the registration URL. |
| 18 | + */ |
| 19 | +class PerunUserGroups extends ProcessingFilter |
| 20 | +{ |
| 21 | + public const STAGE = 'perun:PerunUserGroups'; |
| 22 | + public const DEBUG_PREFIX = self::STAGE . ' - '; |
| 23 | + |
| 24 | + public const INTERFACE = 'interface'; |
| 25 | + |
| 26 | + private $adapter; |
| 27 | + |
| 28 | + public function __construct($config, $reserved) |
| 29 | + { |
| 30 | + parent::__construct($config, $reserved); |
| 31 | + $filterConfig = Configuration::loadFromArray($config); |
| 32 | + |
| 33 | + $interface = $filterConfig->getString(self::INTERFACE, Adapter::RPC); |
| 34 | + |
| 35 | + $this->adapter = Adapter::getInstance($interface); |
| 36 | + } |
| 37 | + |
| 38 | + public function process(&$request) |
| 39 | + { |
| 40 | + assert(is_array($request)); |
| 41 | + assert(array_key_exists(PerunConstants::USER, $request)); |
| 42 | + $user = $request[PerunConstants::PERUN][PerunConstants::USER] ?? null; |
| 43 | + if (empty($user)) { |
| 44 | + throw new Exception( |
| 45 | + self::DEBUG_PREFIX . 'Cannot find Perun user in request. Did you properly configure ' . PerunUser::STAGE . ' filter before this filter in the processing chain?' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + $spEntityId = $request[PerunConstants::SP_METADATA][PerunConstants::SP_METADATA_ENTITYID] ?? null; |
| 50 | + $groups = []; |
| 51 | + if (empty($spEntityId)) { |
| 52 | + Logger::debug(self::DEBUG_PREFIX . 'No SP EntityID available, user groups will be empty'); |
| 53 | + throw new Exception( |
| 54 | + self::DEBUG_PREFIX . 'Cannot find SP EntityID' |
| 55 | + ); |
| 56 | + } else { |
| 57 | + $groups = $this->adapter->getUsersGroupsOnSp($spEntityId, $user->getId()); |
| 58 | + } |
| 59 | + $request[PerunConstants::PERUN][PerunConstants::USER_GROUPS] = $groups; |
| 60 | + } |
| 61 | +} |
0 commit comments