Issues (62)

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/Models/Role.php (4 issues)

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 Spatie\Permission\Models;
4
5
use Spatie\Permission\Guard;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\Traits\HasPermissions;
8
use Spatie\Permission\Exceptions\RoleDoesNotExist;
9
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
10
use Spatie\Permission\Exceptions\RoleAlreadyExists;
11
use Spatie\Permission\Contracts\Role as RoleContract;
12
use Spatie\Permission\Traits\RefreshesPermissionCache;
13
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14
15
class Role extends Model implements RoleContract
16
{
17
    use HasPermissions;
18
    use RefreshesPermissionCache;
19
20
    protected $guarded = ['id'];
21
22
    public function __construct(array $attributes = [])
23
    {
24
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
25
26
        parent::__construct($attributes);
27
    }
28
29
    public function getTable()
30
    {
31
        return config('permission.table_names.roles', parent::getTable());
32
    }
33
34
    public static function create(array $attributes = [])
35
    {
36
        $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
37
38
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
39
            throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
40
        }
41
42
        return static::query()->create($attributes);
43
    }
44
45
    /**
46
     * A role may be given various permissions.
47
     */
48
    public function permissions(): BelongsToMany
49
    {
50
        return $this->belongsToMany(
51
            config('permission.models.permission'),
52
            config('permission.table_names.role_has_permissions'),
53
            'role_id',
54
            'permission_id'
55
        );
56
    }
57
58
    /**
59
     * A role belongs to some users of the model associated with its guard.
60
     */
61 View Code Duplication
    public function users(): BelongsToMany
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        return $this->morphedByMany(
64
            getModelForGuard($this->attributes['guard_name']),
65
            'model',
66
            config('permission.table_names.model_has_roles'),
67
            'role_id',
68
            config('permission.column_names.model_morph_key')
69
        );
70
    }
71
72
    /**
73
     * Find a role by its name and guard name.
74
     *
75
     * @param string $name
76
     * @param string|null $guardName
77
     *
78
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
79
     *
80
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
81
     */
82 View Code Duplication
    public static function findByName(string $name, $guardName = null): RoleContract
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
85
86
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
87
88
        if (! $role) {
89
            throw RoleDoesNotExist::named($name);
90
        }
91
92
        return $role;
93
    }
94
95 View Code Duplication
    public static function findById(int $id, $guardName = null): RoleContract
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
98
99
        $role = static::where('id', $id)->where('guard_name', $guardName)->first();
100
101
        if (! $role) {
102
            throw RoleDoesNotExist::withId($id);
103
        }
104
105
        return $role;
106
    }
107
108
    /**
109
     * Find or create role by its name (and optionally guardName).
110
     *
111
     * @param string $name
112
     * @param string|null $guardName
113
     *
114
     * @return \Spatie\Permission\Contracts\Role
115
     */
116 View Code Duplication
    public static function findOrCreate(string $name, $guardName = null): RoleContract
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
119
120
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
121
122
        if (! $role) {
123
            return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
124
        }
125
126
        return $role;
127
    }
128
129
    /**
130
     * Determine if the user may perform the given permission.
131
     *
132
     * @param string|Permission $permission
133
     *
134
     * @return bool
135
     *
136
     * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
137
     */
138
    public function hasPermissionTo($permission): bool
139
    {
140
        if (config('permission.enable_wildcard_permission', false)) {
141
            return $this->hasWildcardPermission($permission, $this->getDefaultGuardName());
142
        }
143
144
        $permissionClass = $this->getPermissionClass();
145
146
        if (is_string($permission)) {
147
            $permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
148
        }
149
150
        if (is_int($permission)) {
151
            $permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
152
        }
153
154
        if (! $this->getGuardNames()->contains($permission->guard_name)) {
155
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
156
        }
157
158
        return $this->permissions->contains('id', $permission->id);
159
    }
160
}
161