Hi all,
I'm developing an application consisting of frontend and backend part. I want to have the two parts separated as much as possible. So I started developing the backend ("the admin part") as a separated package. I have a little problem with the authentication. The authentications should also be different - that means, if I'm logged in the admin section, it doesn't mean that I'm also logged in the "public" side. So I have two different tables for authentication: "users" and "admin_users". Now I just don't know, how to create the second authentication class. What I tried is, that I created a new service provider for this:
<?php
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Auth\UserInterface;
class MultiUserProvider implements UserProviderInterface {
protected $providers;
public function __construct() {
// This should be moved to the config later...
// This is a list of providers that can be used, including
// their user model, hasher class, and hasher options...
$this->providers = array(
'front' => array(
'model' => 'User',
'hasher' => 'Illuminate\Hashing\BcryptHasher',
),
'admin' => array(
'model' => 'AdminUser',
'hasher' => 'Illuminate\Hashing\BcryptHasher',
),
);
}
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveById($identifier)
{
// Returns the current provider from the session.
// Should throw an error if there is none...
$provider = Session::get('user.provider');
$user = $this->createModel($this->providers[$provider]['model'])->newQuery()->find($identifier);
if ($user){
$user->provider = $provider;
}
return $user;
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Auth\UserInterface|null
*/
public function retrieveByCredentials(array $credentials)
{
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
// Retrieve the provider from the $credentials array.
// Should throw an error if there is none...
$provider = $credentials['provider'];
$query = $this->createModel($this->providers[$provider]['model'])->newQuery();
foreach ($credentials as $key => $value)
{
if ( ! str_contains($key, 'password') && ! str_contains($key, 'provider'))
$query->where($key, $value);
}
$user = $query->first();
if ($user){
Session::put('user.provider', $provider);
$user->provider = $provider;
}
return $user;
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Auth\UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
$plain = $credentials['password'];
// Retrieve the provider from the $credentials array.
// Should throw an error if there is none...
$provider = $credentials['provider'];
$options = array();
if (isset($this->providers[$provider]['options'])){
foreach ($this->providers[$provider]['options'] as $key => $value) {
$options[$key] = $user->$value;
}
}
return $this->createModel($this->providers[$provider]['hasher'])
->check($plain, $user->getAuthPassword(), $options);
}
/**
* Create a new instance of a class.
*
* @param string $name Name of the class
* @return Class
*/
public function createModel($name)
{
$class = '\\'.ltrim($name, '\\');
return new $class;
}
}
What I don't know is how to create filters for the admin part. For example, we have these filters and I need to create their equivalent for the admin part:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest(URL::route('login'));
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
Could anybody help me with this? Or is there a better way how to separate the two application parts or at least the two authentication parts? Every post is appreciated.
You can make a custom filter
Route::filter('AuthBackEnd', function()
{
if (Auth::check()) return Redirect::to('admin');
});
use it in Routes as usual
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community