Skip to content

Commit 09b0fb3

Browse files
done
1 parent 5db9940 commit 09b0fb3

8 files changed

Lines changed: 65 additions & 92 deletions

File tree

app/Helpers/Request.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,30 @@ function client_id () {
5959
return client()?->id ?? 0;
6060

6161
}
62-
function is_admin () {
62+
function is_super () {
6363

64-
return bool(user()?->hasRole('admin'));
64+
return bool(user()?->hasRole('admin')) && bool(user()?->has('super'));
6565

6666
}
67-
function is_client () {
67+
function is_supervisor () {
6868

69-
return bool(user()?->hasRole('client'));
69+
return bool(user()?->hasRole('admin')) && bool(user()?->has('supervisor'));
70+
71+
}
72+
function is_admin () {
73+
74+
return bool(user()?->hasRole('admin'));
7075

7176
}
7277
function is_vendor () {
7378

7479
return bool(user()?->hasRole('vendor'));
7580

81+
}
82+
function is_client () {
83+
84+
return bool(user()?->hasRole('client'));
85+
7686
}
7787
function user_store_id () {
7888

@@ -107,15 +117,6 @@ function forced_user () {
107117

108118
}
109119

110-
function settings ( int $storeId = null ) {
111-
112-
$storeId = $storeId ?? store_id();
113-
114-
return remember('model:setting', "public_{$storeId}", 60, fn() =>
115-
Setting::withoutTenant()->active()->firstWhere('store_id', $storeId)
116-
);
117-
118-
}
119120
function site_info ( int $storeId = null ) {
120121

121122
$storeId = $storeId ?? store_id();
@@ -158,6 +159,11 @@ function store () {
158159

159160
return app()->bound('store') ? app('store') : null;
160161

162+
}
163+
function store_parent () {
164+
165+
return store()?->parent ?? null;
166+
161167
}
162168
function store_admin () {
163169

@@ -168,16 +174,16 @@ function store_owner () {
168174

169175
return User::withoutTenant()->where('id', store()?->owner_id ?? 0)->first();
170176

171-
}
172-
function store_parent () {
173-
174-
return store()?->parent ?? null;
175-
176177
}
177178
function store_id () {
178179

179180
return store()?->id ?? 0;
180181

182+
}
183+
function store_parent_id () {
184+
185+
return store()?->parent_id ?? 0;
186+
181187
}
182188
function store_admin_id () {
183189

@@ -189,25 +195,32 @@ function store_owner_id () {
189195
return store_owner()?->id ?? 0;
190196

191197
}
192-
function store_parent_id () {
198+
function with_store ( mixed $store, mixed $user = null, callable $callback = null ) {
199+
200+
$current = store();
201+
set_store($store);
202+
203+
if ( $user ) set_forced_user($user);
204+
205+
try{ return $callback ? $callback() : null; }
206+
finally{ set_store($current); unset_forced_user(); }
193207

194-
return store_parent()?->id ?? 0;
195-
196208
}
197209

198210
function user_has ( ...$permissions ) {
199211

200-
return user()?->has(...$permissions) ?? false;
212+
return (bool) user()?->has(...$permissions);
201213

202214
}
203-
function settings_has ( ...$permissions ) {
204-
205-
return settings()?->has(...$permissions) ?? false;
215+
function setting_has ( ...$permissions ) {
216+
217+
return (bool) Setting::hasPermission(...$permissions);
206218

207219
}
208220
function store_has ( ...$permissions ) {
209-
210-
return store()?->has(...$permissions) ?? false;
221+
222+
$parent = remember('model:store', 'store_parent_' . store_id(), 60, fn() => store_parent());
223+
return (bool) (!$parent ? true : with_store($parent, callback: fn() => Store::hasPermission(...$permissions)));
211224

212225
}
213226
function permissionkey ( string $permission ) {
@@ -233,7 +246,7 @@ function limiter ( int $limit = 5, string $name = null, int $minutes = 1 ) {
233246
}
234247
function rateLimiter ( int $limit = null, int $minutes = 1 ) {
235248

236-
$limit = $limit ?? 500;
249+
$limit = $limit ?? (is_admin() ? 600 : 60);
237250
return "throttle:{$limit},{$minutes}";
238251

239252
}
@@ -244,7 +257,7 @@ function tenantMiddleware ( int $limit = null ) {
244257
}
245258
function authMiddleware ( string $role = 'client', int $limit = null ) {
246259

247-
if ( config('app.env') === 'local' ) return ["role:{$role}", rateLimiter($limit)];
248-
return ['auth:sanctum', "role:{$role}", rateLimiter($limit)];
260+
if ( config('app.env') === 'local' ) return ["role:{$role}", 'has:allow_logins', rateLimiter($limit)];
261+
return ['auth:sanctum', "role:{$role}", 'has:allow_logins', rateLimiter($limit)];
249262

250263
}

app/Http/Middleware/Has.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
use Illuminate\Http\Request;
5+
use Closure;
6+
7+
class Has {
8+
9+
public function handle ( Request $request, Closure $next, string $permission ) {
10+
11+
$perm = permissionkey($permission);
12+
$verified = (is_super() || user_has($perm)) && setting_has($perm) && store_has($perm);
13+
14+
return $verified ? $next($request) : middlewareFailed();
15+
16+
}
17+
18+
}

app/Http/Middleware/Role.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ class Role {
88

99
public function handle ( Request $request, Closure $next, string $role ) {
1010

11-
return user_role() === strtolower($role) &&
12-
user()?->active &&
13-
user_has('allow_logins') &&
14-
settings_has('allow_logins') &&
15-
store_has('allow_logins')
16-
? $next($request) : middlewareFailed();
11+
$verified = user()?->hasRole($role) && user()?->active;
12+
return $verified ? $next($request) : middlewareFailed();
1713

1814
}
1915

app/Http/Middleware/User.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/Http/Middleware/UserCan.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/Http/Middleware/UserHas.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

bootstrap/app.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020

2121
$middleware->alias([
2222
'store' => \App\Http\Middleware\Store::class,
23-
'user' => \App\Http\Middleware\User::class,
2423
'role' => \App\Http\Middleware\Role::class,
25-
'has' => \App\Http\Middleware\UserHas::class,
26-
'can' => \App\Http\Middleware\UserCan::class,
24+
'has' => \App\Http\Middleware\Has::class,
2725
]);
2826

2927
})

routes/apis/admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
Route::prefix('entities')->name('entities.')->controller(EntityController::class)->group(function(){
3838
Route::prefix('{entity}')->whereNumber('entity')->group(function(){
3939
Route::prefix('permissions')->middleware('has:edit_permissions')->group(function(){
40-
Route::post('{permission?}', 'assignPermission')->name('assign');
40+
Route::post('assign/{permission?}', 'assignPermission')->name('assign');
4141
});
4242
});
4343
});

0 commit comments

Comments
 (0)