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

src/Auth/Database/Permission.php (2 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
use Illuminate\Http\Request;
8
use Illuminate\Support\Str;
9
10
class Permission extends Model
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $fillable = ['name', 'slug', 'http_method', 'http_path'];
16
17
    /**
18
     * @var array
19
     */
20
    public static $httpMethods = [
21
        'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD',
22
    ];
23
24
    /**
25
     * Create a new Eloquent model instance.
26
     *
27
     * @param array $attributes
28
     */
29 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...
30
    {
31
        $connection = config('admin.database.connection') ?: config('database.default');
32
33
        $this->setConnection($connection);
34
35
        $this->setTable(config('admin.database.permissions_table'));
36
37
        parent::__construct($attributes);
38
    }
39
40
    /**
41
     * Permission belongs to many roles.
42
     *
43
     * @return BelongsToMany
44
     */
45 View Code Duplication
    public function roles() : 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...
46
    {
47
        $pivotTable = config('admin.database.role_permissions_table');
48
49
        $relatedModel = config('admin.database.roles_model');
50
51
        return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id');
52
    }
53
54
    /**
55
     * If request should pass through the current permission.
56
     *
57
     * @param Request $request
58
     *
59
     * @return bool
60
     */
61
    public function shouldPassThrough(Request $request) : bool
62
    {
63
        if (empty($this->http_method) && empty($this->http_path)) {
64
            return true;
65
        }
66
67
        $method = $this->http_method;
68
69
        $matches = array_map(function ($path) use ($method) {
70
            $path = trim(config('admin.route.prefix'), '/').$path;
71
72
            if (Str::contains($path, ':')) {
73
                list($method, $path) = explode(':', $path);
74
                $method = explode(',', $method);
75
            }
76
77
            return compact('method', 'path');
78
        }, explode("\r\n", $this->http_path));
79
80
        foreach ($matches as $match) {
81
            if ($this->matchRequest($match, $request)) {
82
                return true;
83
            }
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * If a request match the specific HTTP method and path.
91
     *
92
     * @param array   $match
93
     * @param Request $request
94
     *
95
     * @return bool
96
     */
97
    protected function matchRequest(array $match, Request $request) : bool
98
    {
99
        if (!$request->is(trim($match['path'], '/'))) {
100
            return false;
101
        }
102
103
        $method = collect($match['method'])->filter()->map(function ($method) {
104
            return strtoupper($method);
105
        });
106
107
        return $method->isEmpty() || $method->contains($request->method());
108
    }
109
110
    /**
111
     * @param $method
112
     */
113
    public function setHttpMethodAttribute($method)
114
    {
115
        if (is_array($method)) {
116
            $this->attributes['http_method'] = implode(',', $method);
117
        }
118
    }
119
120
    /**
121
     * @param $method
122
     *
123
     * @return array
124
     */
125
    public function getHttpMethodAttribute($method)
126
    {
127
        if (is_string($method)) {
128
            return array_filter(explode(',', $method));
129
        }
130
131
        return $method;
132
    }
133
134
    /**
135
     * Detach models from the relationship.
136
     *
137
     * @return void
138
     */
139
    protected static function boot()
140
    {
141
        parent::boot();
142
143
        static::deleting(function ($model) {
144
            $model->roles()->detach();
145
        });
146
    }
147
}
148