| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- use App\Models\Role;
- if(!function_exists('getRoles')){
- function getRoles($key = null): array|string
- {
- $roles = Role::NAMES;
- 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('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);
- }
- }
|