|
@@ -2,55 +2,39 @@
|
2
|
2
|
|
3
|
3
|
namespace Luticate\Auth\Business;
|
4
|
4
|
|
5
|
|
-use Illuminate\Http\Request;
|
6
|
5
|
use Luticate\Utils\LuBusiness;
|
7
|
6
|
use Luticate\Auth\DataAccess\LuticateUsersDataAccess;
|
8
|
7
|
use Luticate\Auth\DBO\LuticateUsersDbo;
|
9
|
8
|
|
10
|
9
|
class LuticateUsersBusiness extends LuBusiness {
|
11
|
|
- /**
|
12
|
|
- * @var LuticateUsersDbo
|
13
|
|
- */
|
14
|
|
- private static $_currentUser;
|
15
|
|
-
|
16
|
|
- const TOKEN_HEADER = "X-Authentication";
|
17
|
10
|
|
18
|
11
|
protected static function getDataAccess()
|
19
|
12
|
{
|
20
|
13
|
return new LuticateUsersDataAccess();
|
21
|
14
|
}
|
22
|
15
|
|
23
|
|
- public static function getCurrentUser()
|
|
16
|
+ protected static function badPassword()
|
|
17
|
+ {
|
|
18
|
+ abort(401, "Bad username/password");
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ public static function hashPassword($password)
|
|
22
|
+ {
|
|
23
|
+ return password_hash($password, PASSWORD_BCRYPT);
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ public static function verifyPassword($password, $hash)
|
24
|
27
|
{
|
25
|
|
- return self::$_currentUser;
|
|
28
|
+ return password_verify($password, $hash);
|
26
|
29
|
}
|
27
|
30
|
|
28
|
|
- /**
|
29
|
|
- * @param $permissions string[]
|
30
|
|
- * @param $request Request
|
31
|
|
- * @return bool
|
32
|
|
- */
|
33
|
|
- public static function authFilter($permissions, $request)
|
|
31
|
+ public static function login($username, $password)
|
34
|
32
|
{
|
35
|
|
- $token = $request->header(self::TOKEN_HEADER);
|
36
|
|
- if ($token != null && $token != "") {
|
37
|
|
- $data = JwtHelper::decode($token);
|
38
|
|
- if ($data != null) {
|
39
|
|
- $user_id = $data[JwtHelper::USER_KEY];
|
40
|
|
- self::$_currentUser = self::getById($user_id);
|
41
|
|
- }
|
42
|
|
- }
|
43
|
|
-
|
44
|
|
- if (self::$_currentUser == null && count($permissions) != 0) {
|
45
|
|
- return false;
|
46
|
|
- }
|
47
|
|
-
|
48
|
|
- foreach ($permissions as $permission) {
|
49
|
|
- $value = LuticatePermissionsBusiness::getUserPermission(self::$_currentUser->getId(), $permission);
|
50
|
|
- if (!$value->getVal()) {
|
51
|
|
- return false;
|
52
|
|
- }
|
53
|
|
- }
|
54
|
|
- return true;
|
|
33
|
+ $user = LuticateUsersDataAccess::getByUsername($username);
|
|
34
|
+ if (is_null($user))
|
|
35
|
+ self::badPassword();
|
|
36
|
+ if (!self::verifyPassword($password, $user->getPassword()))
|
|
37
|
+ self::badPassword();
|
|
38
|
+ return $user;
|
55
|
39
|
}
|
56
|
40
|
}
|