Skip to content

Commit 7ef337e

Browse files
committed
Implemented more optimizations.
1 parent aeeed8f commit 7ef337e

22 files changed

+168
-255
lines changed

database/seeders/LaravelGovernorDatabaseSeeder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function run()
1717
$this->call(LaravelGovernorRolesTableSeeder::class);
1818
$this->call(LaravelGovernorSuperAdminSeeder::class);
1919
$this->call(LaravelGovernorAdminSeeder::class);
20+
$this->call(OwnedBySeeder::class);
2021

2122
Model::reguard();
2223
}

database/seeders/OwnedBySeeder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor\Database\Seeders;
6+
7+
use GeneaLabs\LaravelGovernor\Traits\EntityManagement;
8+
use GeneaLabs\LaravelGovernor\Traits\GovernorOwnedByField;
9+
use Illuminate\Database\Seeder;
10+
11+
class OwnedBySeeder extends Seeder
12+
{
13+
use EntityManagement;
14+
use GovernorOwnedByField;
15+
16+
public function run()
17+
{
18+
$this->getPolicies()
19+
->each(function ($policy) {
20+
$this->createGovernorOwnedByFieldsByPolicy(new $policy);
21+
});
22+
}
23+
}

src/Action.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,6 @@ class Action extends Model
2222

2323
public $incrementing = false;
2424

25-
public static function boot()
26-
{
27-
parent::boot();
28-
29-
static::created(function () {
30-
app("cache")->forget("governor-actions");
31-
});
32-
33-
static::deleted(function () {
34-
app("cache")->forget("governor-actions");
35-
});
36-
37-
static::saved(function () {
38-
app("cache")->forget("governor-actions");
39-
});
40-
41-
static::updated(function () {
42-
app("cache")->forget("governor-actions");
43-
});
44-
}
45-
4625
public function permissions(): HasMany
4726
{
4827
return $this->hasMany(

src/Assignment.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
1-
<?php namespace GeneaLabs\LaravelGovernor;
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor;
26

37
use Illuminate\Database\Eloquent\Model;
48
use Illuminate\Database\Eloquent\Relations\BelongsTo;
59

610
class Assignment extends Model
711
{
812
protected $roles;
9-
protected $table ="governor_role_user";
13+
protected $table = "governor_role_user";
1014
protected $users;
1115

12-
public static function boot()
13-
{
14-
parent::boot();
15-
16-
static::created(function () {
17-
app("cache")->forget("governor-assignments");
18-
});
19-
20-
static::deleted(function () {
21-
app("cache")->forget("governor-assignments");
22-
});
23-
24-
static::saved(function () {
25-
app("cache")->forget("governor-assignments");
26-
});
27-
28-
static::updated(function () {
29-
app("cache")->forget("governor-assignments");
30-
});
31-
}
32-
3316
public function __construct()
3417
{
3518
parent::__construct();

src/Entity.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace GeneaLabs\LaravelGovernor;
46

57
use Illuminate\Database\Eloquent\Model;
@@ -20,35 +22,14 @@ class Entity extends Model
2022

2123
public $incrementing = false;
2224

23-
public static function boot()
24-
{
25-
parent::boot();
26-
27-
static::created(function () {
28-
app("cache")->forget("governor-entities");
29-
});
30-
31-
static::deleted(function () {
32-
app("cache")->forget("governor-entities");
33-
});
34-
35-
static::saved(function () {
36-
app("cache")->forget("governor-entities");
37-
});
38-
39-
static::updated(function () {
40-
app("cache")->forget("governor-entities");
41-
});
42-
}
43-
44-
public function group() : BelongsTo
25+
public function group(): BelongsTo
4526
{
4627
return $this->belongsTo(
4728
config('genealabs-laravel-governor.models.group')
4829
);
4930
}
5031

51-
public function permissions() : HasMany
32+
public function permissions(): HasMany
5233
{
5334
return $this->hasMany(
5435
config('genealabs-laravel-governor.models.permission'),

src/Group.php

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<?php namespace GeneaLabs\LaravelGovernor;
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor;
26

3-
use Illuminate\Database\Eloquent\Collection;
47
use Illuminate\Database\Eloquent\Model;
58
use Illuminate\Database\Eloquent\Relations\HasMany;
69

@@ -18,27 +21,6 @@ class Group extends Model
1821

1922
public $incrementing = false;
2023

21-
public static function boot()
22-
{
23-
parent::boot();
24-
25-
static::created(function () {
26-
app("cache")->forget("governor-groups");
27-
});
28-
29-
static::deleted(function () {
30-
app("cache")->forget("governor-groups");
31-
});
32-
33-
static::saved(function () {
34-
app("cache")->forget("governor-groups");
35-
});
36-
37-
static::updated(function () {
38-
app("cache")->forget("governor-groups");
39-
});
40-
}
41-
4224
public function entities() : HasMany
4325
{
4426
return $this->hasMany(

src/Http/Middleware/ParseCustomPolicyActions.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ class ParseCustomPolicyActions
1818
{
1919
public function handle(Request $request, Closure $next): Response
2020
{
21-
if (! cache()->has('governor.registered-custom-actions')) {
22-
$this->registerCustomPolicyActions();
23-
24-
cache()->forever('governor.registered-custom-actions', true);
25-
}
21+
$this->registerCustomPolicyActions();
2622

2723
return $next($request);
2824
}
@@ -34,15 +30,34 @@ protected function registerCustomPolicyActions(): void
3430
->map(function (string $policyClass, string $modelClass): Collection {
3531
return $this->getCustomActionMethods($policyClass)
3632
->map(function (string $method) use ($modelClass): Action {
37-
$action = (new Action)->firstOrCreate([
38-
"name" => "{$modelClass}:{$method}",
39-
]);
40-
(new Permission)->firstOrCreate([
41-
"role_name" => "SuperAdmin",
42-
"entity_name" => $action->entity,
43-
"action_name" => $action->name,
44-
"ownership_name" => "any",
45-
]);
33+
$action = app("governor-actions")
34+
->where("name", "{$modelClass}:{$method}")
35+
->first();
36+
37+
if (! $action) {
38+
$actionClass = app(config('genealabs-laravel-governor.models.action'));
39+
$action = (new $actionClass)
40+
->firstOrCreate([
41+
"name" => "{$modelClass}:{$method}",
42+
]);
43+
}
44+
45+
$permission = app("governor-permissions")
46+
->where("role_name", "SuperAdmin")
47+
->where("entity_name", $action->entity)
48+
->where("action_name", $action->name)
49+
->where("ownership_name", "any")
50+
->first();
51+
52+
if (! $permission) {
53+
$permissionClass = config("genealabs-laravel-governor.models.permission");
54+
(new $permissionClass)->create([
55+
"role_name" => "SuperAdmin",
56+
"entity_name" => $action->entity,
57+
"action_name" => $action->name,
58+
"ownership_name" => "any",
59+
]);
60+
}
4661

4762
return $action;
4863
});

src/Listeners/CreatingListener.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<?php namespace GeneaLabs\LaravelGovernor\Listeners;
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor\Listeners;
26

37
use GeneaLabs\LaravelGovernor\Traits\GovernorOwnedByField;
48
use Illuminate\Database\Eloquent\Model;
@@ -13,7 +17,8 @@ class CreatingListener
1317
*/
1418
public function handle(string $event, array $models)
1519
{
16-
if (Str::contains($event, "Hyn\Tenancy\Models\Website")
20+
if (
21+
Str::contains($event, "Hyn\Tenancy\Models\Website")
1722
|| Str::contains($event, "Hyn\Tenancy\Models\Hostname")
1823
) {
1924
return;
@@ -29,10 +34,10 @@ class_uses_recursive($model)
2934
})
3035
->filter()
3136
->each(function ($model) {
32-
$this->createGovernorOwnedByFields($model);
3337
$model->getEntityFromModel(get_class($model));
3438

35-
if (! $model->governor_owned_by
39+
if (
40+
! $model->governor_owned_by
3641
&& auth()->check()
3742
) {
3843
$model->governor_owned_by = auth()->user()->id;

src/Ownership.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<?php namespace GeneaLabs\LaravelGovernor;
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor;
26

3-
use Illuminate\Database\Eloquent\Collection;
47
use Illuminate\Database\Eloquent\Model;
58
use Illuminate\Database\Eloquent\Relations\HasMany;
69

src/Permission.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
<?php namespace GeneaLabs\LaravelGovernor;
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GeneaLabs\LaravelGovernor;
26

37
use Illuminate\Database\Eloquent\Collection;
48
use Illuminate\Database\Eloquent\Model;
@@ -20,67 +24,46 @@ class Permission extends Model
2024
];
2125
protected $table = "governor_permissions";
2226

23-
public static function boot()
24-
{
25-
parent::boot();
26-
27-
static::created(function () {
28-
app("cache")->forget("governor-permissions");
29-
});
30-
31-
static::deleted(function () {
32-
app("cache")->forget("governor-permissions");
33-
});
34-
35-
static::saved(function () {
36-
app("cache")->forget("governor-permissions");
37-
});
38-
39-
static::updated(function () {
40-
app("cache")->forget("governor-permissions");
41-
});
42-
}
43-
44-
public function entity() : BelongsTo
27+
public function entity(): BelongsTo
4528
{
4629
return $this->belongsTo(
4730
config('genealabs-laravel-governor.models.entity'),
4831
'entity_name'
4932
);
5033
}
5134

52-
public function action() : BelongsTo
35+
public function action(): BelongsTo
5336
{
5437
return $this->belongsTo(
5538
config('genealabs-laravel-governor.models.action'),
5639
'action_name'
5740
);
5841
}
5942

60-
public function ownership() : BelongsTo
43+
public function ownership(): BelongsTo
6144
{
6245
return $this->belongsTo(
6346
config('genealabs-laravel-governor.models.ownership'),
6447
'ownership_name'
6548
);
6649
}
6750

68-
public function role() : BelongsTo
51+
public function role(): BelongsTo
6952
{
7053
return $this->belongsTo(
7154
config('genealabs-laravel-governor.models.role'),
7255
'role_name'
7356
);
7457
}
7558

76-
public function team() : BelongsTo
59+
public function team(): BelongsTo
7760
{
7861
return $this->belongsTo(
7962
config('genealabs-laravel-governor.models.team')
8063
);
8164
}
8265

83-
public function getFilteredBy(string $filter = null, string $value = null) : Collection
66+
public function getFilteredBy(string $filter = null, string $value = null): Collection
8467
{
8568
return $this
8669
->where(function ($query) use ($filter, $value) {

0 commit comments

Comments
 (0)