|
@@ -8,7 +8,7 @@
|
8
|
8
|
|
9
|
9
|
namespace Luticate\Auth\Business;
|
10
|
10
|
|
11
|
|
-use Luticate\Auth\DataAccess\LuUserDataAccess;
|
|
11
|
+use Luticate\Auth\DataAccess\LuUsersDataAccess;
|
12
|
12
|
use Luticate\Auth\Dbo\LuBuiltInPermissions;
|
13
|
13
|
use Luticate\Auth\Dbo\Users\LuUsersAddDbo;
|
14
|
14
|
use Luticate\Auth\Dbo\Users\LuUsersDbo;
|
|
@@ -27,36 +27,36 @@ class LuUsersBusiness extends LuBusiness
|
27
|
27
|
const KEY_DATA = "data";
|
28
|
28
|
|
29
|
29
|
/**
|
30
|
|
- * @return LuUserDataAccess
|
|
30
|
+ * @return LuUsersDataAccess
|
31
|
31
|
*/
|
32
|
32
|
protected static function getDataAccess()
|
33
|
33
|
{
|
34
|
|
- return new LuUserDataAccess();
|
|
34
|
+ return new LuUsersDataAccess();
|
35
|
35
|
}
|
36
|
36
|
|
37
|
|
- protected function badPassword()
|
|
37
|
+ protected static function badPassword()
|
38
|
38
|
{
|
39
|
39
|
static::unauthorized("Bad username/password");
|
40
|
40
|
}
|
41
|
41
|
|
42
|
|
- public function hashPassword(string $password)
|
|
42
|
+ public static function hashPassword(string $password)
|
43
|
43
|
{
|
44
|
44
|
return password_hash($password, PASSWORD_BCRYPT);
|
45
|
45
|
}
|
46
|
46
|
|
47
|
|
- public function verifyPassword(string $password, string $hash)
|
|
47
|
+ public static function verifyPassword(string $password, string $hash)
|
48
|
48
|
{
|
49
|
49
|
return password_verify($password, $hash);
|
50
|
50
|
}
|
51
|
51
|
|
52
|
|
- public function checkPasswordRequirements(string $password)
|
|
52
|
+ public static function checkPasswordRequirements(string $password)
|
53
|
53
|
{
|
54
|
54
|
if (strlen($password) < 5) { //TODO: add a setting
|
55
|
55
|
self::badInput("Password must have at least 5 characters");
|
56
|
56
|
}
|
57
|
57
|
}
|
58
|
58
|
|
59
|
|
- public function getSalt($length = 16)
|
|
59
|
+ public static function getSalt($length = 16)
|
60
|
60
|
{
|
61
|
61
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
62
|
62
|
$charactersLength = strlen($characters);
|
|
@@ -67,7 +67,7 @@ class LuUsersBusiness extends LuBusiness
|
67
|
67
|
return $randomString;
|
68
|
68
|
}
|
69
|
69
|
|
70
|
|
- public function getToken(LuUsersDbo $user, $data = null)
|
|
70
|
+ public static function getToken(LuUsersDbo $user, $data = null)
|
71
|
71
|
{
|
72
|
72
|
$session_time = 30;//TODO add a setting
|
73
|
73
|
return JwtHelper::encode(array(
|
|
@@ -121,13 +121,24 @@ class LuUsersBusiness extends LuBusiness
|
121
|
121
|
}
|
122
|
122
|
}
|
123
|
123
|
|
124
|
|
- public function login(LuUsersLoginDbo $login)
|
|
124
|
+ public static function getByUsernameOrEmail(string $name)
|
|
125
|
+ {
|
|
126
|
+ $user = static::getDataAccess()->getByUsernameOrEmail($name);
|
|
127
|
+ if (is_null($user)) {
|
|
128
|
+ static::notFound(static::getResourceName() . " not found");
|
|
129
|
+ }
|
|
130
|
+ return $user;
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ public static function login(LuUsersLoginDbo $login)
|
125
|
134
|
{
|
126
|
135
|
$user = static::getDataAccess()->getByUsernameOrEmail($login->getUsername());
|
127
|
|
- if (is_null($user))
|
|
136
|
+ if (is_null($user)) {
|
128
|
137
|
self::badPassword();
|
129
|
|
- if (!self::verifyPassword($login->getPassword(), $user->getPassword()))
|
|
138
|
+ }
|
|
139
|
+ if (!self::verifyPassword($login->getPassword(), $user->getPassword())) {
|
130
|
140
|
self::badPassword();
|
|
141
|
+ }
|
131
|
142
|
|
132
|
143
|
/**
|
133
|
144
|
* @var $result LuUsersLoginResultDbo
|
|
@@ -137,7 +148,7 @@ class LuUsersBusiness extends LuBusiness
|
137
|
148
|
return $result;
|
138
|
149
|
}
|
139
|
150
|
|
140
|
|
- public function logout(LuUsersDbo $user)
|
|
151
|
+ public static function logout(LuUsersDbo $user)
|
141
|
152
|
{
|
142
|
153
|
if ($user->getId() != 0) {
|
143
|
154
|
$user->setSalt(self::getSalt());
|
|
@@ -146,7 +157,7 @@ class LuUsersBusiness extends LuBusiness
|
146
|
157
|
return true;
|
147
|
158
|
}
|
148
|
159
|
|
149
|
|
- public function getSingleLiteById($userId)
|
|
160
|
+ public static function getSingleLiteById($userId)
|
150
|
161
|
{
|
151
|
162
|
/**
|
152
|
163
|
* @var $user LuUsersDbo
|
|
@@ -159,7 +170,7 @@ class LuUsersBusiness extends LuBusiness
|
159
|
170
|
* @param LuQueryDbo $query
|
160
|
171
|
* @return LuPaginatedDbo
|
161
|
172
|
*/
|
162
|
|
- public function getAllLite(LuQueryDbo $query)
|
|
173
|
+ public static function getAllLite(LuQueryDbo $query)
|
163
|
174
|
{
|
164
|
175
|
return static::getDataAccess()->getAll($query)->map(function($user)
|
165
|
176
|
{
|
|
@@ -170,7 +181,7 @@ class LuUsersBusiness extends LuBusiness
|
170
|
181
|
});
|
171
|
182
|
}
|
172
|
183
|
|
173
|
|
- public function add(LuUsersAddDbo $user)
|
|
184
|
+ public static function add(LuUsersAddDbo $user)
|
174
|
185
|
{
|
175
|
186
|
static::checkPasswordRequirements($user->getPassword());
|
176
|
187
|
|
|
@@ -205,7 +216,7 @@ class LuUsersBusiness extends LuBusiness
|
205
|
216
|
return self::getById($id);
|
206
|
217
|
}
|
207
|
218
|
|
208
|
|
- public function del(int $userId)
|
|
219
|
+ public static function del(int $userId)
|
209
|
220
|
{
|
210
|
221
|
$user = static::getSingleLiteById($userId);
|
211
|
222
|
if ($userId != 0) {
|
|
@@ -214,7 +225,7 @@ class LuUsersBusiness extends LuBusiness
|
214
|
225
|
return $user;
|
215
|
226
|
}
|
216
|
227
|
|
217
|
|
- public function edit(int $userId, LuUsersEditDbo $user)
|
|
228
|
+ public static function edit(int $userId, LuUsersEditDbo $user)
|
218
|
229
|
{
|
219
|
230
|
$existingUser = static::getSingleLiteById($userId);
|
220
|
231
|
if (!filter_var($user->getEmail(), FILTER_VALIDATE_EMAIL)) {
|
|
@@ -234,9 +245,9 @@ class LuUsersBusiness extends LuBusiness
|
234
|
245
|
return static::getSingleLiteById($existingUser->getId());
|
235
|
246
|
}
|
236
|
247
|
|
237
|
|
- public function setPassword(int $userId, string $password)
|
|
248
|
+ public static function setPassword(int $userId, string $password)
|
238
|
249
|
{
|
239
|
|
- $this->checkPasswordRequirements($password);
|
|
250
|
+ static::checkPasswordRequirements($password);
|
240
|
251
|
|
241
|
252
|
/**
|
242
|
253
|
* @var $existingUser LuUsersDbo
|
|
@@ -250,7 +261,7 @@ class LuUsersBusiness extends LuBusiness
|
250
|
261
|
return true;
|
251
|
262
|
}
|
252
|
263
|
|
253
|
|
- public function setPasswordMe(LuUsersDbo $_user, string $password, string $oldPassword)
|
|
264
|
+ public static function setPasswordMe(LuUsersDbo $_user, string $password, string $oldPassword)
|
254
|
265
|
{
|
255
|
266
|
$loginDbo = new LuUsersLoginDbo();
|
256
|
267
|
$loginDbo->setUsername($_user->getUsername());
|