Completed
Push — master ( aeb1e8...e32bea )
by Wanderson
02:48
created

User::toSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
namespace Win\Authentication;
4
5
use Win\Authentication\UserDAO;
6
7
/**
8
 * Usuários do sistema
9
 */
10
class User {
11
12
	const ACCESS_DISALLOWED = 0;
13
	const ACCESS_ALLOWED = 1;
14
	const ACCESS_ADMIN = 2;
15
16
	private $id;
17
	private $isActive;
18
	private $isLogged;
19
	private $accessLevel;
20
21
	/** @var Group */
22
	private $group;
23
	private $groupId;
24
25
	/** @var Person */
26
	private $person;
27
28
	/** @var string */
29
	private $name;
30
	private $email;
31
	private $password;
32
	private $passwordHash;
33
	private $recoreryHash;
34
	private $image;
35
	private $lastLogin;
36
37
	public function __construct() {
38
		$this->id = 0;
39
		$this->isActive = true;
40
		$this->isLogged = false;
41
		$this->accessLevel = self::ACCESS_DISALLOWED;
42
		$this->group = null;
43
		$this->groupId = 0;
44
		$this->person = null;
45
		$this->name = '';
46
		$this->email = '';
47
		$this->password = '********';
48
		$this->passwordHash = '';
49
		$this->recoreryHash = null;
50
		$this->image = null;
51
		$this->lastLogin = null;
52
53
		if (isset($_SESSION['user'])) {
54
			$this->fromSession();
55
		}
56
	}
57
58
	public function getId() {
59
		return $this->id;
60
	}
61
62
	public function isActive() {
63
		return $this->isActive;
64
	}
65
66
	public function isLogged() {
67
		return $this->isLogged;
68
	}
69
70
	public function getAccessLevel() {
71
		return $this->accessLevel;
72
	}
73
74
	/** @return boolean */
75
	public function isMaster() {
76
		return ($this->accessLevel == self::ACCESS_ADMIN);
77
	}
78
79
	public function getGroup() {
80
		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...
81
			// groupDAO
82
		}
83
		return $this->group;
84
	}
85
86
	public function getGroupId() {
87
		return $this->groupId;
88
	}
89
90
	public function getPerson() {
91
		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...
92
			// personDAO
93
		}
94
		return $this->person;
95
	}
96
97
	public function getName() {
98
		return $this->name;
99
	}
100
101
	public function getEmail() {
102
		return $this->email;
103
	}
104
105
	public function getPassword() {
106
		return $this->password;
107
	}
108
109
	public function getPasswordHash() {
110
		return $this->passwordHash;
111
	}
112
113
	public function getRecoreryHash() {
114
		return $this->recoreryHash;
115
	}
116
117
	public function getImage() {
118
		return $this->image;
119
	}
120
121
	public function getLastLogin() {
122
		return $this->lastLogin;
123
	}
124
125
	public function setId($id) {
126
		$this->id = (int) $id;
127
	}
128
129
	public function setActive($active) {
130
		$this->isActive = (boolean) $active;
131
	}
132
133
	public function setAccessLevel($accessLevel) {
134
		$this->accessLevel = (int) $accessLevel;
135
	}
136
137
	public function setGroup(Group $group) {
138
		$this->group = $group;
139
	}
140
141
	public function setGroupId($groupId) {
142
		$this->groupId = (int) $groupId;
143
	}
144
145
	public function setPerson(Person $person) {
146
		$this->person = $person;
147
	}
148
149
	public function setName($name) {
150
		$this->name = $name;
151
	}
152
153
	public function setEmail($email) {
154
		$this->email = $email;
155
	}
156
157
	public function setPassword($password) {
158
		$this->password = $password;
159
		$this->passwordHash = md5($password);
160
	}
161
162
	public function setPasswordHash($passwordHash) {
163
		$this->passwordHash = $passwordHash;
164
	}
165
166
	public function setRecoreryHash($recoreryHash) {
167
		$this->recoreryHash = $recoreryHash;
168
	}
169
170
	public function setLastLogin($lastLogin) {
171
		$this->lastLogin = $lastLogin;
172
	}
173
174
	public function setImage($image) {
175
		$this->image = $image;
176
	}
177
178
	/**
179
	 * Tenta realizar login
180
	 * @return boolean
181
	 */
182
	public function login() {
183
		$filterLogin = [
184
			'is_active = ?' => true,
185
			'access_level > ?' => 0,
186
			'email = ?' => $this->email,
187
			'password_hash = ?' => $this->passwordHash,
188
		];
189
		$userDAO = new UserDAO();
190
		$users = $userDAO->fetchAll($filterLogin);
0 ignored issues
show
Documentation introduced by
$filterLogin 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...
191
		if (count($users) > 0) {
192
			$this->toSession($users[0]);
193
			$this->fromSession();
194
			$this->updateLastLogin($users[0]);
195
			return true;
196
		}
197
		return false;
198
	}
199
200
	/**
201
	 * Realiza logout
202
	 */
203
	public function logout() {
204
		unset($_SESSION['user']);
205
		$this->__construct();
206
	}
207
208
	/** Objeto > Sessão */
209
	private function toSession(User $user) {
210
		$user->isLogged = TRUE;
211
		$_SESSION['user'] = [
212
			'logged' => $user->isLogged(),
213
			'id' => $user->getId(),
214
			'access_level' => $user->getAccessLevel(),
215
			'group_id' => $user->getGroupId(),
216
			'name' => $user->getName(),
217
			'email' => $user->getEmail(),
218
			'image' => $user->getImage(),
219
			'last_login' => $user->getLastLogin()
220
		];
221
	}
222
223
	/** Objeto < Sessão */
224
	private function fromSession() {
225
		$session = $_SESSION['user'];
226
		$this->isLogged = true;
227
		$this->id = $session['id'];
228
		$this->accessLevel = $session['access_level'];
229
		$this->groupId = $session['group_id'];
230
		$this->name = $session['name'];
231
		$this->email = $session['email'];
232
		$this->image = $session['image'];
233
		$this->lastLogin = $session['last_login'];
234
	}
235
236
	/** Atualiza data ultimo login */
237
	private function updateLastLogin(User $user) {
238
		$userDAO = new UserDAO();
239
		$now = date('Y-m-d H:i:s');
240
		$user->setLastLogin($now);
241
		$userDAO->save($user);
242
	}
243
244
}
245