pradosoft /
prado
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * TUser class file. |
||
| 5 | * |
||
| 6 | * @author Qiang Xue <[email protected]> |
||
| 7 | * @link https://github.com/pradosoft/prado |
||
| 8 | * @license https://github.com/pradosoft/prado/blob/master/LICENSE |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Prado\Security; |
||
| 12 | |||
| 13 | use Prado\TPropertyValue; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * TUser class |
||
| 17 | * |
||
| 18 | * TUser implements basic user functionality for a Prado application. |
||
| 19 | * To get the name of the user, use {@see getName Name} property. |
||
| 20 | * The property {@see getIsGuest IsGuest} tells if the user a guest/anonymous user. |
||
| 21 | * To obtain or test the roles that the user is in, use property |
||
| 22 | * {@see getRoles Roles} and call {@see isInRole()}, respectively. |
||
| 23 | * |
||
| 24 | * TUser is meant to be used together with {@see \Prado\Security\IUserManager}. |
||
| 25 | * |
||
| 26 | * @author Qiang Xue <[email protected]> |
||
| 27 | * @since 3.0 |
||
| 28 | * @method string[] dyDefaultRoles($defaultRoles) |
||
| 29 | * @method bool dyIsInRole($returnValue, $role) |
||
| 30 | */ |
||
| 31 | class TUser extends \Prado\TComponent implements IUser |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var array persistent state |
||
| 35 | */ |
||
| 36 | private $_state; |
||
| 37 | /** |
||
| 38 | * @var bool whether user state is changed |
||
| 39 | */ |
||
| 40 | private $_stateChanged = false; |
||
| 41 | /** |
||
| 42 | * @var IUserManager user manager |
||
| 43 | */ |
||
| 44 | private $_manager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * TUser auto listen to global events. |
||
| 48 | * |
||
| 49 | 11 | * @return bool returns whether or not to listen. |
|
| 50 | */ |
||
| 51 | 11 | public function getAutoGlobalListen() |
|
| 52 | 11 | { |
|
| 53 | 11 | return true; |
|
| 54 | 11 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor. |
||
| 58 | * @param IUserManager $manager user manager |
||
| 59 | 2 | */ |
|
| 60 | public function __construct(IUserManager $manager) |
||
| 61 | 2 | { |
|
| 62 | $this->_state = []; |
||
| 63 | $this->_manager = $manager; |
||
| 64 | $this->setName($manager->getGuestName()); |
||
| 65 | parent::__construct(); |
||
| 66 | } |
||
| 67 | 3 | ||
| 68 | /** |
||
| 69 | 3 | * @return IUserManager user manager |
|
| 70 | */ |
||
| 71 | public function getManager() |
||
| 72 | { |
||
| 73 | return $this->_manager; |
||
| 74 | } |
||
| 75 | 11 | ||
| 76 | /** |
||
| 77 | 11 | * @return string username, defaults to empty string. |
|
| 78 | 11 | */ |
|
| 79 | public function getName() |
||
| 80 | { |
||
| 81 | return $this->getState('Name', ''); |
||
| 82 | } |
||
| 83 | 3 | ||
| 84 | /** |
||
| 85 | 3 | * @param string $value username |
|
| 86 | */ |
||
| 87 | public function setName($value) |
||
| 88 | { |
||
| 89 | $this->setState('Name', $value, ''); |
||
| 90 | } |
||
| 91 | 5 | ||
| 92 | /** |
||
| 93 | 5 | * @return bool if the user is a guest, defaults to true. |
|
| 94 | 3 | */ |
|
| 95 | 3 | public function getIsGuest() |
|
| 96 | { |
||
| 97 | 5 | return $this->getState('IsGuest', true); |
|
| 98 | 5 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param bool $value if the user is a guest |
||
| 102 | */ |
||
| 103 | 4 | public function setIsGuest($value) |
|
| 104 | { |
||
| 105 | 4 | if ($isGuest = TPropertyValue::ensureBoolean($value)) { |
|
| 106 | $this->setName($this->_manager->getGuestName()); |
||
| 107 | $this->setRoles([]); |
||
| 108 | } |
||
| 109 | $this->setState('IsGuest', $isGuest); |
||
| 110 | } |
||
| 111 | |||
| 112 | 7 | /** |
|
| 113 | * @return array list of roles that the user is of |
||
| 114 | 7 | */ |
|
| 115 | 5 | public function getRoles() |
|
| 116 | { |
||
| 117 | 4 | return array_merge($this->getState('Roles', []), $this->dyDefaultRoles([])); |
|
| 118 | 4 | } |
|
| 119 | 4 | ||
| 120 | 4 | /** |
|
| 121 | * @param array|string $value list of roles that the user is of. If it is a string, roles are assumed by separated by comma |
||
| 122 | */ |
||
| 123 | 4 | public function setRoles($value) |
|
| 124 | { |
||
| 125 | 7 | if (!is_array($value)) { |
|
| 126 | $value = array_filter(array_map('trim', explode(',', $value))); |
||
| 127 | } |
||
| 128 | $value = array_diff($value, $this->dyDefaultRoles([])); |
||
| 129 | $this->setState('Roles', $value, []); |
||
| 130 | } |
||
| 131 | 1 | ||
| 132 | /** |
||
| 133 | 1 | * @param string $role role to be tested. Note, role is case-insensitive. |
|
| 134 | 1 | * @return bool whether the user is of this role |
|
| 135 | 1 | */ |
|
| 136 | public function isInRole($role) |
||
| 137 | { |
||
| 138 | 1 | foreach ($this->getRoles() as $r) { |
|
| 139 | if (strcasecmp($role, $r) === 0) { |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | return $this->dyIsInRole(false, $role); |
||
| 144 | 2 | } |
|
| 145 | |||
| 146 | 2 | /** |
|
| 147 | * @return string user data that is serialized and will be stored in session |
||
| 148 | */ |
||
| 149 | public function saveToString() |
||
| 150 | { |
||
| 151 | return serialize($this->_state); |
||
| 152 | } |
||
| 153 | 1 | ||
| 154 | /** |
||
| 155 | 1 | * @param string $data user data that is serialized and restored from session |
|
| 156 | 1 | * @return IUser the user object |
|
| 157 | */ |
||
| 158 | 1 | public function loadFromString($data) |
|
| 159 | { |
||
| 160 | if (!empty($data)) { |
||
| 161 | 1 | $this->_state = unserialize($data); |
|
| 162 | } |
||
| 163 | if (!is_array($this->_state)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 164 | $this->_state = []; |
||
| 165 | } |
||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the value of a variable that is stored in user session. |
||
| 171 | * |
||
| 172 | * This function is designed to be used by TUser descendant classes |
||
| 173 | * who want to store additional user information in user session. |
||
| 174 | * A variable, if stored in user session using {@see setState} can be |
||
| 175 | * retrieved back using this function. |
||
| 176 | * |
||
| 177 | 7 | * @param string $key variable name |
|
| 178 | * @param null|mixed $defaultValue default value |
||
| 179 | 7 | * @return mixed the value of the variable. If it doesn't exist, the provided default value will be returned |
|
| 180 | * @see setState |
||
| 181 | */ |
||
| 182 | protected function getState($key, $defaultValue = null) |
||
| 183 | { |
||
| 184 | return $this->_state[$key] ?? $defaultValue; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Stores a variable in user session. |
||
| 189 | * |
||
| 190 | * This function is designed to be used by TUser descendant classes |
||
| 191 | * who want to store additional user information in user session. |
||
| 192 | * By storing a variable using this function, the variable may be retrieved |
||
| 193 | * back later using {@see getState}. The variable will be persistent |
||
| 194 | * across page requests during a user session. |
||
| 195 | * |
||
| 196 | 11 | * @param string $key variable name |
|
| 197 | * @param mixed $value variable value |
||
| 198 | 11 | * @param null|mixed $defaultValue default value. If $value===$defaultValue, the variable will be removed from persistent storage. |
|
| 199 | 3 | * @see getState |
|
| 200 | */ |
||
| 201 | 11 | protected function setState($key, $value, $defaultValue = null) |
|
| 202 | { |
||
| 203 | 11 | if ($value === $defaultValue) { |
|
| 204 | 11 | unset($this->_state[$key]); |
|
| 205 | } else { |
||
| 206 | $this->_state[$key] = $value; |
||
| 207 | } |
||
| 208 | $this->_stateChanged = true; |
||
| 209 | 1 | } |
|
| 210 | |||
| 211 | 1 | /** |
|
| 212 | * @return bool whether user session state is changed (i.e., setState() is called) |
||
| 213 | */ |
||
| 214 | public function getStateChanged() |
||
| 215 | { |
||
| 216 | return $this->_stateChanged; |
||
| 217 | 1 | } |
|
| 218 | |||
| 219 | 1 | /** |
|
| 220 | 1 | * @param bool $value whether user session state is changed |
|
| 221 | */ |
||
| 222 | public function setStateChanged($value) |
||
| 223 | { |
||
| 224 | $this->_stateChanged = TPropertyValue::ensureBoolean($value); |
||
| 225 | } |
||
| 226 | } |
||
| 227 |