77use App \Exceptions \Internal \FrameworkException ;
88use App \Models \User ;
99use Illuminate \Contracts \Container \BindingResolutionException ;
10- use Illuminate \Database \QueryException ;
11- use Illuminate \Support \Str ;
10+ use Illuminate \Support \Facades \Schema ;
1211use Psr \Container \ContainerExceptionInterface ;
1312use 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