| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- use App\Models\Role;
- use Illuminate\Database\QueryException;
- use Illuminate\Support\Facades\Schema;
- if(!function_exists('getRoles')){
- function getRoles($key = null): array|string
- {
- $roles = Role::NAMES;
- try {
- if (Schema::hasTable('roles')) {
- $roles = Role::query()
- ->where('is_active', true)
- ->orderBy('sort')
- ->orderBy('name')
- ->pluck('name', 'slug')
- ->all() ?: $roles;
- }
- } catch (QueryException) {
- $roles = Role::NAMES;
- }
- if($key && isset($roles[$key])){
- return $roles[$key];
- } else {
- return $roles;
- }
- }
- }
- if(!function_exists('getRoleIdOptions')){
- function getRoleIdOptions($key = null): array|string
- {
- $roles = [];
- try {
- if (Schema::hasTable('roles')) {
- $roles = Role::query()
- ->where('is_active', true)
- ->orderBy('sort')
- ->orderBy('name')
- ->pluck('name', 'id')
- ->all();
- }
- } catch (QueryException) {
- $roles = [];
- }
- if (!$roles) {
- $roles = getRoles();
- }
- if($key && isset($roles[$key])){
- return $roles[$key];
- } else {
- return $roles;
- }
- }
- }
- if(!function_exists('hasRole')){
- function hasRole($roles, $user = null) : bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->hasRole($roles);
- }
- }
- if(!function_exists('roleName')) {
- function roleName($role): string
- {
- return Role::NAMES[$role] ?? (string) $role;
- }
- }
- if(!function_exists('hasPermission')) {
- function hasPermission(string $permission, $user = null): bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->hasPermission($permission);
- }
- }
- if(!function_exists('hasAnyPermission')) {
- function hasAnyPermission(array|string $permissions, $user = null): bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->hasAnyPermission($permissions);
- }
- }
- if(!function_exists('hasAccess')) {
- function hasAccess(string $permission, array|string|null $legacyRoles = null, $user = null): bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->hasPermission($permission)
- || ($legacyRoles !== null && $user->hasRole($legacyRoles));
- }
- }
- if(!function_exists('canViewField')) {
- function canViewField(string $module, string $field, ?string $entity = null, $user = null): bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->canViewField($module, $field, $entity);
- }
- }
- if(!function_exists('canUpdateField')) {
- function canUpdateField(string $module, string $field, ?string $entity = null, $user = null): bool
- {
- if(!$user) $user = auth()->user();
- if(!$user) return false;
- return $user->canUpdateField($module, $field, $entity);
- }
- }
- // return current year
- if(!function_exists('year')) {
- function year(): int
- {
- return (int)session('year', date('Y'));
- }
- }
- if(!function_exists('glob_safe')) {
- function glob_safe(string $str): string
- {
- return str_replace(['[', ']', '*', '?'], ['[[]', '[]]', '[*]', '[?]'], $str);
- }
- }
- if(!function_exists('fileName')) {
- function fileName($file): string
- {
- return preg_replace('/[\\*\\:\\\\\\/\\?<>]/', '_', $file);
- }
- }
|