Skip to content

Commit

Permalink
Add middleware auto registering
Browse files Browse the repository at this point in the history
  • Loading branch information
santigarcor committed Sep 15, 2017
1 parent 35b3ae5 commit 4f128e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/Laratrust/LaratrustServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Factory;
use Laratrust\LaratrustRegistersBladeDirectives;
use Laratrust\SetupTeamsCommand;

class LaratrustServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -42,6 +39,12 @@ class LaratrustServiceProvider extends ServiceProvider
'Upgrade' => 'command.laratrust.upgrade'
];

protected $middlewares = [
'role' => \Laratrust\Middleware\LaratrustRole::class,
'permission' => \Laratrust\Middleware\LaratrustPermission::class,
'ability' => \Laratrust\Middleware\LaratrustAbility::class,
];

/**
* Bootstrap the application events.
*
Expand All @@ -55,12 +58,50 @@ public function boot()
__DIR__.'/../config/laratrust_seeder.php' => config_path('laratrust_seeder.php'),
], 'laratrust');

$this->useMorphMapForRelationships();

$this->autoRegisterMiddlewares();

if (class_exists('\Blade')) {
$this->registerBladeDirectives();
}
}

/**
* If the user wants to use the morphMap it uses the morphMap.
*
* @return void
*/
protected function useMorphMapForRelationships()
{
if ($this->app['config']->get('laratrust.use_morph_map')) {
Relation::morphMap($this->app['config']->get('laratrust.user_models'));
}
}

if (class_exists('\Blade')) {
$this->registerBladeDirectives();
/**
* Register the middlewares automatically.
*
* @return void
*/
protected function autoRegisterMiddlewares()
{
if (!$this->app['config']->get('laratrust.middleware.register')) {
return;
}

$router = $this->app['router'];

if (method_exists($router, 'middleware')) {
$registerMethod = 'middleware';
} elseif (method_exists($router, 'aliasMiddleware')) {
$registerMethod = 'aliasMiddleware';
} else {
return;
}

foreach ($this->middlewares as $key => $class) {
$router->$registerMethod($key, $class);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/config/laratrust.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
|
*/
'middleware' => [
/**
* Define if the laratrust middlewares are registered automatically in the service provider
*/
'register' => false,

/**
* Method to be called in the middleware return case.
* Available: abort|redirect
Expand Down

0 comments on commit 4f128e6

Please sign in to comment.