3 use BookStack\App\AppVersion;
4 use BookStack\App\Model;
5 use BookStack\Facades\Theme;
6 use BookStack\Permissions\PermissionApplicator;
7 use BookStack\Settings\SettingService;
8 use BookStack\Users\Models\User;
11 * Get the path to a versioned file.
15 function versioned_asset(string $file = ''): string
17 $version = AppVersion::get();
20 if (config('app.env') === 'development') {
21 $additional = sha1_file(public_path($file));
24 $path = $file . '?version=' . urlencode($version) . $additional;
30 * Helper method to get the current User.
31 * Defaults to public 'Guest' user if not logged in.
35 return auth()->user() ?: User::getGuest();
39 * Check if the current user has a permission. If an ownable element
40 * is passed in the jointPermissions are checked against that particular item.
42 function userCan(string $permission, ?Model $ownable = null): bool
44 if (is_null($ownable)) {
45 return user()->can($permission);
48 // Check permission on ownable item
49 $permissions = app()->make(PermissionApplicator::class);
51 return $permissions->checkOwnableUserAccess($ownable, $permission);
55 * Check if the current user can perform the given action on any items in the system.
56 * Can be provided the class name of an entity to filter ability to that specific entity type.
58 function userCanOnAny(string $action, string $entityClass = ''): bool
60 $permissions = app()->make(PermissionApplicator::class);
62 return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
66 * Helper to access system settings.
68 * @return mixed|SettingService
70 function setting(?string $key = null, mixed $default = null): mixed
72 $settingService = app()->make(SettingService::class);
75 return $settingService;
78 return $settingService->get($key, $default);
82 * Get a path to a theme resource.
83 * Returns null if a theme is not configured and
84 * therefore a full path is not available for use.
86 function theme_path(string $path = ''): ?string
88 $theme = Theme::getTheme();
93 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));