Skip to content

Commit 4fca689

Browse files
authored
Merge pull request #5335 from Laravel-Backpack/callable-access
2 parents d2c954d + 924dea5 commit 4fca689

File tree

5 files changed

+53
-32
lines changed

5 files changed

+53
-32
lines changed

src/app/Library/CrudPanel/Traits/Access.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
44

55
use Backpack\CRUD\app\Exceptions\AccessDeniedException;
6+
use Illuminate\Database\Eloquent\Model;
67

78
trait Access
89
{
910
/**
1011
* Set an operation as having access using the Settings API.
11-
*
12-
* @param string|array $operation
13-
* @return bool
1412
*/
15-
public function allowAccess($operation)
13+
public function allowAccess(array|string $operation): bool
1614
{
1715
foreach ((array) $operation as $op) {
1816
$this->set($op.'.access', true);
@@ -23,11 +21,8 @@ public function allowAccess($operation)
2321

2422
/**
2523
* Disable the access to a certain operation, or the current one.
26-
*
27-
* @param string|array $operation [description]
28-
* @return [type] [description]
2924
*/
30-
public function denyAccess($operation)
25+
public function denyAccess(array|string $operation): bool
3126
{
3227
foreach ((array) $operation as $op) {
3328
$this->set($op.'.access', false);
@@ -38,25 +33,29 @@ public function denyAccess($operation)
3833

3934
/**
4035
* Check if a operation is allowed for a Crud Panel. Return false if not.
41-
*
42-
* @param string $operation
43-
* @return bool
4436
*/
45-
public function hasAccess($operation)
37+
public function hasAccess(string $operation, $entry = null): bool
4638
{
47-
return $this->get($operation.'.access') ?? false;
39+
$condition = $this->get($operation.'.access');
40+
41+
if (is_callable($condition)) {
42+
// supply the current entry, if $entry is missing
43+
// this also makes sure the entry is null when missing
44+
$entry ??= $this->getCurrentEntry() ?: null;
45+
46+
return $condition($entry);
47+
}
48+
49+
return $condition ?? false;
4850
}
4951

5052
/**
5153
* Check if any operations are allowed for a Crud Panel. Return false if not.
52-
*
53-
* @param string|array $operation_array
54-
* @return bool
5554
*/
56-
public function hasAccessToAny($operation_array)
55+
public function hasAccessToAny(array|string $operation_array, ?Model $entry = null): bool
5756
{
5857
foreach ((array) $operation_array as $key => $operation) {
59-
if ($this->get($operation.'.access') == true) {
58+
if ($this->hasAccess($operation, $entry) == true) {
6059
return true;
6160
}
6261
}
@@ -66,14 +65,11 @@ public function hasAccessToAny($operation_array)
6665

6766
/**
6867
* Check if all operations are allowed for a Crud Panel. Return false if not.
69-
*
70-
* @param array $operation_array Permissions.
71-
* @return bool
7268
*/
73-
public function hasAccessToAll($operation_array)
69+
public function hasAccessToAll(array|string $operation_array, ?Model $entry = null): bool
7470
{
7571
foreach ((array) $operation_array as $key => $operation) {
76-
if (! $this->get($operation.'.access')) {
72+
if (! $this->hasAccess($operation, $entry)) {
7773
return false;
7874
}
7975
}
@@ -84,17 +80,42 @@ public function hasAccessToAll($operation_array)
8480
/**
8581
* Check if a operation is allowed for a Crud Panel. Fail if not.
8682
*
87-
* @param string $operation
88-
* @return bool
89-
*
9083
* @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled
9184
*/
92-
public function hasAccessOrFail($operation)
85+
public function hasAccessOrFail(string $operation, ?Model $entry = null): bool
9386
{
94-
if (! $this->get($operation.'.access')) {
87+
if (! $this->hasAccess($operation, $entry)) {
9588
throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation]));
9689
}
9790

9891
return true;
9992
}
93+
94+
/**
95+
* Get an operation's access condition, if set. A condition
96+
* can be anything, but usually a boolean or a callable.
97+
*/
98+
public function getAccessCondition(string $operation): bool|callable|null
99+
{
100+
return $this->get($operation.'.access');
101+
}
102+
103+
/**
104+
* Set the condition under which an operation is allowed for a Crud Panel.
105+
*/
106+
public function setAccessCondition(array|string $operation, bool|callable|null $condition): void
107+
{
108+
foreach ((array) $operation as $op) {
109+
$this->set($op.'.access', $condition);
110+
}
111+
}
112+
113+
/**
114+
* Check if an operation has an access condition already set.
115+
* A condition can be anything, but usually a boolean or a callable.
116+
*/
117+
public function hasAccessCondition(string $operation): bool
118+
{
119+
return $this->get($operation.'.access') !== null;
120+
}
100121
}

src/resources/views/crud/buttons/delete.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($crud->hasAccess('delete'))
1+
@if ($crud->hasAccess('delete', $entry))
22
<a href="javascript:void(0)" onclick="deleteEntry(this)" data-route="{{ url($crud->route.'/'.$entry->getKey()) }}" class="btn btn-sm btn-link" data-button-type="delete">
33
<span><i class="la la-trash"></i> {{ trans('backpack::crud.delete') }}</span>
44
</a>

src/resources/views/crud/buttons/quick.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$wrapper['class'] = $wrapper['class'] ?? $defaultClass;
2121
@endphp
2222

23-
@if ($access === true || $crud->hasAccess($access))
23+
@if ($access === true || $crud->hasAccess($access, isset($entry) ? $entry : null))
2424
<{{ $wrapper['element'] }}
2525
@foreach ($wrapper as $attribute => $value)
2626
@if (is_string($attribute))

src/resources/views/crud/buttons/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($crud->hasAccess('show'))
1+
@if ($crud->hasAccess('show', $entry))
22
@if (!$crud->model->translationEnabled())
33

44
{{-- Single edit button --}}

src/resources/views/crud/buttons/update.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($crud->hasAccess('update'))
1+
@if ($crud->hasAccess('update', $entry))
22
@if (!$crud->model->translationEnabled())
33

44
{{-- Single edit button --}}

0 commit comments

Comments
 (0)