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
b3550192
Commit
b3550192
authored
Nov 01, 2013
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
some docs on Authentication
parent
7e805864
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
0 deletions
+72
-0
authentication.md
docs/guide/authentication.md
+72
-0
No files found.
docs/guide/authentication.md
View file @
b3550192
Authentication
==============
Authentication is basically what happens when one is trying to sign in. Typically login and passwords are read from
the form and then application checks if there's such user with such password.
In Yii all this is done semi-automatically and what's left to developer is to implement
[
[\yii\web\IdentityInterface
]
].
Typically it is being implemented in
`User`
model. You can find a full featured example in
[
advanced application template
](
installation.md
)
. Below only interface methods are listed:
```
php
class
User
extends
ActiveRecord
implements
IdentityInterface
{
// ...
/**
* Finds an identity by the given ID.
*
* @param string|integer $id the ID to be looked for
* @return IdentityInterface|null the identity object that matches the given ID.
*/
public
static
function
findIdentity
(
$id
)
{
return
static
::
find
(
$id
);
}
/**
* @return int|string current user ID
*/
public
function
getId
()
{
return
$this
->
id
;
}
/**
* @return string current user auth key
*/
public
function
getAuthKey
()
{
return
$this
->
auth_key
;
}
/**
* @param string $authKey
* @return boolean if auth key is valid for current user
*/
public
function
validateAuthKey
(
$authKey
)
{
return
$this
->
getAuthKey
()
===
$authKey
;
}
}
```
First two methods are simple.
`findIdentity`
given ID returns model instance while
`getId`
returns ID itself.
`getAuthKey`
and
`validateAuthKey`
are used to provide extra security to the "remember me" cookie.
`getAuthKey`
should return a string that is unique for each user. A good idea is to save this value when user is
created using
`Security::generateRandomKey()`
:
```
php
public
function
beforeSave
(
$insert
)
{
if
(
parent
::
beforeSave
(
$insert
))
{
if
(
$this
->
isNewRecord
)
{
$this
->
auth_key
=
Security
::
generateRandomKey
();
}
return
true
;
}
return
false
;
}
```
`validateAuthKey`
just compares
`$authKey`
passed as parameter (got from cookie) with the value got from database.
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