|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Licensed under The GPL-3.0 License |
|
4
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
|
5
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 2.0.0 |
|
8
|
|
|
* @author Christopher Castro <[email protected]> |
|
9
|
|
|
* @link http://www.quickappscms.org |
|
10
|
|
|
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
use Cake\Cache\Cache; |
|
14
|
|
|
use Cake\I18n\I18n; |
|
15
|
|
|
use Cake\Network\Session; |
|
16
|
|
|
use Cake\ORM\Entity; |
|
17
|
|
|
use Cake\ORM\TableRegistry; |
|
18
|
|
|
use Cake\Routing\Router; |
|
19
|
|
|
use User\Model\Entity\UserSession; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* These are hard-coded values for user roles and must match values stored in |
|
23
|
|
|
* "roles" DB table. |
|
24
|
|
|
*/ |
|
25
|
|
|
if (!defined('ROLE_ID_ADMINISTRATOR')) { |
|
26
|
|
|
/** |
|
27
|
|
|
* ID for "administrator" role, must match the ID stored in DB. You should |
|
28
|
|
|
* never change this value on production site. |
|
29
|
|
|
*/ |
|
30
|
|
|
define('ROLE_ID_ADMINISTRATOR', 1); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (!defined('ROLE_ID_AUTHENTICATED')) { |
|
34
|
|
|
/** |
|
35
|
|
|
* ID for "authenticated" role, must match the ID stored in DB. You should |
|
36
|
|
|
* never change this value on production site. |
|
37
|
|
|
*/ |
|
38
|
|
|
define('ROLE_ID_AUTHENTICATED', 2); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (!defined('ROLE_ID_ANONYMOUS')) { |
|
42
|
|
|
/** |
|
43
|
|
|
* ID for "anonymous" role, must match the ID stored in DB. You should |
|
44
|
|
|
* never change this value on production site. |
|
45
|
|
|
*/ |
|
46
|
|
|
define('ROLE_ID_ANONYMOUS', 3); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!defined('USER_TOKEN_EXPIRATION')) { |
|
50
|
|
|
/** |
|
51
|
|
|
* How much time user tokens are valid. Defaults to 24 hours. |
|
52
|
|
|
*/ |
|
53
|
|
|
define('USER_TOKEN_EXPIRATION', DAY); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Used by CachedAuthorize. |
|
58
|
|
|
*/ |
|
59
|
|
|
Cache::config('permissions', [ |
|
60
|
|
|
'className' => 'File', |
|
61
|
|
|
'prefix' => 'qa_', |
|
62
|
|
|
'path' => CACHE, |
|
63
|
|
|
'duration' => '+1 hour', |
|
64
|
|
|
'groups' => ['acl'] |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieves current user's information (logged in or not) as an entity object. |
|
69
|
|
|
* |
|
70
|
|
|
* **Usage:** |
|
71
|
|
|
* |
|
72
|
|
|
* ```php |
|
73
|
|
|
* $user = user(); |
|
74
|
|
|
* echo user()->name; |
|
75
|
|
|
* // prints "Anonymous" if not logged in |
|
76
|
|
|
* ``` |
|
77
|
|
|
* @return \User\Model\Entity\UserSession |
|
78
|
|
|
*/ |
|
79
|
|
|
function user() |
|
80
|
|
|
{ |
|
81
|
|
|
static $user = null; |
|
82
|
|
|
if ($user instanceof UserSession) { |
|
83
|
|
|
return $user; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$request = Router::getRequest(); |
|
87
|
|
|
if ($request && $request->is('userLoggedIn')) { |
|
88
|
|
|
$properties = $request->session()->read('Auth.User'); |
|
89
|
|
|
if (!empty($properties['roles'])) { |
|
90
|
|
|
foreach ($properties['roles'] as &$role) { |
|
|
|
|
|
|
91
|
|
|
unset($role['_joinData']); |
|
92
|
|
|
$role = new Entity($role); |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
$properties['roles'] = []; |
|
96
|
|
|
} |
|
97
|
|
|
$properties['roles'][] = TableRegistry::get('User.Roles')->get(ROLE_ID_AUTHENTICATED, ['cache' => 'default']); |
|
98
|
|
|
} else { |
|
99
|
|
|
$properties = [ |
|
100
|
|
|
'id' => null, |
|
101
|
|
|
'name' => __d('user', 'Anonymous'), |
|
102
|
|
|
'username' => __d('user', 'anonymous'), |
|
103
|
|
|
'email' => __d('user', '(no email)'), |
|
104
|
|
|
'locale' => I18n::locale(), |
|
105
|
|
|
'roles' => [TableRegistry::get('User.Roles')->get(ROLE_ID_ANONYMOUS, ['cache' => 'default'])], |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$user = new UserSession($properties); |
|
110
|
|
|
return $user; |
|
111
|
|
|
} |
|
112
|
|
|
|