UserController::actionSwitch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\models\UserModel;
6
use app\modules\admin\models\search\UserSearch;
7
use app\traits\FindModelTrait;
8
use Yii;
9
use yii\filters\VerbFilter;
10
use yii\web\Controller;
11
use yii\web\ForbiddenHttpException;
12
use yii2mod\editable\EditableAction;
13
14
/**
15
 * Class UserController
16
 *
17
 * @package app\modules\admin\controllers
18
 */
19
class UserController extends Controller
20
{
21
    use FindModelTrait;
22
23
    /**
24
     * Name of the session key in which the original user id is saved.
25
     */
26
    const ORIGINAL_USER_SESSION_KEY = 'original_user';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function behaviors(): array
32
    {
33
        return [
34
            'verbs' => [
35
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'index' => ['get'],
38
                    'create' => ['get', 'post'],
39
                    'update' => ['get', 'post'],
40
                    'delete' => ['post'],
41
                ],
42
            ],
43
        ];
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function actions(): array
50
    {
51
        return [
52
            'edit-user' => [
53
                'class' => EditableAction::class,
54
                'modelClass' => UserModel::class,
55
                'forceCreate' => false,
56
            ],
57
            'index' => [
58
                'class' => 'yii2tech\admin\actions\Index',
59
                'newSearchModel' => function () {
60
                    return new UserSearch();
61
                },
62
            ],
63
            'delete' => [
64
                'class' => 'yii2tech\admin\actions\Delete',
65
                'findModel' => function (int $id) {
66
                    return $this->findModel(UserModel::class, $id);
67
                },
68
                'flash' => Yii::t('app', 'User has been deleted.'),
69
            ],
70
        ];
71
    }
72
73
    /**
74
     * Creates a new user.
75
     *
76
     * If creation is successful, the browser will be redirected to the 'index' page.
77
     *
78
     * @return mixed
79
     */
80
    public function actionCreate()
81
    {
82
        $model = new UserModel(['scenario' => 'create']);
83
84 View Code Duplication
        if ($model->load(Yii::$app->request->post())) {
0 ignored issues
show
Duplication introduced by
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...
85
            if ($model->create()) {
86
                Yii::$app->session->setFlash('success', Yii::t('app', 'User has been created.'));
87
88
                return $this->redirect(['index']);
89
            }
90
        }
91
92
        return $this->render('create', [
93
            'model' => $model,
94
        ]);
95
    }
96
97
    /**
98
     * Updates an existing user.
99
     *
100
     * If update is successful, the browser will be redirected to the 'index' page.
101
     *
102
     * @param int $id
103
     *
104
     * @return mixed
105
     */
106
    public function actionUpdate(int $id)
107
    {
108
        /* @var $model UserModel */
109
        $model = $this->findModel(UserModel::class, $id);
110
111
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
112
            if (!empty($model->plainPassword)) {
113
                $model->setPassword($model->plainPassword);
114
            }
115
            $model->save(false);
116
            Yii::$app->session->setFlash('success', Yii::t('app', 'User has been saved.'));
117
118
            return $this->redirect(['index']);
119
        }
120
121
        return $this->render('update', [
122
            'model' => $model,
123
        ]);
124
    }
125
126
    /**
127
     * Switches to the given user for the rest of the Session.
128
     *
129
     * @param int $id
130
     *
131
     * @throws ForbiddenHttpException
132
     *
133
     * @return string
134
     */
135
    public function actionSwitch(int $id)
136
    {
137
        if (Yii::$app->session->has(self::ORIGINAL_USER_SESSION_KEY)) {
138
            $user = $this->findModel(UserModel::class, Yii::$app->session->get(self::ORIGINAL_USER_SESSION_KEY));
139
            Yii::$app->session->remove(self::ORIGINAL_USER_SESSION_KEY);
140
        } else {
141
            $user = $this->findModel(UserModel::class, $id);
142
            Yii::$app->session->set(self::ORIGINAL_USER_SESSION_KEY, Yii::$app->user->id);
143
        }
144
145
        Yii::$app->user->switchIdentity($user, 3600);
146
147
        return $this->goHome();
148
    }
149
}
150