AdminPermission.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Models\Admin;
  3. use Encore\Admin\Traits\AdminBuilder;
  4. use Encore\Admin\Traits\ModelTree;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Str;
  10. class AdminPermission extends Model
  11. {
  12. use AdminBuilder, ModelTree {
  13. ModelTree::boot as treeBoot;
  14. }
  15. protected $table = 'admin_permissions';
  16. public function __construct(array $attributes = [])
  17. {
  18. parent::__construct($attributes);
  19. $this->setParentColumn('parent_id');
  20. $this->setOrderColumn('listorder');
  21. $this->setTitleColumn('name');
  22. }
  23. /**
  24. * @var array
  25. */
  26. protected $fillable = ['name', 'slug', 'http_method', 'http_path', 'parent_id', 'type', 'listorder', 'menu_id'];
  27. /**
  28. * @var array
  29. */
  30. public static $httpMethods = [
  31. 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD',
  32. ];
  33. public function children()
  34. {
  35. return $this->hasMany(AdminPermission::class, 'parent_id')->orderBy('listorder');
  36. }
  37. /**
  38. * Permission belongs to many roles.
  39. *
  40. * @return BelongsTo
  41. */
  42. public function menu() : BelongsTo
  43. {
  44. return $this->belongsTo(AdminMenu::class, 'menu_id');
  45. }
  46. /**
  47. * Permission belongs to many roles.
  48. *
  49. * @return BelongsToMany
  50. */
  51. public function roles() : BelongsToMany
  52. {
  53. $pivotTable = config('admin.database.role_permissions_table');
  54. $relatedModel = config('admin.database.roles_model');
  55. return $this->belongsToMany($relatedModel, $pivotTable, 'permission_id', 'role_id');
  56. }
  57. /**
  58. * If request should pass through the current permission.
  59. *
  60. * @param Request $request
  61. *
  62. * @return bool
  63. */
  64. public function shouldPassThrough(Request $request) : bool
  65. {
  66. if (empty($this->http_path)) {
  67. return false;
  68. }
  69. $method = $this->http_method;
  70. $matches = array_map(function ($path) use ($method) {
  71. $path = trim(config('admin.route.prefix'), '/').$path;
  72. if (Str::contains($path, ':')) {
  73. list($method, $path) = explode(':', $path);
  74. $method = explode(',', $method);
  75. }
  76. return compact('method', 'path');
  77. }, explode("\r\n", $this->http_path));
  78. foreach ($matches as $match) {
  79. if ($this->matchRequest($match, $request)) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * If a request match the specific HTTP method and path.
  87. *
  88. * @param array $match
  89. * @param Request $request
  90. *
  91. * @return bool
  92. */
  93. protected function matchRequest(array $match, Request $request) : bool
  94. {
  95. if (!$request->is(trim($match['path'], '/'))) {
  96. return false;
  97. }
  98. $method = collect($match['method'])->filter()->map(function ($method) {
  99. return strtoupper($method);
  100. });
  101. return $method->isEmpty() || $method->contains($request->method());
  102. }
  103. /**
  104. * @param $method
  105. */
  106. public function setHttpMethodAttribute($method)
  107. {
  108. if (is_array($method)) {
  109. $this->attributes['http_method'] = implode(',', $method);
  110. }
  111. }
  112. /**
  113. * @param $method
  114. *
  115. * @return array
  116. */
  117. public function getHttpMethodAttribute($method)
  118. {
  119. if (is_string($method)) {
  120. return array_filter(explode(',', $method));
  121. }
  122. return $method;
  123. }
  124. /**
  125. * Detach models from the relationship.
  126. *
  127. * @return void
  128. */
  129. protected static function boot()
  130. {
  131. static::treeBoot();
  132. static::deleting(function ($model) {
  133. $model->roles()->detach();
  134. });
  135. }
  136. /**
  137. * 菜单封装装.
  138. * return object
  139. */
  140. public static function permissionArr()
  141. {
  142. $order="`listorder` = 0,`listorder`";
  143. $permissions = self::orderByRaw($order)->select(['id', 'parent_id', 'subsite_id', 'name'])->get();
  144. $permission_array=[];
  145. foreach ($permissions as $permission) {
  146. $permission_array[$permission['id']]=$permission;
  147. }
  148. $permission_res=[];
  149. foreach ($permission_array as $key => $value) {
  150. if (get_subsite_id() == 0) {
  151. $permission_res[$key] = self::getName($value, $permission_array);
  152. } elseif ($value['subsite_id'] == -1 || $value['subsite_id'] == get_subsite_id()) {
  153. $permission_res[$key] = self::getName($value, $permission_array);
  154. }
  155. }
  156. return $permission_res;
  157. }
  158. public static function getName($value, $list)
  159. {
  160. if ($value['parent_id'] == 0) {
  161. return $value['name'];
  162. } elseif (isset($list[$value['parent_id']])) {
  163. return self::getName($list[$value['parent_id']], $list)."-{$value['name']}";
  164. }
  165. return "";
  166. }
  167. }