|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\perun\Auth\Process; |
| 4 | + |
| 5 | +use SimpleSAML\Error\Exception; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class JoinGroupsAndEduPersonEntitlement |
| 9 | + * |
| 10 | + * This filter joins eduPersonEntitlement attribute from perun with groups from PerunGroups filter. |
| 11 | + * |
| 12 | + * @author Dominik Baránek <[email protected]> |
| 13 | + */ |
| 14 | +class JoinGroupsAndEduPersonEntitlement extends \SimpleSAML\Auth\ProcessingFilter |
| 15 | +{ |
| 16 | + const EDU_PERSON_ENTITLEMENT = 'eduPersonEntitlement'; |
| 17 | + const FORWARDED_EDU_PERSON_ENTITLEMENT = 'forwardedEduPersonEntitlement'; |
| 18 | + |
| 19 | + private $eduPersonEntitlement; |
| 20 | + private $forwardedEduPersonEntitlement; |
| 21 | + |
| 22 | + public function __construct($config, $reserved) |
| 23 | + { |
| 24 | + parent::__construct($config, $reserved); |
| 25 | + |
| 26 | + assert('is_array($config)'); |
| 27 | + |
| 28 | + if (!isset($config[self::EDU_PERSON_ENTITLEMENT])) { |
| 29 | + throw new Exception( |
| 30 | + "perun:JoinGroupsAndEduPersonEntitlement: missing mandatory configuration option " . |
| 31 | + self::EDU_PERSON_ENTITLEMENT . "." |
| 32 | + ); |
| 33 | + } |
| 34 | + $this->eduPersonEntitlement = $config[self::EDU_PERSON_ENTITLEMENT]; |
| 35 | + |
| 36 | + if (!isset($config[self::FORWARDED_EDU_PERSON_ENTITLEMENT])) { |
| 37 | + throw new Exception( |
| 38 | + "perun:JoinGroupsAndEduPersonEntitlement: missing mandatory configuration option " . |
| 39 | + self::FORWARDED_EDU_PERSON_ENTITLEMENT . "." |
| 40 | + ); |
| 41 | + } |
| 42 | + $this->forwardedEduPersonEntitlement = $config[self::FORWARDED_EDU_PERSON_ENTITLEMENT]; |
| 43 | + } |
| 44 | + |
| 45 | + public function process(&$request) |
| 46 | + { |
| 47 | + if (isset($request['Attributes'][$this->eduPersonEntitlement]) && |
| 48 | + isset($request['Attributes'][$this->forwardedEduPersonEntitlement])) { |
| 49 | + $request['Attributes'][$this->eduPersonEntitlement] = array_merge( |
| 50 | + $request['Attributes'][$this->eduPersonEntitlement], |
| 51 | + $request['Attributes'][$this->forwardedEduPersonEntitlement] |
| 52 | + ); |
| 53 | + } else { |
| 54 | + throw new Exception( |
| 55 | + "perun:JoinGroupsAndEduPersonEntitlement: " . |
| 56 | + "missing at least one of mandatory fields ('Attributes." . $this->eduPersonEntitlement . |
| 57 | + "' or 'Attributes." . $this->forwardedEduPersonEntitlement . "' in request." |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + unset($request['Attributes'][$this->forwardedEduPersonEntitlement]); |
| 62 | + } |
| 63 | +} |
0 commit comments