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
32:53 queued 18:04
created

Access::hasAccessCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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
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
            if (! $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

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