Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — feature-deny-all-access ( a51d43 )
by
unknown
11:54
created

Access::hasAccessOrFail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Backpack\CRUD\app\Exceptions\AccessDeniedException;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait Access
9
{
10
    /**
11
     * Set an operation as having access using the Settings API.
12
     */
13
    public function allowAccess(array|string $operation): bool
14
    {
15
        foreach ((array) $operation as $op) {
16
            $this->set($op.'.access', true);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
            $this->/** @scrutinizer ignore-call */ 
17
                   set($op.'.access', true);
Loading history...
17
        }
18
19
        return $this->hasAccessToAll($operation);
20
    }
21
22
    /**
23
     * Disable the access to a certain operation, or the current one.
24
     */
25
    public function denyAccess(array|string $operation): bool
26
    {
27
        foreach ((array) $operation as $op) {
28
            $this->set($op.'.access', false);
29
        }
30
31
        return ! $this->hasAccessToAny($operation);
32
    }
33
34
    /**
35
     * Check if a operation is allowed for a Crud Panel. Return false if not.
36
     */
37
    public function hasAccess(string $operation, $entry = null): bool
38
    {
39
        $condition = $this->get($operation.'.access');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        /** @scrutinizer ignore-call */ 
40
        $condition = $this->get($operation.'.access');
Loading history...
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;
0 ignored issues
show
Bug introduced by
It seems like getCurrentEntry() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            $entry ??= $this->/** @scrutinizer ignore-call */ getCurrentEntry() ?: null;
Loading history...
45
46
            return $condition($entry);
47
        }
48
49
        return $condition ?? false;
50
    }
51
52
    /**
53
     * Check if any operations are allowed for a Crud Panel. Return false if not.
54
     */
55
    public function hasAccessToAny(array|string $operation_array, ?Model $entry = null): bool
56
    {
57
        foreach ((array) $operation_array as $key => $operation) {
58
            if ($this->hasAccess($operation, $entry) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
59
                return true;
60
            }
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Check if all operations are allowed for a Crud Panel. Return false if not.
68
     */
69
    public function hasAccessToAll(array|string $operation_array, ?Model $entry = null): bool
70
    {
71
        foreach ((array) $operation_array as $key => $operation) {
72
            if (! $this->hasAccess($operation, $entry)) {
73
                return false;
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Check if a operation is allowed for a Crud Panel. Fail if not.
82
     *
83
     * @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled
84
     */
85
    public function hasAccessOrFail(string $operation, ?Model $entry = null): bool
86
    {
87
        if (! $this->hasAccess($operation, $entry)) {
88
            throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation]));
89
        }
90
91
        return true;
92
    }
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
    }
121
122
    /**
123
     * Remove the access to all available operations.
124
     */
125
    public function denyAllAccess(): void
126
    {
127
        $this->denyAccess($this->getAvailableOperationsList());
0 ignored issues
show
Bug introduced by
It seems like getAvailableOperationsList() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        $this->denyAccess($this->/** @scrutinizer ignore-call */ getAvailableOperationsList());
Loading history...
128
    }
129
}
130