Completed
Pull Request — master (#6)
by
unknown
02:38
created

TokensController::checkAdminAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\providers;
4
5
use Craft;
6
use craft\helpers\ArrayHelper;
7
use flipbox\patron\actions\token\DeleteToken;
8
use flipbox\patron\actions\token\DisableToken;
9
use flipbox\patron\actions\token\EnableToken;
10
use flipbox\patron\actions\token\UpdateToken;
11
use flipbox\patron\cp\controllers\AbstractController;
12
use flipbox\patron\Patron;
13
use flipbox\patron\records\Token;
14
15
class TokensController extends AbstractController
16
{
17
    /**
18
     * @return array
19
     */
20
    public function behaviors()
21
    {
22
        return ArrayHelper::merge(
23
            parent::behaviors(),
24
            [
25
                'error' => [
26
                    'default' => 'provider'
27
                ],
28
                'redirect' => [
29
                    'only' => ['update', 'delete', 'enable', 'disable'],
30
                    'actions' => [
31
                        'update' => [200],
32
                        'delete' => [204],
33
                        'enable' => [200],
34
                        'disable' => [200]
35
                    ]
36
                ],
37
                'flash' => [
38
                    'actions' => [
39
                        'update' => [
40
                            200 => Craft::t('patron', "Token successfully updated."),
41
                            400 => Craft::t('patron', "Failed to updated token.")
42
                        ],
43
                        'delete' => [
44
                            204 => Craft::t('patron', "Token successfully deleted."),
45
                            400 => Craft::t('patron', "Failed to delete token.")
46
                        ],
47
                        'enable' => [
48
                            200 => Craft::t('patron', "Token successfully enabled."),
49
                            400 => Craft::t('patron', "Failed to enabled token.")
50
                        ],
51
                        'disable' => [
52
                            200 => Craft::t('patron', "Token successfully disable."),
53
                            400 => Craft::t('patron', "Failed to disable token.")
54
                        ],
55
                    ]
56
                ]
57
            ]
58
        );
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function verbs(): array
65
    {
66
        return [
67
            'update' => ['post'],
68
            'enable' => ['post'],
69
            'disable' => ['post'],
70
            'delete' => ['post', 'delete']
71
        ];
72
    }
73
74
    /**
75
     * @param null $token
76
     * @return mixed
77
     * @throws \yii\base\InvalidConfigException
78
     * @throws \yii\web\BadRequestHttpException
79
     */
80
    public function actionUpdate($token = null)
81
    {
82
        if (null === $token) {
83
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
84
        }
85
86
        $action = Craft::createObject([
87
            'class' => UpdateToken::class,
88
            'checkAccess' => [$this, 'checkAdminAccess']
89
        ], [
90
            'update',
91
            $this
92
        ]);
93
94
        return $action->runWithParams([
95
            'token' => $token
96
        ]);
97
    }
98
99
    /**
100
     * @param null $token
101
     * @return mixed
102
     * @throws \yii\base\InvalidConfigException
103
     * @throws \yii\web\BadRequestHttpException
104
     */
105
    public function actionDisable($token = null)
106
    {
107
        if (null === $token) {
108
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
109
        }
110
111
        $action = Craft::createObject([
112
            'class' => DisableToken::class,
113
            'checkAccess' => [$this, 'checkAdminAccess']
114
        ], [
115
            'revoke',
116
            $this
117
        ]);
118
119
        return $action->runWithParams([
120
            'token' => $token
121
        ]);
122
    }
123
124
    /**
125
     * @param null $token
126
     * @return mixed
127
     * @throws \yii\base\InvalidConfigException
128
     * @throws \yii\web\BadRequestHttpException
129
     */
130
    public function actionEnable($token = null)
131
    {
132
        if (null === $token) {
133
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
134
        }
135
136
        $action = Craft::createObject([
137
            'class' => EnableToken::class,
138
            'checkAccess' => [$this, 'checkAdminAccess']
139
        ], [
140
            'reinstate',
141
            $this
142
        ]);
143
144
        return $action->runWithParams([
145
            'token' => $token
146
        ]);
147
    }
148
149
    /**
150
     * @param null $token
151
     * @return mixed
152
     * @throws \yii\base\InvalidConfigException
153
     * @throws \yii\web\BadRequestHttpException
154
     */
155
    public function actionDelete($token = null)
156
    {
157
        if (null === $token) {
158
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
159
        }
160
161
        $action = Craft::createObject([
162
            'class' => DeleteToken::class,
163
            'checkAccess' => [$this, 'checkAdminAccess']
164
        ], [
165
            'delete',
166
            $this
167
        ]);
168
169
        return $action->runWithParams([
170
            'token' => $token
171
        ]);
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function checkAdminAccess(): bool
178
    {
179
        $this->requireAdmin(false);
180
        return true;
181
    }
182
}
183