Skip to content

Commit 167721f

Browse files
author
Mateus Junges
authored
Merge pull request mateusjunges#95 from mateusjunges/add-db-connectio-check-before-run-service-provider
Add database connection check before run service provider
2 parents 994e18d + 8c2d0a9 commit 167721f

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `mateusjunges/laravel-acl` will be documented in this file.
44

5+
## 1.7.5
6+
- Fix [#93](https://github.com/mateusjunges/laravel-acl/issues/93)
7+
- Add database connection check before register gates
8+
59
## 1.7.4
610
- Add package tests
711
- Continuous integration with TravisCI

src/ACLAuthServiceProvider.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Junges\ACL;
44

5-
use Illuminate\Support\Facades\Auth;
5+
use DB;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\Facades\Blade;
88
use Illuminate\Support\Facades\Schema;
@@ -26,13 +26,15 @@ public function boot()
2626
? $permissionModel = app(config('acl.models.permission'))
2727
: $permissionModel = app(\Junges\ACL\Http\Models\Permission::class);
2828

29-
if (config('acl.tables.permissions') !== null) {
30-
if (Schema::hasTable(config('acl.tables.permissions'))) {
31-
$permissionModel->all()->map(function ($permission) {
32-
Gate::define($permission->slug, function ($user) use ($permission) {
33-
return $user->hasPermission($permission) || $user->isAdmin();
29+
if ($this->checkConnectionStatus()) {
30+
if (config('acl.tables.permissions') !== null) {
31+
if (Schema::hasTable(config('acl.tables.permissions'))) {
32+
$permissionModel->all()->map(function ($permission) {
33+
Gate::define($permission->slug, function ($user) use ($permission) {
34+
return $user->hasPermission($permission) || $user->isAdmin();
35+
});
3436
});
35-
});
37+
}
3638
}
3739
}
3840

@@ -138,4 +140,19 @@ private function registerBladeDirectives()
138140
return '<?php } ?>';
139141
});
140142
}
143+
144+
/**
145+
* Check for database connection.
146+
* @return bool
147+
*/
148+
private function checkConnectionStatus()
149+
{
150+
try {
151+
DB::connection()->getPdo();
152+
153+
return true;
154+
} catch (\Exception $exception) {
155+
return false;
156+
}
157+
}
141158
}

src/ACLServiceProvider.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Contracts\Config\Repository;
88
use Illuminate\Contracts\Events\Dispatcher;
99
use Junges\ACL\Console\Commands\CreateGroup;
10-
use Junges\ACL\Http\Observers\GroupObserver;
1110
use Junges\ACL\Console\Commands\ShowPermissions;
1211
use Junges\ACL\Console\Commands\UserPermissions;
1312
use Junges\ACL\Console\Commands\CreatePermission;
@@ -40,9 +39,6 @@ public function boot(Dispatcher $events, Repository $config, Factory $view)
4039

4140
//Load translations
4241
$this->loadTranslations();
43-
44-
//Load observers
45-
$this->loadObservers();
4642
}
4743

4844
/**
@@ -66,6 +62,9 @@ public function publishConfig()
6662
], 'config');
6763
}
6864

65+
/**
66+
* Load package commands.
67+
*/
6968
public function loadCommands()
7069
{
7170
if ($this->app->runningInConsole()) {
@@ -78,6 +77,9 @@ public function loadCommands()
7877
}
7978
}
8079

80+
/**
81+
* Load package migrations.
82+
*/
8183
public function loadMigrations()
8284
{
8385
$customMigrations = config('acl.custom_migrations');
@@ -91,6 +93,9 @@ public function loadMigrations()
9193
], 'migrations');
9294
}
9395

96+
/**
97+
* Load package translations.
98+
*/
9499
public function loadTranslations()
95100
{
96101
$translationsPath = __DIR__.'/resources/lang';
@@ -100,12 +105,6 @@ public function loadTranslations()
100105
], 'translations');
101106
}
102107

103-
public function loadObservers()
104-
{
105-
// $groupModel = app(config('acl.models.group'));
106-
// $groupModel->observe(GroupObserver::class);
107-
}
108-
109108
/**
110109
* Register any application services.
111110
*

0 commit comments

Comments
 (0)