1 | <?php |
||
19 | abstract class AbstractProvider extends ActiveRecord implements ProviderInterface |
||
20 | { |
||
21 | |||
22 | use traits\EntityDescriptor, traits\KeyChain, traits\MapUser, Ember; |
||
23 | |||
24 | const METADATA_HASH_ALGORITHM = 'sha256'; |
||
25 | const DEFAULT_GROUPS_ATTRIBUTE_NAME = 'groups'; |
||
26 | |||
27 | protected $metadataModel; |
||
28 | protected $cachedKeychain; |
||
29 | |||
30 | /** |
||
31 | * This method helps initiate the login process for a remote provider. |
||
32 | * When using this method, say your craft site is the SP. This method will be helpful |
||
33 | * on the IDP provider record. Going to this login path will |
||
34 | * initiate the login process for this IDP. Returns null when you getLoginPath for the |
||
35 | * local provider (the current craft site). |
||
36 | * |
||
37 | * @return string|null |
||
38 | */ |
||
39 | abstract public function getLoginPath(); |
||
40 | |||
41 | /** |
||
42 | * Similar to getLoginPath(), this method initiates logout with the intended remote |
||
43 | * provider. |
||
44 | * |
||
45 | * @return string|null |
||
46 | */ |
||
47 | abstract public function getLogoutPath(); |
||
48 | |||
49 | |||
50 | /** |
||
51 | * @inheritDoc |
||
52 | */ |
||
53 | public function loadDefaultValues($skipIfSet = true) |
||
54 | { |
||
55 | parent::loadDefaultValues($skipIfSet); |
||
56 | |||
57 | // fix the issue with postgres pulling zero as default |
||
58 | if (!trim($this->uid)) { |
||
59 | $this->uid = StringHelper::UUID(); |
||
60 | } |
||
61 | |||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | */ |
||
68 | public function beforeSave($insert) |
||
69 | { |
||
70 | if (! $this->entityId) { |
||
|
|||
71 | $this->entityId = $this->getEntityId(); |
||
72 | } |
||
73 | |||
74 | if (is_array($this->mapping)) { |
||
75 | $this->mapping = json_encode($this->mapping); |
||
76 | } |
||
77 | |||
78 | if ($this->groupOptions instanceof GroupOptions) { |
||
79 | $this->groupOptions = json_encode($this->groupOptions); |
||
80 | } |
||
81 | |||
82 | if ($this->metadataOptions instanceof MetadataOptions) { |
||
83 | $this->metadataOptions = json_encode($this->metadataOptions); |
||
84 | } |
||
85 | |||
86 | $this->sha256 = hash(static::METADATA_HASH_ALGORITHM, $this->metadata); |
||
87 | |||
88 | $this->metadata = $this->getMetadataModel()->toXML()->ownerDocument->saveXML(); |
||
89 | |||
90 | return parent::beforeSave($insert); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return string |
||
95 | */ |
||
96 | public function getEntityId() |
||
97 | { |
||
98 | $metadata = $this->getMetadataModel(); |
||
99 | return $metadata->getEntityID(); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return EntityDescriptor |
||
104 | */ |
||
105 | public function getMetadataModel() |
||
106 | { |
||
107 | |||
108 | if (! $this->metadataModel && $this->metadata) { |
||
109 | $this->metadataModel = new EntityDescriptor( |
||
110 | DOMDocumentFactory::fromString($this->metadata)->documentElement |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | return $this->metadataModel; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param EntityDescriptor $descriptor |
||
119 | * @return $this |
||
120 | */ |
||
121 | public function setMetadataModel(EntityDescriptor $descriptor) |
||
122 | { |
||
123 | $this->metadataModel = $descriptor; |
||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function isIdentityProvider() |
||
131 | { |
||
132 | return $this->providerType === static::TYPE_IDP; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function isServiceProvider() |
||
139 | { |
||
140 | return $this->providerType === static::TYPE_SP; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getType() |
||
147 | { |
||
148 | return $this->providerType; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return ActiveQuery |
||
153 | */ |
||
154 | public function getLink() |
||
155 | { |
||
156 | return $this->hasOne(LinkRecord::class, [ |
||
157 | 'providerId' => 'id', |
||
158 | ]); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return ActiveQuery |
||
163 | */ |
||
164 | public function getKeychain() |
||
165 | { |
||
166 | return $this->hasOne(KeyChainRecord::class, [ |
||
167 | 'id' => 'keyChainId', |
||
168 | ])->viaTable(LinkRecord::tableName(), [ |
||
169 | 'providerId' => 'id', |
||
170 | ]); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param KeyChainRecord $keyChain |
||
175 | * @return AbstractProvider |
||
176 | */ |
||
177 | public function setKeychain(KeyChainRecord $keyChain) |
||
178 | { |
||
179 | $this->populateRelation('keychain', $keyChain); |
||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param array $mapping |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setMapping(array $mapping) |
||
188 | { |
||
189 | $this->mapping = array_values($mapping); |
||
190 | |||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return array |
||
196 | */ |
||
197 | public function getMapping() |
||
198 | { |
||
199 | $mapping = []; |
||
200 | if ($this->hasMapping()) { |
||
201 | $mapping = json_decode($this->mapping, true); |
||
202 | } |
||
203 | |||
204 | return $mapping; |
||
205 | } |
||
206 | |||
207 | public function setMetadataOptions(MetadataOptions $metadataOptions) |
||
208 | { |
||
209 | $this->metadataOptions = $metadataOptions; |
||
210 | |||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | public function getMetadataOptions(): MetadataOptions |
||
215 | { |
||
216 | $metadataOptions = new MetadataOptions(); |
||
217 | if ($this->hasMetadataOptions()) { |
||
218 | $metadataOptions = MetadataOptions::jsonUnserialize($this->metadataOptions); |
||
219 | } |
||
220 | |||
221 | return $metadataOptions; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param GroupOptions $groupOptions |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function setGroupOptions(GroupOptions $groupOptions) |
||
229 | { |
||
230 | $this->groupOptions = $groupOptions; |
||
231 | |||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | public function getGroupOptions(): GroupOptions |
||
236 | { |
||
237 | $groupOptions = new GroupOptions(); |
||
238 | if ($this->hasGroupOptions()) { |
||
239 | $groupOptions = GroupOptions::jsonUnserialize($this->groupOptions); |
||
240 | } |
||
241 | |||
242 | return $groupOptions; |
||
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function hasMapping() |
||
253 | |||
254 | /** |
||
255 | * @return bool |
||
256 | */ |
||
257 | public function hasGroupOptions(): bool |
||
261 | |||
262 | /** |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function hasMetadataOptions(): bool |
||
266 | { |
||
267 | return $this->hasJsonProperty('metadataOptions'); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param string $property |
||
272 | * @return bool |
||
273 | */ |
||
274 | protected function hasJsonProperty(string $property) |
||
286 | } |
||
287 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.