/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { $auth = Yii::$app->authManager; $role = $auth->getRole($this->jabatan); if ($this->validate() && !empty($role)) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->validate() && $user->save()) { $person = new Person(); $person->first_name = $this->first_name; $person->last_name = $this->last_name; $person->gender = $this->gender; $person->birth_date = $this->birth_date; $person->user_id = $user->id; if ($person->validate() && $person->save()) { $auth->assign($role, $user->id); return true; } } } return false; }
/** * Creates a new Person model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Person(); $modelUser = new User(); $modelAuthRule = new AuthRule(); $modelAuthItem = new AuthItem(); $authRule = AuthRule::find()->all(); $authItem = AuthItem::find()->all(); if (Yii::$app->request->isPost) { // do transaction if fails it will not saved $transaction = Yii::$app->db->beginTransaction(); try { if ($modelUser->load(Yii::$app->request->post()) && $modelUser->validate()) { $modelUser->generateAuthKey(); // first attempt save user record if ($modelUser->save()) { if ($model->load(Yii::$app->request->post())) { $model->user_id = $modelUser->id; // second attemp save person record if ($model->validate() && $model->save()) { if ($modelAuthItem->load(Yii::$app->request->post()) && $modelAuthItem->validate()) { $auth = Yii::$app->authManager; $role = $auth->getRole($modelAuthItem->name); if (!empty($role)) { // thrid attemp assign role to user $auth->assign($role, $modelUser->id); $transaction->commit(); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Data Karyawan Berhasil Disimpan')); return $this->redirect(['index']); } else { throw new \Exception("AuthRole search data checkpoint fail to save"); } } else { throw new \Exception("AuthItem (Role) validation checkpoint fail to save"); } } else { throw new \Exception("Person save checkpoint fail to save"); } } else { throw new \Exception("Person loaded checkpoint fail to save"); } } else { throw new \Exception("User save checkpoint fail to save"); } } else { throw new \Exception("User validation checkpoint fail to save"); } } catch (\Exception $e) { $transaction->rollback(); Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Karyawan Gagal Disimpan')); } } return $this->render('create', ['model' => $model, 'modelUser' => $modelUser, 'modelAuthRule' => $modelAuthRule, 'authRule' => $authRule, 'modelAuthItem' => $modelAuthItem, 'authItem' => $authItem]); }
public function actionCreate() { $user = new User(['scenario' => 'admin-create']); $profile = new Profile(); $statusArray = User::getStatusArray(); $roleArray = User::getRoleArray(); if ($user->load(Yii::$app->request->post())) { if ($user->validate()) { $transaction = Yii::$app->db->beginTransaction(); if ($user->save(false)) { $profile->user_id = $user->getId(); if ($profile->save(false)) { $transaction->commit(); return $this->redirect(['index']); } else { $transaction->rollback(); print_r($user->getErrors()); return false; } } else { Yii::$app->session->setFlash('danger', 'Ошибка'); return $this->refresh(); } } } // $transaction = Yii::$app->db->beginTransaction(); // if ($user->save()) // { // $profile->user_id = $user->getId(); // if($profile->save()) // { // $transaction->commit(); // return $user; // } // } // else // { // $transaction->rollback(); // print_r($user->getErrors()); // return false; // } return $this->render('create', ['model' => $user, 'statusArray' => $statusArray, 'roleArray' => $roleArray]); }
public function testValidateCorrectData() { $model = new User(['username' => 'other_user', 'email' => '*****@*****.**', 'status' => 1]); expect('model is valid', $model->validate())->true(); }