|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Auth\Authorize; |
|
13
|
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Action; |
|
15
|
|
|
use CakeDC\Api\Service\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Cake\Core\Configure; |
|
18
|
|
|
use Cake\Http\ServerRequest; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class CrudAuthorize |
|
22
|
|
|
* |
|
23
|
|
|
* Configuration for Crud Auth is defined as next Configure structure: |
|
24
|
|
|
* with Api.Auth.Crud prefix |
|
25
|
|
|
* It could one of next types given in order of priorities: |
|
26
|
|
|
* - service action permission |
|
27
|
|
|
* ['ServiceName' => ['actionName' => permission, ...], ...], |
|
28
|
|
|
* - action level global permission |
|
29
|
|
|
* ['actionName' => permission, ...], |
|
30
|
|
|
* - service level permission - define access to service in common |
|
31
|
|
|
* ['Services' => ['ServiceName' => permission, ...]] |
|
32
|
|
|
* |
|
33
|
|
|
* Additionally one can define default permission as Api.Auth.Crud.default. |
|
34
|
|
|
* |
|
35
|
|
|
* Permission defined as next rule: |
|
36
|
|
|
* permission ::= <allow> | <deny> | <auth> |
|
37
|
|
|
* |
|
38
|
|
|
* @package |
|
39
|
|
|
* @package CakeDC\Api\Service\Auth\Authorize |
|
40
|
|
|
*/ |
|
41
|
|
|
class CrudAuthorize extends BaseAuthorize |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Checks user authorization. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $user Active user data. |
|
48
|
|
|
* @param \Cake\Http\ServerRequest $request Request instance. |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
47 |
|
public function authorize($user, ServerRequest $request) |
|
52
|
|
|
{ |
|
53
|
47 |
|
return $this->_actionAuth($this->_action); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Authorize. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Action $action An Action instance. |
|
60
|
|
|
* @return bool|null |
|
61
|
|
|
*/ |
|
62
|
47 |
|
protected function _actionAuth(Action $action) |
|
63
|
|
|
{ |
|
64
|
47 |
|
$actionName = $action->getName(); |
|
65
|
47 |
|
$serviceName = $action->getService()->getName(); |
|
66
|
47 |
|
$service = $action->getService(); |
|
67
|
|
|
|
|
68
|
47 |
|
$serviceActionAuth = $this->_permission($service, $serviceName . '.' . $actionName); |
|
69
|
47 |
|
if ($serviceActionAuth !== null) { |
|
70
|
|
|
$result = $serviceActionAuth === 'allow' || $serviceActionAuth == 'auth' && !empty($action->Auth->user()); |
|
71
|
|
|
|
|
72
|
|
|
return $result; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
47 |
|
$serviceAuth = $this->_permission($service, 'Service.' . $serviceName); |
|
76
|
|
|
|
|
77
|
47 |
|
$actionAuth = $this->_permission($service, $actionName); |
|
78
|
47 |
|
if ($actionAuth !== null) { |
|
79
|
|
|
$allow = $actionAuth === 'allow' && ($serviceAuth === null || is_string($serviceAuth) && $serviceAuth === 'allow'); |
|
80
|
|
|
$authenticated = $actionAuth === 'auth' && ($serviceAuth === null || is_string($serviceAuth) && in_array($serviceAuth, ['auth', 'allow'])) && !empty($action->Auth->user()); |
|
81
|
|
|
|
|
82
|
|
|
return $allow || $authenticated; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
47 |
|
return $this->_serviceAuth($action->getService(), $action); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Authorize service. |
|
90
|
|
|
* |
|
91
|
|
|
* @param Service $service A Service instance. |
|
92
|
|
|
* @param Action $action An Action instance. |
|
93
|
|
|
* @return bool|null |
|
94
|
|
|
*/ |
|
95
|
47 |
|
protected function _serviceAuth(Service $service, Action $action) |
|
96
|
|
|
{ |
|
97
|
47 |
|
$serviceName = $service->getName(); |
|
98
|
47 |
|
$serviceAuth = $this->_permission($service, 'Service.' . $serviceName); |
|
99
|
47 |
|
if ($serviceAuth === null) { |
|
100
|
47 |
|
$serviceAuth = $this->_permission($service, 'default'); |
|
101
|
47 |
|
} |
|
102
|
47 |
|
if ($serviceAuth !== null && is_string($serviceAuth)) { |
|
103
|
47 |
|
$result = $serviceAuth === 'allow' || $serviceAuth == 'auth' && !empty($action->Auth->user()); |
|
104
|
|
|
|
|
105
|
47 |
|
return $result; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Check permission. |
|
113
|
|
|
* |
|
114
|
|
|
* @param Service $service A Service instance. |
|
115
|
|
|
* @param string $key permission key. |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
47 |
|
protected function _permission(Service $service, $key) |
|
119
|
|
|
{ |
|
120
|
47 |
|
$prefix = 'Api.Auth.Crud.'; |
|
121
|
47 |
|
$useVersioning = Configure::read('Api.useVersioning'); |
|
122
|
47 |
|
$versionPrefix = Configure::read('Api.versionPrefix'); |
|
123
|
47 |
|
$version = $service->getVersion(); |
|
124
|
47 |
|
if ($useVersioning) { |
|
125
|
|
|
$permission = Configure::read($prefix . $versionPrefix . $version . '.' . $key); |
|
126
|
|
|
if (!empty($permission)) { |
|
127
|
|
|
return $permission; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
47 |
|
$permission = Configure::read($prefix . $key); |
|
131
|
47 |
|
if (empty($permission)) { |
|
132
|
47 |
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
47 |
|
return $permission; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|