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\Action\Extension; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\Action\Action; |
15
|
|
|
use Cake\Event\Event; |
16
|
|
|
use Cake\Event\EventListenerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class CorsExtension |
20
|
|
|
* |
21
|
|
|
* @package CakeDC\Api\Service\Action\Extension |
22
|
|
|
*/ |
23
|
|
|
class CorsExtension extends Extension implements EventListenerInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Events supported by this extension. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
1 |
|
public function implementedEvents() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
1 |
|
'Action.beforeProcess' => 'onAction', |
35
|
1 |
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* new entity |
40
|
|
|
* |
41
|
|
|
* @param Event $Event An Event instance |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
|
public function onAction(Event $Event) |
45
|
|
|
{ |
46
|
1 |
|
$action = $Event->getSubject(); |
47
|
1 |
|
$request = $action->getService()->getRequest(); |
48
|
1 |
|
$response = $action->getService()->getResponse(); |
49
|
1 |
|
$action->getService()->setResponse( |
50
|
1 |
|
$response->cors($request) |
51
|
1 |
|
->allowOrigin($this->getConfig('origin') ?: ['*']) |
52
|
1 |
|
->allowMethods($this->getConfig('methods') ?: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH']) |
53
|
1 |
|
->allowHeaders($this->getConfig('headers') ?: [ |
54
|
1 |
|
'X-CSRF-Token', |
55
|
1 |
|
'Content-Type', |
56
|
1 |
|
'Access-Control-Allow-Headers', |
57
|
1 |
|
'Access-Control-Allow-Origin', |
58
|
1 |
|
'Authorization', |
59
|
|
|
'X-Requested-With' |
60
|
1 |
|
]) |
61
|
1 |
|
->allowCredentials() |
62
|
1 |
|
->maxAge($this->getConfig('maxAge') ?: 300) |
63
|
1 |
|
->build() |
64
|
1 |
|
); |
65
|
1 |
|
} |
66
|
|
|
} |
67
|
|
|
|