/
AuthenticationType.php
528 lines (477 loc) · 15.7 KB
/
AuthenticationType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
namespace Concrete\Core\Authentication;
use Concrete\Authentication\Concrete\Controller;
use Concrete\Core\Foundation\ConcreteObject;
use Concrete\Core\Package\PackageList;
use Core;
use Environment;
use Exception;
use Loader;
use Package;
class AuthenticationType extends ConcreteObject
{
/** @var Controller */
public $controller;
protected $authTypeID;
protected $authTypeName;
protected $authTypeHandle;
protected $authTypeDisplayOrder;
protected $authTypeIsEnabled;
protected $pkgID;
public static function getListSorted()
{
return self::getList(true);
}
/**
* Return a raw list of authentication types.
*
* @param bool $sorted true: Sort by display order, false: sort by install order
* @param bool $activeOnly true: include only active types, false: include active and inactive types
*
* @return AuthenticationType[]
*/
public static function getList($sorted = false, $activeOnly = false)
{
$list = [];
$db = Loader::db();
$q = $db->query('SELECT * FROM AuthenticationTypes'
. ($activeOnly ? ' WHERE authTypeIsEnabled=1 ' : '')
. ' ORDER BY ' . ($sorted ? 'authTypeDisplayOrder' : 'authTypeID'));
while ($row = $q->fetch()) {
$list[] = self::load($row);
}
return $list;
}
/**
* Load an AuthenticationType from an array.
*
* @param array $arr should be an array of the following key/value pairs to create an object from:
* <pre>
* array(
* 'authTypeID' => int,
* 'authTypeHandle' => string,
* 'authTypeName' => string,
* 'authTypeDisplayOrder' => int,
* 'authTypeIsEnabled' => tinyint,
* 'pkgID' => int
* )
* </pre>
*
* @return bool|\Concrete\Core\Authentication\AuthenticationType
*/
public static function load($arr)
{
$extract = [
'authTypeID',
'authTypeName',
'authTypeHandle',
'authTypeDisplayOrder',
'authTypeIsEnabled',
'pkgID',
];
$obj = new self();
foreach ($extract as $key) {
if (!isset($arr[$key])) {
return false;
}
$obj->{$key} = $arr[$key];
}
$obj->loadController();
return $obj;
}
/**
* Load the AuthenticationTypeController into the AuthenticationType.
*/
protected function loadController()
{
$env = Environment::get();
$r = $env->getRecord(DIRNAME_AUTHENTICATION . '/' . $this->authTypeHandle . '/' . FILENAME_CONTROLLER);
$prefix = $r->override ? true : $this->getPackageHandle();
$authTypeHandle = Core::make('helper/text')->camelcase($this->authTypeHandle);
$class = core_class('Authentication\\' . $authTypeHandle . '\\Controller', $prefix);
$this->controller = Core::make($class, [$this]);
}
/**
* AuthenticationType::getPackageHandle
* Return the package handle.
*/
public function getPackageHandle()
{
return PackageList::getHandle($this->pkgID);
}
/**
* Return an array of AuthenticationTypes that are associated with a specific package.
*
* @param Package $pkg
*
* @return AuthenticationType[]
*/
public static function getListByPackage($pkg)
{
$db = Loader::db();
$list = [];
$q = $db->query('SELECT * FROM AuthenticationTypes WHERE pkgID=?', [$pkg->getPackageID()]);
while ($row = $q->fetch()) {
$list[] = self::load($row);
}
return $list;
}
/**
* @param string $atHandle New AuthenticationType handle
* @param string $atName New AuthenticationType name, expect this to be presented with "%s Authentication Type"
* @param int $order Order int, used to order the display of AuthenticationTypes
* @param bool|\Package $pkg package object to which this AuthenticationType is associated
*
* @throws \Exception
*
* @return AuthenticationType returns a loaded authentication type
*/
public static function add($atHandle, $atName, $order = 0, $pkg = false)
{
$die = true;
try {
self::getByHandle($atHandle);
} catch (exception $e) {
$die = false;
}
if ($die) {
throw new Exception(t('Authentication type with handle %s already exists!', $atHandle));
}
$pkgID = 0;
if (is_object($pkg)) {
$pkgID = $pkg->getPackageID();
}
$db = Loader::db();
$db->Execute(
'INSERT INTO AuthenticationTypes (authTypeHandle, authTypeName, authTypeIsEnabled, authTypeDisplayOrder, pkgID) values (?, ?, ?, ?, ?)',
[$atHandle, $atName, 1, intval($order), $pkgID]);
$est = self::getByHandle($atHandle);
$r = $est->mapAuthenticationTypeFilePath(FILENAME_AUTHENTICATION_DB);
if ($r->exists()) {
Package::installDB($r->file);
}
return $est;
}
/**
* Return loaded AuthenticationType with the given handle.
*
* @param string $atHandle authenticationType handle
*
* @throws \Exception when an invalid handle is provided
*
* @return AuthenticationType
*/
public static function getByHandle($atHandle)
{
$db = Loader::db();
$row = $db->GetRow('SELECT * FROM AuthenticationTypes WHERE authTypeHandle=?', [$atHandle]);
if (!$row) {
throw new Exception(t('Invalid Authentication Type Handle'));
}
$at = self::load($row);
return $at;
}
/**
* Return loaded AuthenticationType with the given ID.
*
* @param int $authTypeID
*
* @throws \Exception
*
* @return AuthenticationType
*/
public static function getByID($authTypeID)
{
$db = Loader::db();
$row = $db->GetRow('SELECT * FROM AuthenticationTypes where authTypeID=?', [$authTypeID]);
if (!$row) {
throw new Exception(t('Invalid Authentication Type ID'));
}
$at = self::load($row);
$at->loadController();
return $at;
}
public function getAuthenticationTypeName()
{
return $this->authTypeName;
}
/**
* Returns the display name for this instance (localized and escaped accordingly to $format)
*
* @param string $format = 'html' Escape the result in html format (if $format is 'html'). If $format is 'text' or any other value, the display name won't be escaped.
*
* @return string
*/
public function getAuthenticationTypeDisplayName($format = 'html')
{
$value = tc('AuthenticationType', $this->getAuthenticationTypeName());
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
public function getAuthenticationTypeDisplayOrder()
{
return $this->authTypeDisplayOrder;
}
public function getAuthenticationTypePackageID()
{
return $this->pkgID;
}
public function getController()
{
return $this->controller;
}
public function getAuthenticationTypeIconHTML()
{
return $this->controller->getAuthenticationTypeIconHTML();
}
/**
* Update the name.
*
* @param string $authTypeName
*/
public function setAuthenticationTypeName($authTypeName)
{
$db = Loader::db();
$db->Execute(
'UPDATE AuthenticationTypes SET authTypeName=? WHERE authTypeID=?',
[$authTypeName, $this->getAuthenticationTypeID()]);
}
/**
* AuthenticationType::setAuthenticationTypeDisplayOrder
* Update the order for display.
*
* @param int $order value from 0-n to signify order
*/
public function setAuthenticationTypeDisplayOrder($order)
{
$db = Loader::db();
$db->Execute(
'UPDATE AuthenticationTypes SET authTypeDisplayOrder=? WHERE authTypeID=?',
[$order, $this->getAuthenticationTypeID()]);
}
public function getAuthenticationTypeID()
{
return $this->authTypeID;
}
/**
* AuthenticationType::toggle
* Toggle the active state of an AuthenticationType.
*/
public function toggle()
{
return $this->isEnabled() ? $this->disable() : $this->enable();
}
public function isEnabled()
{
return (bool) $this->getAuthenticationTypeStatus();
}
public function getAuthenticationTypeStatus()
{
return $this->authTypeIsEnabled;
}
/**
* AuthenticationType::disable
* Disable an authentication type.
*/
public function disable()
{
if ($this->getAuthenticationTypeID() == 1) {
throw new Exception(t('The core authentication cannot be disabled.'));
}
$db = Loader::db();
$db->Execute(
'UPDATE AuthenticationTypes SET authTypeIsEnabled=0 WHERE AuthTypeID=?',
[$this->getAuthenticationTypeID()]);
}
/**
* AuthenticationType::enable
* Enable an authentication type.
*/
public function enable()
{
$db = Loader::db();
$db->Execute(
'UPDATE AuthenticationTypes SET authTypeIsEnabled=1 WHERE AuthTypeID=?',
[$this->getAuthenticationTypeID()]);
}
/**
* AuthenticationType::delete
* Remove an AuthenticationType, this should be used sparingly.
*/
public function delete()
{
$db = Loader::db();
if (method_exists($this->controller, 'deleteType')) {
$this->controller->deleteType();
}
$db->Execute('DELETE FROM AuthenticationTypes WHERE authTypeID=?', [$this->authTypeID]);
}
/**
* Return the path to a file.
*
* @param string $_file the relative path to the file
*
* @return bool|string
*/
public function getAuthenticationTypeFilePath($_file)
{
$f = $this->mapAuthenticationTypeFilePath($_file);
if ($f->exists()) {
return $f->url;
}
return false;
}
/**
* Return the first existing file path in this order:
* - /models/authentication/types/HANDLE
* - /packages/PKGHANDLE/authentication/types/HANDLE
* - /concrete/models/authentication/types/HANDLE
* - /concrete/core/models/authentication/types/HANDLE.
*
* @param string $_file the filename you want
*
* @return string this will return false if the file is not found
*/
protected function mapAuthenticationTypeFilePath($_file)
{
$atHandle = $this->getAuthenticationTypeHandle();
$env = Environment::get();
$pkgHandle = PackageList::getHandle($this->pkgID);
$r = $env->getRecord(implode('/', [DIRNAME_AUTHENTICATION, $atHandle, $_file]), $pkgHandle);
return $r;
}
public function getAuthenticationTypeHandle()
{
return $this->authTypeHandle;
}
/**
* Render the settings form for this type.
* Settings forms are expected to handle their own submissions and redirect to the appropriate page.
* Otherwise, if the method exists, all $_REQUEST variables with the arrangement: HANDLE[]
* in an array to the AuthenticationTypeController::saveTypeForm.
*/
public function renderTypeForm()
{
$type_form = $this->mapAuthenticationTypeFilePath('type_form.php');
if ($type_form->exists()) {
ob_start();
$this->controller->edit();
extract($this->controller->getSets());
require_once $type_form->file; // We use the $this method to prevent extract overwrite.
$out = ob_get_contents();
ob_end_clean();
echo $out;
} else {
echo '<p>' . t('This authentication type does not require any customization.') . '</p>';
}
}
/**
* Render the login form for this authentication type.
*
* @param string $element
* @param array $params
*/
public function renderForm($element = 'form', $params = [])
{
$this->controller->requireAsset('javascript', 'backstretch');
$form_element = $this->mapAuthenticationTypeFilePath($element . '.php');
if (!$form_element->exists()) {
$form_element = $this->mapAuthenticationTypeFilePath('form.php');
if (method_exists($this->controller, 'form')) {
call_user_func_array([$this->controller, 'form'], $params);
}
}
ob_start();
if (method_exists($this->controller, $element)) {
call_user_func_array([$this->controller, $element], array_values($params));
} else {
$this->controller->view();
}
extract(array_merge($params, $this->controller->getSets()));
require $form_element->file;
$out = ob_get_contents();
ob_end_clean();
echo $out;
}
/**
* Render the hook form for saving the profile settings.
* All settings are expected to be saved by each individual authentication type.
*/
public function renderHook()
{
$form_hook = $this->mapAuthenticationTypeFilePath('hook.php');
if (method_exists($this->controller, 'hook') || $form_hook->exists()) {
ob_start();
if (method_exists($this->controller, 'hook')) {
$this->controller->hook();
}
if ($form_hook->exists()) {
$controller = $this->controller;
extract($this->controller->getSets());
require_once $form_hook->file;
}
$out = ob_get_contents();
ob_end_clean();
echo $out;
}
}
public function hasHook()
{
$form_hook = $this->mapAuthenticationTypeFilePath('hook.php');
return method_exists($this->controller, 'hook') || $form_hook->exists();
}
/**
* Render the a form to be displayed when the authentication type is already hooked.
* All settings are expected to be saved by each individual authentication type.
*/
public function renderHooked()
{
$form_hooked = $this->mapAuthenticationTypeFilePath('hooked.php');
if (method_exists($this->controller, 'hooked') || $form_hooked->exists()) {
ob_start();
if (method_exists($this->controller, 'hooked')) {
$this->controller->hooked();
}
if ($form_hooked->exists()) {
$controller = $this->controller;
extract($this->controller->getSets());
require_once $form_hooked->file;
}
$out = ob_get_contents();
ob_end_clean();
echo $out;
}
}
/**
* Does this authentication type support rendering a form when it has already been hooked?
*
* @return bool
*/
public function hasHooked()
{
$form_hooked = $this->mapAuthenticationTypeFilePath('hooked.php');
return method_exists($this->controller, 'hooked') || $form_hooked->exists();
}
/**
* Is this authentication type already hooked for a specific user?
*
* @param \Concrete\Core\User\User|\Concrete\Core\User\UserInfo|\Concrete\Core\Entity\User\User|int $user
*
* @return bool|null returns null if the controller does not implement a way to determine if a user is already hooked or not
*/
public function isHooked($user)
{
$result = null;
if (is_callable([$this->controller, 'getBindingForUser'])) {
$result = $this->controller->getBindingForUser($user) !== null;
} else {
$result = null;
}
return $result;
}
}