From 90cfb6ebb837a76a4e38a93184a0baca3ca9e8ac Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Thu, 25 Apr 2013 23:17:58 -0400 Subject: [PATCH] ActiveForm wip --- app/protected/controllers/SiteController.php | 7 +++--- app/protected/models/LoginForm.php | 37 ++++++++++++++++++++++++++++ app/protected/models/User.php | 24 +++++++++++++++--- app/protected/views/site/index.php | 2 +- app/protected/views/site/login.php | 7 ++++++ framework/base/Application.php | 2 +- framework/base/View.php | 2 +- framework/widgets/ActiveForm.php | 19 ++++++++++++++ 8 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 app/protected/models/LoginForm.php create mode 100644 app/protected/views/site/login.php diff --git a/app/protected/controllers/SiteController.php b/app/protected/controllers/SiteController.php index 58e9568..776c927 100644 --- a/app/protected/controllers/SiteController.php +++ b/app/protected/controllers/SiteController.php @@ -9,9 +9,10 @@ class SiteController extends \yii\web\Controller public function actionLogin() { - $user = app\models\User::findIdentity(100); - Yii::$app->getUser()->login($user); - Yii::$app->getResponse()->redirect(array('site/index')); + echo $this->render('login'); +// $user = app\models\User::findIdentity(100); +// Yii::$app->getUser()->login($user); +// Yii::$app->getResponse()->redirect(array('site/index')); } public function actionLogout() diff --git a/app/protected/models/LoginForm.php b/app/protected/models/LoginForm.php new file mode 100644 index 0000000..7151bce --- /dev/null +++ b/app/protected/models/LoginForm.php @@ -0,0 +1,37 @@ + + * @since 2.0 + */ +class LoginForm extends Model +{ + public $username; + public $password; + + public function rules() + { + return array( + array('username', 'required'), + array('password', 'required'), + array('password', 'validatePassword'), + ); + } + + public function validatePassword() + { + $user = User::findByUsername($this->username); + if (!$user && $user->validatePassword($this->password)) { + $this->addError('password', 'Incorrect username or password.'); + } + } +} \ No newline at end of file diff --git a/app/protected/models/User.php b/app/protected/models/User.php index cebf1da..fcbf14a 100644 --- a/app/protected/models/User.php +++ b/app/protected/models/User.php @@ -5,19 +5,22 @@ namespace app\models; class User extends \yii\base\Object implements \yii\web\Identity { public $id; - public $name; + public $username; + public $password; public $authKey; private static $users = array( '100' => array( 'id' => '100', + 'username' => 'admin', + 'password' => 'admin', 'authKey' => 'test100key', - 'name' => 'admin', ), '101' => array( 'id' => '101', + 'username' => 'demo', + 'password' => 'demo', 'authKey' => 'test101key', - 'name' => 'demo', ), ); @@ -26,6 +29,16 @@ class User extends \yii\base\Object implements \yii\web\Identity return isset(self::$users[$id]) ? new self(self::$users[$id]) : null; } + public static function findByUsername($username) + { + foreach (self::$users as $user) { + if (strcasecmp($user['username'], $username) === 0) { + return new self($user); + } + } + return null; + } + public function getId() { return $this->id; @@ -40,4 +53,9 @@ class User extends \yii\base\Object implements \yii\web\Identity { return $this->authKey === $authKey; } + + public function validatePassword($password) + { + return $this->password === $password; + } } \ No newline at end of file diff --git a/app/protected/views/site/index.php b/app/protected/views/site/index.php index 3b83080..66e4dd1 100644 --- a/app/protected/views/site/index.php +++ b/app/protected/views/site/index.php @@ -9,7 +9,7 @@ $user = Yii::$app->getUser(); if ($user->isGuest) { echo Html::a('login', array('login')); } else { - echo "You are logged in as " . $user->identity->name . "
"; + echo "You are logged in as " . $user->identity->username . "
"; echo Html::a('logout', array('logout')); } ?> diff --git a/app/protected/views/site/login.php b/app/protected/views/site/login.php new file mode 100644 index 0000000..3178f14 --- /dev/null +++ b/app/protected/views/site/login.php @@ -0,0 +1,7 @@ +

Login

+ +

Please fill out the following fields to login:

+ +beginWidget('yii\widgets\ActiveForm', array('method' => 'put')); ?> + +endWidget(); ?> \ No newline at end of file diff --git a/framework/base/Application.php b/framework/base/Application.php index c498a8e..6dca5cf 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -176,7 +176,7 @@ class Application extends Module */ public function getRuntimePath() { - if ($this->_runtimePath !== null) { + if ($this->_runtimePath === null) { $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime'); } return $this->_runtimePath; diff --git a/framework/base/View.php b/framework/base/View.php index 10a7053..edb67be 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -352,7 +352,7 @@ class View extends Component if (!isset($properties['view'])) { $properties['view'] = $this; } - return Yii::createObject($properties, $this); + return Yii::createObject($properties); } /** diff --git a/framework/widgets/ActiveForm.php b/framework/widgets/ActiveForm.php index 83506dd..79331e5 100644 --- a/framework/widgets/ActiveForm.php +++ b/framework/widgets/ActiveForm.php @@ -59,6 +59,25 @@ class ActiveForm extends Widget public $options = array(); + + /** + * Initializes the widget. + * This renders the form open tag. + */ + public function init() + { + echo Html::beginForm($this->action, $this->method, $this->options); + } + + /** + * Runs the widget. + * This registers the necessary javascript code and renders the form close tag. + */ + public function run() + { + echo Html::endForm(); + } + /** * @param Model|Model[] $models * @param array $options