silverstripe /
silverstripe-realme
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\RealMe\Model; |
||
| 4 | |||
| 5 | use SilverStripe\Core\Config\Config; |
||
| 6 | use SilverStripe\RealMe\Extension\MemberExtension; |
||
| 7 | use SilverStripe\RealMe\RealMeService; |
||
| 8 | use SilverStripe\Security\Member; |
||
| 9 | use SilverStripe\View\ArrayData; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class RealMeUser |
||
| 13 | * |
||
| 14 | * Holds information about a RealMe user, as stored and retrieved from session. |
||
| 15 | * |
||
| 16 | * @property string SPNameID |
||
| 17 | * @property string SessionIndex |
||
| 18 | * @property ArrayData Attributes |
||
| 19 | * @property FederatedIdentity FederatedIdentity |
||
| 20 | */ |
||
| 21 | class User extends ArrayData |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @return bool true if the data given to this object is sufficient to ensure the user is valid for the given |
||
| 25 | * authentication type |
||
| 26 | */ |
||
| 27 | public function isValid() |
||
| 28 | { |
||
| 29 | // Login Assertion requires only the SPNameID and Session ID. |
||
| 30 | $validLogin = is_string($this->SPNameID) && is_string($this->SessionIndex); |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 31 | if (Config::inst()->get(RealMeService::class, "integration_type") === RealMeService::TYPE_LOGIN) { |
||
| 32 | return $validLogin; |
||
| 33 | } |
||
| 34 | |||
| 35 | // Federated login requires the FIT. |
||
| 36 | $hasFederatedLogin = |
||
| 37 | $validLogin && is_string($this->UserFederatedTag) && $this->Attributes instanceof ArrayData; |
||
|
0 ignored issues
–
show
The property
UserFederatedTag does not exist on SilverStripe\RealMe\Model\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||
| 38 | |||
| 39 | if ($hasFederatedLogin && $this->getFederatedIdentity()) { |
||
| 40 | return $this->getFederatedIdentity()->isValid(); |
||
| 41 | } |
||
| 42 | |||
| 43 | return false; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Alias of isValid(), but called this way so it's clear that a valid RealMeUser object is semantically the same as |
||
| 48 | * an authenticated user |
||
| 49 | * @return bool |
||
| 50 | */ |
||
| 51 | public function isAuthenticated() |
||
| 52 | { |
||
| 53 | return $this->isValid(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return Member |
||
| 58 | * @throws \SilverStripe\ORM\ValidationException |
||
| 59 | */ |
||
| 60 | public function getMember() |
||
| 61 | { |
||
| 62 | $memberExtended = Member::has_extension(MemberExtension::class); |
||
| 63 | $member = null; |
||
| 64 | |||
| 65 | if ($memberExtended) { |
||
| 66 | $member = Member::get()->filter('RealmeSPNameID', $this->SPNameID)->first(); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!$member) { |
||
| 70 | $memberAttributes = []; |
||
| 71 | |||
| 72 | if (RealMeService::config()->get('integration_type') === RealMeService::TYPE_ASSERT) { |
||
| 73 | $memberAttributes = [ |
||
| 74 | 'FirstName' => $this->getFederatedIdentity()->getField('FirstName'), |
||
| 75 | 'Surname' => $this->getFederatedIdentity()->getField('LastName'), |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($memberExtended) { |
||
| 80 | $memberAttributes += ["RealmeSPNameID" => $this->SPNameID]; |
||
| 81 | } |
||
| 82 | |||
| 83 | $member = Member::create($memberAttributes); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $member; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return FederatedIdentity|null |
||
| 91 | */ |
||
| 92 | public function getFederatedIdentity() |
||
| 93 | { |
||
| 94 | // Check if identity is present |
||
| 95 | if (!array_key_exists('FederatedIdentity', $this->array)) { |
||
| 96 | return null; |
||
| 97 | } |
||
| 98 | |||
| 99 | // Get federated identity from array |
||
| 100 | $id = $this->array['FederatedIdentity']; |
||
| 101 | |||
| 102 | // Sanity check class |
||
| 103 | if (!$id instanceof FederatedIdentity) { |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | return $id; |
||
| 108 | } |
||
| 109 | } |
||
| 110 |