Completed
Push — master ( 659231...788af6 )
by Wanderson
05:06
created

User::getCurrentUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Win\Authentication;
4
5
use Win\Authentication\UserDAO;
6
use Win\Helper\Url;
7
use Win\Mvc\Block;
8
use Win\Mailer\Email;
9
10
/**
11
 * Usuários do sistema
12
 */
13
class User {
14
15
	const ACCESS_DENIED = 0;
16
	const ACCESS_ALLOWED = 1;
17
	const ACCESS_ADMIN = 2;
18
19
	private $id;
20
	private $isEnabled;
21
	private $isLogged;
22
	private $accessLevel;
23
	private $name;
24
	private $email;
25
	private $password;
26
	private $passwordHash;
27
	private $recoreryHash;
28
	private $image;
29
	private $lastLogin;
30
31
	/** @var Group */
32
	private $group;
33
	private $groupId;
34
35
	/** @var Person */
36
	private $person;
37
38
	public function __construct() {
39
		$this->id = 0;
40
		$this->isEnabled = true;
41
		$this->isLogged = false;
42
		$this->accessLevel = self::ACCESS_DENIED;
43
		$this->name = '';
44
		$this->email = '';
45
		$this->password = '********';
46
		$this->passwordHash = '';
47
		$this->recoreryHash = null;
48
		$this->image = null;
49
		$this->lastLogin = null;
50
		$this->group = null;
51
		$this->groupId = 0;
52
		$this->person = null;
53
	}
54
55
	public function getId() {
56
		return $this->id;
57
	}
58
59
	public function isEnabled() {
60
		return $this->isEnabled;
61
	}
62
63
	public function isLogged() {
64
		return $this->isLogged;
65
	}
66
67
	public function getAccessLevel() {
68
		return $this->accessLevel;
69
	}
70
71
	/** @return boolean */
72
	public function isMaster() {
73
		return ($this->accessLevel == self::ACCESS_ADMIN);
74
	}
75
76
	public function getGroup() {
77
		if (is_null($this->group)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
78
			// groupDAO
79
		}
80
		return $this->group;
81
	}
82
83
	public function getGroupId() {
84
		return $this->groupId;
85
	}
86
87
	public function getPerson() {
88
		if (is_null($this->person)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
89
			// personDAO
90
		}
91
		return $this->person;
92
	}
93
94
	public function getName() {
95
		return $this->name;
96
	}
97
98
	public function getEmail() {
99
		return $this->email;
100
	}
101
102
	public function getPassword() {
103
		return $this->password;
104
	}
105
106
	public function getPasswordHash() {
107
		return $this->passwordHash;
108
	}
109
110
	public function getRecoreryHash() {
111
		return $this->recoreryHash;
112
	}
113
114
	public function getImage() {
115
		return $this->image;
116
	}
117
118
	public function getLastLogin() {
119
		return $this->lastLogin;
120
	}
121
122
	public function setId($id) {
123
		$this->id = (int) $id;
124
	}
125
126
	public function setEnabled($enabled) {
127
		$this->isEnabled = (boolean) $enabled;
128
	}
129
130
	public function setAccessLevel($accessLevel) {
131
		$this->accessLevel = (int) $accessLevel;
132
	}
133
134
	public function setGroup(Group $group) {
135
		$this->group = $group;
136
	}
137
138
	public function setGroupId($groupId) {
139
		$this->groupId = (int) $groupId;
140
	}
141
142
	public function setPerson(Person $person) {
143
		$this->person = $person;
144
	}
145
146
	public function setName($name) {
147
		$this->name = $name;
148
	}
149
150
	public function setEmail($email) {
151
		$this->email = $email;
152
	}
153
154
	public function setPassword($password) {
155
		$this->password = $password;
156
		$this->passwordHash = md5($password);
157
	}
158
159
	public function setPasswordHash($passwordHash) {
160
		$this->passwordHash = $passwordHash;
161
	}
162
163
	public function setRecoreryHash($recoreryHash) {
164
		$this->recoreryHash = $recoreryHash;
165
	}
166
167
	public function setLastLogin($lastLogin) {
168
		$this->lastLogin = $lastLogin;
169
	}
170
171
	public function setImage($image) {
172
		$this->image = $image;
173
	}
174
175
	/**
176
	 * Tenta realizar login
177
	 * @return boolean
178
	 */
179
	public function login() {
180
		$filters = [
181
			'is_enabled = ?' => true,
182
			'access_level > ?' => 0,
183
			'email = ?' => $this->email,
184
			'password_hash = ?' => $this->passwordHash
185
		];
186
		$uDAO = new UserDAO();
187
		$user = $uDAO->fetch($filters);
0 ignored issues
show
Documentation introduced by
$filters is of type array<string,boolean|int...rd_hash = ?":"string"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
188
189
		if ($user->getId() > 0) {
190
			$this->setCurrentUser($user);
191
			$uDAO->updateLastLogin($user);
192
		}
193
		return $user->isLogged;
194
	}
195
196
	/** Realiza logout */
197
	public function logout() {
198
		unset($_SESSION['user']);
199
	}
200
201
	/** Objeto > Sessão */
202
	private function setCurrentUser(User $user) {
203
		$_SESSION['user'] = $user;
204
		$this->isLogged = true;
205
		$this->id = $user->getId();
206
		$this->accessLevel = $user->getAccessLevel();
207
		$this->name = $user->getName();
208
		$this->lastLogin = $user->getLastLogin();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->lastLogin is correct as $user->getLastLogin() (which targets Win\Authentication\User::getLastLogin()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
209
	}
210
211
	/** Objeto < Sessão */
212
	public static function getCurrentUser() {
213
		return (isset($_SESSION['user'])) ? $_SESSION['user'] : new User();
214
	}
215
216
	/** Obriga o usuário a se logar */
217
	public function requireLogin() {
218
		if (!$this->isLogged) {
219
			Url::instance()->redirect('login');
220
		}
221
	}
222
223
	/** Obriga o usuário a logar como ADMIN */
224
	public function requireMaster() {
225
		if (!$this->isLogged || $this->getAccessLevel() != static::ACCESS_ADMIN) {
226
			Url::instance()->redirect('login');
227
		}
228
	}
229
230
	/**
231
	 * Envia link de recuperacao de senha via Email
232
	 * @return boolean
233
	 */
234
	public function sendRecoveryHash() {
235
		$success = false;
236
		$filters = ['is_enabled = ?' => true, 'access_level > ?' => 0, 'email = ?' => $this->email];
237
		$uDAO = new UserDAO();
238
		$user = $uDAO->fetch($filters);
0 ignored issues
show
Documentation introduced by
$filters is of type array<string,boolean|int...,"email = ?":"string"}>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
239
240
		if ($user->getId() > 0) {
241
			$success = true;
242
			$uDAO->updateRecoveryHash($user);
243
			$body = new Block('email/html/recovery-password', ['user' => $user]);
244
245
			$mail = new Email();
246
			$mail->setSubject('Recuperação de Senha');
247
			$mail->addAddress($user->getEmail(), $user->getName());
248
			$mail->setBody($body);
249
			$mail->send();
250
		}
251
		return $success;
252
	}
253
254
	/** Define os atributos que são salvos na SESSAO */
255
	public function __sleep() {
256
		return ['id', 'isEnabled', 'isLogged', 'accessLevel', 'name', 'email', 'image', 'last_login', 'group_id'];
257
	}
258
259
}
260