Completed
Push — master ( a54fa8...fcdb22 )
by Antonio
04:50
created

AdminController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 2
cts 2
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Controller;
13
14
use Da\User\Event\UserEvent;
15
use Da\User\Factory\MailFactory;
16
use Da\User\Filter\AccessRuleFilter;
17
use Da\User\Model\Profile;
18
use Da\User\Model\User;
19
use Da\User\Query\UserQuery;
20
use Da\User\Search\UserSearch;
21
use Da\User\Service\SwitchIdentityService;
22
use Da\User\Service\UserBlockService;
23
use Da\User\Service\UserConfirmationService;
24
use Da\User\Service\UserCreateService;
25
use Da\User\Traits\ContainerAwareTrait;
26
use Da\User\Validator\AjaxRequestModelValidator;
27
use Yii;
28
use yii\base\Module;
29
use yii\db\ActiveRecord;
30
use yii\filters\AccessControl;
31
use yii\filters\VerbFilter;
32
use yii\helpers\Url;
33
use yii\web\Controller;
34
35
class AdminController extends Controller
36
{
37
    use ContainerAwareTrait;
38
39
    /**
40
     * @var UserQuery
41
     */
42
    protected $userQuery;
43
44
    /**
45
     * AdminController constructor.
46
     *
47
     * @param string    $id
48
     * @param Module    $module
49
     * @param UserQuery $userQuery
50
     * @param array     $config
51
     */
52 2
    public function __construct($id, Module $module, UserQuery $userQuery, array $config = [])
53
    {
54 2
        $this->userQuery = $userQuery;
55 2
        parent::__construct($id, $module, $config);
56 2
    }
57
58
    /**
59
     * @param \yii\base\Action $action
60
     *
61
     * @return bool
62
     */
63 2
    public function beforeAction($action)
64
    {
65 2
        if (in_array($action->id, ['index', 'update', 'update-profile', 'info', 'assignments'], true)) {
66 2
            Url::remember('', 'actions-redirect');
67
        }
68
69 2
        return parent::beforeAction($action);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function behaviors()
76
    {
77
        return [
78 2
            'verbs' => [
79
                'class' => VerbFilter::class,
80
                'actions' => [
81
                    'delete' => ['post'],
82
                    'confirm' => ['post'],
83
                    'block' => ['post'],
84
                    'switch-identity' => ['post']
85
                ],
86
            ],
87
            'access' => [
88
                'class' => AccessControl::class,
89
                'ruleConfig' => [
90
                    'class' => AccessRuleFilter::class,
91
                ],
92
                'rules' => [
93
                    [
94
                        'allow' => true,
95
                        'actions' => ['switch-identity'],
96
                        'roles' => ['@'],
97
                    ],
98
                    [
99
                        'allow' => true,
100
                        'roles' => ['admin'],
101
                    ],
102
                ],
103
            ],
104
        ];
105
    }
106
107
    public function actionIndex()
108
    {
109
        $searchModel = $this->make(UserSearch::class);
110
        $dataProvider = $searchModel->search(Yii::$app->request->get());
111
112
        return $this->render(
113
            'index',
114
            [
115
                'dataProvider' => $dataProvider,
116
                'searchModel' => $searchModel,
117
            ]
118
        );
119
    }
120
121 1
    public function actionCreate()
122
    {
123
        /** @var User $user */
124 1
        $user = $this->make(User::class, [], ['scenario' => 'create']);
125
126
        /** @var UserEvent $event */
127 1
        $event = $this->make(UserEvent::class, [$user]);
128
129 1
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
130
131 1
        if ($user->load(Yii::$app->request->post())) {
132 1
            $this->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
133
134 1
            $mailService = MailFactory::makeWelcomeMailerService($user);
135
136 1
            if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
137 1
                Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User has been created'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
138 1
                $this->trigger(UserEvent::EVENT_AFTER_CREATE, $event);
139
140 1
                return $this->redirect(['update', 'id' => $user->id]);
141
            }
142
        }
143
144 1
        return $this->render('create', ['user' => $user]);
145
    }
146
147 2
    public function actionUpdate($id)
148
    {
149 2
        $user = $this->userQuery->where(['id' => $id])->one();
150 2
        $user->setScenario('update');
151
        /** @var UserEvent $event */
152 2
        $event = $this->make(UserEvent::class, [$user]);
153
154 2
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
155
156 2
        if ($user->load(Yii::$app->request->post())) {
157 1
            $this->trigger(ActiveRecord::EVENT_BEFORE_UPDATE, $event);
158
159 1
            if ($user->save()) {
160 1
                Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Account details have been updated'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
161 1
                $this->trigger(ActiveRecord::EVENT_AFTER_UPDATE, $event);
162
163 1
                return $this->refresh();
164
            }
165
        }
166
167 2
        return $this->render('_account', ['user' => $user]);
168
    }
169
170
    public function actionUpdateProfile($id)
171
    {
172
        /** @var User $user */
173
        $user = $this->userQuery->where(['id' => $id])->one();
174
        /** @var Profile $profile */
175
        $profile = $user->profile;
176
        if ($profile === null) {
177
            $profile = $this->make(Profile::class);
178
            $profile->link('user', $user);
179
        }
180
        /** @var UserEvent $event */
181
        $event = $this->make(UserEvent::class, [$user]);
182
183
        $this->make(AjaxRequestModelValidator::class, [$profile])->validate();
184
185
        if ($profile->load(Yii::$app->request->post())) {
186
            if ($profile->save()) {
187
                $this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
188
                Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Profile details have been updated'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
189
                $this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);
190
191
                return $this->refresh();
192
            }
193
        }
194
195
        return $this->render(
196
            '_profile',
197
            [
198
                'user' => $user,
199
                'profile' => $profile,
200
            ]
201
        );
202
    }
203
204
    public function actionInfo($id)
205
    {
206
        /** @var User $user */
207
        $user = $this->userQuery->where(['id' => $id])->one();
208
209
        return $this->render(
210
            '_info',
211
            [
212
                'user' => $user,
213
            ]
214
        );
215
    }
216
217
    public function actionAssignments($id)
218
    {
219
        /** @var User $user */
220
        $user = $this->userQuery->where(['id' => $id])->one();
221
222
        return $this->render(
223
            '_assignments',
224
            [
225
                'user' => $user,
226
                'params' => Yii::$app->request->post(),
227
            ]
228
        );
229
    }
230
231
    public function actionConfirm($id)
232
    {
233
        /** @var User $user */
234
        $user = $this->userQuery->where(['id' => $id])->one();
235
        /** @var UserEvent $event */
236
        $event = $this->make(UserEvent::class, [$user]);
237
238
        $this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
239
240
        if ($this->make(UserConfirmationService::class, [$user])->run()) {
241
            Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User has been confirmed'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
242
            $this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
243
        } else {
244
            Yii::$app->getSession()->setFlash(
245
                'warning',
246
                Yii::t('usuario', 'Unable to confirm user. Please, try again.')
247
            );
248
        }
249
250
        return $this->redirect(Url::previous('actions-redirect'));
251
    }
252
253
    public function actionDelete($id)
254
    {
255
        if ((int)$id === Yii::$app->user->getId()) {
256
            Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'You cannot remove your own account'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
257
        } else {
258
            /** @var User $user */
259
            $user = $this->userQuery->where(['id' => $id])->one();
260
            /** @var UserEvent $event */
261
            $event = $this->make(UserEvent::class, [$user]);
262
            $this->trigger(ActiveRecord::EVENT_BEFORE_DELETE, $event);
263
264
            if ($user->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->delete() of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
265
                Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User has been deleted'));
266
                $this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event);
267
            } else {
268
                Yii::$app->getSession()->setFlash(
269
                    'warning',
270
                    Yii::t('usuario', 'Unable to delete user. Please, try again later.')
271
                );
272
            }
273
        }
274
275
        return $this->redirect(['index']);
276
    }
277
278
    public function actionBlock($id)
279
    {
280
        if ((int)$id === Yii::$app->user->getId()) {
281
            Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'You cannot remove your own account'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
282
        } else {
283
            /** @var User $user */
284
            $user = $this->userQuery->where(['id' => $id])->one();
285
            /** @var UserEvent $event */
286
            $event = $this->make(UserEvent::class, [$user]);
287
288
            if ($this->make(UserBlockService::class, [$user, $event, $this])->run()) {
289
                Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User block status has been updated.'));
290
            } else {
291
                Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to update block status.'));
292
            }
293
        }
294
295
        return $this->redirect(Url::previous('actions-redirect'));
296
    }
297
298
    public function actionSwitchIdentity($id = null)
299
    {
300
        /** @var \Da\User\Module $module */
301
        $module = $this->module;
302
        if (false === $module->enableSwitchIdentities) {
303
            Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Switch identities is disabled.'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
304
305
            return $this->redirect(['index']);
306
        }
307
308
        $this->make(SwitchIdentityService::class, [$this, 2 => $id])->run();
309
310
        return $this->goHome();
311
    }
312
}
313