/** * 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(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * 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() { $model = new User(['scenario' => 'admin-create']); $statusArray = User::getStatusArray(); $roleArray = User::getRoleArray(); if ($model->load(Yii::$app->request->post())) { if ($model->save()) { return $this->redirect(['index']); } } return $this->render('create', ['model' => $model, 'statusArray' => $statusArray, 'roleArray' => $roleArray]); }
public function Register() { if ($this->validate()) { $user = new User(); $userProps['User'] = $this->getAttributes(); $user->load($userProps); $user->save(); Yii::$app->user->login($user, 60 * 60 * 24 * 365); return true; } else { return false; } }
/** * 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(); if ($model->load(Yii::$app->request->post())) { $security = new Security(); $model->password = $security->generatePasswordHash(md5($model->password)); $model->auth_key = $security->generateRandomString(64); $model->created_date = time(); if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model]); } }
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]); }