1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 1/11/18 |
6
|
|
|
* Time: 8:30 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\sp\services; |
10
|
|
|
|
11
|
|
|
use craft\base\Component; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use flipbox\saml\core\exceptions\InvalidMessage; |
14
|
|
|
use flipbox\saml\core\helpers\MessageHelper; |
15
|
|
|
use flipbox\saml\sp\events\UserLogin; |
16
|
|
|
use flipbox\saml\sp\models\Settings; |
17
|
|
|
use flipbox\saml\sp\records\ProviderIdentityRecord; |
18
|
|
|
use flipbox\saml\sp\records\ProviderRecord; |
19
|
|
|
use flipbox\saml\sp\Saml; |
20
|
|
|
use flipbox\saml\sp\services\login\AssertionTrait; |
21
|
|
|
use SAML2\Response as SamlResponse; |
22
|
|
|
use yii\base\UserException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Consumer |
26
|
|
|
* @package flipbox\saml\sp\services\login |
27
|
|
|
*/ |
28
|
|
|
class Login extends Component |
29
|
|
|
{ |
30
|
|
|
use AssertionTrait; |
31
|
|
|
|
32
|
|
|
const EVENT_BEFORE_RESPONSE_TO_USER = 'eventBeforeResponseToUser'; |
33
|
|
|
const EVENT_AFTER_RESPONSE_TO_USER = 'eventAfterResponseToUser'; |
34
|
|
|
|
35
|
3 |
|
public function transformToUser( |
36
|
|
|
User $user, |
37
|
|
|
SamlResponse $response, |
38
|
|
|
ProviderRecord $idp, |
39
|
|
|
ProviderRecord $sp, |
40
|
|
|
Settings $settings |
41
|
|
|
) { |
42
|
|
|
/** |
43
|
|
|
* Before user save |
44
|
|
|
*/ |
45
|
3 |
|
$event = new UserLogin(); |
46
|
3 |
|
$event->response = $response; |
47
|
3 |
|
$event->user = $user; |
48
|
|
|
|
49
|
3 |
|
$this->trigger( |
50
|
3 |
|
static::EVENT_BEFORE_RESPONSE_TO_USER, |
51
|
1 |
|
$event |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
// Sync |
55
|
3 |
|
Saml::getInstance()->getUser()->sync( |
56
|
3 |
|
$user, |
57
|
1 |
|
$response, |
58
|
1 |
|
$idp, |
59
|
1 |
|
$sp, |
60
|
1 |
|
$settings |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* After user save |
65
|
|
|
*/ |
66
|
3 |
|
$event = new UserLogin(); |
67
|
3 |
|
$event->response = $response; |
68
|
3 |
|
$event->user = $user; |
69
|
|
|
|
70
|
3 |
|
$this->trigger( |
71
|
3 |
|
static::EVENT_BEFORE_RESPONSE_TO_USER, |
72
|
1 |
|
$event |
73
|
|
|
); |
74
|
|
|
|
75
|
3 |
|
return $user; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ProviderIdentityRecord $identityRecord |
80
|
|
|
* @throws UserException |
81
|
|
|
* @throws \Throwable |
82
|
|
|
*/ |
83
|
|
|
public function byIdentity(ProviderIdentityRecord $identityRecord) |
84
|
|
|
{ |
85
|
|
|
/** |
86
|
|
|
* Log user in |
87
|
|
|
*/ |
88
|
|
|
if (! Saml::getInstance()->getUser()->login($identityRecord)) { |
89
|
|
|
throw new UserException("Unknown error while logging in."); |
90
|
|
|
} |
91
|
|
|
/** |
92
|
|
|
* User's successfully logged in so we can now set the lastLogin for the |
93
|
|
|
* provider identity and save it to the db. |
94
|
|
|
*/ |
95
|
|
|
$identityRecord->lastLoginDate = new \DateTime(); |
|
|
|
|
96
|
|
|
if (! Saml::getInstance()->getProviderIdentity()->save($identityRecord)) { |
97
|
|
|
throw new UserException("Error while saving identity."); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.