* @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, ]); } }