UserServiceProvider.php 4.6 KB
Newer Older
1 2 3 4 5
<?php

namespace Modules\User\Providers;

use Cartalyst\Sentinel\Laravel\SentinelServiceProvider;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\ServiceProvider;
8
use Modules\Core\Events\BuildingSidebar;
9
use Modules\Core\Events\LoadingBackendTranslations;
10
use Modules\Core\Traits\CanGetSidebarClassForModule;
11
use Modules\Core\Traits\CanPublishConfiguration;
12
use Modules\User\Console\GrantModulePermissionsCommand;
13
use Modules\User\Console\RemoveModulePermissionsCommand;
14 15
use Modules\User\Contracts\Authentication;
use Modules\User\Entities\UserToken;
16
use Modules\User\Events\Handlers\RegisterUserSidebar;
Nicolas Widart's avatar
Nicolas Widart committed
17
use Modules\User\Guards\Sentinel;
18 19 20 21 22 23 24 25 26 27 28 29 30
use Modules\User\Http\Middleware\AuthorisedApiToken;
use Modules\User\Http\Middleware\AuthorisedApiTokenAdmin;
use Modules\User\Http\Middleware\GuestMiddleware;
use Modules\User\Http\Middleware\LoggedInMiddleware;
use Modules\User\Http\Middleware\TokenCan;
use Modules\User\Repositories\Cache\CacheUserTokenDecorator;
use Modules\User\Repositories\Eloquent\EloquentUserTokenRepository;
use Modules\User\Repositories\RoleRepository;
use Modules\User\Repositories\UserRepository;
use Modules\User\Repositories\UserTokenRepository;

class UserServiceProvider extends ServiceProvider
{
31
    use CanPublishConfiguration, CanGetSidebarClassForModule;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * @var array
     */
    protected $providers = [
        'Sentinel' => SentinelServiceProvider::class,
    ];

    /**
     * @var array
     */
    protected $middleware = [
        'auth.guest' => GuestMiddleware::class,
        'logged.in' => LoggedInMiddleware::class,
        'api.token' => AuthorisedApiToken::class,
        'api.token.admin' => AuthorisedApiTokenAdmin::class,
        'token-can' => TokenCan::class,
    ];

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->register($this->getUserPackageServiceProvider());

        $this->registerBindings();
67 68 69 70 71

        $this->app['events']->listen(
            BuildingSidebar::class,
            $this->getSidebarClassForModule('user', RegisterUserSidebar::class)
        );
72 73 74 75
        $this->app['events']->listen(LoadingBackendTranslations::class, function (LoadingBackendTranslations $event) {
            $event->load('users', array_dot(trans('user::users')));
            $event->load('roles', array_dot(trans('user::roles')));
        });
76
        $this->commands([
77
            GrantModulePermissionsCommand::class,
78
            RemoveModulePermissionsCommand::class,
79
        ]);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }

    /**
     */
    public function boot()
    {
        $this->registerMiddleware();

        $this->publishes([
            __DIR__ . '/../Resources/views' => base_path('resources/views/asgard/user'),
        ]);

        $this->publishConfig('user', 'permissions');
        $this->publishConfig('user', 'config');

        $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
96 97 98 99

        Auth::extend('sentinel-guard', function () {
            return new Sentinel();
        });
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array();
    }

    private function registerBindings()
    {
        $driver = config('asgard.user.config.driver', 'Sentinel');

        $this->app->bind(
            UserRepository::class,
            "Modules\\User\\Repositories\\{$driver}\\{$driver}UserRepository"
        );
        $this->app->bind(
            RoleRepository::class,
            "Modules\\User\\Repositories\\{$driver}\\{$driver}RoleRepository"
        );
        $this->app->bind(
            Authentication::class,
            "Modules\\User\\Repositories\\{$driver}\\{$driver}Authentication"
        );
        $this->app->bind(UserTokenRepository::class, function () {
            $repository = new EloquentUserTokenRepository(new UserToken());

            if (! config('app.cache')) {
                return $repository;
            }

            return new CacheUserTokenDecorator($repository);
        });
    }

    private function registerMiddleware()
    {
        foreach ($this->middleware as $name => $class) {
142
            $this->app['router']->aliasMiddleware($name, $class);
143 144 145 146 147 148 149 150 151 152 153 154 155 156
        }
    }

    private function getUserPackageServiceProvider()
    {
        $driver = config('asgard.user.config.driver', 'Sentinel');

        if (!isset($this->providers[$driver])) {
            throw new \Exception("Driver [{$driver}] does not exist");
        }

        return $this->providers[$driver];
    }
}