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
Pull Request — main (#5335)
by Cristian
10:57
created

Access::getAccessCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Backpack\CRUD\app\Exceptions\AccessDeniedException;
6
7
trait Access
8
{
9
    /**
10
     * Set an operation as having access using the Settings API.
11
     *
12
     * @param  string|array  $operation
13
     * @return bool
14
     */
15
    public function allowAccess(array|string $operation): bool
16
    {
17
        foreach ((array) $operation as $op) {
18
            $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

18
            $this->/** @scrutinizer ignore-call */ 
19
                   set($op.'.access', true);
Loading history...
19
        }
20
21
        return $this->hasAccessToAll($operation);
22
    }
23
24
    /**
25
     * Disable the access to a certain operation, or the current one.
26
     *
27
     * @param  string|array  $operation  [description]
28
     * @return [type] [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
29
     */
30
    public function denyAccess(array|string $operation)
31
    {
32
        foreach ((array) $operation as $op) {
33
            $this->set($op.'.access', false);
34
        }
35
36
        return ! $this->hasAccessToAny($operation);
37
    }
38
39
    /**
40
     * Check if a operation is allowed for a Crud Panel. Return false if not.
41
     *
42
     * @param  string  $operation
43
     * @param  \Model  $entry
0 ignored issues
show
Bug introduced by
The type Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
     * @return bool
45
     */
46
    public function hasAccess(string $operation, $entry = null): bool
47
    {
48
        $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

48
        /** @scrutinizer ignore-call */ 
49
        $condition = $this->get($operation.'.access');
Loading history...
49
50
        if (is_callable($condition)) {
51
            $entry = $entry ?? $this->getCurrentEntry();
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

51
            $entry = $entry ?? $this->/** @scrutinizer ignore-call */ getCurrentEntry();
Loading history...
52
53
            return $condition($entry);
54
        }
55
56
        return $condition ?? false;
57
    }
58
59
    /**
60
     * Check if any operations are allowed for a Crud Panel. Return false if not.
61
     *
62
     * @param  string|array  $operation_array
63
     * @param  \Model  $entry
64
     * @return bool
65
     */
66
    public function hasAccessToAny(array|string $operation_array, $entry = null): bool
67
    {
68
        foreach ((array) $operation_array as $key => $operation) {
69
            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...
70
                return true;
71
            }
72
        }
73
74
        return false;
75
    }
76
77
    /**
78
     * Check if all operations are allowed for a Crud Panel. Return false if not.
79
     *
80
     * @param  array  $operation_array  Permissions.
81
     * @param  \Model  $entry
82
     * @return bool
83
     */
84
    public function hasAccessToAll(array|string $operation_array, $entry = null): bool
85
    {
86
        foreach ((array) $operation_array as $key => $operation) {
87
            if (! $this->hasAccess($operation, $entry)) {
88
                return false;
89
            }
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * Check if a operation is allowed for a Crud Panel. Fail if not.
97
     *
98
     * @param  string  $operation
99
     * @param  \Model  $entry
100
     * @return bool
101
     *
102
     * @throws \Backpack\CRUD\Exception\AccessDeniedException in case the operation is not enabled
103
     */
104
    public function hasAccessOrFail(string $operation, $entry = null): bool
105
    {
106
        if (! $this->hasAccess($operation, $entry)) {
107
            throw new AccessDeniedException(trans('backpack::crud.unauthorized_access', ['access' => $operation]));
108
        }
109
110
        return true;
111
    }
112
113
    /**
114
     * Get an operation's access condition, if set. A condition
115
     * can be anything, but usually a boolean or a callable.
116
     *
117
     * @param  string  $operation
118
     * @return bool|callable|null
119
     */
120
    public function getAccessCondition(string $operation): bool|callable|null
121
    {
122
        return $this->get($operation.'.access');
123
    }
124
125
    /**
126
     * Set the condition under which an operation is allowed for a Crud Panel.
127
     *
128
     * @param  string|array  $operation
129
     * @param  bool|callable|null  $condition
130
     * @return void
131
     */
132
    public function setAccessCondition(array|string $operation, bool|callable|null $condition): void
133
    {
134
        foreach ((array) $operation as $op) {
135
            $this->set($op.'.access', $condition);
136
        }
137
    }
138
139
    /**
140
     * Check if an operation has an access condition already set.
141
     * A condition can be anything, but usually a boolean or a callable.
142
     *
143
     * @param  string  $operation
144
     * @return bool
145
     */
146
    public function hasAccessCondition(string $operation): bool
147
    {
148
        return $this->get($operation.'.access') !== null;
149
    }
150
}
151