1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Peto16\User; |
4
|
|
|
|
5
|
|
|
class UserService |
6
|
|
|
{ |
7
|
|
|
private $userStorage; |
8
|
|
|
private $session; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Constructor for UserService |
14
|
|
|
* @param object $di dependency injection. |
15
|
|
|
*/ |
16
|
17 |
|
public function __construct(\Anax\DI\DIFactoryConfig $di) |
17
|
|
|
{ |
18
|
17 |
|
$this->userStorage = new UserStorage(); |
19
|
17 |
|
$this->userStorage->setDb($di->get("db")); |
20
|
17 |
|
$this->session = $di->get("session"); |
21
|
17 |
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create user. |
27
|
|
|
* |
28
|
|
|
* @param object $user User object to store. |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
1 |
|
public function createUser(User $user) |
32
|
|
|
{ |
33
|
1 |
|
if ($this->userStorage->getUserByField("email", $user->email)) { |
34
|
1 |
|
throw new Exception("E-postadress används redan."); |
35
|
|
|
} |
36
|
1 |
|
if ($this->userStorage->getUserByField("username", $user->username)) { |
37
|
1 |
|
throw new Exception("Användarnamn redan taget."); |
38
|
|
|
} |
39
|
1 |
|
$this->userStorage->createUser($user); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Update user. |
46
|
|
|
* |
47
|
|
|
* @param object $user User object to update. |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
2 |
|
public function updateUser($user) |
51
|
|
|
{ |
52
|
2 |
|
$this->userStorage->updateUser($user); |
53
|
2 |
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Delete user. Validates if user is admin to be able to delete |
59
|
|
|
* |
60
|
|
|
* @param integer $id user id. |
61
|
|
|
* |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
2 |
|
public function deleteUser($id) |
65
|
|
|
{ |
66
|
2 |
|
if ($this->validLoggedInAdmin()) { |
67
|
2 |
|
return $this->userStorage->deleteUser($id); |
68
|
|
|
} |
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dynamicly get user by propertie. |
76
|
|
|
* |
77
|
|
|
* @param string $field field to search by. |
78
|
|
|
* |
79
|
|
|
* @param array $data to search for. |
80
|
|
|
* |
81
|
|
|
* @return User |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
10 |
|
public function getUserByField($field, $data) |
85
|
|
|
{ |
86
|
10 |
|
$user = new User(); |
87
|
10 |
|
$userVarArray = get_object_vars($user); |
88
|
10 |
|
$arrayKeys = array_keys($userVarArray); |
89
|
10 |
|
$userData = $this->userStorage->getUserByField($field, $data); |
90
|
10 |
|
if (empty($userData)) { |
91
|
2 |
|
return $user; |
92
|
|
|
} |
93
|
9 |
|
foreach ($arrayKeys as $key) { |
94
|
9 |
|
$user->{$key} = $userData->$key; |
95
|
9 |
|
} |
96
|
9 |
|
return $user; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Find all users stored. |
103
|
|
|
* |
104
|
|
|
* @return array Of users |
105
|
|
|
*/ |
106
|
2 |
|
public function findAllUsers() |
107
|
|
|
{ |
108
|
2 |
|
return $this->userStorage->findAllUsers(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if user is logged in. |
115
|
|
|
* |
116
|
|
|
* @return boolean |
117
|
|
|
*/ |
118
|
2 |
|
public function checkLoggedin() |
119
|
|
|
{ |
120
|
2 |
|
return $this->session->has("user"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Login user and redirect to admin. |
127
|
|
|
* |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
2 |
|
public function login($username, $password) |
131
|
|
|
{ |
132
|
2 |
|
$user = $this->getUserByField("username", $username); |
133
|
|
|
|
134
|
2 |
|
if ($password === null) { |
135
|
2 |
|
throw new Exception("Empty password field."); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
if ($user->id === null) { |
139
|
1 |
|
throw new Exception("Error, not valid credentials."); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
if ($user->deleted !== null) { |
143
|
|
|
throw new Exception("User deleted."); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
if ((int)$user->enabled === 0) { |
147
|
1 |
|
throw new Exception("Error, disabled account."); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
if ($this->validatePassword($password, $user->password)) { |
151
|
1 |
|
$this->session->set("user", $user); |
152
|
1 |
|
return true; |
153
|
|
|
} |
154
|
1 |
|
throw new Exception("Error, not valid credentials."); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Check if a user is logged in and returns that user |
161
|
|
|
* |
162
|
|
|
* @return obj user or null |
163
|
|
|
*/ |
164
|
6 |
|
public function getCurrentLoggedInUser() |
165
|
|
|
{ |
166
|
6 |
|
return $this->session->get("user"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Validate pasword |
173
|
|
|
* |
174
|
|
|
* @method password_verify Method to verify password |
175
|
|
|
* |
176
|
|
|
* @param string $password Password to be validated. |
177
|
|
|
* |
178
|
|
|
* @return boolean Return true if valid else false. |
179
|
|
|
*/ |
180
|
1 |
|
private function validatePassword($password, $dbpassword) |
181
|
|
|
{ |
182
|
1 |
|
return password_verify($password, $dbpassword); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Check if logged in user is valid and admin. |
189
|
|
|
* |
190
|
|
|
* @return boolean Returns true or false if user is valid administrator. |
191
|
|
|
*/ |
192
|
2 |
|
public function validLoggedInAdmin() |
193
|
|
|
{ |
194
|
2 |
|
$loggedInUser = $this->getCurrentLoggedInUser(); |
195
|
|
|
if ($loggedInUser |
196
|
2 |
|
&& $loggedInUser->administrator |
197
|
2 |
|
&& $loggedInUser->deleted === null |
198
|
2 |
|
&& $loggedInUser->enabled) { |
199
|
2 |
|
return true; |
200
|
|
|
} |
201
|
1 |
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Generate gravatar from email or return default avatar. |
208
|
|
|
* |
209
|
|
|
* @param string $email email adress |
210
|
|
|
* @return string Gravatar url. |
211
|
|
|
*/ |
212
|
1 |
|
public function generateGravatarUrl($email = "") |
213
|
|
|
{ |
214
|
1 |
|
if ($email === "") { |
215
|
1 |
|
return "http://www.gravatar.com/avatar/?d=identicon"; |
216
|
|
|
} |
217
|
1 |
|
return "https://s.gravatar.com/avatar/" . md5(strtolower(trim($email))); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|