Skip to content

Commit 424293e

Browse files
committed
Optimized database queries.
1 parent 1ab33c5 commit 424293e

File tree

5 files changed

+92
-54
lines changed

5 files changed

+92
-54
lines changed

.nova/Configuration.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"workspace.color" : 4
3+
}

src/Providers/Service.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,12 @@ function () {
5656
$entityClass = app(config('genealabs-laravel-governor.models.entity'));
5757

5858
return (new $entityClass)
59-
->with("group")
59+
->select("name")
60+
->with("group:name")
6061
->orderBy("name")
6162
->get();
6263
}
6364
);
64-
$this->app->singleton(
65-
'governor-permissions',
66-
function () {
67-
$permissionClass = app(config('genealabs-laravel-governor.models.permission'));
68-
69-
return (new $permissionClass)
70-
->where(function ($query) {
71-
$roleNames = auth()->user()->roles->pluck("name")->toArray();
72-
$teamIds = auth()->user()->teams->pluck("id")->toArray();
73-
74-
$query->whereIn("role_name", $roleNames)
75-
->orWhereIn("team_id", $teamIds);
76-
})
77-
->get();
78-
}
79-
);
8065

8166
$this->mergeConfigFrom(__DIR__ . '/../../config/config.php', 'genealabs-laravel-governor');
8267
$this->commands(Publish::class);

src/Traits/Governable.php

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

37
use Illuminate\Database\Eloquent\Builder;
48
use Illuminate\Database\Eloquent\Relations\BelongsTo;
59
use Illuminate\Database\Eloquent\Relations\MorphToMany;
610
use Illuminate\Support\Collection;
11+
use Illuminate\Support\Facades\Cache;
712

813
trait Governable
914
{
1015
use EntityManagement;
1116

12-
protected function applyPermissionToQuery(Builder $query, string $ability) : Builder
17+
protected function applyPermissionToQuery(Builder $query, string $ability): Builder
1318
{
1419
$entityName = $this->getEntityFromModel(get_class($this));
1520
$ownerships = $this->getOwnershipsForEntity($entityName, $ability);
16-
21+
1722
return $this->filterQuery($query, $ownerships);
1823
}
1924

20-
protected function filterQuery(Builder $query, Collection $ownerships) : Builder
25+
protected function filterQuery(Builder $query, Collection $ownerships): Builder
2126
{
2227
if ($ownerships->contains("any")
2328
|| auth()->user()->hasRole("SuperAdmin")
@@ -52,7 +57,7 @@ protected function filterQuery(Builder $query, Collection $ownerships) : Builder
5257
if ($query->getModel()->getTable() === $authTable) {
5358
return $query->where($query->getModel()->getKeyName(), auth()->user()->getKey());
5459
}
55-
60+
5661
return $query->where(
5762
"{$query->getModel()->getTable()}.governor_owned_by",
5863
auth()->user()->getKey()
@@ -62,29 +67,39 @@ protected function filterQuery(Builder $query, Collection $ownerships) : Builder
6267
return $query->whereRaw("1 = 2");
6368
}
6469

65-
protected function getOwnershipsForEntity(string $entityName, string $ability) : Collection
66-
{
70+
protected function getOwnershipsForEntity(
71+
string $entityName,
72+
string $ability,
73+
): Collection {
6774
if (! $entityName) {
6875
return collect();
6976
}
7077

71-
$result = app("governor-permissions")
72-
->where("action_name", $ability)
73-
->where("entity_name", $entityName)
74-
->pluck("ownership_name");
78+
$permissionClass = app(config('genealabs-laravel-governor.models.permission'));
79+
$result = Cache::remember(
80+
"governor-permissions",
81+
5,
82+
function () use ($ability, $entityName, $permissionClass) {
83+
return (new $permissionClass)
84+
->select("ownership_name")
85+
->where("action_name", $ability)
86+
->where("entity_name", $entityName)
87+
->get();
88+
},
89+
);
7590

7691
return $result;
7792
}
7893

79-
public function ownedBy() : BelongsTo
94+
public function ownedBy(): BelongsTo
8095
{
8196
return $this->belongsTo(
8297
config("genealabs-laravel-governor.models.auth"),
8398
"governor_owned_by"
8499
);
85100
}
86101

87-
public function teams() : MorphToMany
102+
public function teams(): MorphToMany
88103
{
89104
return $this->MorphToMany(
90105
config("genealabs-laravel-governor.models.team"),
@@ -93,32 +108,32 @@ public function teams() : MorphToMany
93108
);
94109
}
95110

96-
public function scopeDeletable(Builder $query) : Builder
111+
public function scopeDeletable(Builder $query): Builder
97112
{
98113
return $this->applyPermissionToQuery($query, "delete");
99114
}
100115

101-
public function scopeForceDeletable(Builder $query) : Builder
116+
public function scopeForceDeletable(Builder $query): Builder
102117
{
103118
return $this->applyPermissionToQuery($query, "forceDelete");
104119
}
105120

106-
public function scopeRestorable(Builder $query) : Builder
121+
public function scopeRestorable(Builder $query): Builder
107122
{
108123
return $this->applyPermissionToQuery($query, "restore");
109124
}
110125

111-
public function scopeUpdatable(Builder $query) : Builder
126+
public function scopeUpdatable(Builder $query): Builder
112127
{
113128
return $this->applyPermissionToQuery($query, "update");
114129
}
115130

116-
public function scopeViewable(Builder $query) : Builder
131+
public function scopeViewable(Builder $query): Builder
117132
{
118133
return $this->applyPermissionToQuery($query, "view");
119134
}
120135

121-
public function scopeViewAnyable(Builder $query) : Builder
136+
public function scopeViewAnyable(Builder $query): Builder
122137
{
123138
return $this->applyPermissionToQuery($query, "viewAny");
124139
}

src/Traits/Governing.php

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

37
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
48
use Illuminate\Database\Eloquent\Relations\HasMany;
59
use Illuminate\Support\Collection;
10+
use Illuminate\Support\Facades\Cache;
611

712
trait Governing
813
{
914
use Governable;
10-
11-
public function hasRole(string $name) : bool
15+
16+
public function hasRole(string $name): bool
1217
{
13-
if ($this->roles->isEmpty()) {
18+
$roles = Cache::remember(
19+
"roles" . auth()->user()->getKey(),
20+
5,
21+
function () {
22+
return $this->roles()
23+
->select('name')
24+
->get();
25+
}
26+
);
27+
28+
if ($roles->count() === 0) {
1429
return false;
1530
}
1631

1732
$roleClass = config("genealabs-laravel-governor.models.role");
18-
$role = (new $roleClass)
19-
->find($name);
33+
$role = Cache::remember(
34+
"role{$roleClass}{$name}",
35+
5,
36+
function () use ($name, $roleClass) {
37+
return (new $roleClass)
38+
->select('name')
39+
->find($name);
40+
},
41+
);
2042

2143
if (! $role) {
2244
return false;
2345
}
2446

25-
return $this->roles->contains($role->name)
26-
|| $this->roles->contains("SuperAdmin");
47+
return $roles->contains($role->name)
48+
|| $roles->contains("SuperAdmin");
2749
}
2850

29-
public function roles() : BelongsToMany
51+
public function roles(): BelongsToMany
3052
{
3153
$roleClass = config("genealabs-laravel-governor.models.role");
3254

3355
return $this->belongsToMany($roleClass, 'governor_role_user', 'user_id', 'role_name');
3456
}
3557

36-
public function ownedTeams() : HasMany
58+
public function ownedTeams(): HasMany
3759
{
3860
return $this->hasMany(
3961
config("genealabs-laravel-governor.models.team"),
4062
"governor_owned_by"
4163
);
4264
}
4365

44-
public function teams() : BelongsToMany
66+
public function teams(): BelongsToMany
4567
{
4668
return $this->belongsToMany(
4769
config('genealabs-laravel-governor.models.team'),
@@ -51,15 +73,15 @@ public function teams() : BelongsToMany
5173
);
5274
}
5375

54-
public function getPermissionsAttribute() : Collection
76+
public function getPermissionsAttribute(): Collection
5577
{
5678
$permissionClass = config("genealabs-laravel-governor.models.permission");
5779
$roleNames = $this->roles->pluck('name');
5880

5981
return (new $permissionClass)->whereIn('role_name', $roleNames)->get();
6082
}
6183

62-
public function getEffectivePermissionsAttribute() : Collection
84+
public function getEffectivePermissionsAttribute(): Collection
6385
{
6486
$results = collect();
6587
$groupedPermissions = $this->permissions

src/Traits/GovernorOwnedByField.php

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

37
use GeneaLabs\LaravelGovernor\Policies\BasePolicy;
48
use Illuminate\Database\Eloquent\Model;
59
use Illuminate\Database\Schema\Blueprint;
10+
use Illuminate\Support\Facades\Cache;
611
use Illuminate\Support\Facades\Schema;
712
use ReflectionClass;
813

@@ -27,7 +32,7 @@ protected function createGovernorOwnedByFieldsByPolicy(BasePolicy $policy) : boo
2732
return $this->createGovernorOwnedByFields($model);
2833
}
2934

30-
protected function createGovernorOwnedByFields(Model $model) : bool
35+
protected function createGovernorOwnedByFields(Model $model): bool
3136
{
3237
if (! in_array("GeneaLabs\\LaravelGovernor\\Traits\\Governable", class_uses_recursive($model))) {
3338
return false;
@@ -36,9 +41,17 @@ protected function createGovernorOwnedByFields(Model $model) : bool
3641
$connection = $model
3742
->getConnection()
3843
->getName();
39-
$governorOwnedByExists = Schema::connection($connection)
40-
->hasColumn($model->getTable(), 'governor_owned_by');
41-
44+
$table = $model->getTable();
45+
46+
$governorOwnedByExists = Cache::remember(
47+
"{$connection}{$table}governor_owned_by",
48+
5,
49+
function () use ($connection, $model, $table) {
50+
return Schema::connection($connection)
51+
->hasColumn($model->getTable(), 'governor_owned_by');
52+
},
53+
);
54+
4255
if ($governorOwnedByExists) {
4356
return false;
4457
}

0 commit comments

Comments
 (0)