Completed
Push — master ( 0d226c...578453 )
by Klochok
05:19
created

AccountController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
/**
12
 * @see    http://hiqdev.com/hipanel-module-hosting
13
 * @license http://hiqdev.com/hipanel-module-hosting/license
14
 * @copyright Copyright (c) 2015 HiQDev
15
 */
16
17
namespace hipanel\modules\hosting\controllers;
18
19
use hipanel\actions\IndexAction;
20
use hipanel\actions\PrepareBulkAction;
21
use hipanel\actions\RedirectAction;
22
use hipanel\actions\RenderJsonAction;
23
use hipanel\actions\SearchAction;
24
use hipanel\actions\SmartCreateAction;
25
use hipanel\actions\SmartDeleteAction;
26
use hipanel\actions\SmartUpdateAction;
27
use hipanel\actions\ValidateFormAction;
28
use hipanel\actions\ViewAction;
29
use hipanel\filters\EasyAccessControl;
30
use Yii;
31
use yii\base\Event;
32
33
class AccountController extends \hipanel\base\CrudController
34
{
35
    public function behaviors()
36
    {
37
        return array_merge(parent::behaviors(), [
38
            [
39
                'class' => EasyAccessControl::class,
40
                'actions' => [
41
                    'create' => 'account.create',
42
                    'create-ftponly' => 'account.create',
43
                    'change-password' => 'account.update',
44
                    'set-allowed-ips' => 'account.update',
45
                    'delete' => 'account.delete',
46
                    '*' => 'account.read',
47
                ],
48
            ],
49
        ]);
50
    }
51
52
    public function actions()
53
    {
54
        return array_merge(parent::actions(), [
55
            'index' => [
56
                'class' => IndexAction::class,
57
                'data' => function ($action) {
58
                    return [
59
                        'stateData' => $action->controller->getStateData(),
60
                        'typeData' => $action->controller->getTypeData(),
61
                    ];
62
                },
63
                'filterStorageMap' => [
64
                    'login_like' => 'hosting.account.login',
65
                    'server' => 'server.server.name',
66
                    'state' => 'hosting.account.state',
67
                    'type' => 'hosting.account.type',
68
                    'client_id' => 'client.client.id',
69
                    'seller_id' => 'client.client.seller_id',
70
                ],
71
            ],
72
            'view' => [
73
                'class' => ViewAction::class,
74
                'findOptions' => [
75
                    'with_mail_settings' => true,
76
                ],
77 View Code Duplication
                'on beforePerform' => function (Event $event) {
0 ignored issues
show
Duplication introduced by tofid
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
                    /** @var \hipanel\actions\SearchAction $action */
79
                    $action = $event->sender;
80
                    $dataProvider = $action->getDataProvider();
81
                    $dataProvider->query->joinWith(['blocking']);
82
                    $dataProvider->query->andWhere(['with_blocking' => 1]);
83
                },
84
                'data' => function ($action) {
85
                    return [
86
                        'blockReasons' => $action->controller->getBlockReasons(),
87
                    ];
88
                },
89
            ],
90
            'create' => [
91
                'class' => SmartCreateAction::class,
92
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
93
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
94
            ],
95
            'create-ftponly' => [
96
                'class' => SmartCreateAction::class,
97
                'success' => Yii::t('hipanel:hosting', 'Account creating task has been added to queue'),
98
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to create account'),
99
            ],
100
            'change-password' => [
101
                'class' => SmartUpdateAction::class,
102
                'view' => '_changePasswordModal',
103
                'POST' => [
104
                    'save' => true,
105
                    'success' => [
106
                        'class' => RenderJsonAction::class,
107
                        'return' => function ($action) {
108
                            return ['success' => !$action->collection->hasErrors()];
109
                        },
110
                    ],
111
                ],
112
            ],
113
            'set-allowed-ips' => [
114
                'class' => SmartUpdateAction::class,
115
                'view' => '_ipRestrictionsModal',
116
                'success' => Yii::t('hipanel:hosting', 'Allowed IPs changing task has been successfully added to queue'),
117
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change allowed IPs'),
118
            ],
119
            'set-mail-settings' => [
120
                'class' => SmartUpdateAction::class,
121
                'view' => '_setMailSettings',
122
                'success' => Yii::t('hipanel:hosting', 'Mail settings where changed'),
123
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to change mail settings'),
124
            ],
125
            'enable-block' => [
126
                'class' => SmartUpdateAction::class,
127
                'success' => Yii::t('hipanel:hosting', 'Account was blocked successfully'),
128
                'error' => Yii::t('hipanel:hosting', 'Error during the account blocking'),
129
                'POST html' => [
130
                    'save' => true,
131
                    'success' => [
132
                        'class' => RedirectAction::class,
133
                    ],
134
                ],
135
                'on beforeSave' => function (Event $event) {
136
                    /** @var \hipanel\actions\Action $action */
137
                    $action = $event->sender;
138
                    $type = Yii::$app->request->post('type');
139
                    $comment = Yii::$app->request->post('comment');
140 View Code Duplication
                    if (!empty($type)) {
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
                        foreach ($action->collection->models as $model) {
142
                            $model->setAttributes([
143
                                'type' => $type,
144
                                'comment' => $comment,
145
                            ]);
146
                        }
147
                    }
148
                },
149
            ],
150
            'bulk-enable-block-modal' => [
151
                'class' => PrepareBulkAction::class,
152
                'view' => '_bulkEnableBlock',
153
                'data' => function ($action, $data) {
154
                    return array_merge($data, [
155
                        'blockReasons' => $this->getBlockReasons(),
156
                    ]);
157
                },
158
            ],
159
            'disable-block' => [
160
                'class' => SmartUpdateAction::class,
161
                'success' => Yii::t('hipanel:hosting', 'Account was unblocked successfully'),
162
                'error' => Yii::t('hipanel:hosting', 'Error during the account unblocking'),
163
                'POST html' => [
164
                    'save' => true,
165
                    'success' => [
166
                        'class' => RedirectAction::class,
167
                    ],
168
                ],
169 View Code Duplication
                'on beforeSave' => function (Event $event) {
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
                    /** @var \hipanel\actions\Action $action */
171
                    $action = $event->sender;
172
                    $type = Yii::$app->request->post('type');
173
                    $comment = Yii::$app->request->post('comment');
174
                    if (!empty($type)) {
175
                        foreach ($action->collection->models as $model) {
176
                            $model->setAttribute('comment', $comment);
177
                        }
178
                    }
179
                },
180
            ],
181
            'bulk-disable-block-modal' => [
182
                'class' => PrepareBulkAction::class,
183
                'scenario' => 'disable-block',
184
                'view' => '_bulkDisableBlock',
185
                'data' => function ($action, $data) {
186
                    return array_merge($data, [
187
                        'blockReasons' => $this->getBlockReasons(),
188
                    ]);
189
                },
190
            ],
191
            'validate-form' => [
192
                'class' => ValidateFormAction::class,
193
            ],
194
            'single-validate-form' => [
195
                'class' => ValidateFormAction::class,
196
                'validatedInputId' => false,
197
            ],
198
            'delete' => [
199
                'class' => SmartDeleteAction::class,
200
                'success' => Yii::t('hipanel:hosting', 'Account deleting task has been added to queue'),
201
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete account'),
202
            ],
203
            'bulk-delete-modal' => [
204
                'class' => PrepareBulkAction::class,
205
                'view' => '_bulkDelete',
206
            ],
207
            'get-directories-list' => [
208
                'class' => SearchAction::class,
209
                'findOptions' => ['with_directories' => true],
210
                'ajaxResponseFormatter' => function ($action) {
211
                    $results = [];
212
213
                    $model = $action->collection->first;
214
                    $pathLike = Yii::$app->request->post('path_like');
215
216
                    foreach ($model['path'] as $path) {
217
                        if ($pathLike) {
218
                            if (preg_match('|' . $pathLike . '|', $path)) {
219
                                array_unshift($results, ['id' => $path, 'text' => $path]);
220
                                continue;
221
                            }
222
                        }
223
224
                        $results[] = ['id' => $path, 'text' => $path];
225
                    }
226
227
                    return $results;
228
                },
229
            ],
230
        ]);
231
    }
232
233
    public function getStateData()
234
    {
235
        return $this->getRefs('state,account', 'hipanel:hosting');
236
    }
237
238
    public function getTypeData()
239
    {
240
        return $this->getRefs('type,account', 'hipanel:hosting');
241
    }
242
}
243