From dabca4608140e21a4f3ac602bb369622d7c4556b Mon Sep 17 00:00:00 2001 From: trieyouth Date: Sun, 13 Dec 2015 22:19:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E4=BA=86=E7=8B=97=E7=9A=84=E7=99=BB?= =?UTF-8?q?=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/HomeController.php | 25 ++++++++++++++++++++++++- controllers/LoginController.php | 18 ++++++++++-------- models/LoginForm.php | 9 ++------- models/User.php | 29 ++++++++--------------------- util/IntentUtil.php | 2 +- 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/controllers/HomeController.php b/controllers/HomeController.php index 8c23420..44c1758 100644 --- a/controllers/HomeController.php +++ b/controllers/HomeController.php @@ -10,13 +10,36 @@ use app\util\IntentUtil; use Yii; use yii\base\Controller; +use yii\filters\AccessControl; class HomeController extends Controller { - public $layout = "@app/views/layouts/base.php"; + + public function behaviors() + { + return [ + 'access' => [ + 'class' => AccessControl::className(), + 'only' => ['index'], + 'rules' => [ + [ + 'allow' => true, + 'actions' => [], + 'roles' => ['?'], + ], + [ + 'allow' => true, + 'actions' => ['index'], + 'roles' => ['@'], + ], + ], + ], + ]; + } public function actionIndex() { + $this->layout = "@app/views/layouts/base.php"; IntentUtil::sendParams('youth','@web/img/welcome.png'); return $this->render('index'); } diff --git a/controllers/LoginController.php b/controllers/LoginController.php index bbf04b0..64a0c9d 100644 --- a/controllers/LoginController.php +++ b/controllers/LoginController.php @@ -8,6 +8,7 @@ namespace app\controllers; use app\models\LoginForm; +use app\models\User; use Yii; use yii\base\Controller; use yii\filters\AccessControl; @@ -25,10 +26,10 @@ public function behaviors()//行为验证 是登陆状态还是登出状态 要 'class' => AccessControl::className(), 'only' => ['login', 'logout', 'signup'], 'denyCallback' => function ($rule, $action) { - if(strcmp($action->id,'logout')==0){ + if (strcmp($action->id, 'logout') == 0) { throw new \Exception('您还没有登陆'); } - if(strcmp($action->id,'login')==0 || strcmp($action->id,'signup')==0){ + if (strcmp($action->id, 'login') == 0 || strcmp($action->id, 'signup') == 0) { throw new \Exception('您已经登陆'); } }, @@ -55,18 +56,19 @@ public function actionIndex() public function actionLogin() { - if(!Yii::$app->user->isGuest){ + + if (!Yii::$app->user->isGuest) { return Yii::$app->response->redirect(['home/index']); } $login = new LoginForm(); - if($login->load(Yii::$app->request->post()) && $login->validate()){ + if ($login->load(Yii::$app->request->post()) && $login->login()) { return Yii::$app->response->redirect(['home/index']); - }else{ - return $this->render('login',[ - 'model' => $login, - ]); + } else { + return $this->render('login', [ + 'model' => $login, + ]); } } diff --git a/models/LoginForm.php b/models/LoginForm.php index 0b053ee..a546bda 100644 --- a/models/LoginForm.php +++ b/models/LoginForm.php @@ -27,10 +27,10 @@ public function rules() [['username', 'password'], 'required','message'=>'不能为空'], // rememberMe must be a boolean value ['rememberMe', 'boolean'], - // password is validated by validatePassword() - ['password', 'validatePassword','message'=>'密码或用户名错误'], ['username','email','message'=>'用户名格式不正确'], ['password','string','max'=>18, 'min'=>6, 'tooLong'=>'密码请输入长度为6-18位字符', 'tooShort'=>'密码请输入长度为6-18位字符'], + // password is validated by validatePassword() + ['password', 'validatePassword','message'=>'密码或用户名错误'], ]; } @@ -46,11 +46,6 @@ public function validatePassword($attribute, $params) { if (!$this->hasErrors()) { $user = $this->getUser(); - if($user){ - echo $user->id; - }else{ - echo 'no user'; - } if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, '用户名或密码错误'); } diff --git a/models/User.php b/models/User.php index c4cf4f4..0c9f3b0 100644 --- a/models/User.php +++ b/models/User.php @@ -2,16 +2,12 @@ namespace app\models; +use yii\base\Object; use yii\db\ActiveRecord; use yii\web\IdentityInterface; class User extends ActiveRecord implements IdentityInterface { - public $id; - public $name; - public $pwd; - public $authKey; - public $token; public static function tableName() { @@ -23,12 +19,7 @@ public static function tableName() */ public static function findIdentity($id) { - $user = static::findOne($id); - if($user != null) { - echo $user->token."ddddd".$user->name."nnnnn"; - }else{ - echo 'user is null'; - } + $user = User::findOne($id); return $user; } @@ -38,18 +29,18 @@ public static function findIdentity($id) public static function findIdentityByAccessToken($token, $type = null) { - return static::findOne(['token'=>$token]); + return static::findOne(['token' => $token]); } /** * Finds user by username * - * @param string $username + * @param string $username * @return static|null */ public static function findByUsername($username) { - return static::findOne(['name'=>$username]); + return static::findOne(['name' => $username]); } /** @@ -65,7 +56,7 @@ public function getId() */ public function getAuthKey() { - return $this->authKey; + return $this->auth_key; } /** @@ -73,21 +64,17 @@ public function getAuthKey() */ public function validateAuthKey($authKey) { - return $this->authKey === $authKey; + return $this->auth_key === $authKey; } /** * Validates password * - * @param string $password password to validate + * @param string $password password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { - $user = $this->find(); - foreach ($user->all() as $u){ - echo $u->id.'ccccc'; - } return $this->pwd === $password; } } diff --git a/util/IntentUtil.php b/util/IntentUtil.php index 04da26d..27d90ed 100644 --- a/util/IntentUtil.php +++ b/util/IntentUtil.php @@ -19,7 +19,7 @@ public static function sendParams($name,$head){ throw new \Exception("util IntentUtil sendParams方法的head参数不可以是空"); } $view = \Yii::$app->view; - $view->params['name']=$name; + $view->params['name']=\Yii::$app->user->getId().'ddddd'; $view->params['head']=Url::to($head,true); $view->params['back']=Url::to('@web/img/fond.png',true); }