Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
9db8e1bb
Commit
9db8e1bb
authored
Sep 11, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplified hashing. re-worked the auth class for a little more flexibility.
parent
7f2e1e9c
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
112 deletions
+65
-112
application/config/auth.php
+37
-16
laravel/config/container.php
+2
-2
laravel/security/authenticator.php
+26
-54
laravel/security/hashing/hasher.php
+0
-40
No files found.
application/config/auth.php
View file @
9db8e1bb
...
...
@@ -4,41 +4,61 @@ return array(
/*
|--------------------------------------------------------------------------
| Retrieve
Users By ID
| Retrieve
The Current User
|--------------------------------------------------------------------------
|
| This method is called by the Auth::user() method when attempting to
| retrieve a user by their user ID, such as when retrieving a user by the
| user ID stored in the session.
| This closure is called by the Auth::user() method when attempting to
| retrieve a user by their ID stored in the session.
|
| You are free to change this method for your application however you wish.
| Simply return an object representing the user with the given ID. Or, if
| no user with the given ID is registered to use your application, you do
| not need to return anything.
|
| Of course, a simple, elegant authentication solution is already provided
| for you using Eloquent and the default Laravel hashing engine.
|
*/
'
by_id
'
=>
function
(
$id
)
'
user
'
=>
function
(
$id
)
{
return
User
::
find
(
$id
);
if
(
!
is_null
(
$id
))
return
User
::
find
(
$id
);
},
/*
|--------------------------------------------------------------------------
|
Retrieve Users By Username
|
Authenticate User Credentials
|--------------------------------------------------------------------------
|
| This method is called by the Auth::check() method when attempting to
| retrieve a user by their username, such as when checking credentials
| received from a login form.
| This closure is called by the Auth::attempt() method when attempting to
| authenticate a user that is logging into your application.
|
| You are free to change this method for your application however you wish.
| If the provided credentials are correct, simply return an object that
| represents the user being authenticated. If the credentials are not
| valid, don't return anything.
|
| Note: This method must return an object that has "id" and "password"
| properties. The type of object returned does not matter.
| Note: If a user object is returned, it must have an "id" property.
|
*/
'by_username'
=>
function
(
$username
)
'attempt'
=>
function
(
$username
,
$password
)
{
if
(
!
is_null
(
$user
=
User
::
where
(
'email'
,
'='
,
$username
)
->
first
()))
{
return
User
::
where_email
(
$username
)
->
first
();
if
(
Hasher
::
check
(
$password
,
$user
->
password
))
return
$user
;
}
},
/*
|--------------------------------------------------------------------------
| Logout
|--------------------------------------------------------------------------
|
| Here you may do anything that needs to be done when a user logs out of
| your application, such as call the logout method on a third-party API
| you are using for authentication, or anything else you desire.
|
*/
'logout'
=>
function
(
$id
)
{}
);
\ No newline at end of file
laravel/config/container.php
View file @
9db8e1bb
...
...
@@ -14,9 +14,9 @@ return array(
}),
'laravel.auth'
=>
array
(
'resolver'
=>
function
(
$container
)
'laravel.auth'
=>
array
(
'
singleton'
=>
true
,
'
resolver'
=>
function
(
$container
)
{
return
new
Security\Authenticator
(
$container
->
resolve
(
'laravel.
session'
),
$container
->
resolve
(
'laravel.hasher
'
));
return
new
Security\Authenticator
(
$container
->
resolve
(
'laravel.
config'
),
$container
->
resolve
(
'laravel.session
'
));
}),
...
...
laravel/security/authenticator.php
View file @
9db8e1bb
<?php
namespace
Laravel\Security
;
use
Laravel\IoC
;
use
Laravel\Session\Driver
;
class
Authenticator
{
...
...
@@ -8,14 +7,9 @@ class Authenticator {
/**
* The current user of the application.
*
* If no user is logged in, this will be NULL. Otherwise, it will contain the result
* of the "by_id" closure in the authentication configuration file.
*
* Typically, the user should be accessed via the "user" method.
*
* @var object
*/
p
ublic
$user
;
p
rotected
$user
;
/**
* The session driver being used by the Auth instance.
...
...
@@ -25,30 +19,23 @@ class Authenticator {
protected
$session
;
/**
* The
hashing engine that should be used to perform hashing
.
* The
configuration manager instance
.
*
* @var
Hashing\Engine
* @var
Config
*/
protected
$
hasher
;
protected
$
engine
;
/**
*
The key used to store the user ID in the session
.
*
Create a new authenticator instance
.
*
* @var string
*/
protected
static
$key
=
'laravel_user_id'
;
/**
* Create a new Auth class instance.
*
* @param Session\Driver $driver
* @param Hashing\Engine $hasher
* @param Config $config
* @param Session\Driver $session
* @return void
*/
public
function
__construct
(
Driver
$driver
,
Hashing\Engine
$hasher
)
public
function
__construct
(
Config
$config
,
Driver
$session
)
{
$this
->
hasher
=
$hasher
;
$this
->
session
=
$
driver
;
$this
->
config
=
$config
;
$this
->
session
=
$
session
;
}
/**
...
...
@@ -64,58 +51,43 @@ class Authenticator {
/**
* Get the current user of the application.
*
* To retrieve the user, the user ID stored in the session will be passed to
* the "by_id" closure in the authentication configuration file. The result
* of the closure will be cached and returned.
* If the current user is not authenticated, NULL will be returned.
*
* @return object
*/
public
function
user
()
{
if
(
is_null
(
$this
->
user
)
and
$this
->
session
->
has
(
static
::
$key
))
{
$this
->
user
=
call_user_func
(
Config
::
get
(
'auth.by_id'
),
$this
->
session
->
get
(
static
::
$key
));
}
if
(
!
is_null
(
$this
->
user
))
return
$this
->
user
;
return
$this
->
user
;
return
$this
->
user
=
call_user_func
(
$this
->
config
->
get
(
'auth.user'
),
$this
->
session
->
get
(
'laravel_user_id'
))
;
}
/**
* Attempt to log a user into your application.
*
* If the user credentials are valid. The user's ID will be stored in the session and the
* user will be considered "logged in" on subsequent requests to the application.
* Attempt to log a user into the application.
*
*
The password passed to the method should be plain text, as it will be hashed
*
by the Hash class when authenticating
.
*
If the given credentials are valid, the user will be considered logged into the
*
application and their user ID will be stored in the session data
.
*
* @param string $username
* @param string $password
* @return bool
*/
public
function
login
(
$username
,
$password
)
public
function
attempt
(
$username
,
$password
=
null
)
{
if
(
!
is_null
(
$user
=
call_user_func
(
Config
::
get
(
'auth.by_username'
),
$username
)))
{
if
(
$this
->
hasher
->
check
(
$password
,
$user
->
password
))
if
(
!
is_null
(
$user
=
call_user_func
(
$this
->
config
->
get
(
'auth.attempt'
),
$username
,
$password
)))
{
$this
->
remember
(
$user
);
return
true
;
}
}
return
false
;
}
/**
* Log a user into
your
application.
* Log a user into
the
application.
*
* The user's ID will be stored in the session and the user will be considered
* "logged in" on subsequent requests to your application. This method is called
* by the login method after determining a user's credentials are valid.
*
* Note: The user given to this method should be an object having an "id" property.
* The user ID will be stored in the session so it is available on subsequent requests.
*
* @param object $user
* @return void
...
...
@@ -124,22 +96,21 @@ class Authenticator {
{
$this
->
user
=
$user
;
$this
->
session
->
put
(
static
::
$key
,
$user
->
id
);
$this
->
session
->
put
(
'laravel_user_id'
,
$user
->
id
);
}
/**
* Log the user out of your application.
*
* The user ID will be removed from the session and the user will no longer
* be considered logged in on subsequent requests to your application.
* Log the current user out of the application.
*
* @return void
*/
public
function
logout
()
{
call_user_func
(
$this
->
config
->
get
(
'auth.logout'
),
$this
->
user
()
->
id
);
$this
->
user
=
null
;
$this
->
session
->
forget
(
static
::
$key
);
$this
->
session
->
forget
(
'laravel_user_id'
);
}
}
\ No newline at end of file
laravel/security/hashing/hasher.php
deleted
100644 → 0
View file @
7f2e1e9c
<?php
namespace
Laravel\Security\Hashing
;
class
Hasher
{
/**
* The hashing engine being used to perform the hashing.
*
* @var Hash\Engine
*/
protected
$engine
;
/**
* Create a new Hasher instance.
*
* @param Engine $engine
* @return void
*/
public
function
__construct
(
Engine
$engine
)
{
$this
->
engine
=
$engine
}
/**
* Magic Method for delegating method calls to the hashing engine.
*/
public
function
__call
(
$method
,
$parameters
)
{
return
call_user_func_array
(
array
(
$this
->
engine
,
$method
),
$parameters
);
}
/**
* Magic Method for performing methods on the default hashing engine.
*/
public
static
function
__callStatic
(
$method
,
$parameters
)
{
return
call_user_func_array
(
array
(
static
::
make
()
->
engine
,
$method
),
$parameters
);
}
}
\ No newline at end of file
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