Skip to content

Commit 2cc842f

Browse files
authored
More robust fix on HasAdminUser (#1684)
1 parent bcede41 commit 2cc842f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

app/Http/Middleware/Checks/HasAdminUser.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
use App\Exceptions\Internal\FrameworkException;
88
use App\Models\User;
99
use Illuminate\Contracts\Container\BindingResolutionException;
10-
use Illuminate\Database\QueryException;
11-
use Illuminate\Support\Str;
10+
use Illuminate\Support\Facades\Schema;
1211
use Psr\Container\ContainerExceptionInterface;
1312
use Psr\Container\NotFoundExceptionInterface;
1413

@@ -19,16 +18,19 @@ class HasAdminUser implements MiddlewareCheck
1918
*/
2019
public function assert(): bool
2120
{
22-
try {
23-
return User::query()->where('may_administrate', '=', true)->count() > 0;
24-
} catch (QueryException $e) {
25-
if (Str::contains($e->getMessage(), 'column "may_administrate" does not exist')) {
26-
// We are doing an upgrade, therefore we can assume there exists an admin.
27-
return true;
21+
// This is necessary in the case that someone is using the web-updater.
22+
// In such case the tables already exist so the IsInstalled will return true.
23+
// However this middleware will throw an error because may_administrate does not exist yet.
24+
if (Schema::hasColumn('users', 'may_administrate')) {
25+
try {
26+
return User::query()->where('may_administrate', '=', true)->count() > 0;
27+
} catch (BindingResolutionException|NotFoundExceptionInterface|ContainerExceptionInterface $e) {
28+
throw new FrameworkException('Laravel\'s container component', $e);
2829
}
29-
throw $e;
30-
} catch (BindingResolutionException|NotFoundExceptionInterface|ContainerExceptionInterface $e) {
31-
throw new FrameworkException('Laravel\'s container component', $e);
30+
} else {
31+
// If the column does not exist yet but we are executing this script
32+
// it means that there exists already an admin user (with ID = 0).
33+
return true;
3234
}
3335
}
3436
}

0 commit comments

Comments
 (0)