Auth::_isAllowed()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 2
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
/**
13
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
14
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 *
16
 * Licensed under The MIT License
17
 * For full copyright and license information, please see the LICENSE.txt
18
 * Redistributions of files must retain the above copyright notice.
19
 *
20
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
21
 * @link          http://cakephp.org CakePHP(tm) Project
22
 * @since         0.10.0
23
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
24
 */
25
26
namespace CakeDC\Api\Service\Auth;
27
28
use CakeDC\Api\Exception\UnauthenticatedException;
29
use CakeDC\Api\Service\Action\Action;
30
use CakeDC\Api\Service\Service;
31
use Cake\Core\InstanceConfigTrait;
32
use Cake\Event\Event;
33
use Cake\Event\EventDispatcherTrait;
34
use Cake\Http\Exception\ForbiddenException;
35
use Cake\Http\Response;
36
use Cake\Log\LogTrait;
37
38
/**
39
 * Class Auth
40
 *
41
 * @package CakeDC\Api\Service\Auth
42
 */
43
class Auth
44
{
45
    use AuthenticateTrait;
46
    use AuthorizeTrait;
47
    use EventDispatcherTrait;
48
    use InstanceConfigTrait;
49
    use LogTrait;
50
    use StorageTrait;
51
52
    /**
53
     * Actions for which user validation is not required.
54
     *
55
     * @var array
56
     */
57
    public $allowedActions = [];
58
59
    /**
60
     * Request object
61
     *
62
     * @var \Cake\Http\ServerRequest
63
     */
64
    public $request;
65
66
    /**
67
     * Response object
68
     *
69
     * @var \Cake\Http\Response
70
     */
71
    public $response;
72
73
    /**
74
     * Default config
75
     *
76
     * These are merged with user-provided config when the component is used.
77
     *
78
     * @var array
79
     */
80
    protected $_defaultConfig = [
81
        'storage' => 'Memory',
82
    ];
83
84
    protected $_registry = null;
85
86
    /**
87
     * @var Service
88
     */
89
    protected $_service;
90
91
    /**
92
     * @var Action
93
     */
94
    protected $_action;
95
96
    /**
97
     * Constructor
98
     *
99
     * @param array $config Array of configuration settings.
100
     */
101 123
    public function __construct(array $config = [])
102
    {
103 123
        if (array_key_exists('request', $config)) {
104 123
            $this->request = $config['request'];
105 123
        }
106 123
        if (array_key_exists('response', $config)) {
107 123
            $this->response = $config['response'];
108 123
        }
109 123
        $this->setConfig($config);
110 123
        $this->initialize($config);
111 123
    }
112
113
    /**
114
     * Initialize properties.
115
     *
116
     * @param array $config The config data.
117
     * @return void
118
     */
119 123
    public function initialize(array $config)
120
    {
121 123
        if (array_key_exists('service', $config)) {
122 123
            $this->_service = $config['service'];
123 123
        }
124 123
        if (array_key_exists('action', $config)) {
125 123
            $this->_action = $config['action'];
126 123
        }
127 123
        $this->setEventManager($this->_action->getEventManager());
128 123
    }
129
130
    /**
131
     * Sets defaults for configs.
132
     *
133
     * @return void
134
     */
135 60
    protected function _setDefaults()
136
    {
137
        $defaults = [
138 60
            'authenticate' => ['CakeDC/Api.Token'],
139 60
            'authError' => __d('CakeDC/Api', 'You are not authorized to access that location.')
140 60
        ];
141
142 60
        $config = $this->getConfig();
143 60
        foreach ($config as $key => $value) {
144 60
            if ($value !== null) {
145 60
                unset($defaults[$key]);
146 60
            }
147 60
        }
148 60
        $this->setConfig($defaults);
149 60
    }
150
151
    /**
152
     * Takes a list of actions in the current controller for which authentication is not required, or
153
     * no parameters to allow all actions.
154
     *
155
     * You can use allow with either an array or a simple string.
156
     *
157
     * ```
158
     * $this->Auth->allow('view');
159
     * $this->Auth->allow(['edit', 'add']);
160
     *
161
     * @param string|array $actions Controller action name or array of actions
162
     * @return void
163
     */
164 14
    public function allow($actions)
165
    {
166 14
        $this->allowedActions = array_merge($this->allowedActions, (array)$actions);
167 14
    }
168
169
    /**
170
     * Removes items from the list of allowed/no authentication required actions.
171
     *
172
     * You can use deny with either an array or a simple string.
173
     *
174
     * ```
175
     * $this->Auth->deny('view');
176
     * $this->Auth->deny(['edit', 'add']);
177
     * ```
178
     * or
179
     * ```
180
     * $this->Auth->deny();
181
     * ```
182
     * to remove all items from the allowed list
183
     *
184
     * @param string|array|null $actions Controller action name or array of actions
185
     * @return void
186
     */
187
    public function deny($actions = null)
188
    {
189
        if ($actions === null) {
190
            $this->allowedActions = [];
191
192
            return;
193
        }
194
        foreach ((array)$actions as $action) {
195
            $i = array_search($action, $this->allowedActions);
196
            if (is_int($i)) {
197
                unset($this->allowedActions[$i]);
198
            }
199
        }
200
        $this->allowedActions = array_values($this->allowedActions);
201
    }
202
203
    /**
204
     * Main execution method, handles initial authentication check and redirection
205
     * of invalid users.
206
     *
207
     * The auth check is done when event name is same as the one configured in
208
     * `checkAuthIn` config.
209
     *
210
     * @param \Cake\Event\Event $event Event instance.
211
     * @return Response|null
212
     */
213 60
    public function authCheck(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215 60
        $action = $this->_action;
216
217 60
        $this->_setDefaults();
218
219 60
        if ($this->_isAllowed($action)) {
220 12
            return null;
221
        }
222
223 48
        if (!$this->_getUser()) {
224 1
            throw new UnauthenticatedException();
225
        }
226
227 47
        if (empty($this->_config['authorize']) ||
228 47
            $this->isAuthorized($this->user())
229 47
        ) {
230 47
            return null;
231
        }
232
233
        throw new ForbiddenException($this->_config['authError']);
234
    }
235
236
    /**
237
     * Checks whether current action is accessible without authentication.
238
     *
239
     * @param Action $action An Action instance.
240
     * @return bool True if action is accessible without authentication else false
241
     */
242 60
    protected function _isAllowed(Action $action)
243
    {
244 60
        $action = strtolower($action->getName());
245
246 60
        return in_array($action, array_map('strtolower', $this->allowedActions)) ||
247 60
            in_array('*', $this->allowedActions);
248
    }
249
250
    /**
251
     * __get method this method will return an attribute of this class
252
     *
253
     * @param string $name Name
254
     * @return mixed
255
     */
256
    public function __get($name)
257
    {
258
        if (isset($this->{$name})) {
259
            return $this->{$name};
260
        }
261
262
        return null;
263
    }
264
265
    /**
266
     * __set method this method will allow you set the value for an attribute of this class
267
     *
268
     * @param string $name name of the attribute
269
     * @param string $value value of the attribute
270
     * @return void
271
     */
272
    public function __set($name, $value)
273
    {
274
        $this->{$name} = $value;
275
    }
276
}
277