|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
|
6
|
|
|
use MultiTenantLaravel\App\Commands\CreateTenant; |
|
7
|
|
|
use MultiTenantLaravel\App\Commands\CreateUser; |
|
8
|
|
|
use MultiTenantLaravel\App\Commands\SyncRolesPermissionsFeatures; |
|
9
|
|
|
|
|
10
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
|
11
|
|
|
|
|
12
|
|
|
class MultiTenantServiceProvider extends ServiceProvider |
|
13
|
|
|
{ |
|
14
|
|
|
protected $gate; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Bootstrap the application services. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function boot(Gate $gate) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->gate = $gate; |
|
22
|
|
|
|
|
23
|
|
|
// Publish the configurable config file for the user |
|
24
|
|
|
$this->publishes([__DIR__.'/config/multi-tenant.php' => config_path('multi-tenant.php')], 'multi-tenant'); |
|
25
|
|
|
|
|
26
|
|
|
// Make views publishable to the vendor folder in a project |
|
27
|
|
|
$this->publishes([__DIR__.'/resources/views' => resource_path('views/vendor/multi-tenant')]); |
|
28
|
|
|
|
|
29
|
|
|
// Load any routes |
|
30
|
|
|
$this->loadRoutesFrom(__DIR__.'/routes/routes.php'); |
|
31
|
|
|
|
|
32
|
|
|
// Load any migrations |
|
33
|
|
|
$this->loadMigrationsFrom(__DIR__.'/database/migrations'); |
|
34
|
|
|
|
|
35
|
|
|
// Load any views |
|
36
|
|
|
$this->loadViewsFrom(__DIR__.'/resources/views', 'multi-tenant'); |
|
37
|
|
|
|
|
38
|
|
|
// Setup a middleware for the multi tenacy |
|
39
|
|
|
app('router')->aliasMiddleware('multi-tenant', \MultiTenantLaravel\App\Http\Middleware\MultiTenantMiddleware::class); |
|
40
|
|
|
|
|
41
|
|
|
// Bind any Facades to the app |
|
42
|
|
|
$this->app->bind('multi-tenant', function(){ |
|
43
|
|
|
return new \MultiTenantLaravel\MultiTenant; |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
// Register all permissions on the gate |
|
47
|
|
|
$this->registerPermissions(); |
|
48
|
|
|
|
|
49
|
|
|
// Register any commands we want available to the user |
|
50
|
|
|
if ($this->app->runningInConsole() || config('app.env') === 'testing') { |
|
51
|
|
|
$this->commands([ |
|
52
|
|
|
CreateTenant::class, |
|
53
|
|
|
CreateUser::class, |
|
54
|
|
|
SyncRolesPermissionsFeatures::class, |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Register the application services. |
|
61
|
|
|
*/ |
|
62
|
|
|
public function register() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/config.php', 'multi-tenant-config'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Register the permission to the gate |
|
69
|
|
|
*/ |
|
70
|
|
|
public function registerPermissions() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->gate->before(function ($user, string $ability) { |
|
73
|
|
|
$permission = config('multi-tenant.permission_class')::where('name', $ability)->firstOrFail(); |
|
74
|
|
|
|
|
75
|
|
|
return $user->hasRole($permission->roles); |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|