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 — callable-access ( 5bcaa7 )
by Cristian
12:12
created

Access   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 141
rs 10
c 1
b 0
f 0
wmc 18

9 Methods

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