AdminUser.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App;
  3. use App\AdminRole;
  4. use App\Utility\Rbac;
  5. use Illuminate\Container\Container;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Hash;
  10. class AdminUser extends Model
  11. {
  12. protected $connection = 'write';
  13. protected $fillable = ['avatar', 'nickname', 'account', 'password', 'lottery_amount', 'recharge_amount', 'type','channel','locale'];
  14. protected $hidden = ['password'];
  15. public function roles()
  16. {
  17. return $this->belongsToMany(AdminRole::class);
  18. }
  19. public function getMenus()
  20. {
  21. $roles = $this->roles;
  22. $dealTopMenuFunc = function ($topMenu) {
  23. $topMenu->hasChild = $topMenu->children->isNotEmpty();
  24. return $topMenu;
  25. };
  26. if ($this->hasSuperRole()) {
  27. $menus = AdminMenu::where('pid', 0)
  28. ->get()
  29. ->map($dealTopMenuFunc);
  30. } else {
  31. $menus = $roles
  32. ->map(function ($role) {
  33. return $role->menus()->where('pid', 0)->get();
  34. })
  35. ->collapse()
  36. ->map($dealTopMenuFunc);
  37. }
  38. return $menus;
  39. }
  40. function get_tree($array, $parent_id = 0)
  41. {
  42. $tree = [];
  43. foreach ($array as &$val) {
  44. if ($val['pid'] == $parent_id) {
  45. $val['child'] = $this->get_tree($array, $val['id']);
  46. $tree[] = $val;
  47. }
  48. }
  49. return $tree;
  50. }
  51. public function getPermissionRoutes()
  52. {
  53. if ($this->hasSuperRole()) {
  54. $permissions = Rbac::getAllRoutes()
  55. ->map(function ($route) {
  56. return $route->rbacRule;
  57. });
  58. } else {
  59. $permissions = $this->roles->map(function ($role) {
  60. return $role->permissions;
  61. })
  62. ->collapse()
  63. ->map(function ($permission) {
  64. return $permission->routes;
  65. })
  66. ->collapse();
  67. }
  68. return $permissions;
  69. }
  70. public function hasSuperRole()
  71. {
  72. $hasSuperRole = false;
  73. $this->roles->each(function ($role) use (&$hasSuperRole) {
  74. if ($role->id === 1 || $role->id === 12) {
  75. $hasSuperRole = true;
  76. return false;
  77. }
  78. });
  79. return $hasSuperRole;
  80. }
  81. public static function isExist(string $account, string $type)
  82. {
  83. $instance = new static;
  84. return $instance->where('account', $account)->where('type', $type)->count() > 0;
  85. }
  86. public function isExistForUpdate(string $account, string $type)
  87. {
  88. return $this->where('id', '!=', $this->id)
  89. ->where('account', $account)
  90. ->where('type', $type)
  91. ->count() > 0;
  92. }
  93. public function getAvatarAttribute($avatar): string
  94. {
  95. return $avatar ?? '/uploads/avatar/20181031/5bd90252493d1.jpg';
  96. }
  97. protected function setPasswordAttribute($value)
  98. {
  99. if (is_null($value) || $value === '') {
  100. return;
  101. }
  102. $this->attributes['password'] = Hash::make($value);
  103. }
  104. }