Passed
Push — master ( 83ba19...3d13ab )
by Fabio
04:49
created

TUser::loadFromString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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