Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Auth/Database/Role.php (3 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 Encore\Admin\Auth\Database;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
8
class Role extends Model
9
{
10
    protected $fillable = ['name', 'slug'];
11
12
    /**
13
     * Create a new Eloquent model instance.
14
     *
15
     * @param array $attributes
16
     */
17 View Code Duplication
    public function __construct(array $attributes = [])
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...
18
    {
19
        $connection = config('admin.database.connection') ?: config('database.default');
20
21
        $this->setConnection($connection);
22
23
        $this->setTable(config('admin.database.roles_table'));
24
25
        parent::__construct($attributes);
26
    }
27
28
    /**
29
     * A role belongs to many users.
30
     *
31
     * @return BelongsToMany
32
     */
33 View Code Duplication
    public function administrators() : 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...
34
    {
35
        $pivotTable = config('admin.database.role_users_table');
36
37
        $relatedModel = config('admin.database.users_model');
38
39
        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'user_id');
40
    }
41
42
    /**
43
     * A role belongs to many permissions.
44
     *
45
     * @return BelongsToMany
46
     */
47 View Code Duplication
    public function permissions() : 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...
48
    {
49
        $pivotTable = config('admin.database.role_permissions_table');
50
51
        $relatedModel = config('admin.database.permissions_model');
52
53
        return $this->belongsToMany($relatedModel, $pivotTable, 'role_id', 'permission_id');
54
    }
55
56
    /**
57
     * Check user has permission.
58
     *
59
     * @param $permission
60
     *
61
     * @return bool
62
     */
63
    public function can(string $permission) : bool
64
    {
65
        return $this->permissions()->where('slug', $permission)->exists();
66
    }
67
68
    /**
69
     * Check user has no permission.
70
     *
71
     * @param $permission
72
     *
73
     * @return bool
74
     */
75
    public function cannot(string $permission) : bool
76
    {
77
        return !$this->can($permission);
78
    }
79
80
    /**
81
     * Detach models from the relationship.
82
     *
83
     * @return void
84
     */
85
    protected static function boot()
86
    {
87
        parent::boot();
88
89
        static::deleting(function ($model) {
90
            $model->administrators()->detach();
91
92
            $model->permissions()->detach();
93
        });
94
    }
95
}
96