-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathIdentityHelper.php
More file actions
137 lines (124 loc) · 3.82 KB
/
IdentityHelper.php
File metadata and controls
137 lines (124 loc) · 3.82 KB
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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 1.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Authentication\View\Helper;
use Authentication\IdentityInterface;
use Cake\Utility\Hash;
use Cake\View\Helper;
/**
* Identity Helper
*
* A convenience helper to access the identity data
*/
class IdentityHelper extends Helper
{
/**
* Configuration options
*
* - `identityAttribute` - The request attribute which holds the identity.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'identityAttribute' => 'identity',
];
/**
* Identity Object
*
* @var \Authentication\IdentityInterface|null
*/
protected ?IdentityInterface $_identity = null;
/**
* Constructor hook method.
*
* Implement this method to avoid having to overwrite the constructor and call parent.
*
* @param array<string, mixed> $config The configuration settings provided to this helper.
* @return void
*/
public function initialize(array $config): void
{
$this->_identity = $this->_View->getRequest()->getAttribute($this->getConfig('identityAttribute'));
}
/**
* Gets the id of the current logged in identity
*
* @return array|string|int|null
*/
public function getId(): array|string|int|null
{
if ($this->_identity === null) {
return null;
}
return $this->_identity->getIdentifier();
}
/**
* Checks if a user is logged in
*
* @return bool
*/
public function isLoggedIn(): bool
{
return $this->_identity !== null;
}
/**
* This check can be used to tell if a record that belongs to some user is
* the current logged in user and compare other fields as well
*
* If you have more complex requirements on visibility checks based on some
* kind of permission you should use the Authorization plugin instead:
*
* https://github.com/cakephp/authorization
*
* This method is mostly a convenience method for simple cases and not
* intended to replace any kind of proper authorization implementation.
*
* @param string|int $id Identity id to check against
* @param string $field Name of the field in the identity data to check against, id by default
* @return bool
*/
public function is(int|string $id, string $field = 'id'): bool
{
return $id === $this->get($field);
}
/**
* Get data from the identity.
*
* You can use dot notation to fetch nested data.
* Calling the method without any argument will return
* the entire data array/object (same as `IdentityInterface::getOriginalData()`).
*
* @param string|null $key Key of something you want to get from the identity data
* @return mixed
*/
public function get(?string $key = null): mixed
{
if ($this->_identity === null) {
return null;
}
if ($key === null) {
return $this->_identity->getOriginalData();
}
return Hash::get($this->_identity, $key);
}
/**
* Returns the identity instance.
*
* @return \Authentication\IdentityInterface|null
*/
public function getIdentity(): ?IdentityInterface
{
return $this->_identity;
}
}