roles.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. }
  91. }
  92. if(!function_exists('visibilityScope')) {
  93. function visibilityScope(string $module, $user = null): ?string
  94. {
  95. if(!$user) $user = auth()->user();
  96. if(!$user) return null;
  97. return $user->visibilityScope($module);
  98. }
  99. }
  100. if(!function_exists('hasVisibilityScope')) {
  101. function hasVisibilityScope(string $module, string $scope, $user = null): bool
  102. {
  103. if(!$user) $user = auth()->user();
  104. if(!$user) return false;
  105. return $user->hasVisibilityScope($module, $scope);
  106. }
  107. }
  108. if(!function_exists('canViewField')) {
  109. function canViewField(string $module, string $field, ?string $entity = null, $user = null): bool
  110. {
  111. if(!$user) $user = auth()->user();
  112. if(!$user) return false;
  113. return $user->canViewField($module, $field, $entity);
  114. }
  115. }
  116. if(!function_exists('canUpdateField')) {
  117. function canUpdateField(string $module, string $field, ?string $entity = null, $user = null): bool
  118. {
  119. if(!$user) $user = auth()->user();
  120. if(!$user) return false;
  121. return $user->canUpdateField($module, $field, $entity);
  122. }
  123. }
  124. // return current year
  125. if(!function_exists('year')) {
  126. function year(): int
  127. {
  128. return (int)session('year', date('Y'));
  129. }
  130. }
  131. if(!function_exists('glob_safe')) {
  132. function glob_safe(string $str): string
  133. {
  134. return str_replace(['[', ']', '*', '?'], ['[[]', '[]]', '[*]', '[?]'], $str);
  135. }
  136. }
  137. if(!function_exists('fileName')) {
  138. function fileName($file): string
  139. {
  140. return preg_replace('/[\\*\\:\\\\\\/\\?<>]/', '_', $file);
  141. }
  142. }