1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 2/28/18 |
6
|
|
|
* Time: 9:37 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\services; |
10
|
|
|
|
11
|
|
|
use craft\elements\User; |
12
|
|
|
use flipbox\saml\core\EnsureSAMLPlugin; |
13
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
14
|
|
|
use flipbox\saml\core\records\AbstractProviderIdentity; |
15
|
|
|
use flipbox\saml\core\records\ProviderIdentityInterface; |
16
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
17
|
|
|
use yii\base\Component; |
18
|
|
|
use yii\helpers\Json; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AbstractProviderIdentityService |
22
|
|
|
* @package flipbox\saml\core\services |
23
|
|
|
* @property \DateTime $lastLoginDate |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractProviderIdentityService extends Component implements ProviderIdentityServiceInterface, EnsureSAMLPlugin |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function findByNameId(string $nameId, ProviderInterface $provider) |
31
|
|
|
{ |
32
|
|
|
return $this->find([ |
33
|
|
|
'nameId' => $nameId, |
34
|
|
|
'providerId' => $provider->id, |
|
|
|
|
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function findByUser(User $user) |
42
|
|
|
{ |
43
|
|
|
return $this->find([ |
44
|
|
|
'userId' => $user->getId(), |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param User $user |
50
|
|
|
* @param AbstractProvider $provider |
51
|
|
|
* @return ProviderInterface|null |
52
|
|
|
*/ |
53
|
|
|
public function findByUserAndProvider(User $user, AbstractProvider $provider) |
54
|
|
|
{ |
55
|
|
|
return $this->find([ |
56
|
|
|
'userId' => $user->getId(), |
57
|
|
|
'providerId' => $provider->id, |
|
|
|
|
58
|
|
|
])->one(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function findByUserAndProviderOrCreate(User $user, AbstractProvider $provider) |
62
|
|
|
{ |
63
|
|
|
$recordClass = $this->getPlugin()->getProviderIdentityRecordClass(); |
64
|
|
|
return |
65
|
|
|
$this->findByUserAndProvider($user, $provider) |
66
|
|
|
?? |
67
|
|
|
new $recordClass([ |
68
|
|
|
'nameId' => $provider->assignNameId($user), |
69
|
|
|
'providerId' => $provider->id, |
|
|
|
|
70
|
|
|
'enabled' => true, |
71
|
|
|
'userId' => $user->getId(), |
72
|
|
|
'lastLoginDate' => new \DateTime, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function find($condition = []) |
80
|
|
|
{ |
81
|
|
|
/** @var AbstractProviderIdentity $class */ |
82
|
|
|
$class = $this->getPlugin()->getProviderIdentityRecordClass(); |
83
|
|
|
|
84
|
|
|
/** @var AbstractProviderIdentity $class */ |
85
|
|
|
$providerId = $class::find()->where($condition); |
86
|
|
|
|
87
|
|
|
return $providerId; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ProviderIdentityInterface $record |
92
|
|
|
* @param bool $runValidation |
93
|
|
|
* @param array|null $attributeNames |
94
|
|
|
* @return ProviderIdentityInterface |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function save( |
98
|
|
|
ProviderIdentityInterface $record, |
99
|
|
|
$runValidation = true, |
100
|
|
|
$attributeNames = null |
101
|
|
|
): ProviderIdentityInterface |
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
if (! $record->save($runValidation, $attributeNames)) { |
105
|
|
|
throw new \Exception(Json::encode($record->getErrors())); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $record; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: