3 use BookStack\App\AppVersion;
4 use BookStack\App\Model;
5 use BookStack\Facades\Theme;
6 use BookStack\Permissions\Permission;
7 use BookStack\Permissions\PermissionApplicator;
8 use BookStack\Settings\SettingService;
9 use BookStack\Users\Models\User;
12 * Get the path to a versioned file.
16 function versioned_asset(string $file = ''): string
18 $version = AppVersion::get();
21 if (config('app.env') === 'development') {
22 $additional = sha1_file(public_path($file));
25 $path = $file . '?version=' . urlencode($version) . $additional;
31 * Helper method to get the current User.
32 * Defaults to public 'Guest' user if not logged in.
36 return auth()->user() ?: User::getGuest();
40 * Check if the current user has a permission. If an ownable element
41 * is passed in the jointPermissions are checked against that particular item.
43 function userCan(string|Permission $permission, ?Model $ownable = null): bool
45 if (is_null($ownable)) {
46 return user()->can($permission);
49 // Check permission on ownable item
50 $permissions = app()->make(PermissionApplicator::class);
52 return $permissions->checkOwnableUserAccess($ownable, $permission);
56 * Check if the current user can perform the given action on any items in the system.
57 * Can be provided the class name of an entity to filter ability to that specific entity type.
59 function userCanOnAny(string|Permission $action, string $entityClass = ''): bool
61 $permissions = app()->make(PermissionApplicator::class);
63 return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
67 * Helper to access system settings.
69 * @return mixed|SettingService
71 function setting(?string $key = null, mixed $default = null): mixed
73 $settingService = app()->make(SettingService::class);
76 return $settingService;
79 return $settingService->get($key, $default);
83 * Get a path to a theme resource.
84 * Returns null if a theme is not configured and
85 * therefore a full path is not available for use.
87 function theme_path(string $path = ''): ?string
89 $theme = Theme::getTheme();
94 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));