1
|
|
|
<?php |
2
|
|
|
namespace CJ\User; |
3
|
|
|
|
4
|
|
|
use \Anax\Database\ActiveRecordModel; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A database driven model. |
8
|
|
|
*/ |
9
|
|
|
class User extends ActiveRecordModel |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string $tableName name of the database table. |
13
|
|
|
*/ |
14
|
|
|
protected $tableName = "User"; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Columns in the table. |
18
|
|
|
* |
19
|
|
|
* @var integer $id primary key auto incremented. |
20
|
|
|
*/ |
21
|
|
|
public $id; |
22
|
|
|
public $mail; |
23
|
|
|
public $password; |
24
|
|
|
public $created; |
25
|
|
|
public $updated; |
26
|
|
|
public $deleted; |
27
|
|
|
public $active; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creating $session var for inject |
32
|
|
|
*/ |
33
|
|
|
private $session; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Init the class with session and database |
37
|
|
|
*/ |
38
|
10 |
|
public function init($db, $session) |
39
|
|
|
{ |
40
|
10 |
|
$this->session = $session; |
41
|
10 |
|
$this->setDb($db); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set the password. |
46
|
|
|
* |
47
|
|
|
* @param string $password the password to use. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
2 |
|
public function setPassword($password) |
52
|
|
|
{ |
53
|
2 |
|
$this->password = password_hash($password, PASSWORD_DEFAULT); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Verify the acronym and the password, if successful the object contains |
59
|
|
|
* all details from the database row. |
60
|
|
|
* |
61
|
|
|
* @param string $acronym acronym to check. |
|
|
|
|
62
|
|
|
* @param string $password the password to use. |
63
|
|
|
* |
64
|
|
|
* @return boolean true if acronym and password matches, else false. |
65
|
|
|
*/ |
66
|
1 |
|
public function verifyPassword($mail, $password) |
67
|
|
|
{ |
68
|
1 |
|
$this->find("mail", $mail); |
69
|
1 |
|
return password_verify($password, $this->password); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* Get all users. |
74
|
|
|
*/ |
75
|
1 |
|
public function getAllUsers() |
76
|
|
|
{ |
77
|
1 |
|
return $this->findAll(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
* Get all users. |
83
|
|
|
*/ |
84
|
2 |
|
public function deleteUser($id) |
85
|
|
|
{ |
86
|
2 |
|
$this->find("id", $id); |
87
|
2 |
|
$this->delete(); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* Get a user based on ID |
93
|
|
|
*/ |
94
|
1 |
|
public function getUser($id) |
95
|
|
|
{ |
96
|
1 |
|
return $this->find("id", $id); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* Get a user based on ID |
101
|
|
|
*/ |
102
|
2 |
|
public function isLoggedIn() |
103
|
|
|
{ |
104
|
2 |
|
return $this->session->has("user"); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/* |
109
|
|
|
* Get a user based on ID |
110
|
|
|
*/ |
111
|
2 |
|
public function getLoggedInUserId() |
112
|
|
|
{ |
113
|
2 |
|
return $this->session->get("user"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/* |
118
|
|
|
* Get logged in user |
119
|
|
|
* @return user class |
120
|
|
|
*/ |
121
|
1 |
|
public function getLoggedInUser() |
122
|
|
|
{ |
123
|
1 |
|
if ($this->session->has("user")) { |
124
|
1 |
|
return $this->find("id", $this->session->get("user")); |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
1 |
|
public function getUserImg($mail, $classes = "", $size = 125) |
130
|
|
|
{ |
131
|
1 |
|
$hash = md5($mail); |
132
|
|
|
|
133
|
1 |
|
$html = "<img src='https://www.gravatar.com/avatar/$hash?s=$size&default=mm' class='$classes'>"; |
134
|
1 |
|
return $html; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/* |
139
|
|
|
* Return true is user is admin |
140
|
|
|
*/ |
141
|
1 |
|
public function isUserAdmin() |
142
|
|
|
{ |
143
|
1 |
|
if ($this->isLoggedIn()) { |
144
|
1 |
|
$id = $this->getLoggedInUserId(); |
145
|
|
|
|
146
|
1 |
|
$this->find("id", $id); |
147
|
1 |
|
if ($this->userType == "admin") { |
|
|
|
|
148
|
1 |
|
return true; |
149
|
|
|
} |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
|
return false; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.