roles.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. use App\Models\Role;
  3. use Illuminate\Database\QueryException;
  4. use Illuminate\Support\Facades\Schema;
  5. if(!function_exists('getRoles')){
  6. function getRoles($key = null): array|string
  7. {
  8. $roles = Role::NAMES;
  9. try {
  10. if (Schema::hasTable('roles')) {
  11. $roles = Role::query()
  12. ->where('is_active', true)
  13. ->orderBy('sort')
  14. ->orderBy('name')
  15. ->pluck('name', 'slug')
  16. ->all() ?: $roles;
  17. }
  18. } catch (QueryException) {
  19. $roles = Role::NAMES;
  20. }
  21. if($key && isset($roles[$key])){
  22. return $roles[$key];
  23. } else {
  24. return $roles;
  25. }
  26. }
  27. }
  28. if(!function_exists('getRoleIdOptions')){
  29. function getRoleIdOptions($key = null): array|string
  30. {
  31. $roles = [];
  32. try {
  33. if (Schema::hasTable('roles')) {
  34. $roles = Role::query()
  35. ->where('is_active', true)
  36. ->orderBy('sort')
  37. ->orderBy('name')
  38. ->pluck('name', 'id')
  39. ->all();
  40. }
  41. } catch (QueryException) {
  42. $roles = [];
  43. }
  44. if (!$roles) {
  45. $roles = getRoles();
  46. }
  47. if($key && isset($roles[$key])){
  48. return $roles[$key];
  49. } else {
  50. return $roles;
  51. }
  52. }
  53. }
  54. if(!function_exists('hasRole')){
  55. function hasRole($roles, $user = null) : bool
  56. {
  57. if(!$user) $user = auth()->user();
  58. if(!$user) return false;
  59. return $user->hasRole($roles);
  60. }
  61. }
  62. if(!function_exists('roleName')) {
  63. function roleName($role): string
  64. {
  65. return Role::NAMES[$role] ?? (string) $role;
  66. }
  67. }
  68. if(!function_exists('hasPermission')) {
  69. function hasPermission(string $permission, $user = null): bool
  70. {
  71. if(!$user) $user = auth()->user();
  72. if(!$user) return false;
  73. return $user->hasPermission($permission);
  74. }
  75. }
  76. if(!function_exists('hasAnyPermission')) {
  77. function hasAnyPermission(array|string $permissions, $user = null): bool
  78. {
  79. if(!$user) $user = auth()->user();
  80. if(!$user) return false;
  81. return $user->hasAnyPermission($permissions);
  82. }
  83. }
  84. if(!function_exists('hasAccess')) {
  85. function hasAccess(string $permission, array|string|null $legacyRoles = null, $user = null): bool
  86. {
  87. if(!$user) $user = auth()->user();
  88. if(!$user) return false;
  89. return $user->hasPermission($permission)
  90. || ($legacyRoles !== null && $user->hasRole($legacyRoles));
  91. }
  92. }
  93. if(!function_exists('canViewField')) {
  94. function canViewField(string $module, string $field, ?string $entity = null, $user = null): bool
  95. {
  96. if(!$user) $user = auth()->user();
  97. if(!$user) return false;
  98. return $user->canViewField($module, $field, $entity);
  99. }
  100. }
  101. if(!function_exists('canUpdateField')) {
  102. function canUpdateField(string $module, string $field, ?string $entity = null, $user = null): bool
  103. {
  104. if(!$user) $user = auth()->user();
  105. if(!$user) return false;
  106. return $user->canUpdateField($module, $field, $entity);
  107. }
  108. }
  109. // return current year
  110. if(!function_exists('year')) {
  111. function year(): int
  112. {
  113. return (int)session('year', date('Y'));
  114. }
  115. }
  116. if(!function_exists('glob_safe')) {
  117. function glob_safe(string $str): string
  118. {
  119. return str_replace(['[', ']', '*', '?'], ['[[]', '[]]', '[*]', '[?]'], $str);
  120. }
  121. }
  122. if(!function_exists('fileName')) {
  123. function fileName($file): string
  124. {
  125. return preg_replace('/[\\*\\:\\\\\\/\\?<>]/', '_', $file);
  126. }
  127. }