3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
6 use BookStack\Settings\SettingService;
9 * Get the path to a versioned file.
15 function versioned_asset(string $file = ''): string
17 static $version = null;
19 if (is_null($version)) {
20 $versionFile = base_path('version');
21 $version = trim(file_get_contents($versionFile));
25 if (config('app.env') === 'development') {
26 $additional = sha1_file(public_path($file));
29 $path = $file . '?version=' . urlencode($version) . $additional;
34 * Helper method to get the current User.
35 * Defaults to public 'Guest' user if not logged in.
40 return auth()->user() ?: User::getDefault();
44 * Check if current user is a signed in user.
46 function signedInUser(): bool
48 return auth()->user() && !auth()->user()->isDefault();
52 * Check if the current user has general access.
54 function hasAppAccess(): bool
56 return !auth()->guest() || setting('app-public');
60 * Check if the current user has a permission.
61 * If an ownable element is passed in the jointPermissions are checked against
62 * that particular item.
64 function userCan(string $permission, Ownable $ownable = null): bool
66 if ($ownable === null) {
67 return user() && user()->can($permission);
70 // Check permission on ownable item
71 $permissionService = app(PermissionService::class);
72 return $permissionService->checkOwnableUserAccess($ownable, $permission);
76 * Check if the current user has the given permission
77 * on any item in the system.
78 * @param string $permission
79 * @param string|null $entityClass
82 function userCanOnAny(string $permission, string $entityClass = null): bool
84 $permissionService = app(PermissionService::class);
85 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
89 * Helper to access system settings.
92 * @return bool|string|SettingService
94 function setting(string $key = null, $default = false)
96 $settingService = resolve(SettingService::class);
98 return $settingService;
100 return $settingService->get($key, $default);
104 * Get a path to a theme resource.
105 * @param string $path
108 function theme_path(string $path = ''): string
110 $theme = config('view.theme');
115 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
119 * Get fetch an SVG icon as a string.
120 * Checks for icons defined within a custom theme before defaulting back
121 * to the 'resources/assets/icons' folder.
123 * Returns an empty string if icon file not found.
125 * @param array $attrs
128 function icon(string $name, array $attrs = []): string
130 $attrs = array_merge([
131 'class' => 'svg-icon',
132 'data-icon' => $name,
133 'role' => 'presentation',
136 foreach ($attrs as $attrName => $attr) {
137 $attrString .= $attrName . '="' . $attr . '" ';
140 $iconPath = resource_path('icons/' . $name . '.svg');
141 $themeIconPath = theme_path('icons/' . $name . '.svg');
142 if ($themeIconPath && file_exists($themeIconPath)) {
143 $iconPath = $themeIconPath;
144 } else if (!file_exists($iconPath)) {
148 $fileContents = file_get_contents($iconPath);
149 return str_replace('<svg', '<svg' . $attrString, $fileContents);
153 * Generate a url with multiple parameters for sorting purposes.
154 * Works out the logic to set the correct sorting direction
155 * Discards empty parameters and allows overriding.
156 * @param string $path
158 * @param array $overrideData
161 function sortUrl(string $path, array $data, array $overrideData = []): string
163 $queryStringSections = [];
164 $queryData = array_merge($data, $overrideData);
166 // Change sorting direction is already sorted on current attribute
167 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
168 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
170 $queryData['order'] = 'asc';
173 foreach ($queryData as $name => $value) {
174 $trimmedVal = trim($value);
175 if ($trimmedVal === '') {
178 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
181 if (count($queryStringSections) === 0) {
185 return url($path . '?' . implode('&', $queryStringSections));