Authority.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. 4 => "ANDROID"
  21. ];
  22. function roles(){
  23. return $this->belongsToMany('App\Role','authority_role','id_authority','id_role');
  24. }
  25. function getNameFilteredAttribute(){
  26. preg_match('#(.*)(_[0-9]*?$)#',$this['name'],$arr);
  27. if($arr){
  28. $id=str_replace('_','',$arr[2]);
  29. $owner = Owner::find($id);
  30. if($owner){return "{$arr[1]}({$owner['name']})";}
  31. }
  32. return $this['name'];
  33. }
  34. function getOwnerIdAttribute(){
  35. preg_match('#_([0-9]*?)$#',$this['name'],$arr);
  36. if(count($arr)>1&&$arr[1]){
  37. return $arr[1];
  38. }
  39. return '';
  40. }
  41. public static function filterRecycle(Collection $authorities)
  42. {
  43. $owners = Owner::query()->whereNotNull("deleted_at")->get();
  44. $owner_keys = $owners->map(function($owner){
  45. return '_'.$owner['id'];
  46. })->toArray();
  47. return $authorities->filter(function ($authority)use($owner_keys){
  48. return !in_array($authority->name,$owner_keys);
  49. });
  50. }
  51. }