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
5b36503c
Commit
5b36503c
authored
Nov 16, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
One less SQL query for login sequence in basic and advanced apps
parent
b06caa29
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
4 deletions
+34
-4
LoginForm.php
apps/advanced/common/models/LoginForm.php
+17
-2
LoginForm.php
apps/basic/models/LoginForm.php
+17
-2
No files found.
apps/advanced/common/models/LoginForm.php
View file @
5b36503c
...
@@ -13,6 +13,7 @@ class LoginForm extends Model
...
@@ -13,6 +13,7 @@ class LoginForm extends Model
public
$username
;
public
$username
;
public
$password
;
public
$password
;
public
$rememberMe
=
true
;
public
$rememberMe
=
true
;
private
$_user
;
/**
/**
* @return array the validation rules.
* @return array the validation rules.
...
@@ -35,7 +36,7 @@ class LoginForm extends Model
...
@@ -35,7 +36,7 @@ class LoginForm extends Model
*/
*/
public
function
validatePassword
()
public
function
validatePassword
()
{
{
$user
=
User
::
find
ByUsername
(
$this
->
username
);
$user
=
$this
->
getUser
ByUsername
(
$this
->
username
);
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
}
}
...
@@ -48,11 +49,25 @@ class LoginForm extends Model
...
@@ -48,11 +49,25 @@ class LoginForm extends Model
public
function
login
()
public
function
login
()
{
{
if
(
$this
->
validate
())
{
if
(
$this
->
validate
())
{
$user
=
User
::
find
ByUsername
(
$this
->
username
);
$user
=
$this
->
getUser
ByUsername
(
$this
->
username
);
Yii
::
$app
->
user
->
login
(
$user
,
$this
->
rememberMe
?
3600
*
24
*
30
:
0
);
Yii
::
$app
->
user
->
login
(
$user
,
$this
->
rememberMe
?
3600
*
24
*
30
:
0
);
return
true
;
return
true
;
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
}
/**
* Finds user by username
*
* @param string $username
* @return User|null
*/
private
function
getUserByUsername
(
$username
)
{
if
(
$this
->
_user
===
null
)
{
$this
->
_user
=
User
::
findByUsername
(
$username
);
}
return
$this
->
_user
;
}
}
}
apps/basic/models/LoginForm.php
View file @
5b36503c
...
@@ -13,6 +13,7 @@ class LoginForm extends Model
...
@@ -13,6 +13,7 @@ class LoginForm extends Model
public
$username
;
public
$username
;
public
$password
;
public
$password
;
public
$rememberMe
=
true
;
public
$rememberMe
=
true
;
private
$_user
;
/**
/**
* @return array the validation rules.
* @return array the validation rules.
...
@@ -35,7 +36,7 @@ class LoginForm extends Model
...
@@ -35,7 +36,7 @@ class LoginForm extends Model
*/
*/
public
function
validatePassword
()
public
function
validatePassword
()
{
{
$user
=
User
::
find
ByUsername
(
$this
->
username
);
$user
=
$this
->
getUser
ByUsername
(
$this
->
username
);
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
if
(
!
$user
||
!
$user
->
validatePassword
(
$this
->
password
))
{
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
$this
->
addError
(
'password'
,
'Incorrect username or password.'
);
}
}
...
@@ -48,11 +49,25 @@ class LoginForm extends Model
...
@@ -48,11 +49,25 @@ class LoginForm extends Model
public
function
login
()
public
function
login
()
{
{
if
(
$this
->
validate
())
{
if
(
$this
->
validate
())
{
$user
=
User
::
find
ByUsername
(
$this
->
username
);
$user
=
$this
->
getUser
ByUsername
(
$this
->
username
);
Yii
::
$app
->
user
->
login
(
$user
,
$this
->
rememberMe
?
3600
*
24
*
30
:
0
);
Yii
::
$app
->
user
->
login
(
$user
,
$this
->
rememberMe
?
3600
*
24
*
30
:
0
);
return
true
;
return
true
;
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
}
/**
* Finds user by username
*
* @param string $username
* @return User|null
*/
private
function
getUserByUsername
(
$username
)
{
if
(
$this
->
_user
===
null
)
{
$this
->
_user
=
User
::
findByUsername
(
$username
);
}
return
$this
->
_user
;
}
}
}
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