Passed
Push — master ( c104d5...1dbfe3 )
by Matthew
04:39
created

EActiveController::forbidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * Date: 2017-12-07
5
 * Time: 03:18
6
 */
7
8
namespace MP\ExtendedApi;
9
10
use Yii;
11
use yii\data\DataProviderInterface;
12
use yii\db\ActiveRecordInterface;
13
use yii\rest\ActiveController;
14
use yii\web\NotFoundHttpException;
15
use MP\Services\ImplementServices;
16
use yii\web\ForbiddenHttpException;
17
18
/**
19
 * Class    EActiveController
20
 * @package MP\ExtendedApi
21
 * @author  Yarmaliuk Mikhail
22
 * @version 1.0
23
 */
24
class EActiveController extends ActiveController
25
{
26
    use ImplementServices;
27
28
    const FILTER_ERROR_CODE = 405;
29
30
    /**
31
     * Search model class
32
     *
33
     * @var ActiveRecordInterface
34
     */
35
    public $searchClass;
36
37
    /**
38
     * Return error if empty filtered result
39
     *
40
     * @var bool
41
     */
42
    public $errorFilter = false;
43
44
    /**
45
     * List external actions
46
     *
47
     * 'delete-all' => true,
48
     *
49
     * @var array
50
     */
51
    public $externalActions = [];
52
53
    /**
54
     * Check action access
55
     *
56
     * 'index'  => 'rule',
57
     * 'update' => 'permission',
58
     *
59
     * @var array
60
     */
61
    public $checkAccessRules = [];
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function actions(): array
67
    {
68
        $actions = parent::actions();
69
70
        $actions['index']['class']  = EIndexAction::class;
71
        $actions['delete']['class'] = EDeleteAction::class;
72
        $actions['view']['class']   = EViewAction::class;
73
74
        if (!empty($this->searchClass)) {
75
            $actions['index']['dataFilter'] = [
76
                'class'       => EActiveDataFilter::class,
77
                'searchModel' => $this->searchClass,
78
            ];
79
        }
80
81
        foreach ($this->externalActions as $externalAction => $value) {
82
            if ($value) {
83
                switch ($externalAction) {
84
                    case 'delete-all':
85
                        $actions[$externalAction]          = $actions['index'];
86
                        $actions[$externalAction]['class'] = EDeleteAllAction::class;
87
                    break;
88
                }
89
            }
90
        }
91
92
        return $actions;
93
    }
94
95
    /**
96
     * Throw error empty filtered result
97
     *
98
     * @throws NotFoundHttpException
99
     */
100
    public function filterError(): void
101
    {
102
        throw new NotFoundHttpException(Yii::t('app', 'Nothing found'), self::FILTER_ERROR_CODE);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     *
108
     * @param EIndexAction $action
109
     * @param mixed        $result
110
     *
111
     * @throws NotFoundHttpException
112
     */
113
    public function afterAction($action, $result)
114
    {
115
        if ($action->id === 'index' && $result instanceof DataProviderInterface) {
116
            if ($this->errorFilter && !empty($action->dataFilter->filter) && empty($result->getModels())) {
117
                $this->filterError();
118
            }
119
        }
120
121
        return parent::afterAction($action, $result);
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function checkAccess($action, $model = null, $params = [])
128
    {
129
        if ($this->checkAccessRules[$action] ?? null) {
130
            $allow = Yii::$app->user->can($this->checkAccessRules[$action], ['model' => $model, 'params' => $params]);
0 ignored issues
show
Bug introduced by
The method can() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
            /** @scrutinizer ignore-call */ 
131
            $allow = Yii::$app->user->can($this->checkAccessRules[$action], ['model' => $model, 'params' => $params]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
132
            if (!$allow) {
133
                $this->forbidden();
134
            }
135
        }
136
    }
137
138
    /**
139
     * Throw forbidden error
140
     *
141
     * @throws ForbiddenHttpException
142
     */
143
    protected function forbidden(): void
144
    {
145
        throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
146
    }
147
}
148