| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace App;
- use App\AdminRole;
- use App\Utility\Rbac;
- use Illuminate\Container\Container;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- class AdminUser extends Model
- {
- protected $connection = 'write';
- protected $fillable = ['avatar', 'nickname', 'account', 'password', 'lottery_amount', 'recharge_amount', 'type','channel','locale'];
- protected $hidden = ['password'];
- public function roles()
- {
- return $this->belongsToMany(AdminRole::class);
- }
- public function getMenus()
- {
- $roles = $this->roles;
- $dealTopMenuFunc = function ($topMenu) {
- $topMenu->hasChild = $topMenu->children->isNotEmpty();
- return $topMenu;
- };
- if ($this->hasSuperRole()) {
- $menus = AdminMenu::where('pid', 0)
- ->get()
- ->map($dealTopMenuFunc);
- } else {
- $menus = $roles
- ->map(function ($role) {
- return $role->menus()->where('pid', 0)->get();
- })
- ->collapse()
- ->map($dealTopMenuFunc);
- }
- return $menus;
- }
- function get_tree($array, $parent_id = 0)
- {
- $tree = [];
- foreach ($array as &$val) {
- if ($val['pid'] == $parent_id) {
- $val['child'] = $this->get_tree($array, $val['id']);
- $tree[] = $val;
- }
- }
- return $tree;
- }
- public function getPermissionRoutes()
- {
- if ($this->hasSuperRole()) {
- $permissions = Rbac::getAllRoutes()
- ->map(function ($route) {
- return $route->rbacRule;
- });
- } else {
- $permissions = $this->roles->map(function ($role) {
- return $role->permissions;
- })
- ->collapse()
- ->map(function ($permission) {
- return $permission->routes;
- })
- ->collapse();
- }
- return $permissions;
- }
- public function hasSuperRole()
- {
- $hasSuperRole = false;
- $this->roles->each(function ($role) use (&$hasSuperRole) {
- if ($role->id === 1 || $role->id === 12) {
- $hasSuperRole = true;
- return false;
- }
- });
- return $hasSuperRole;
- }
- public static function isExist(string $account, string $type)
- {
- $instance = new static;
- return $instance->where('account', $account)->where('type', $type)->count() > 0;
- }
- public function isExistForUpdate(string $account, string $type)
- {
- return $this->where('id', '!=', $this->id)
- ->where('account', $account)
- ->where('type', $type)
- ->count() > 0;
- }
- public function getAvatarAttribute($avatar): string
- {
- return $avatar ?? '/uploads/avatar/20181031/5bd90252493d1.jpg';
- }
- protected function setPasswordAttribute($value)
- {
- if (is_null($value) || $value === '') {
- return;
- }
- $this->attributes['password'] = Hash::make($value);
- }
- }
|