3 use BookStack\App\Model;
4 use BookStack\Permissions\PermissionApplicator;
5 use BookStack\Settings\SettingService;
6 use BookStack\Users\Models\User;
9 * Get the path to a versioned file.
13 function versioned_asset(string $file = ''): string
15 static $version = null;
17 if (is_null($version)) {
18 $versionFile = base_path('version');
19 $version = trim(file_get_contents($versionFile));
23 if (config('app.env') === 'development') {
24 $additional = sha1_file(public_path($file));
27 $path = $file . '?version=' . urlencode($version) . $additional;
33 * Helper method to get the current User.
34 * Defaults to public 'Guest' user if not logged in.
38 return auth()->user() ?: User::getGuest();
42 * Check if the current user has a permission. If an ownable element
43 * is passed in the jointPermissions are checked against that particular item.
45 function userCan(string $permission, Model $ownable = null): bool
47 if ($ownable === null) {
48 return user()->can($permission);
51 // Check permission on ownable item
52 $permissions = app()->make(PermissionApplicator::class);
54 return $permissions->checkOwnableUserAccess($ownable, $permission);
58 * Check if the current user can perform the given action on any items in the system.
59 * Can be provided the class name of an entity to filter ability to that specific entity type.
61 function userCanOnAny(string $action, string $entityClass = ''): bool
63 $permissions = app()->make(PermissionApplicator::class);
65 return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
69 * Helper to access system settings.
71 * @return mixed|SettingService
73 function setting(string $key = null, $default = null)
75 $settingService = app()->make(SettingService::class);
78 return $settingService;
81 return $settingService->get($key, $default);
85 * Get a path to a theme resource.
86 * Returns null if a theme is not configured and
87 * therefore a full path is not available for use.
89 function theme_path(string $path = ''): ?string
91 $theme = config('view.theme');
97 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
101 * Generate a URL with multiple parameters for sorting purposes.
102 * Works out the logic to set the correct sorting direction
103 * Discards empty parameters and allows overriding.
105 function sortUrl(string $path, array $data, array $overrideData = []): string
107 $queryStringSections = [];
108 $queryData = array_merge($data, $overrideData);
110 // Change sorting direction is already sorted on current attribute
111 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
112 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
113 } elseif (isset($overrideData['sort'])) {
114 $queryData['order'] = 'asc';
117 foreach ($queryData as $name => $value) {
118 $trimmedVal = trim($value);
119 if ($trimmedVal === '') {
122 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
125 if (count($queryStringSections) === 0) {
129 return url($path . '?' . implode('&', $queryStringSections));