Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
90cfb6eb
Commit
90cfb6eb
authored
Apr 25, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ActiveForm wip
parent
bf22d96b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
94 additions
and
9 deletions
+94
-9
SiteController.php
app/protected/controllers/SiteController.php
+4
-3
LoginForm.php
app/protected/models/LoginForm.php
+38
-0
User.php
app/protected/models/User.php
+22
-3
index.php
app/protected/views/site/index.php
+1
-1
login.php
app/protected/views/site/login.php
+8
-0
Application.php
framework/base/Application.php
+1
-1
View.php
framework/base/View.php
+1
-1
ActiveForm.php
framework/widgets/ActiveForm.php
+19
-0
No files found.
app/protected/controllers/SiteController.php
View file @
90cfb6eb
...
...
@@ -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
()
...
...
app/protected/models/LoginForm.php
0 → 100644
View file @
90cfb6eb
<?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
app/protected/models/User.php
View file @
90cfb6eb
...
...
@@ -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
app/protected/views/site/index.php
View file @
90cfb6eb
...
...
@@ -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
.
"<br/>"
;
echo
"You are logged in as "
.
$user
->
identity
->
user
name
.
"<br/>"
;
echo
Html
::
a
(
'logout'
,
array
(
'logout'
));
}
?>
...
...
app/protected/views/site/login.php
0 → 100644
View file @
90cfb6eb
<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
framework/base/Application.php
View file @
90cfb6eb
...
...
@@ -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
;
...
...
framework/base/View.php
View file @
90cfb6eb
...
...
@@ -352,7 +352,7 @@ class View extends Component
if
(
!
isset
(
$properties
[
'view'
]))
{
$properties
[
'view'
]
=
$this
;
}
return
Yii
::
createObject
(
$properties
,
$this
);
return
Yii
::
createObject
(
$properties
);
}
/**
...
...
framework/widgets/ActiveForm.php
View file @
90cfb6eb
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment