1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 2/10/18 |
6
|
|
|
* Time: 12:12 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\core\services; |
10
|
|
|
|
11
|
|
|
use craft\base\Component; |
12
|
|
|
use craft\helpers\Json; |
13
|
|
|
use flipbox\keychain\records\KeyChainRecord; |
14
|
|
|
use flipbox\saml\core\helpers\SerializeHelper; |
15
|
|
|
use flipbox\saml\core\records\AbstractProvider; |
16
|
|
|
use flipbox\saml\core\records\LinkRecord; |
17
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
18
|
|
|
use flipbox\saml\core\traits\EnsureSamlPlugin; |
19
|
|
|
use LightSaml\Model\Metadata\EntityDescriptor; |
20
|
|
|
|
21
|
|
|
abstract class AbstractProviderService extends Component implements ProviderServiceInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
use EnsureSamlPlugin; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
abstract public function findOwn(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function find($condition = []) |
35
|
|
|
{ |
36
|
|
|
/** @var AbstractProvider $class */ |
37
|
|
|
$class = $this->getSamlPlugin()->getProviderRecordClass(); |
38
|
|
|
|
39
|
|
|
if (! $provider = $class::find()->where($condition)) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $provider; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
|
|
public function findByIdp($condition = []) |
50
|
|
|
{ |
51
|
|
|
return $this->findByType('idp', $condition); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
|
|
public function findBySp($condition = []) |
58
|
|
|
{ |
59
|
|
|
return $this->findByType('sp', $condition); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
protected function findByType($type, $condition = []) |
66
|
|
|
{ |
67
|
|
|
if (! in_array($type, ['sp', 'idp'])) { |
68
|
|
|
throw new \InvalidArgumentException("Type must be idp or sp."); |
69
|
|
|
} |
70
|
|
|
return $this->find( |
71
|
|
|
array_merge( |
72
|
|
|
[ |
73
|
|
|
'enabled' => 1, |
74
|
|
|
'providerType' => $type, |
75
|
|
|
], |
76
|
|
|
$condition |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function findByEntityId($entityId) |
85
|
|
|
{ |
86
|
|
|
return $this->find([ |
87
|
|
|
'entityId' => $entityId, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function create(EntityDescriptor $entityDescriptor, KeyChainRecord $keyChainRecord = null): ProviderInterface |
95
|
|
|
{ |
96
|
|
|
|
97
|
|
|
$recordClass = $this->getSamlPlugin()->getProviderRecordClass(); |
98
|
|
|
|
99
|
|
|
/** @var ProviderInterface $provider */ |
100
|
|
|
$provider = (new $recordClass()) |
101
|
|
|
->loadDefaultValues(); |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
$provider->providerType = $this->getSamlPlugin()->getMyType(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
\Craft::configure($provider, [ |
107
|
|
|
'entityId' => $entityDescriptor->getEntityID(), |
108
|
|
|
'metadata' => SerializeHelper::toXml($entityDescriptor), |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
if ($keyChainRecord) { |
112
|
|
|
$provider->setKeychain($keyChainRecord); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $provider; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function save(AbstractProvider $record, $runValidation = true, $attributeNames = null) |
122
|
|
|
{ |
123
|
|
|
if ($record->isNewRecord) { |
124
|
|
|
$record->loadDefaultValues(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//save record |
128
|
|
|
if (! $record->save($runValidation, $attributeNames)) { |
129
|
|
|
throw new \Exception(Json::encode($record->getErrors())); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($record->keychain) { |
|
|
|
|
133
|
|
|
$this->linkToKey( |
134
|
|
|
$record, |
135
|
|
|
$record->keychain |
|
|
|
|
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $record; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
public function linkToKey( |
146
|
|
|
AbstractProvider $provider, |
147
|
|
|
KeyChainRecord $keyChain, |
148
|
|
|
$runValidation = true, |
149
|
|
|
$attributeNames = null |
150
|
|
|
) { |
151
|
|
|
if (! $provider->id && ! $keyChain->id) { |
|
|
|
|
152
|
|
|
throw new \Exception('Provider id and keychain id must exist before linking.'); |
153
|
|
|
} |
154
|
|
|
$linkAttributes = [ |
155
|
|
|
'providerId' => $provider->id, |
|
|
|
|
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
/** @var LinkRecord $link */ |
159
|
|
|
if (! $link = LinkRecord::find()->where($linkAttributes)->one()) { |
160
|
|
|
$link = new LinkRecord($linkAttributes); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$linkAttributes['keyChainId'] = $keyChain->id; |
164
|
|
|
\Craft::configure( |
165
|
|
|
$link, |
|
|
|
|
166
|
|
|
$linkAttributes |
167
|
|
|
); |
168
|
|
|
if (! $link->save($runValidation, $attributeNames)) { |
169
|
|
|
throw new \Exception(Json::encode($record->getErrors())); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function delete(ProviderInterface $provider) |
177
|
|
|
{ |
178
|
|
|
return $provider->delete(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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: