Ejemplo n.º 1
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new User(['scenario' => 'create']);
     if ($model->load(Yii::$app->request->post())) {
         $model->setPassword($model->password);
         //强制入库
         $model->generateAuthKey();
         //强制入库
         if ($model->save()) {
             Yii::$app->getAuthManager()->assign(Yii::$app->getAuthManager()->getRole($model->role_name), $model->id);
             //授权操作
             Yii::$app->getSession()->setFlash('success', Yii::t('common', 'Create Success!'));
             return $this->redirect(['index']);
         } else {
             Yii::$app->getSession()->setFlash('warning', Yii::t('common', 'Create Failure!'));
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Ejemplo n.º 2
0
 /**
  * Creates a new User model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $user = new User(['scenario' => User::SCENARIO_CREATE]);
     if (Yii::$app->request->isAjax && $user->load(Yii::$app->request->post())) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($user);
     }
     $transaction = Yii::$app->db->beginTransaction();
     if ($user->load(Yii::$app->request->post()) && $user->validate()) {
         $user->setPassword($user->new_password);
         $user->generateAuthKey();
         if ($user->save()) {
             $user->saveUserRbac($user);
             $transaction->commit();
             return $this->redirect(['view', 'id' => $user->id]);
         } else {
             $transaction->rollBack();
         }
     }
     return $this->render('create', ['model' => $user]);
 }