3 use BookStack\App\Model;
4 use BookStack\Facades\Theme;
5 use BookStack\Permissions\PermissionApplicator;
6 use BookStack\Settings\SettingService;
7 use BookStack\Users\Models\User;
10 * Get the path to a versioned file.
14 function versioned_asset(string $file = ''): string
16 static $version = null;
18 if (is_null($version)) {
19 $versionFile = base_path('version');
20 $version = trim(file_get_contents($versionFile));
24 if (config('app.env') === 'development') {
25 $additional = sha1_file(public_path($file));
28 $path = $file . '?version=' . urlencode($version) . $additional;
34 * Helper method to get the current User.
35 * Defaults to public 'Guest' user if not logged in.
39 return auth()->user() ?: User::getGuest();
43 * Check if the current user has a permission. If an ownable element
44 * is passed in the jointPermissions are checked against that particular item.
46 function userCan(string $permission, Model $ownable = null): bool
48 if ($ownable === null) {
49 return user()->can($permission);
52 // Check permission on ownable item
53 $permissions = app()->make(PermissionApplicator::class);
55 return $permissions->checkOwnableUserAccess($ownable, $permission);
59 * Check if the current user can perform the given action on any items in the system.
60 * Can be provided the class name of an entity to filter ability to that specific entity type.
62 function userCanOnAny(string $action, string $entityClass = ''): bool
64 $permissions = app()->make(PermissionApplicator::class);
66 return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
70 * Helper to access system settings.
72 * @return mixed|SettingService
74 function setting(string $key = null, $default = null)
76 $settingService = app()->make(SettingService::class);
79 return $settingService;
82 return $settingService->get($key, $default);
86 * Get a path to a theme resource.
87 * Returns null if a theme is not configured and
88 * therefore a full path is not available for use.
90 function theme_path(string $path = ''): ?string
92 $theme = Theme::getTheme();
97 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));