123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace App\Models;
- use App\Traits\ModelHelpers;
- use App\Utils\Admin;
- use App\Utils\HasPermissions;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Tymon\JWTAuth\Contracts\JWTSubject;
- class AdminUser extends Authenticatable implements JWTSubject
- {
- use HasPermissions;
- use Notifiable;
- use ModelHelpers;
- protected $fillable = ['account', 'password','pid','job_number','status', 'name', 'avatar','phone','admin_id','area_id','type','is_login_app'];
- protected $hidden = ['password'];
- const TYPE_WORKER = 2;
- const TYPE_ADMIN = 1;
- public $typeMaps = [
- self::TYPE_ADMIN => "admin",
- self::TYPE_WORKER => "worker",
- ];
- const LOGIN_APP_OK = 1;
- const LOGIN_APP_NO = 0;
- public $loginAppMaps = [
- self::LOGIN_APP_NO => '不能登录运维端',
- self::LOGIN_APP_OK => '能登录运维端',
- ];
- const TYPE_STATUS_OK = 1;
- const TYPE_STATUS_NO = 0;
- public $statusMaps = [
- self::TYPE_STATUS_NO => '禁用',
- self::TYPE_STATUS_OK => '正常',
- ];
- public function getJWTIdentifier()
- {
- return $this->getKey();
- }
- /**
- * 额外在 JWT 载荷中增加的自定义内容
- *
- * @return array
- */
- public function getJWTCustomClaims()
- {
- return [];
- }
- public function area(){
- // 此处为地勤人员关联区域
- return $this->belongsTo(Area::class,'area_id','id');
- }
- public function areas(){
- // 此处为管理员关联区域
- return $this->belongsToMany(Area::class,'admin_area','admin_id','area_id');
- }
- public function roles()
- {
- return $this->belongsToMany(
- AdminRole::class,
- 'admin_user_role',
- 'user_id',
- 'role_id'
- );
- }
- public function permissions()
- {
- return $this->belongsToMany(
- AdminPermission::class,
- 'admin_user_permission',
- 'user_id',
- 'permission_id'
- );
- }
- /**
- * 从请求数据中添加用户
- *
- * @param array $inputs
- * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
- *
- * @return AdminUser|\Illuminate\Database\Eloquent\Model
- */
- public static function createUser($inputs, $hashedPassword = false)
- {
- if (!$hashedPassword) {
- $inputs['password'] = bcrypt($inputs['password']);
- }
- return static::create($inputs);
- }
- /**
- * 从请求数据中, 更新一条记录
- *
- * @param array $inputs
- * @param bool $hashedPassword 传入的密码, 是否是没有哈希处理的明文密码
- *
- * @return bool
- */
- public function updateUser($inputs, $hashedPassword = false)
- {
- // 更新时, 填了密码, 且没有经过哈希处理
- if (
- isset($inputs['password']) &&
- !$hashedPassword
- ) {
- $inputs['password'] = bcrypt($inputs['password']);
- }
- return $this->update($inputs);
- }
- public function getPersIds(){
- return $this->allPermissions()->pluck('id');
- }
- /**
- * getAreaIdsByAdminId 根据管理员id 查询拥有哪些区域
- *
- * @param $admin_id
- * @return array
- * @author Fx
- *
- */
- public static function getAreaIdsByAdminId($admin_id){
- if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) {
- return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
- }
- $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
- if (in_array('99999', $area_ids1)) {
- $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
- return $area_ids;
- }
- $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
- $area_ids = array_merge($area_ids1, $area_ids2);
- $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
- $area_ids = array_diff($area_ids, $area_ids_no);
- return $area_ids;
- }
- // public static function getAreaIdsByAdminId($admin_id){
- // if (Admin::isAdministrator() || Admin::isNormalAdministrator()) {
- // $area_ids = Area::query()->pluck('id')->toArray();
- // return $area_ids;
- // }
- // $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
- // if (in_array('99999', $area_ids1)) {
- // $area_ids = Area::query()->pluck('id')->toArray();
- // return $area_ids;
- // }
- // $area_ids2 = Area::query()->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
- // $area_ids = array_merge($area_ids1, $area_ids2);
- // $area_ids_no = Area::query()->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
- // $area_ids = array_diff($area_ids, $area_ids_no);
- // return $area_ids;
- //
- // }
- /**
- * 获取区域ids saas
- * @param $admin_id
- * @return array
- */
- public static function getAreaIdsByAdminIdMch($admin_id){
- if (Admin::isAdministrator() || Admin::user()->isRole(AdminRole::MERCHANTSADMIN) || Admin::isNormalAdministrator()) {
- return Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
- }
- $area_ids1 = AdminUserArea::query()->where('admin_id', $admin_id)->pluck('area_id')->toArray(); // 管理员拥有那些区域权限ids
- if (in_array('99999', $area_ids1)) {
- $area_ids = Area::query()->where(AdminMerchant::getMerchantWhere())->pluck('id')->toArray();
- return $area_ids;
- }
- $area_ids2 = Area::query()->where(AdminMerchant::getMerchantWhere())->where('admin_id', $admin_id)->pluck('id')->toArray(); // 区域创建者ids
- $area_ids = array_merge($area_ids1, $area_ids2);
- $area_ids_no = Area::query()->where(AdminMerchant::getMerchantWhere())->where('status', Area::STATUS_PAUSE)->pluck('id')->toArray(); // 禁用的区域ids
- $area_ids = array_diff($area_ids, $area_ids_no);
- return $area_ids;
- }
- /**
- * 获取所有区域管理员
- * */
- public static function getAreaAdmin(){
- return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
- $q->where('slug',AdminRole::AREA_ADMIN);
- })->get();
- }
- /**
- * 获取所有超级管理员
- * */
- public static function getAAdmin(){
- return self::query()->where('type',self::TYPE_ADMIN)->whereHas('roles',function($q){
- $q->where('slug',AdminRole::ADMINISTROTOR);
- })->get();
- }
- }
|