-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathControllersCommonTrait.php
59 lines (52 loc) · 1.4 KB
/
ControllersCommonTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Trait that contains needed behaviors for protect controller by OAuth2
* @author Ihor Karas <[email protected]>
* Date: 20.04.15
* Time: 18:54
*/
namespace api\components\traits;
use Yii;
use api\components\filters\OAuth2AccessFilter;
use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
trait ControllersCommonTrait
{
public function behaviors()
{
$behaviors = parent::behaviors();
// replace to top contentNegotiator filter for displaying errors in correct format
$content_negotiator = $behaviors['contentNegotiator'];
unset($behaviors['contentNegotiator']);
$content_negotiator['formats'] = Yii::$app->params['formats'];
$behaviors = ArrayHelper::merge(
[
'contentNegotiator' => $content_negotiator,
'oauth2access' => [ // should be before "authenticator" filter
'class' => OAuth2AccessFilter::className()
],
'exceptionFilter' => [
'class' => ErrorToExceptionFilter::className()
],
],
$behaviors,
[
'access' => [ // need to set after contentNegotiator filter for caching errors
'class' => AccessControl::className(),
'rules' => $this->accessRules(),
'ruleConfig' => ['class' => 'api\components\AccessRule'],
],
]
);
return $behaviors;
}
/**
* Access rules for access behavior
* @return array
*/
public function accessRules()
{
return [];
}
}