Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 03d5856

Browse files
authored
fix: permission to avoid any conflict with laravel policy (#62)
1 parent 43f45c9 commit 03d5856

File tree

7 files changed

+134
-135
lines changed

7 files changed

+134
-135
lines changed

src/Http/Controllers/ChartsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct()
4242
*/
4343
public function index(): JsonResponse
4444
{
45-
$this->authorize('simple-charts', [request()->except('timezone')]);
45+
$this->can('simpleCharts', request()->except('timezone'));
4646

4747
$name = request()->route()->parameter('collection');
4848
$model = $this->getModel(ucfirst($name));
@@ -66,7 +66,7 @@ public function index(): JsonResponse
6666
*/
6767
public function liveQuery(): JsonResponse
6868
{
69-
$this->authorize('liveQuery', [request()->input('query')]);
69+
$this->can('liveQuery', request()->input('query'));
7070
$repository = App::make('\ForestAdmin\LaravelForestAdmin\Repositories\Charts\LiveQuery\\' . $this->type);
7171

7272
return response()->json(

src/Http/Controllers/ForestController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ForestAdmin\LaravelForestAdmin\Http\Controllers;
44

5+
use ForestAdmin\LaravelForestAdmin\Permissions\Permission;
56
use Illuminate\Auth\Access\AuthorizationException;
67
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
78
use Illuminate\Routing\Controller;
@@ -21,17 +22,15 @@ class ForestController extends Controller
2122
}
2223

2324
/**
24-
* @param $ability
25-
* @param $arguments
26-
* @return void
25+
* @param string $ability
26+
* @param $arguments
27+
* @return mixed
2728
* @throws AuthorizationException
2829
*/
29-
public function authorize($ability, $arguments = [])
30+
public function can(string $ability, $arguments)
3031
{
31-
if (Auth::guard('forest')->check()) {
32-
Auth::shouldUse('forest');
32+
if (! Permission::$ability(Auth::guard('forest')->user(), $arguments)) {
33+
throw new AuthorizationException();
3334
}
34-
35-
$this->baseAuthorize($ability, $arguments);
3635
}
3736
}

src/Http/Controllers/ResourcesController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function callAction($method, $parameters)
7474
public function index()
7575
{
7676
$authorizeAction = $this->requestFormat === 'csv' ? 'export' : 'viewAny';
77-
$this->authorize($authorizeAction, $this->model);
77+
$this->can($authorizeAction, $this->model);
7878

7979
$repository = new ResourceGetter($this->model);
8080

@@ -107,7 +107,7 @@ public function index()
107107
*/
108108
public function show(): JsonResponse
109109
{
110-
$this->authorize('view', $this->model);
110+
$this->can('view', $this->model);
111111

112112
$repository = new ResourceGetter($this->model);
113113

@@ -129,7 +129,7 @@ public function show(): JsonResponse
129129
*/
130130
public function store(): JsonResponse
131131
{
132-
$this->authorize('create', $this->model);
132+
$this->can('create', $this->model);
133133

134134
try {
135135
$repository = new ResourceCreator($this->model);
@@ -150,7 +150,7 @@ public function store(): JsonResponse
150150
*/
151151
public function update(): JsonResponse
152152
{
153-
$this->authorize('update', $this->model);
153+
$this->can('update', $this->model);
154154

155155
try {
156156
$repository = new ResourceUpdater($this->model);
@@ -170,7 +170,7 @@ public function update(): JsonResponse
170170
*/
171171
public function destroy(): JsonResponse
172172
{
173-
$this->authorize('delete', $this->model);
173+
$this->can('delete', $this->model);
174174

175175
try {
176176
$id = request()->route()->parameter($this->model->getKeyName());
@@ -188,7 +188,7 @@ public function destroy(): JsonResponse
188188
*/
189189
public function count(): JsonResponse
190190
{
191-
$this->authorize('viewAny', $this->model);
191+
$this->can('viewAny', $this->model);
192192

193193
$repository = new ResourceGetter($this->model);
194194

@@ -201,7 +201,7 @@ public function count(): JsonResponse
201201
*/
202202
public function destroyBulk(): JsonResponse
203203
{
204-
$this->authorize('delete', $this->model);
204+
$this->can('delete', $this->model);
205205

206206
try {
207207
$repository = new ResourceRemover($this->model);

src/Http/Controllers/SmartActionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __invoke(Route $route)
5454
*/
5555
public function executeAction(): JsonResponse
5656
{
57-
$this->authorize('smartAction', [$this->collection, Str::slug($this->smartAction->getKey())]);
57+
$this->can('smartAction', [$this->collection, Str::slug($this->smartAction->getKey())]);
5858

5959
return response()->json(
6060
call_user_func($this->smartAction->getExecute())

src/Permissions/Permission.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace ForestAdmin\LaravelForestAdmin\Permissions;
4+
5+
use ForestAdmin\LaravelForestAdmin\Auth\Guard\Model\ForestUser;
6+
use Illuminate\Auth\Access\Response;
7+
use Illuminate\Support\Str;
8+
9+
/**
10+
* Class Permission
11+
*
12+
* @package Laravel-forestadmin
13+
* @license GNU https://www.gnu.org/licences/licences.html
14+
* @link https://github.com/ForestAdmin/laravel-forestadmin
15+
*/
16+
class Permission
17+
{
18+
/**
19+
* @param ForestUser $forestUser
20+
* @param mixed $collection
21+
* @return Response|bool
22+
*/
23+
public static function viewAny(ForestUser $forestUser, $collection)
24+
{
25+
return $forestUser->hasPermission(self::getCollectionName($collection), 'browseEnabled');
26+
}
27+
28+
/**
29+
* @param ForestUser $forestUser
30+
* @param mixed $collection
31+
* @return Response|bool
32+
*/
33+
public static function view(ForestUser $forestUser, $collection)
34+
{
35+
return $forestUser->hasPermission(self::getCollectionName($collection), 'readEnabled');
36+
}
37+
38+
/**
39+
* @param ForestUser $forestUser
40+
* @param mixed $collection
41+
* @return Response|bool
42+
*/
43+
public static function create(ForestUser $forestUser, $collection)
44+
{
45+
return $forestUser->hasPermission(self::getCollectionName($collection), 'addEnabled');
46+
}
47+
48+
/**
49+
* @param ForestUser $forestUser
50+
* @param mixed $collection
51+
* @return Response|bool
52+
*/
53+
public static function update(ForestUser $forestUser, $collection)
54+
{
55+
return $forestUser->hasPermission(self::getCollectionName($collection), 'editEnabled');
56+
}
57+
58+
/**
59+
* @param ForestUser $forestUser
60+
* @param $collection
61+
* @return Response|bool
62+
*/
63+
public static function delete(ForestUser $forestUser, $collection)
64+
{
65+
return $forestUser->hasPermission(self::getCollectionName($collection), 'deleteEnabled');
66+
}
67+
68+
/**
69+
* @param ForestUser $forestUser
70+
* @param mixed $collection
71+
* @return Response|bool
72+
*/
73+
public static function export(ForestUser $forestUser, $collection)
74+
{
75+
return $forestUser->hasPermission(self::getCollectionName($collection), 'exportEnabled');
76+
}
77+
78+
/**
79+
* @param ForestUser $forestUser
80+
* @param array $arguments
81+
* @return bool
82+
*/
83+
public static function smartAction(ForestUser $forestUser, array $arguments = [])
84+
{
85+
[$collection, $action] = $arguments;
86+
return $forestUser->hasSmartActionPermission(self::getCollectionName($collection), $action);
87+
}
88+
89+
/**
90+
* @param ForestUser $forestUser
91+
* @param $query
92+
* @return bool
93+
*/
94+
public static function liveQuery(ForestUser $forestUser, $query)
95+
{
96+
return $forestUser->hasLiveQueryPermission($query);
97+
}
98+
99+
/**
100+
* @param ForestUser $forestUser
101+
* @param $query
102+
* @return bool
103+
*/
104+
public static function simpleCharts(ForestUser $forestUser, $query)
105+
{
106+
return $forestUser->hasSimpleChartPermission($query);
107+
}
108+
109+
/**
110+
* @param mixed $collection
111+
* @return string
112+
*/
113+
private static function getCollectionName($collection): string
114+
{
115+
return Str::camel((class_basename(get_class($collection))));
116+
}
117+
}

src/Policies/PermissionPolicy.php

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

src/Providers/AuthorizationProvider.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Firebase\JWT\JWT;
66
use Firebase\JWT\Key;
77
use ForestAdmin\LaravelForestAdmin\Auth\Guard\Model\ForestUser;
8-
use ForestAdmin\LaravelForestAdmin\Policies\PermissionPolicy;
98
use Illuminate\Foundation\Support\Providers\AuthServiceProvider;
109
use Illuminate\Http\Request;
1110
use Illuminate\Support\Facades\Auth;
@@ -37,21 +36,5 @@ static function (Request $request) {
3736
}
3837
}
3938
);
40-
41-
Gate::guessPolicyNamesUsing(static fn() => PermissionPolicy::class);
42-
43-
Gate::define(
44-
'liveQuery',
45-
static function (ForestUser $user, string $query) {
46-
return $user->hasLiveQueryPermission($query);
47-
}
48-
);
49-
50-
Gate::define(
51-
'simple-charts',
52-
static function (ForestUser $user, array $payload) {
53-
return $user->hasSimpleChartPermission($payload);
54-
}
55-
);
5639
}
5740
}

0 commit comments

Comments
 (0)