Browse Source

begin users

develop
Robin Thoni 8 years ago
parent
commit
fee8c52e16

+ 5
- 1
config.example.json View File

@@ -10,5 +10,9 @@
10 10
       "host": "localhost", "port": "5432", "database": "luticate",
11 11
       "username": "luticate", "password": "password"
12 12
     }
13
-  ]
13
+  ],
14
+  "settings": {
15
+    "MCRYPT_KEY": "ffffffffffffffffffffffff",
16
+    "JWT_KEY": "ffffffffffffffffffffffffffffffffffffffff"
17
+  }
14 18
 }

+ 12
- 4
src/Auth/Business/JwtHelper.php View File

@@ -8,16 +8,21 @@
8 8
 
9 9
 namespace Luticate\Auth\Business;
10 10
 
11
+use Luticate\Utils\Controller\LuticateApplication;
12
+
11 13
 class JwtHelper
12 14
 {
13 15
     const EXPIRATION_KEY =  "expiration_date";
16
+    const SETTING_MCRYPT_KEY = "MCRYPT_KEY";
17
+    const SETTING_JWT_KEY = "JWT_KEY";
14 18
 
15 19
     public static function decode($token)
16 20
     {
17
-        $jwt = mcrypt_decrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), base64_decode($token), "ecb");
21
+        $app = LuticateApplication::getInstance();
18 22
         try
19 23
         {
20
-            $data = (array)\JWT::decode($jwt, env("JWT_KEY"), ['HS256']);
24
+            $jwt = mcrypt_decrypt(MCRYPT_TRIPLEDES, $app->getSetting(self::SETTING_MCRYPT_KEY), base64_decode($token), "cbc");
25
+            $data = (array)\JWT::decode($jwt, $app->getSetting(self::SETTING_JWT_KEY), ['HS256']);
21 26
         }
22 27
         catch (\Exception $e)
23 28
         {
@@ -25,18 +30,21 @@ class JwtHelper
25 30
         }
26 31
 
27 32
         $expiration_date = array_key_exists(self::EXPIRATION_KEY, $data) ? $data[self::EXPIRATION_KEY] : null;
28
-        if (!is_numeric($expiration_date) || $expiration_date < time())
33
+        if (!is_numeric($expiration_date) || $expiration_date < time()) {
29 34
             return null;
35
+        }
30 36
 
31 37
         return $data;
32 38
     }
33 39
 
34 40
     public static function encode($data, $session_time)
35 41
     {
42
+        $app = LuticateApplication::getInstance();
36 43
         $date = new \DateTime("now", new \DateTimeZone("Europe/Paris"));
37 44
         $date->modify("+${session_time} day");
38 45
         $data[self::EXPIRATION_KEY] = $date->getTimestamp();
39 46
 
40
-        return base64_encode(mcrypt_encrypt(MCRYPT_TRIPLEDES, env("MCRYPT_KEY"), \JWT::encode($data, env("JWT_KEY")), "ecb"));
47
+        return base64_encode(mcrypt_encrypt(MCRYPT_TRIPLEDES, $app->getSetting(self::SETTING_MCRYPT_KEY),
48
+            \JWT::encode($data, $app->getSetting(self::SETTING_JWT_KEY)), "cbc"));
41 49
     }
42 50
 }

+ 35
- 0
src/Auth/Business/LuPermissionsBusiness.php View File

@@ -0,0 +1,35 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/2/16
6
+ * Time: 9:47 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Business;
10
+
11
+use Luticate\Auth\DataAccess\LuPermissionsDataAccess;
12
+use Luticate\Utils\Business\LuBusiness;
13
+
14
+class LuPermissionsBusiness extends LuBusiness
15
+{
16
+    public static function getDataAccess()
17
+    {
18
+        return new LuPermissionsDataAccess();
19
+    }
20
+
21
+    public static function getUserEffectivePermissionById(int $userId, int $permissionId)
22
+    {
23
+        return static::getDataAccess()->getUserEffectivePermissionById($userId, $permissionId);
24
+    }
25
+
26
+    public static function getUserEffectivePermissionByName(int $userId, string $permissionName)
27
+    {
28
+        return static::getDataAccess()->getUserEffectivePermissionByName($userId, $permissionName);
29
+    }
30
+
31
+    public static function getUserEffectivePermissions(int $userId)
32
+    {
33
+        return static::getDataAccess()->getUserEffectivePermissions($userId);
34
+    }
35
+}

+ 114
- 0
src/Auth/Business/LuUsersBusiness.php View File

@@ -0,0 +1,114 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/2/16
6
+ * Time: 10:15 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Business;
10
+
11
+use Illuminate\Database\Query\Builder;
12
+use Luticate\Auth\DataAccess\LuUserDataAccess;
13
+use Luticate\Auth\Dbo\LuUsersDbo;
14
+use Luticate\Auth\Dbo\LuUsersLiteDbo;
15
+use Luticate\Auth\Dbo\LuUsersLoginDbo;
16
+use Luticate\Auth\Dbo\LuUsersLoginResultDbo;
17
+use Luticate\Utils\Business\LuBusiness;
18
+use Luticate\Utils\Dbo\LuQueryDbo;
19
+
20
+class LuUsersBusiness extends LuBusiness
21
+{
22
+    const KEY_USER_ID =  "user_id";
23
+    const KEY_SALT =  "salt";
24
+    const KEY_DATA =  "data";
25
+
26
+    /**
27
+     * @return LuUserDataAccess
28
+     */
29
+    protected static function getDataAccess()
30
+    {
31
+        return new LuUserDataAccess();
32
+    }
33
+
34
+    protected static function badPassword()
35
+    {
36
+        static::unauthorized("Bad username/password");
37
+    }
38
+
39
+    public static function hashPassword($password)
40
+    {
41
+        return password_hash($password, PASSWORD_BCRYPT);
42
+    }
43
+
44
+    public static function verifyPassword($password, $hash)
45
+    {
46
+        return password_verify($password, $hash);
47
+    }
48
+
49
+    public static function getSalt($length = 10)
50
+    {
51
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
52
+        $charactersLength = strlen($characters);
53
+        $randomString = '';
54
+        for ($i = 0; $i < $length; $i++) {
55
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
56
+        }
57
+        return $randomString;
58
+    }
59
+
60
+    /**
61
+     * @param $user LuUsersDbo
62
+     * @param $data mixed
63
+     * @return string
64
+     */
65
+    public static function getToken($user, $data = null)
66
+    {
67
+        $session_time = 30;//LuticateSettingsBusiness::getValue("LU_SESSION_DAYS");
68
+        return JwtHelper::encode(array(
69
+            self::KEY_USER_ID => $user->getId(),
70
+            self::KEY_SALT => $user->getSalt(),
71
+            self::KEY_DATA => $data
72
+        ), $session_time);
73
+    }
74
+
75
+    public static function login(LuUsersLoginDbo $login)
76
+    {
77
+        $user = static::getDataAccess()->getByUsernameOrEmail($login->getUsername());
78
+        if (is_null($user))
79
+            self::badPassword();
80
+        if (!self::verifyPassword($login->getPassword(), $user->getPassword()))
81
+            self::badPassword();
82
+
83
+        /**
84
+         * @var $result LuUsersLoginResultDbo
85
+         */
86
+        $result = $user->castAs(LuUsersLoginResultDbo::class);
87
+        $result->setToken(self::getToken($user));
88
+        return $result;
89
+    }
90
+
91
+    public static function logout(LuUsersDbo $user)
92
+    {
93
+        if ($user->getId() != 0) {
94
+            $user->setSalt(self::getSalt());
95
+            static::getDataAccess()->editSingleById($user);
96
+        }
97
+        return true;
98
+    }
99
+
100
+    /**
101
+     * @param LuQueryDbo $query
102
+     * @return LuUsersLiteDbo[]
103
+     */
104
+    public static function getAllLite(LuQueryDbo $query)
105
+    {
106
+        return static::getDataAccess()->getAll($query)->map(function($user)
107
+        {
108
+            /**
109
+             * @var $user LuUsersDbo
110
+             */
111
+            return $user->castAs(LuUsersLiteDbo::class);
112
+        });
113
+    }
114
+}

+ 55
- 0
src/Auth/Controller/LuUsersController.php View File

@@ -0,0 +1,55 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/2/16
6
+ * Time: 9:45 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Controller;
10
+
11
+use Luticate\Auth\Business\LuUsersBusiness;
12
+use Luticate\Auth\Dbo\LuUsersDbo;
13
+use Luticate\Auth\Dbo\LuUsersLiteDbo;
14
+use Luticate\Auth\Dbo\LuUsersLoginDbo;
15
+use Luticate\Auth\Dbo\LuUsersLoginResultDbo;
16
+use Luticate\Utils\Controller\LuController;
17
+use Luticate\Utils\Dbo\LuQueryDbo;
18
+
19
+class LuUsersController extends LuController
20
+{
21
+    public function getBusiness()
22
+    {
23
+        return new LuUsersBusiness();
24
+    }
25
+
26
+    /**
27
+     * Login the user
28
+     * @param $login LuUsersLoginDbo The user authentication data
29
+     * @return LuUsersLoginResultDbo
30
+     */
31
+    public function login(LuUsersLoginDbo $login)
32
+    {
33
+        return static::getBusiness()->login($login);
34
+    }
35
+
36
+    /**
37
+     * Logout the logged user
38
+     * @param $_user LuUsersDbo The logged user
39
+     * @return bool
40
+     */
41
+    public function logout(LuUsersDbo $_user)
42
+    {
43
+        return static::getBusiness()->logout($_user);
44
+    }
45
+
46
+    /**
47
+     * Get all users
48
+     * @param LuQueryDbo $query The filter query
49
+     * @return LuUsersLiteDbo[]
50
+     */
51
+    public function getAllLite(LuQueryDbo $query)
52
+    {
53
+        return static::getBusiness()->getAllLite($query);
54
+    }
55
+}

+ 51
- 0
src/Auth/DataAccess/LuUserDataAccess.php View File

@@ -0,0 +1,51 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/7/16
6
+ * Time: 4:41 PM
7
+ */
8
+
9
+namespace Luticate\Auth\DataAccess;
10
+
11
+
12
+use Illuminate\Database\Query\Builder;
13
+use Luticate\Auth\Dbo\LuUsersDbo;
14
+use Luticate\Utils\DataAccess\LuDataAccess;
15
+use Luticate\Utils\Dbo\LuQueryDbo;
16
+
17
+class LuUserDataAccess extends LuDataAccess
18
+{
19
+    protected static $_connection = "luticatedb";
20
+    protected static $_table = "lu_users";
21
+    protected static $_dboClass = LuUsersDbo::class;
22
+
23
+    /**
24
+     * @param $username
25
+     * @return LuUsersDbo|null
26
+     */
27
+    public static function getByUsernameOrEmail($username)
28
+    {
29
+        return static::getSingle(function($q) use ($username)
30
+        {
31
+            /**
32
+             * @var $q Builder
33
+             */
34
+            $q->where("username", "=", $username)->orWhere("email", "=", $username);
35
+            return $q;
36
+        });
37
+    }
38
+
39
+    public static function getAll(LuQueryDbo $query)
40
+    {
41
+        return static::getMultiplePaginated(function ($q) use($query)
42
+        {
43
+            /**
44
+             * @var $q Builder
45
+             */
46
+            $q->whereRaw("sp_match_texts(:_query, username, email, firstname, lastname)");
47
+            return $q;
48
+        }, $query->getPage(), $query->getPerPage());
49
+    }
50
+
51
+}

+ 15
- 0
src/Auth/Dbo/LuBuiltInPermissions.php View File

@@ -0,0 +1,15 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/2/16
6
+ * Time: 11:18 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Dbo;
10
+
11
+
12
+class LuBuiltInPermissions
13
+{
14
+    const USER_LOGIN = "LU_USER_LOGIN";
15
+}

+ 209
- 0
src/Auth/Dbo/LuUsersDbo.php View File

@@ -0,0 +1,209 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/7/16
6
+ * Time: 4:36 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Dbo;
10
+
11
+use Luticate\Utils\Dbo\LuDbo;
12
+
13
+class LuUsersDbo extends LuDbo
14
+{
15
+    /**
16
+     * @var $_id int
17
+     */
18
+    protected $_id;
19
+
20
+    /**
21
+     * @var $_username string
22
+     */
23
+    protected $_username;
24
+
25
+    /**
26
+     * @var $_password string
27
+     * @nullable
28
+     */
29
+    protected $_password;
30
+
31
+    /**
32
+     * @var $_salt string
33
+     * @between 10 10
34
+     */
35
+    protected $_salt;
36
+
37
+    /**
38
+     * @var $_profileId int
39
+     * @nullable
40
+     */
41
+    protected $_profileId;
42
+
43
+    /**
44
+     * @var $_externalAuth int
45
+     * @nullable
46
+     */
47
+    protected $_externalAuth;
48
+
49
+    /**
50
+     * @var $_email string
51
+     */
52
+    protected $_email;
53
+
54
+    /**
55
+     * @var $_firstname string
56
+     * @nullable
57
+     */
58
+    protected $_firstname;
59
+
60
+    /**
61
+     * @var $_lastname string
62
+     * @nullable
63
+     */
64
+    protected $_lastname;
65
+
66
+    /**
67
+     * @return int
68
+     */
69
+    public function getId()
70
+    {
71
+        return $this->_id;
72
+    }
73
+
74
+    /**
75
+     * @param int $id
76
+     */
77
+    public function setId($id)
78
+    {
79
+        $this->_id = $id;
80
+    }
81
+
82
+    /**
83
+     * @return string
84
+     */
85
+    public function getUsername()
86
+    {
87
+        return $this->_username;
88
+    }
89
+
90
+    /**
91
+     * @param string $username
92
+     */
93
+    public function setUsername($username)
94
+    {
95
+        $this->_username = $username;
96
+    }
97
+
98
+    /**
99
+     * @return string
100
+     */
101
+    public function getPassword()
102
+    {
103
+        return $this->_password;
104
+    }
105
+
106
+    /**
107
+     * @param string $password
108
+     */
109
+    public function setPassword($password)
110
+    {
111
+        $this->_password = $password;
112
+    }
113
+
114
+    /**
115
+     * @return string
116
+     */
117
+    public function getSalt()
118
+    {
119
+        return $this->_salt;
120
+    }
121
+
122
+    /**
123
+     * @param string $salt
124
+     */
125
+    public function setSalt($salt)
126
+    {
127
+        $this->_salt = $salt;
128
+    }
129
+
130
+    /**
131
+     * @return int
132
+     */
133
+    public function getProfileId()
134
+    {
135
+        return $this->_profileId;
136
+    }
137
+
138
+    /**
139
+     * @param int $profileId
140
+     */
141
+    public function setProfileId($profileId)
142
+    {
143
+        $this->_profileId = $profileId;
144
+    }
145
+
146
+    /**
147
+     * @return int
148
+     */
149
+    public function getExternalAuth()
150
+    {
151
+        return $this->_externalAuth;
152
+    }
153
+
154
+    /**
155
+     * @param int $externalAuth
156
+     */
157
+    public function setExternalAuth($externalAuth)
158
+    {
159
+        $this->_externalAuth = $externalAuth;
160
+    }
161
+
162
+    /**
163
+     * @return string
164
+     */
165
+    public function getEmail()
166
+    {
167
+        return $this->_email;
168
+    }
169
+
170
+    /**
171
+     * @param string $email
172
+     */
173
+    public function setEmail($email)
174
+    {
175
+        $this->_email = $email;
176
+    }
177
+
178
+    /**
179
+     * @return string
180
+     */
181
+    public function getFirstname()
182
+    {
183
+        return $this->_firstname;
184
+    }
185
+
186
+    /**
187
+     * @param string $firstname
188
+     */
189
+    public function setFirstname($firstname)
190
+    {
191
+        $this->_firstname = $firstname;
192
+    }
193
+
194
+    /**
195
+     * @return string
196
+     */
197
+    public function getLastname()
198
+    {
199
+        return $this->_lastname;
200
+    }
201
+
202
+    /**
203
+     * @param string $lastname
204
+     */
205
+    public function setLastname($lastname)
206
+    {
207
+        $this->_lastname = $lastname;
208
+    }
209
+}

+ 167
- 0
src/Auth/Dbo/LuUsersLiteDbo.php View File

@@ -0,0 +1,167 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/8/16
6
+ * Time: 1:52 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Dbo;
10
+
11
+use Luticate\Utils\Dbo\LuDbo;
12
+
13
+class LuUsersLiteDbo extends LuDbo
14
+{
15
+    /**
16
+     * @var $_id int
17
+     */
18
+    protected $_id;
19
+
20
+    /**
21
+     * @var $_username string
22
+     */
23
+    protected $_username;
24
+
25
+    /**
26
+     * @var $_profileId int
27
+     * @nullable
28
+     */
29
+    protected $_profileId;
30
+
31
+    /**
32
+     * @var $_externalAuth int
33
+     * @nullable
34
+     */
35
+    protected $_externalAuth;
36
+
37
+    /**
38
+     * @var $_email string
39
+     */
40
+    protected $_email;
41
+
42
+    /**
43
+     * @var $_firstname string
44
+     * @nullable
45
+     */
46
+    protected $_firstname;
47
+
48
+    /**
49
+     * @var $_lastname string
50
+     * @nullable
51
+     */
52
+    protected $_lastname;
53
+
54
+    /**
55
+     * @return int
56
+     */
57
+    public function getId()
58
+    {
59
+        return $this->_id;
60
+    }
61
+
62
+    /**
63
+     * @param int $id
64
+     */
65
+    public function setId($id)
66
+    {
67
+        $this->_id = $id;
68
+    }
69
+
70
+    /**
71
+     * @return string
72
+     */
73
+    public function getUsername()
74
+    {
75
+        return $this->_username;
76
+    }
77
+
78
+    /**
79
+     * @param string $username
80
+     */
81
+    public function setUsername($username)
82
+    {
83
+        $this->_username = $username;
84
+    }
85
+
86
+    /**
87
+     * @return int
88
+     */
89
+    public function getProfileId()
90
+    {
91
+        return $this->_profileId;
92
+    }
93
+
94
+    /**
95
+     * @param int $profileId
96
+     */
97
+    public function setProfileId($profileId)
98
+    {
99
+        $this->_profileId = $profileId;
100
+    }
101
+
102
+    /**
103
+     * @return int
104
+     */
105
+    public function getExternalAuth()
106
+    {
107
+        return $this->_externalAuth;
108
+    }
109
+
110
+    /**
111
+     * @param int $externalAuth
112
+     */
113
+    public function setExternalAuth($externalAuth)
114
+    {
115
+        $this->_externalAuth = $externalAuth;
116
+    }
117
+
118
+    /**
119
+     * @return string
120
+     */
121
+    public function getEmail()
122
+    {
123
+        return $this->_email;
124
+    }
125
+
126
+    /**
127
+     * @param string $email
128
+     */
129
+    public function setEmail($email)
130
+    {
131
+        $this->_email = $email;
132
+    }
133
+
134
+    /**
135
+     * @return string
136
+     */
137
+    public function getFirstname()
138
+    {
139
+        return $this->_firstname;
140
+    }
141
+
142
+    /**
143
+     * @param string $firstname
144
+     */
145
+    public function setFirstname($firstname)
146
+    {
147
+        $this->_firstname = $firstname;
148
+    }
149
+
150
+    /**
151
+     * @return string
152
+     */
153
+    public function getLastname()
154
+    {
155
+        return $this->_lastname;
156
+    }
157
+
158
+    /**
159
+     * @param string $lastname
160
+     */
161
+    public function setLastname($lastname)
162
+    {
163
+        $this->_lastname = $lastname;
164
+    }
165
+    
166
+    
167
+}

+ 57
- 0
src/Auth/Dbo/LuUsersLoginDbo.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/7/16
6
+ * Time: 4:46 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Dbo;
10
+
11
+
12
+use Luticate\Utils\Dbo\LuDbo;
13
+
14
+class LuUsersLoginDbo extends LuDbo
15
+{
16
+    /**
17
+     * @var $_username string
18
+     */
19
+    protected $_username;
20
+
21
+    /**
22
+     * @var $_password string
23
+     */
24
+    protected $_password;
25
+
26
+    /**
27
+     * @return string
28
+     */
29
+    public function getUsername()
30
+    {
31
+        return $this->_username;
32
+    }
33
+
34
+    /**
35
+     * @param string $username
36
+     */
37
+    public function setUsername($username)
38
+    {
39
+        $this->_username = $username;
40
+    }
41
+
42
+    /**
43
+     * @return string
44
+     */
45
+    public function getPassword()
46
+    {
47
+        return $this->_password;
48
+    }
49
+
50
+    /**
51
+     * @param string $password
52
+     */
53
+    public function setPassword($password)
54
+    {
55
+        $this->_password = $password;
56
+    }
57
+}

+ 33
- 0
src/Auth/Dbo/LuUsersLoginResultDbo.php View File

@@ -0,0 +1,33 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/7/16
6
+ * Time: 4:53 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Dbo;
10
+
11
+class LuUsersLoginResultDbo extends LuUsersLiteDbo
12
+{
13
+    /**
14
+     * @var $_token string
15
+     */
16
+    protected $_token;
17
+
18
+    /**
19
+     * @return string
20
+     */
21
+    public function getToken()
22
+    {
23
+        return $this->_token;
24
+    }
25
+
26
+    /**
27
+     * @param string $token
28
+     */
29
+    public function setToken($token)
30
+    {
31
+        $this->_token = $token;
32
+    }
33
+}

+ 78
- 0
src/Auth/Middleware/LuAuthMiddleware.php View File

@@ -0,0 +1,78 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 7/2/16
6
+ * Time: 10:54 PM
7
+ */
8
+
9
+namespace Luticate\Auth\Middleware;
10
+
11
+use Luticate\Auth\Business\JwtHelper;
12
+use Luticate\Auth\Business\LuPermissionsBusiness;
13
+use Luticate\Auth\Business\LuUsersBusiness;
14
+use Luticate\Auth\Dbo\LuBuiltInPermissions;
15
+use Luticate\Auth\Dbo\LuUsersDbo;
16
+use Luticate\Utils\Business\LuBusiness;
17
+use Luticate\Utils\Business\LuLog;
18
+use Luticate\Utils\Middleware\LuAbstractMiddleware;
19
+
20
+class LuAuthMiddleware implements LuAbstractMiddleware
21
+{
22
+    const TOKEN_HEADER = "X-Luticate-Token";
23
+    
24
+    public function onBefore($_parameters, $_headers, $permissions = [])
25
+    {
26
+        $user = null;
27
+        $token = $_headers[self::TOKEN_HEADER] ?? null;
28
+        if ($token != null) {
29
+            $token = trim($token);
30
+            if ($token == "") {
31
+                $token = null;
32
+            }
33
+        }
34
+        if ($token != null) {
35
+            $data = JwtHelper::decode($token);
36
+            if ($data != null) {
37
+                /**
38
+                 * @var $user LuUsersDbo
39
+                 */
40
+                $user_id = intval($data[LuUsersBusiness::KEY_USER_ID]);
41
+                $salt = $data[LuUsersBusiness::KEY_SALT];
42
+                $user = LuUsersBusiness::getById($user_id);
43
+                if ($user->getSalt() !== $salt) {
44
+                    $user = null;
45
+                }
46
+            }
47
+        }
48
+
49
+        if (is_null($user)) {
50
+            if ($token != null) {
51
+                LuBusiness::unauthorized("Invalid token");
52
+            }
53
+            $user = LuUsersBusiness::getById(0);
54
+        }
55
+
56
+        $_parameters["_user"] = $user;
57
+
58
+        $perm = LuPermissionsBusiness::getUserEffectivePermissionByName($user->getId(), LuBuiltInPermissions::USER_LOGIN);
59
+        if (!$perm) {
60
+            LuBusiness::unauthorized("Account is disabled");
61
+        }
62
+        
63
+        foreach ($permissions as $permission) {
64
+            try {
65
+                $perm = LuPermissionsBusiness::getUserEffectivePermissionByName($user->getId(), $permission);
66
+                if (!$perm) {
67
+                    LuBusiness::unauthorized("Permission denied");
68
+                }
69
+            } catch (\Exception $e)
70
+            {
71
+                LuLog::log($e);
72
+                LuBusiness::unauthorized("Permission denied");
73
+            }
74
+        }
75
+
76
+        return $_parameters;
77
+    }
78
+}

Loading…
Cancel
Save