PermissionEnum.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Repositories\Enums;
  11. use Closure;
  12. use Illuminate\Support\Str;
  13. use Jiannei\Enum\Laravel\Contracts\LocalizedEnumContract;
  14. use Jiannei\Enum\Laravel\Enum;
  15. class PermissionEnum extends Enum implements LocalizedEnumContract
  16. {
  17. // 系统级别内置的权限:不允许分配权限给普通用户;(比如,只能超级管理员才能拥有)
  18. // const SYSTEM = '000000';
  19. // const SYSTEM_ACTIVITY_LOG_CLEAN = '000001';
  20. // const SYSTEM_CACHE_CLEAR = '000002';
  21. /**
  22. * 任务
  23. */
  24. // const controller_openAppointment_all = '100100';
  25. const controller_task_list = '100101';
  26. const controller_task_add = '100102';
  27. const controller_task_del = '100103';
  28. const controller_task_allot = '100104';
  29. const controller_task_allot_all = '100105';
  30. const model_task_all = '200100';
  31. const model_task_me = '200101';
  32. const controller_paper_list = '100201';
  33. const controller_paper_add = '100202';
  34. const controller_paper_del = '100203';
  35. const model_paper_all = '200200';
  36. const model_paper_monitor = '200201';
  37. const model_paper_me = '200202';
  38. const controller_paperResult_list = '100301';
  39. const controller_paperResult_add = '100302';
  40. const controller_paperResult_del = '100303';
  41. const controller_paperResult_answer = '100304';
  42. const model_paperResult_all = '200300';
  43. const model_paperResult_monitor = '200301';
  44. const model_paperResult_me = '200302';
  45. const controller_topic_list = '100401';
  46. const controller_topic_add = '100402';
  47. const controller_topic_del = '100403';
  48. const controller_admin_list = '100501';
  49. const controller_admin_add = '100502';
  50. const controller_admin_del = '100503';
  51. const model_admin_all = '200500';
  52. const model_admin_monitor = '200501';
  53. public $name;
  54. public $code;
  55. public $type;
  56. public $description;
  57. public function __construct($enumValue, $strict = true)
  58. {
  59. parent::__construct($enumValue, $strict);
  60. $this->name = Str::lower(str_replace('_', ':', $this->key));
  61. $this->code = $enumValue;
  62. $this->type = Str::lower(current(explode('_', $this->key)));
  63. }
  64. /**
  65. * 根据常量定义构建权限.
  66. *
  67. * @return array
  68. */
  69. public static function makePermissions(): array
  70. {
  71. $rawPermissions = self::getInstances();
  72. $permissions = [];
  73. collect($rawPermissions)->each(function ($item) use (&$permissions) {
  74. $permissions[] = [
  75. // 'name' => $item->name,
  76. 'name' => $item->value,
  77. ];
  78. });
  79. return $permissions;
  80. }
  81. /**
  82. * 前置-授权拦截检查.
  83. *
  84. * @return Closure
  85. */
  86. public static function gateBeforeCallback(): Closure
  87. {
  88. return function ($user, $ability) {
  89. return $user->isSuperAdmin() ?: null;
  90. };
  91. }
  92. }