/** * 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(); $model->scenario = User::SCENARIO_CREATE; if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (!($role = User::getAuthItem($model->role))) { $model->addError('role', 'Role does not exist'); } else { $transaction = $model->getDb()->beginTransaction(); try { if ($model->save(false)) { if (!$model->assignRole()) { throw new Exception(); } if (!Yii::$app->user->can(User::PERMISSION_CAN_CUD, $model)) { throw new Exception(); } $transaction->commit(); return $this->redirect('index'); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('create', ['model' => $model]); }
/** **创建一个新的用户.如果创建成功,浏览器会跳转到该用户的详情页面. * @return mixed */ public function actionCreate() { $model = new User(); try { if ($model->load($_POST) && $model->save()) { return $this->redirect(Url::previous()); } elseif (!\Yii::$app->request->isPost) { $model->load($_GET); } } catch (\Exception $e) { $msg = isset($e->errorInfo[2]) ? $e->errorInfo[2] : $e->getMessage(); $model->addError('_exception', $msg); } return $this->render('create', ['model' => $model]); }
/** * 绑定账号 */ public function actionBindAccount() { $this->layout = false; $old_login_uid = ZCommonSessionFun::get_user_id(); if (ZCommonSessionFun::get_user_id() < 1) { $url = Yii::$app->urlManager->createUrl([ZCommonSessionFun::urlLoginUserStr]); return $this->redirect($url); } $model = new User(); if (isset($_POST['User']['user']) && isset($_POST['op'])) { $post = $_POST['User']; $user = isset($post['user']) ? $post['user'] : ''; $pass = isset($post['pass']) ? $post['pass'] : ''; $post = $_POST['User']; //已有账户绑定 if ($_POST['op'] == 1) { $condition['user'] = $user; $model_find = new User(); $model_find = $model_find->find()->where($condition)->one(); if (!$model_find) { $model->addError('user', '用户没有找到'); } else { if ($model_find->pass != ZCommonFun::getPass($pass)) { $model->addError('pass', '密码错误'); } else { if ($model_find->is_bind_user == 1) { $model->addError('user', '账户已被绑定过'); } else { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $model_find->is_bind_user = 1; $model_find->save(); $model_Oauth = new OauthBind(); $model_Oauth_condition['uid'] = $old_login_uid; $model_Oauth_attributes['uid'] = $model_find->uid; $model_Oauth->updateAll($model_Oauth_attributes, $model_Oauth_condition); $transaction->commit(); ZCommonSessionFun::set_user_session($model_find->attributes); $url = Yii::$app->urlManager->createUrl(['user-profile/bind-list']); return $this->redirect($url); } catch (\Exception $e) { $transaction->rollBack(); $model->addError('user', '绑定失败'); } } } } } else { $model->pass = ZCommonFun::getPass($model->pass); $model = $model->findOne(ZCommonSessionFun::get_user_id()); if ($model) { $model->user = $user; $model->pass = ZCommonFun::getPass($pass); $model->is_bind_user = 1; if ($model->save()) { ZCommonSessionFun::set_user_session($model->attributes); $url = Yii::$app->urlManager->createUrl(['user-profile/bind-list']); return $this->redirect($url); } } else { $model = new User(); $model->user = $user; $model->pass = $pass; $model->addError('user', '用户已被删除'); } } } return $this->render('bind-account', ['model' => $model]); }