add login,register page

尚未检验登录注册是否工作
This commit is contained in:
Chenx221 2024-02-09 12:47:48 +08:00
parent cde47b6261
commit 7303af1048
4 changed files with 80 additions and 3 deletions

View File

@ -5,9 +5,11 @@ namespace app\controllers;
use app\models\User;
use app\models\UserSearch;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;
/**
* UserController implements the CRUD actions for User model.
@ -135,6 +137,8 @@ class UserController extends Controller
/**
* Displays the login page.
* visit via https://devs.chenx221.cyou:8081/index.php?r=user%2Flogin
*
* @return string|\yii\web\Response
*/
public function actionLogin()
@ -143,7 +147,7 @@ class UserController extends Controller
return $this->goHome();
}
$model = new User();
$model = new User(['scenario' => 'login']);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->login()) {
@ -171,11 +175,13 @@ class UserController extends Controller
/**
* Displays the registration page.
* @return string|\yii\web\Response
* visit via https://devs.chenx221.cyou:8081/index.php?r=user%2Fregister
* @return string|Response
* @throws Exception
*/
public function actionRegister()
{
$model = new User();
$model = new User(['scenario' => 'register']);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->password = Yii::$app->security->generatePasswordHash($model->password);

View File

@ -52,6 +52,7 @@ class User extends \yii\db\ActiveRecord implements IdentityInterface
'status' => 'Status',
];
}
/**
* Finds an identity by the given ID.
*
@ -107,4 +108,29 @@ class User extends \yii\db\ActiveRecord implements IdentityInterface
{
return $this->getAuthKey() === $authKey;
}
/**
* 验证用户名密码
*
* @return bool 返回用户名密码验证状态
*/
public function login()
{
$user = User::findOne(['username' => $this->username]);
if ($user !== null && $user->validatePassword($this->password)) {
return Yii::$app->user->login($user);
}
return false;
}
/**
* @param $password
* @return bool
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
}

22
views/user/login.php Normal file
View File

@ -0,0 +1,22 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/** @var yii\web\View $this */
/** @var app\models\User $model */
/** @var ActiveForm $form */
?>
<div class="user-login">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->label('用户名') ?>
<?= $form->field($model, 'password')->label('密码') ?>
<div class="form-group">
<?= Html::submitButton('登录', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div><!-- user-login -->

23
views/user/register.php Normal file
View File

@ -0,0 +1,23 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/** @var yii\web\View $this */
/** @var app\models\User $model */
/** @var ActiveForm $form */
?>
<div class="user-register">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'username')->label('用户名') ?>
<?= $form->field($model, 'password')->label('密码') ?>
<?= $form->field($model, 'email')->label('电子邮箱') ?>
<div class="form-group">
<?= Html::submitButton('注册', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div><!-- user-register -->