Commit 90cfb6eb by Qiang Xue

ActiveForm wip

parent bf22d96b
...@@ -9,9 +9,10 @@ class SiteController extends \yii\web\Controller ...@@ -9,9 +9,10 @@ class SiteController extends \yii\web\Controller
public function actionLogin() public function actionLogin()
{ {
$user = app\models\User::findIdentity(100); echo $this->render('login');
Yii::$app->getUser()->login($user); // $user = app\models\User::findIdentity(100);
Yii::$app->getResponse()->redirect(array('site/index')); // Yii::$app->getUser()->login($user);
// Yii::$app->getResponse()->redirect(array('site/index'));
} }
public function actionLogout() public function actionLogout()
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\models;
use yii\base\Model;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @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
...@@ -5,19 +5,22 @@ namespace app\models; ...@@ -5,19 +5,22 @@ namespace app\models;
class User extends \yii\base\Object implements \yii\web\Identity class User extends \yii\base\Object implements \yii\web\Identity
{ {
public $id; public $id;
public $name; public $username;
public $password;
public $authKey; public $authKey;
private static $users = array( private static $users = array(
'100' => array( '100' => array(
'id' => '100', 'id' => '100',
'username' => 'admin',
'password' => 'admin',
'authKey' => 'test100key', 'authKey' => 'test100key',
'name' => 'admin',
), ),
'101' => array( '101' => array(
'id' => '101', 'id' => '101',
'username' => 'demo',
'password' => 'demo',
'authKey' => 'test101key', 'authKey' => 'test101key',
'name' => 'demo',
), ),
); );
...@@ -26,6 +29,16 @@ class User extends \yii\base\Object implements \yii\web\Identity ...@@ -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; 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() public function getId()
{ {
return $this->id; return $this->id;
...@@ -40,4 +53,9 @@ class User extends \yii\base\Object implements \yii\web\Identity ...@@ -40,4 +53,9 @@ class User extends \yii\base\Object implements \yii\web\Identity
{ {
return $this->authKey === $authKey; return $this->authKey === $authKey;
} }
public function validatePassword($password)
{
return $this->password === $password;
}
} }
\ No newline at end of file
...@@ -9,7 +9,7 @@ $user = Yii::$app->getUser(); ...@@ -9,7 +9,7 @@ $user = Yii::$app->getUser();
if ($user->isGuest) { if ($user->isGuest) {
echo Html::a('login', array('login')); echo Html::a('login', array('login'));
} else { } else {
echo "You are logged in as " . $user->identity->name . "<br/>"; echo "You are logged in as " . $user->identity->username . "<br/>";
echo Html::a('logout', array('logout')); echo Html::a('logout', array('logout'));
} }
?> ?>
......
<h1>Login</h1>
<p>Please fill out the following fields to login:</p>
<?php $form = $this->beginWidget('yii\widgets\ActiveForm', array('method' => 'put')); ?>
<?php $this->endWidget(); ?>
\ No newline at end of file
...@@ -176,7 +176,7 @@ class Application extends Module ...@@ -176,7 +176,7 @@ class Application extends Module
*/ */
public function getRuntimePath() public function getRuntimePath()
{ {
if ($this->_runtimePath !== null) { if ($this->_runtimePath === null) {
$this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime'); $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
} }
return $this->_runtimePath; return $this->_runtimePath;
......
...@@ -352,7 +352,7 @@ class View extends Component ...@@ -352,7 +352,7 @@ class View extends Component
if (!isset($properties['view'])) { if (!isset($properties['view'])) {
$properties['view'] = $this; $properties['view'] = $this;
} }
return Yii::createObject($properties, $this); return Yii::createObject($properties);
} }
/** /**
......
...@@ -59,6 +59,25 @@ class ActiveForm extends Widget ...@@ -59,6 +59,25 @@ class ActiveForm extends Widget
public $options = array(); 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 Model|Model[] $models
* @param array $options * @param array $options
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment