Yeelight /
cloud2
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Yeelight\Models; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||||
| 6 | use Illuminate\Http\Request; |
||||
| 7 | use Illuminate\Support\Str; |
||||
| 8 | use Prettus\Repository\Contracts\Transformable; |
||||
| 9 | use Prettus\Repository\Traits\TransformableTrait; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Class AdminPermission |
||||
| 13 | * |
||||
| 14 | * @category Yeelight |
||||
| 15 | * |
||||
| 16 | * @package Yeelight\Models |
||||
| 17 | * |
||||
| 18 | * @author Sheldon Lee <[email protected]> |
||||
| 19 | * |
||||
| 20 | * @license https://opensource.org/licenses/MIT MIT |
||||
| 21 | * |
||||
| 22 | * @link https://www.yeelight.com |
||||
| 23 | */ |
||||
| 24 | class AdminPermission extends BaseModel implements Transformable |
||||
| 25 | { |
||||
| 26 | use TransformableTrait; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Indicates if the model should be auto set user_id. |
||||
| 30 | * |
||||
| 31 | * @var bool |
||||
| 32 | */ |
||||
| 33 | protected $autoUserId = false; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Indicates if the model should be recorded ips. |
||||
| 37 | * |
||||
| 38 | * @var bool |
||||
| 39 | */ |
||||
| 40 | protected $ips = false; |
||||
| 41 | |||||
| 42 | /** |
||||
| 43 | * Indicates if the model should be recorded users. |
||||
| 44 | * |
||||
| 45 | * @var bool |
||||
| 46 | */ |
||||
| 47 | protected $update_users = false; |
||||
| 48 | |||||
| 49 | protected $primaryKey = 'id'; |
||||
| 50 | |||||
| 51 | // Fields to be converted to Carbon object automatically |
||||
| 52 | protected $dates = []; |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * $fillable |
||||
| 56 | * |
||||
| 57 | * @var array |
||||
| 58 | */ |
||||
| 59 | protected $fillable = ['name', 'slug', 'http_method', 'http_path']; |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * $httpMethods |
||||
| 63 | * |
||||
| 64 | * @var array |
||||
| 65 | */ |
||||
| 66 | public static $httpMethods = [ |
||||
| 67 | 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', |
||||
| 68 | ]; |
||||
| 69 | |||||
| 70 | public static $methodColors = [ |
||||
| 71 | 'GET' => 'green', |
||||
| 72 | 'POST' => 'yellow', |
||||
| 73 | 'PUT' => 'blue', |
||||
| 74 | 'DELETE' => 'red', |
||||
| 75 | ]; |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * Create a new Eloquent model instance. |
||||
| 79 | * |
||||
| 80 | * @param array $attributes attributes |
||||
| 81 | */ |
||||
| 82 | public function __construct(array $attributes = []) |
||||
| 83 | { |
||||
| 84 | $this->setTable(config('yeelight.backend.database.admin_permissions_table')); |
||||
| 85 | |||||
| 86 | parent::__construct($attributes); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * Permission belongs to many roles. |
||||
| 91 | * |
||||
| 92 | * @return BelongsToMany |
||||
| 93 | */ |
||||
| 94 | public function roles() : BelongsToMany |
||||
| 95 | { |
||||
| 96 | $pivotTable = config('yeelight.backend.database.admin_role_permissions_table'); |
||||
| 97 | |||||
| 98 | $relatedModel = config('yeelight.backend.database.admin_roles_model'); |
||||
| 99 | |||||
| 100 | return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id'); |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | /** |
||||
| 104 | * If request should pass through the current permission. |
||||
| 105 | * |
||||
| 106 | * @param Request $request Request |
||||
| 107 | * |
||||
| 108 | * @return bool |
||||
| 109 | */ |
||||
| 110 | public function shouldPassThrough(Request $request) : bool |
||||
| 111 | { |
||||
| 112 | if (empty($this->http_method) && empty($this->http_path)) { |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
The property
http_path does not exist on Yeelight\Models\AdminPermission. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 113 | return true; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | $method = $this->http_method; |
||||
| 117 | |||||
| 118 | $matches = array_map(function ($path) use ($method) { |
||||
| 119 | $path = trim(config('yeelight.backend.route.prefix'), '/').$path; |
||||
| 120 | |||||
| 121 | if (Str::contains($path, ':')) { |
||||
| 122 | list($method, $path) = explode(':', $path); |
||||
| 123 | $method = explode(',', $method); |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | return compact('method', 'path'); |
||||
| 127 | }, explode("\r\n", $this->http_path)); |
||||
|
0 ignored issues
–
show
It seems like
$this->http_path can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 128 | |||||
| 129 | foreach ($matches as $match) { |
||||
| 130 | if ($this->matchRequest($match, $request)) { |
||||
| 131 | return true; |
||||
| 132 | } |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | return false; |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * If a request match the specific HTTP method and path. |
||||
| 140 | * |
||||
| 141 | * @param array $match match |
||||
| 142 | * @param Request $request Request |
||||
| 143 | * |
||||
| 144 | * @return bool |
||||
| 145 | */ |
||||
| 146 | protected function matchRequest(array $match, Request $request) : bool |
||||
| 147 | { |
||||
| 148 | if (!$request->is(trim($match['path'], '/'))) { |
||||
|
0 ignored issues
–
show
trim($match['path'], '/') of type string is incompatible with the type Illuminate\Http\dynamic expected by parameter $patterns of Illuminate\Http\Request::is().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 149 | return false; |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | $method = collect($match['method'])->filter()->map(function ($method) { |
||||
| 153 | return strtoupper($method); |
||||
| 154 | }); |
||||
| 155 | |||||
| 156 | return $method->isEmpty() || $method->contains($request->method()); |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | /** |
||||
| 160 | * @param $method |
||||
| 161 | */ |
||||
| 162 | public function setHttpMethodAttribute($method) |
||||
| 163 | { |
||||
| 164 | if (is_array($method)) { |
||||
| 165 | $this->attributes['http_method'] = implode(',', $method); |
||||
| 166 | } |
||||
| 167 | } |
||||
| 168 | |||||
| 169 | /** |
||||
| 170 | * @param $method |
||||
| 171 | * |
||||
| 172 | * @return array |
||||
| 173 | */ |
||||
| 174 | public function getHttpMethodAttribute($method) |
||||
| 175 | { |
||||
| 176 | if (is_string($method)) { |
||||
| 177 | return array_filter(explode(',', $method)); |
||||
| 178 | } |
||||
| 179 | |||||
| 180 | return $method; |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | public function onDeleting() |
||||
| 184 | { |
||||
| 185 | parent::onDeleting(); |
||||
| 186 | // Detach models from the relationship. |
||||
| 187 | $this->roles()->detach(); |
||||
| 188 | } |
||||
| 189 | } |
||||
| 190 |