SystemRole.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\system;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. /**
  15. * 身份管理 model
  16. * Class SystemRole
  17. * @package app\admin\model\system
  18. */
  19. class SystemRole extends ModelBasic
  20. {
  21. use ModelTrait;
  22. public static function setRulesAttr($value)
  23. {
  24. return is_array($value) ? implode(',',$value) : $value;
  25. }
  26. /**
  27. * 选择管理员身份
  28. * @param int $level
  29. * @param string $field
  30. * @return array
  31. */
  32. public static function getRole($level = 0 ,$field='id,role_name')
  33. {
  34. return self::where('status',1)->where('level','=',$level)->column($field);
  35. }
  36. public static function rolesByAuth($rules)
  37. {
  38. if(empty($rules)) return [];
  39. $rules = self::where('id','IN',$rules)->where('status','1')->column('rules');
  40. $rules = array_unique(explode(',',implode(',',$rules)));
  41. $_auth = SystemMenus::all(function($query) use($rules){
  42. $query->where('id','IN',$rules)
  43. ->where('controller|action','<>','')
  44. ->field('module,controller,action,params');
  45. });
  46. return self::tidyAuth($_auth?:[]);
  47. }
  48. public static function getAllAuth()
  49. {
  50. static $auth = null;
  51. $auth === null && ($auth = self::tidyAuth(SystemMenus::all(function($query){
  52. $query->where('controller|action','<>','')->field('module,controller,action,params');
  53. })?:[]));
  54. return $auth;
  55. }
  56. protected static function tidyAuth($_auth)
  57. {
  58. $auth = [];
  59. foreach ($_auth as $k=>$val){
  60. $auth[] = SystemMenus::getAuthName($val['action'],$val['controller'],$val['module'],$val['params']);
  61. }
  62. return $auth;
  63. }
  64. public static function systemPage($where){
  65. $model = new self;
  66. if($where['role_name'] != '') $model = $model->where('role_name','LIKE',"%$where[role_name]%");
  67. if($where['status'] != '') $model = $model->where('status',$where['status']);
  68. $model->where('level','=',bcadd($where['level'],1,0));
  69. return self::page($model,(function($item,$key){
  70. $item->rules = SystemMenus::where('id','IN',$item->rules)->column('menu_name');
  71. }),$where);
  72. }
  73. }