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
29:40 queued 14:41
created

Access   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
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 allowAccess() 0 7 2
A denyAccess() 0 7 2
A hasAccess() 0 11 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
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
            $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

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