Authority.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. use App\Traits\ModelLogChanging;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Illuminate\Support\Facades\DB;
  9. class Authority extends Model
  10. {
  11. use ModelLogChanging;
  12. use SoftDeletes;
  13. use ModelTimeFormat;
  14. protected $fillable = ['name','parent_id','alias_name','permission',"route","method"];
  15. const METHOD = [
  16. 0 => "GET",
  17. 1 => "POST",
  18. 2 => "PUT",
  19. 3 => "DELETE",
  20. ];
  21. function roles(){
  22. return $this->belongsToMany('App\Role','authority_role','id_authority','id_role');
  23. }
  24. function getNameFilteredAttribute(){
  25. preg_match('#(.*)(_[0-9]*?$)#',$this['name'],$arr);
  26. if($arr){
  27. $id=str_replace('_','',$arr[2]);
  28. $owner = Owner::find($id);
  29. if($owner){return "{$arr[1]}({$owner['name']})";}
  30. }
  31. return $this['name'];
  32. }
  33. function getOwnerIdAttribute(){
  34. preg_match('#_([0-9]*?)$#',$this['name'],$arr);
  35. if(count($arr)>1&&$arr[1]){
  36. return $arr[1];
  37. }
  38. return '';
  39. }
  40. public static function filterRecycle(Collection $authorities)
  41. {
  42. $owners = Owner::query()->whereNotNull("deleted_at")->get();
  43. $owner_keys = $owners->map(function($owner){
  44. return '_'.$owner['id'];
  45. })->toArray();
  46. return $authorities->filter(function ($authority)use($owner_keys){
  47. return !in_array($authority->name,$owner_keys);
  48. });
  49. }
  50. }