Browse Source

ActiveForm wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
90cfb6ebb8
  1. 7
      app/protected/controllers/SiteController.php
  2. 37
      app/protected/models/LoginForm.php
  3. 24
      app/protected/models/User.php
  4. 2
      app/protected/views/site/index.php
  5. 7
      app/protected/views/site/login.php
  6. 2
      framework/base/Application.php
  7. 2
      framework/base/View.php
  8. 19
      framework/widgets/ActiveForm.php

7
app/protected/controllers/SiteController.php

@ -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()

37
app/protected/models/LoginForm.php

@ -0,0 +1,37 @@
<?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.');
}
}
}

24
app/protected/models/User.php

@ -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
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
{ {
return $this->authKey === $authKey; return $this->authKey === $authKey;
} }
public function validatePassword($password)
{
return $this->password === $password;
}
} }

2
app/protected/views/site/index.php

@ -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'));
} }
?> ?>

7
app/protected/views/site/login.php

@ -0,0 +1,7 @@
<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(); ?>

2
framework/base/Application.php

@ -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;

2
framework/base/View.php

@ -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);
} }
/** /**

19
framework/widgets/ActiveForm.php

@ -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

Loading…
Cancel
Save