UserController.php
4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace backend\modules\user\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
//
use thread\app\base\controllers\BackendController;
use thread\modules\user\models\{
Profile, form\CreateForm
};
use thread\actions\Update;
//
use backend\modules\user\models\{
User, search\User as filterUserModel
};
/**
* Class UserController
*
* @package admin\modules\user\controllers
* @author FilamentV <vortex.filament@gmail.com>
* @copyright (c), Thread
*/
class UserController extends BackendController
{
public $name = 'user';
public $title = "User";
protected $model = User::class;
protected $filterModel = filterUserModel::class;
/**
* @return array
*/
public function behaviors()
{
return [
'AccessControl' => [
'class' => AccessControl::class,
'rules' => [
[
'allow' => true,
'actions' => ['create', 'update', 'list', 'validation', 'trash', 'published', 'delete'],
'roles' => ['admin'],
'matchCallback' => function ($rule, $action) {
return (Yii::$app->getUser()->id === 1) ? true : false;
}
],
[
'allow' => true,
'actions' => ['outtrash', 'intrash'],
'roles' => ['admin'],
'matchCallback' => function ($rule, $action) {
return (Yii::$app->getUser()->id === 1) ? true : false;
}
],
[
'allow' => false,
],
],
],
];
}
/**
*
* @return array
*/
public function actions()
{
$action = parent::actions();
unset($action['create']);
return ArrayHelper::merge(
$action,
[
'list' => [
'layout' => 'list-user',
],
'update' => [
'class' => Update::class,
'redirect' => function () {
return $_POST['save_and_exit'] ? $this->actionListLinkStatus : [
'update',
'id' => $this->action->getModel()->id
];
}
],
]
);
}
/**
* @return string|\yii\web\Response
* @throws \Exception
*/
public function actionCreate()
{
$this->layout = '@app/layouts/create';
$model = new CreateForm();
$model->setScenario('userCreate');
if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) {
$user = new User([
'group_id' => $model->group_id,
'username' => $model->username,
'email' => $model->email,
'published' => $model->published,
'scenario' => 'userCreate',
]);
$user->setPassword($model->password)->generateAuthKey();
/** @var PDO $transaction */
$transaction = $user::getDb()->beginTransaction();
try {
$save = $user->save();
if ($save) {
$profile = new Profile([
'user_id' => $user->id,
'scenario' => 'basicCreate',
]);
$profile->save();
}
if ($save) {
$transaction->commit();
return $this->redirect(($_POST['save_and_exit']) ? $this->actionListLinkStatus : [
'update',
'id' => $user->id
]);
} else {
$transaction->rollBack();
}
} catch (\Exception $e) {
$transaction->rollBack();
throw new \Exception($e);
}
}
return $this->render('create', [
'model' => $model,
]);
}
}