Issues (43)

Security Analysis    no request data  

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/Traits/InteractsWithRole.php (7 issues)

Labels
Severity

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 Yajra\Acl\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Yajra\Acl\Models\Role;
8
9
/**
10
 * @property \Illuminate\Database\Eloquent\Collection|Role[] roles
11
 * @method static Builder havingRoles($roleIds)
12
 * @method static Builder havingRolesBySlugs($slugs)
13
 */
14
trait InteractsWithRole
15
{
16
    private $roleClass;
17
18
    /**
19
     * Check if user has the given role.
20
     *
21
     * @param  string|array  $role
22
     * @return bool
23
     */
24
    public function hasRole($role): bool
25
    {
26
        if (is_array($role)) {
27
            $roles = $this->getRoleSlugs();
28
29
            $intersection = array_intersect($roles, (array) $role);
30
            $intersectionCount = count($intersection);
31
32
            return $intersectionCount > 0;
33
        }
34
35
        return $this->roles->contains('slug', $role);
36
    }
37
38
    /**
39
     * Get all user roles.
40
     *
41
     * @return array
42
     */
43
    public function getRoleSlugs(): array
44
    {
45
        return $this->roles->pluck('slug')->toArray();
46
    }
47
48
    /**
49
     * Attach a role to user using slug.
50
     *
51
     * @param  string  $slug
52
     */
53
    public function attachRoleBySlug(string $slug)
54
    {
55
        $this->attachRole($this->findRoleBySlug($slug));
56
57
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
58
    }
59
60
    /**
61
     * Attach a role to user.
62
     *
63
     * @param  mixed  $role
64
     * @param  array  $attributes
65
     * @param  bool  $touch
66
     */
67
    public function attachRole($role, array $attributes = [], $touch = true)
68
    {
69
        $this->roles()->attach($role, $attributes, $touch);
70
71
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
72
    }
73
74
    /**
75
     * Model can have many roles.
76
     *
77
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
78
     */
79
    public function roles(): BelongsToMany
80
    {
81
        return $this->belongsToMany(config('acl.role', Role::class))->withTimestamps();
0 ignored issues
show
It seems like belongsToMany() 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...
82
    }
83
84
    /**
85
     * Find a role by slug.
86
     *
87
     * @param  string  $slug
88
     * @return \Illuminate\Database\Eloquent\Model|static
89
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
90
     */
91
    protected function findRoleBySlug(string $slug): Role
92
    {
93
        return $this->getRoleClass()->newQuery()->where('slug', $slug)->firstOrFail();
94
    }
95
96
    /**
97
     * Get Role class.
98
     *
99
     * @return Role
100
     */
101
    public function getRoleClass(): Role
102
    {
103
        if (!isset($this->roleClass)) {
104
            $this->roleClass = resolve(config('acl.role'));
105
        }
106
107
        return $this->roleClass;
108
    }
109
110
    /**
111
     * Query scope for user having the given roles.
112
     *
113
     * @param  \Illuminate\Database\Eloquent\Builder  $query
114
     * @param  mixed  $roles
115
     * @return \Illuminate\Database\Eloquent\Builder
116
     */
117
    public function scopeHavingRoles(Builder $query, $roles): Builder
118
    {
119
        return $query->whereExists(function ($query) use ($roles) {
120
            $query->selectRaw('1')
121
                ->from('role_user')
122
                ->whereRaw('role_user.user_id = users.id')
123
                ->whereIn('role_id', $roles);
124
        });
125
    }
126
127
    /**
128
     * Query scope for user having the given roles by slugs.
129
     *
130
     * @param  \Illuminate\Database\Eloquent\Builder  $query
131
     * @param  mixed  $slugs
132
     * @return \Illuminate\Database\Eloquent\Builder
133
     */
134
    public function scopeHavingRolesBySlugs(Builder $query, $slugs): Builder
135
    {
136
        return $query->whereHas('roles', function ($query) use ($slugs) {
137
            $query->whereIn('roles.slug', $slugs);
138
        });
139
    }
140
141
    /**
142
     * Revokes the given role from the user using slug.
143
     *
144
     * @param  string|array  $slug
145
     * @param  bool  $touch
146
     * @return int
147
     */
148
    public function revokeRoleBySlug($slug, $touch = true): int
149
    {
150
        $roles = $this->getRoleClass()
151
            ->newQuery()
152
            ->whereIn('slug', (array) $slug)
153
            ->get();
154
155
        $detached = $this->roles()->detach($roles, $touch);
156
157
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
158
159
        return $detached;
160
    }
161
162
    /**
163
     * Revokes the given role from the user.
164
     *
165
     * @param  mixed  $role
166
     * @param  bool  $touch
167
     * @return int
168
     */
169
    public function revokeRole($role, $touch = true): int
170
    {
171
        $detached = $this->roles()->detach($role, $touch);
172
173
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
174
175
        return $detached;
176
    }
177
178
    /**
179
     * Syncs the given role(s) with the user.
180
     *
181
     * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array  $roles
182
     * @param  bool  $detaching
183
     * @return array
184
     */
185
    public function syncRoles($roles, $detaching = true): array
186
    {
187
        $synced = $this->roles()->sync($roles, $detaching);
188
189
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
190
191
        return $synced;
192
    }
193
194
    /**
195
     * Revokes all roles from the user.
196
     *
197
     * @return int
198
     */
199
    public function revokeAllRoles(): int
200
    {
201
        $detached = $this->roles()->detach();
202
203
        $this->load('roles');
0 ignored issues
show
It seems like load() 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...
204
205
        return $detached;
206
    }
207
}
208