3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
6 use BookStack\Settings\SettingService;
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::getDefault();
42 * Check if current user is a signed in user.
44 function signedInUser(): bool
46 return auth()->user() && !auth()->user()->isDefault();
50 * Check if the current user has general access.
52 function hasAppAccess(): bool
54 return !auth()->guest() || setting('app-public');
58 * Check if the current user has a permission. If an ownable element
59 * is passed in the jointPermissions are checked against that particular item.
61 function userCan(string $permission, Model $ownable = null): bool
63 if ($ownable === null) {
64 return user() && user()->can($permission);
67 // Check permission on ownable item
68 $permissionService = app(PermissionService::class);
70 return $permissionService->checkOwnableUserAccess($ownable, $permission);
74 * Check if the current user has the given permission
75 * on any item in the system.
77 function userCanOnAny(string $permission, string $entityClass = null): bool
79 $permissionService = app(PermissionService::class);
81 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
85 * Helper to access system settings.
87 * @return mixed|SettingService
89 function setting(string $key = null, $default = null)
91 $settingService = resolve(SettingService::class);
94 return $settingService;
97 return $settingService->get($key, $default);
101 * Get a path to a theme resource.
102 * Returns null if a theme is not configured and
103 * therefore a full path is not available for use.
105 function theme_path(string $path = ''): ?string
107 $theme = config('view.theme');
113 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
117 * Get fetch an SVG icon as a string.
118 * Checks for icons defined within a custom theme before defaulting back
119 * to the 'resources/assets/icons' folder.
121 * Returns an empty string if icon file not found.
123 function icon(string $name, array $attrs = []): string
125 $attrs = array_merge([
126 'class' => 'svg-icon',
127 'data-icon' => $name,
128 'role' => 'presentation',
131 foreach ($attrs as $attrName => $attr) {
132 $attrString .= $attrName . '="' . $attr . '" ';
135 $iconPath = resource_path('icons/' . $name . '.svg');
136 $themeIconPath = theme_path('icons/' . $name . '.svg');
138 if ($themeIconPath && file_exists($themeIconPath)) {
139 $iconPath = $themeIconPath;
140 } elseif (!file_exists($iconPath)) {
144 $fileContents = file_get_contents($iconPath);
146 return str_replace('<svg', '<svg' . $attrString, $fileContents);
150 * Generate a url with multiple parameters for sorting purposes.
151 * Works out the logic to set the correct sorting direction
152 * Discards empty parameters and allows overriding.
154 function sortUrl(string $path, array $data, array $overrideData = []): string
156 $queryStringSections = [];
157 $queryData = array_merge($data, $overrideData);
159 // Change sorting direction is already sorted on current attribute
160 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
161 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
162 } elseif (isset($overrideData['sort'])) {
163 $queryData['order'] = 'asc';
166 foreach ($queryData as $name => $value) {
167 $trimmedVal = trim($value);
168 if ($trimmedVal === '') {
171 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
174 if (count($queryStringSections) === 0) {
178 return url($path . '?' . implode('&', $queryStringSections));