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