Issues (65)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Component/Concerns/HasAccess.php (2 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Sco\Admin\Component\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
8
trait HasAccess
9
{
10
    /**
11
     * The abilities of access.
12
     *
13
     * @var \Illuminate\Support\Collection
14
     */
15
    protected $abilities;
16
17
    /**
18
     * Access observer class.
19
     *
20
     * @var string
21
     */
22
    protected $observer;
23
24
    /**
25
     * User exposed observable abilities.
26
     *
27
     * @var array
28
     */
29
    protected $observables = [];
30
31
    /**
32
     * Initialize access.
33
     */
34 36
    public function bootHasAccess()
35
    {
36 36
        $this->abilities = new Collection();
37
38 36
        $this->observe($this->observer);
39 36
    }
40
41
    /**
42
     * Determine if the entity have access to display.
43
     *
44
     * @return bool
45
     */
46
    public function isDisplay()
47
    {
48
        return method_exists($this, 'callDisplay') && $this->can('display');
49
    }
50
51
    /**
52
     * Check if the entity have access to create.
53
     *
54
     * @return bool
55
     */
56
    public function isCreate()
57
    {
58
        return method_exists($this, 'callCreate') && $this->can('create');
59
    }
60
61
    /**
62
     * Check if the entity have access to edit.
63
     *
64
     * @return bool
65
     */
66
    public function isEdit()
67
    {
68
        return method_exists($this, 'callEdit') && $this->can('edit');
69
    }
70
71
    /**
72
     * Check if the entity have access to delete.
73
     *
74
     * @return mixed
75
     */
76
    public function isDelete()
77
    {
78
        return $this->can('delete');
79
    }
80
81
    /**
82
     * Check if the entity have access to destroy.
83
     *
84
     * @return bool
85
     */
86
    public function isDestroy()
87
    {
88
        return $this->isRestorableModel() && $this->can('destroy');
89
    }
90
91
    /**
92
     * Check if the entity have access to restore.
93
     *
94
     * @return bool
95
     */
96
    public function isRestore()
97
    {
98
        return $this->isRestorableModel() && $this->can('restore');
99
    }
100
101
    /**
102
     * Whether the model can be restored
103
     *
104
     * @return mixed
105
     */
106
    protected function isRestorableModel()
107
    {
108
        return $this->getRepository()->isRestorable();
0 ignored issues
show
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
109
    }
110
111
    /**
112
     * Register an observer with the Component.
113
     *
114
     * @param $class
115
     */
116 36
    public function observe($class)
117
    {
118 36
        $className = is_string($class) ? $class : get_class($class);
119
120 36
        if (!class_exists($className)) {
121
            return;
122
        }
123
124 36
        foreach ($this->getObservableAbilities() as $ability) {
125 36
            if (method_exists($class, $ability)) {
126 36
                $this->registerAbility($ability, $className . '@' . $ability);
127
            }
128
        }
129 36
    }
130
131
    /**
132
     * Get the observable ability names.
133
     *
134
     * @return array
135
     */
136 36
    public function getObservableAbilities()
137
    {
138 36
        return array_merge([
139 36
            'display',
140
            'create',
141
            'edit',
142
            'delete',
143
            'destroy',
144
            'restore',
145 36
        ], $this->observables);
146
    }
147
148
    /**
149
     * register ability to access.
150
     *
151
     * @param string $ability
152
     * @param string|\Closure $callback
153
     */
154
    public function registerAbility($ability, $callback)
155
    {
156
        $this->abilities->put($ability, $this->makeAbilityCallback($callback));
157
    }
158
159
    /**
160
     * @param string|\Closure $callback
161
     *
162
     * @return \Closure
163
     */
164
    protected function makeAbilityCallback($callback)
165
    {
166
        return function ($component) use ($callback) {
167
            if (is_callable($callback)) {
168
                return $callback($component);
169
            }
170
            if (is_string($callback)) {
171
                list($class, $method) = Str::parseCallback($callback);
172
173
                return call_user_func([$this->app->make($class), $method], $component);
0 ignored issues
show
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
174
            }
175
        };
176
    }
177
178
    /**
179
     * Determine if the entity has a given ability.
180
     *
181
     * @param string $ability
182
     *
183
     * @return bool
184
     */
185
    final public function can($ability)
186
    {
187
        if (! $this->abilities->has($ability)) {
188
            return false;
189
        }
190
        $value = $this->abilities->get($ability);
191
192
        return $value($this) ? true : false;
193
    }
194
195
    /**
196
     * Get all ability.
197
     *
198
     * @return Collection
199
     */
200
    public function getAccesses()
201
    {
202 3
        return $this->abilities->mapWithKeys(function ($item, $key) {
203
            return [$key => $this->can($key)];
204 3
        });
205
    }
206
}
207