1 | <?php |
||
23 | class User |
||
24 | { |
||
25 | use AssertionTrait; |
||
26 | |||
27 | /** |
||
28 | * @param SamlResponse $response |
||
29 | * @return UserElement |
||
30 | * @throws InvalidMessage |
||
31 | * @throws UserException |
||
32 | */ |
||
33 | public function getByResponse(SamlResponse $response) |
||
34 | { |
||
35 | |||
36 | $assertion = $this->getFirstAssertion($response); |
||
37 | |||
38 | if (! $assertion->getNameId()) { |
||
39 | throw new InvalidMessage('Name ID is missing.'); |
||
40 | } |
||
41 | |||
42 | Saml::debug('NameId: ' . $assertion->getNameId()->getValue()); |
||
43 | /** |
||
44 | * Get username from the NameID |
||
45 | * |
||
46 | * @todo Give an option to map another attribute value to $username (like email) |
||
47 | */ |
||
48 | $username = $assertion->getNameId()->getValue(); |
||
49 | |||
50 | return $this->find($username); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param ProviderIdentityRecord $identity |
||
55 | * @return bool |
||
56 | * @throws UserException |
||
57 | * @throws \Throwable |
||
58 | */ |
||
59 | public function login(\flipbox\saml\sp\records\ProviderIdentityRecord $identity) |
||
82 | |||
83 | /** |
||
84 | * @param UserElement $user |
||
85 | * @param SamlResponse $response |
||
86 | * @throws UserException |
||
87 | * @throws \Throwable |
||
88 | * @throws \craft\errors\ElementNotFoundException |
||
89 | * @throws \yii\base\Exception |
||
90 | */ |
||
91 | public function sync(UserElement $user, SamlResponse $response) |
||
109 | |||
110 | /** |
||
111 | * @param UserElement $user |
||
112 | * @return bool |
||
113 | * @throws UserException |
||
114 | * @throws \Throwable |
||
115 | * @throws \craft\errors\ElementNotFoundException |
||
116 | * @throws \yii\base\Exception |
||
117 | */ |
||
118 | protected function save(UserElement $user) |
||
129 | |||
130 | /** |
||
131 | * Response Based Methods |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * @param UserElement $user |
||
136 | * @param SamlResponse $response |
||
137 | * @throws UserException |
||
138 | * @throws \Throwable |
||
139 | */ |
||
140 | protected function construct(UserElement $user, SamlResponse $response) |
||
141 | { |
||
142 | /** |
||
143 | * Is User Active? |
||
144 | */ |
||
145 | if (! UserHelper::isUserActive($user)) { |
||
146 | if (! Saml::getInstance()->getSettings()->enableUsers) { |
||
147 | throw new UserException('User access denied.'); |
||
148 | } |
||
149 | UserHelper::enableUser($user); |
||
150 | } |
||
151 | |||
152 | foreach ($this->getAssertions($response) as $assertion) { |
||
153 | $hasAttributes = count($assertion->getAttributes()) > 1; |
||
154 | Saml::debug('assertion attributes: ' . \json_encode($assertion->getAttributes())); |
||
155 | if ($hasAttributes) { |
||
156 | $this->transform($response, $user); |
||
157 | } else { |
||
158 | /** |
||
159 | * There doesn't seem to be any attribute statements. |
||
160 | * Try and use username for the email and move on. |
||
161 | */ |
||
162 | Saml::warning( |
||
163 | 'No attribute statements found! Trying to assign username as the email.' |
||
164 | ); |
||
165 | $user->email = $user->email ?: $user->username; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param SamlResponse $response |
||
172 | * @param UserElement $user |
||
173 | * @return UserElement |
||
174 | */ |
||
175 | protected function transform( |
||
176 | SamlResponse $response, |
||
177 | UserElement $user |
||
178 | ) { |
||
179 | |||
180 | foreach ($this->getAssertions($response) as $assertion) { |
||
181 | /** |
||
182 | * Check the provider first |
||
183 | */ |
||
184 | $attributeMap = ProviderHelper::providerMappingToKeyValue( |
||
185 | $idpProvider = Saml::getInstance()->getProvider()->findByEntityId( |
||
186 | MessageHelper::getIssuer($response->getIssuer()) |
||
187 | )->one() |
||
188 | ) ?: |
||
189 | Saml::getInstance()->getSettings()->responseAttributeMap; |
||
190 | |||
191 | Saml::debug('Attribute Map: ' . json_encode($attributeMap)); |
||
192 | |||
193 | /** |
||
194 | * Loop thru attributes and set to the user |
||
195 | */ |
||
196 | foreach ($assertion->getAttributes() as $attributeName => $attributeValue) { |
||
197 | Saml::debug('Attributes: ' . $attributeName . ' ' . json_encode($attributeValue)); |
||
198 | if (isset($attributeMap[$attributeName])) { |
||
199 | $craftProperty = $attributeMap[$attributeName]; |
||
200 | $this->assignProperty( |
||
201 | $user, |
||
202 | $attributeName, |
||
203 | $attributeValue, |
||
204 | $craftProperty |
||
205 | ); |
||
206 | } else { |
||
207 | Saml::debug('No match for: ' . $attributeName); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return $user; |
||
213 | } |
||
214 | |||
215 | protected function assignProperty( |
||
250 | |||
251 | /************************************************** |
||
252 | * Craft User Methods |
||
253 | **************************************************/ |
||
254 | |||
255 | /** |
||
256 | * @param $username |
||
257 | * @return UserElement |
||
258 | * @throws UserException |
||
259 | */ |
||
260 | protected function find($username) |
||
264 | |||
265 | /** |
||
266 | * @param $username |
||
267 | * @return UserElement |
||
268 | * @throws UserException |
||
269 | */ |
||
270 | protected function forceGet($username) |
||
302 | |||
303 | /** |
||
304 | * @param $emailOrUsername |
||
305 | * @return UserElement|null |
||
306 | */ |
||
307 | protected function getByUsernameOrEmail($usernameOrEmail, bool $archived = false) |
||
323 | } |
||
324 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.