Skip to content

Commit

Permalink
Fix listeners, remove governor table checks to speed things up
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Jul 18, 2019
1 parent 568142a commit 5ed314f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 28 deletions.
1 change: 1 addition & 0 deletions database/seeds/LaravelGovernorDatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function run()
$this->call('LaravelGovernorActionsTableSeeder');
$this->call('LaravelGovernorOwnershipsTableSeeder');
$this->call('LaravelGovernorRolesTableSeeder');
$this->call('LaravelGovernorSuperAdminSeeder');

Eloquent::reguard();
}
Expand Down
27 changes: 2 additions & 25 deletions database/seeds/LaravelGovernorRolesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,14 @@ class LaravelGovernorRolesTableSeeder extends Seeder
{
public function run()
{
$users = app()->make(config('genealabs-laravel-governor.models.auth'));
$superUserEmail = config('genealabs-laravel-governor.superadmin.email');
$superuser = null;

if ($superUserEmail) {
$superuser = $users
->firstOrNew([
"email" => $superUserEmail,
]);

if (! $superuser->exists) {
$superuser->fill([
"name" => config('genealabs-laravel-governor.superadmin.name'),
"password" => bcrypt(config('genealabs-laravel-governor.superadmin.password')),
]);
$superuser->save();
}
}

$roleClass = config("genealabs-laravel-governor.models.role");
$superAdminRole = (new $roleClass)->firstOrCreate([
(new $roleClass)->firstOrCreate([
'name' => 'SuperAdmin',
'description' => 'This role is for the main administrator of your site. They will be able to do absolutely everything. (This role cannot be edited.)',
]);
$memberRole = (new $roleClass)->firstOrCreate([
(new $roleClass)->firstOrCreate([
'name' => 'Member',
'description' => 'Represents the baseline registered user. Customize permissions as best suits your site.',
]);

if ($superuser) {
$superuser->roles()->syncWithoutDetaching([$superAdminRole->name, $memberRole->name]);
}
}
}
42 changes: 42 additions & 0 deletions database/seeds/LaravelGovernorSuperAdminSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Database\Seeder;

class LaravelGovernorSuperAdminSeeder extends Seeder
{
public function run()
{
$users = app()->make(config('genealabs-laravel-governor.models.auth'));
$superUserEmail = config('genealabs-laravel-governor.superadmin.email');
$superuser = null;

if ($superUserEmail) {
$superuser = $users
->firstOrNew([
"email" => $superUserEmail,
]);

if (! $superuser->exists) {
$superuser->fill([
"name" => config('genealabs-laravel-governor.superadmin.name'),
"password" => bcrypt(config('genealabs-laravel-governor.superadmin.password')),
]);
$superuser->save();
}
}

$roleClass = config("genealabs-laravel-governor.models.role");
$superAdminRole = (new $roleClass)->firstOrCreate([
'name' => 'SuperAdmin',
'description' => 'This role is for the main administrator of your site. They will be able to do absolutely everything. (This role cannot be edited.)',
]);
$memberRole = (new $roleClass)->firstOrCreate([
'name' => 'Member',
'description' => 'Represents the baseline registered user. Customize permissions as best suits your site.',
]);

if ($superuser) {
$superuser->roles()->syncWithoutDetaching([$superAdminRole->name, $memberRole->name]);
}
}
}
12 changes: 10 additions & 2 deletions src/Listeners/CreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function handle(string $event, array $models)
{
if (str_contains($event, "Hyn\Tenancy\Models\Website")
|| str_contains($event, "Hyn\Tenancy\Models\Hostname")
|| ! Schema::hasTable('governor_roles')
) {
return;
}
Expand All @@ -24,7 +23,16 @@ public function handle(string $event, array $models)
&& get_class($model) === config('genealabs-laravel-governor.models.auth');
})
->each(function ($model) {
$model->roles()->attach('Member');
try {
$model->roles()->attach('Member');
} catch (Exception $exception) {
$roleClass = config("genealabs-laravel-governor.models.role");
(new $roleClass)->firstOrCreate([
'name' => 'Member',
'description' => 'Represents the baseline registered user. Customize permissions as best suits your site.',
]);
$model->roles()->attach('Member');
}
});
}
}
1 change: 0 additions & 1 deletion src/Listeners/CreatingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function handle(string $event, array $models)
{
if (str_contains($event, "Hyn\Tenancy\Models\Website")
|| str_contains($event, "Hyn\Tenancy\Models\Hostname")
|| ! Schema::hasTable('governor_roles')
) {
return;
}
Expand Down

0 comments on commit 5ed314f

Please sign in to comment.