Last active
August 29, 2015 14:21
-
-
Save cizario/a4ee596b6013872eb0eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http; | |
/** | |
* 01 - Set the auth config as usual in app/config/auth.php file. | |
* This example uses Eloquent driver... | |
* | |
* 02 - Create a Middleware for each auth context ex: Admin, Customers. | |
* | |
* 03 - Register the Middlewares in app/Http/Kernel.php routeMiddleware array... | |
* | |
* 04 - Create one Auth Controller for each auth context: AdminAuthController, CustomerAuthController. | |
* After authenticate, you may set the auth context, example Session::set( 'auth.context', 'admin' ); | |
* This will prevent, for example a logged Customer access content in other context... | |
* | |
* 05 - All Controllers related to Admin access will extend from BackendControler, | |
* wich has $this->middleware( 'adminUser' ) call in constructor (the Middleware you registered | |
* in app/Http/Kernel.php routeMiddleware array for this kind of user). | |
* | |
* All Controllers related to Customer access will extend from CustomerControler, wich | |
* has $this->middleware( 'customerUser' ) call in constructor (the Middleware you registered | |
* in app/Http/Kernel.php routeMiddleware array for this kind of user). | |
* | |
* That's it. | |
*/ | |
use Illuminate\Foundation\Http\Kernel as HttpKernel; | |
class Kernel extends HttpKernel { | |
/** | |
* The application's global HTTP middleware stack. | |
* | |
* @var array | |
*/ | |
protected $middleware = [ | |
'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode', | |
'Illuminate\Cookie\Middleware\EncryptCookies', | |
'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', | |
'Illuminate\Session\Middleware\StartSession', | |
'Illuminate\View\Middleware\ShareErrorsFromSession', | |
'App\Http\Middleware\VerifyCsrfToken', | |
]; | |
/** | |
* The application's route middleware. | |
* | |
* @var array | |
*/ | |
protected $routeMiddleware = [ | |
'auth' => 'App\Http\Middleware\Authenticate', | |
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth', | |
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated', | |
'authUsuario' => 'App\Neo\Usuario\Http\Middleware\Authenticate', | |
'authCliente' => 'App\Neo\Cliente\Http\Middleware\Authenticate', | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Cliente\Http\Controllers; | |
use Illuminate\Foundation\Bus\DispatchesCommands; | |
use Illuminate\Routing\Controller as Controller; | |
use Illuminate\Foundation\Validation\ValidatesRequests; | |
use Response; | |
use ReflectionClass; | |
abstract class AreaClienteBaseController extends Controller { | |
use DispatchesCommands, ValidatesRequests; | |
protected $modelName = null; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
//filtro de autenticação de usuários | |
$this->middleware( 'authCliente' ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Cliente\Http\Controllers\Auth; | |
use App\Neo\Support\Http\Controllers\BaseController; | |
use Config; | |
use Input; | |
use Auth; | |
use Redirect; | |
use Session; | |
class AuthController extends BaseController { | |
/** | |
* Mostra o formulário de login | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function login() | |
{ | |
return view('cliente::auth.login'); | |
} | |
/** | |
* Process Auth form login | |
* | |
*/ | |
public function autentica() | |
{ | |
Config::set( 'auth.model' , 'App\Neo\Cliente\Models\Cliente' ); | |
Config::set( 'auth.table' , 'clientes' ); | |
$usuario = [ | |
'usuario' => Input::get('usuario'), | |
'password' => Input::get('senha') //'password' campo obrigatório para o sistema de autenticação | |
]; | |
if ( Auth::attempt($usuario) ) | |
{ | |
Session::set( 'auth.contexto', 'cliente' ); | |
return Redirect::route( 'areaCliente.index' ); | |
} | |
else | |
{ | |
return Redirect::route( 'cliente.login' ) | |
->withInput( Input::only('usuario') ) | |
->withErrors([ | |
'usuario' => 'A combinação de usuário/senha não está correta.' | |
]); | |
} | |
} | |
/** | |
* Logout user | |
*/ | |
public function logout() | |
{ | |
Auth::logout(); | |
return Redirect::to('cliente/login'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Cliente\Http\Middleware; | |
use Closure; | |
use Illuminate\Contracts\Auth\Guard; | |
class Authenticate { | |
/** | |
* The Guard implementation. | |
* | |
* @var Guard | |
*/ | |
protected $auth; | |
/** | |
* Create a new filter instance. | |
* | |
* @param Guard $auth | |
* @return void | |
*/ | |
public function __construct(Guard $auth) | |
{ | |
$this->auth = $auth; | |
} | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if ( $this->auth->guest() or $this->contextoIncorreto() ) | |
{ | |
if ($request->ajax()) | |
{ | |
return response('Unauthorized.', 401); | |
} | |
else | |
{ | |
return redirect()->guest('cliente/login'); | |
} | |
} | |
return $next($request); | |
} | |
/** | |
* Certifica que o usuário logado está no contexto correto | |
* | |
* @return boolean | |
*/ | |
private function contextoIncorreto() | |
{ | |
return session( 'auth.contexto' ) != 'cliente'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Support\Http\Controllers; | |
use Illuminate\Foundation\Bus\DispatchesCommands; | |
use Illuminate\Routing\Controller as Controller; | |
use Illuminate\Foundation\Validation\ValidatesRequests; | |
use Response; | |
use ReflectionClass; | |
abstract class BackendController extends Controller { | |
use DispatchesCommands, ValidatesRequests; | |
protected $modelName = null; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
//filtro de autenticação de usuários | |
$this->middleware( 'authUsuario' ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Usuario\Http\Controllers\Auth; | |
use App\Neo\Support\Http\Controllers\BaseController; | |
use Input; | |
use Auth; | |
use Redirect; | |
use Session; | |
class AuthController extends BaseController { | |
/** | |
* Show the application login form. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function login() | |
{ | |
return view('usuario::auth.login'); | |
} | |
/** | |
* Process Auth form login | |
* | |
*/ | |
public function autentica() | |
{ | |
$usuario = [ | |
'usuario' => Input::get('usuario'), | |
'password' => Input::get('senha') //'password' campo obrigatório para o sistema de autenticação | |
]; | |
if ( Auth::attempt($usuario) ) | |
{ | |
Session::set( 'auth.contexto', 'usuario' ); | |
return Redirect::to('admin'); | |
} | |
else | |
{ | |
return Redirect::to('adm') | |
->withInput( Input::only('usuario') ) | |
->withErrors([ | |
'usuario' => 'A combinação de usuário/senha não está correta.' | |
]); | |
} | |
} | |
/** | |
* Logout user | |
*/ | |
public function logout() | |
{ | |
Auth::logout(); | |
return Redirect::to('adm'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Neo\Usuario\Http\Middleware; | |
use Closure; | |
use Illuminate\Contracts\Auth\Guard; | |
class Authenticate { | |
/** | |
* The Guard implementation. | |
* | |
* @var Guard | |
*/ | |
protected $auth; | |
/** | |
* Create a new filter instance. | |
* | |
* @param Guard $auth | |
* @return void | |
*/ | |
public function __construct(Guard $auth) | |
{ | |
$this->auth = $auth; | |
} | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if ( $this->auth->guest() or $this->contextoIncorreto() ) | |
{ | |
if ($request->ajax()) | |
{ | |
return response('Unauthorized.', 401); | |
} | |
else | |
{ | |
return redirect()->guest('adm'); | |
} | |
} | |
return $next($request); | |
} | |
/** | |
* Certifica que o usuário logado está no contexto correto | |
* | |
* @return boolean | |
*/ | |
private function contextoIncorreto() | |
{ | |
return session( 'auth.contexto' ) != 'usuario'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment