示例#1
0
 public function actionRegister()
 {
     $errors = null;
     $model = new UserForm();
     if (Yii::$app->request->isPost) {
         $model->setAttributes(Yii::$app->request->post());
         if ($model->validate()) {
             $user = new User();
             $user->setAttributes($model->getAttributes());
             $user->setPassword($model->password);
             $user->generateAuthKey();
             $save = $user->save();
             if ($save) {
                 $purse = new Purse();
                 $purse->user_id = $user->id;
                 $purse->active = 1;
                 $purse->balance = 0;
                 $purse->name = "Основной";
                 $purse->save();
                 $login = Yii::$app->user->login($user, 3600 * 24 * 14);
                 if ($login) {
                     return $this->goHome();
                 }
             }
         } else {
             $errors = $model->getErrors();
         }
     }
     return $this->renderPartial('register', ['errors' => $errors, 'model' => $model]);
 }
示例#2
0
 public function actionIndex()
 {
     $model = new UserForm();
     $model->id = -1;
     $message = "";
     $error = "";
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $user = UserForm::findOne(['username' => $model->username, 'password' => $model->password]);
         if (sizeof($user) > 0) {
             $model->id = $user->id;
             $session = Yii::$app->session;
             $session->open();
             $session["user"] = $model;
             return $this->redirect("index.php", 302);
         }
     } else {
         $error = $model->getErrors();
     }
     return $this->renderPartial("index", ['model' => $model, 'message' => $message, 'error' => $error]);
 }