3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
6 use BookStack\Settings\SettingService;
9 * Get the path to a versioned file.
12 function versioned_asset(string $file = ''): string
14 static $version = null;
16 if (is_null($version)) {
17 $versionFile = base_path('version');
18 $version = trim(file_get_contents($versionFile));
22 if (config('app.env') === 'development') {
23 $additional = sha1_file(public_path($file));
26 $path = $file . '?version=' . urlencode($version) . $additional;
31 * Helper method to get the current User.
32 * Defaults to public 'Guest' user if not logged in.
36 return auth()->user() ?: User::getDefault();
40 * Check if current user is a signed in user.
42 function signedInUser(): bool
44 return auth()->user() && !auth()->user()->isDefault();
48 * Check if the current user has general access.
50 function hasAppAccess(): bool
52 return !auth()->guest() || setting('app-public');
56 * Check if the current user has a permission. If an ownable element
57 * is passed in the jointPermissions are checked against that particular item.
59 function userCan(string $permission, Model $ownable = null): bool
61 if ($ownable === null) {
62 return user() && user()->can($permission);
65 // Check permission on ownable item
66 $permissionService = app(PermissionService::class);
67 return $permissionService->checkOwnableUserAccess($ownable, $permission);
71 * Check if the current user has the given permission
72 * on any item in the system.
74 function userCanOnAny(string $permission, string $entityClass = null): bool
76 $permissionService = app(PermissionService::class);
77 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
81 * Helper to access system settings.
82 * @return bool|string|SettingService
84 function setting(string $key = null, $default = false)
86 $settingService = resolve(SettingService::class);
89 return $settingService;
92 return $settingService->get($key, $default);
96 * Get a path to a theme resource.
98 function theme_path(string $path = ''): string
100 $theme = config('view.theme');
106 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
110 * Get fetch an SVG icon as a string.
111 * Checks for icons defined within a custom theme before defaulting back
112 * to the 'resources/assets/icons' folder.
114 * Returns an empty string if icon file not found.
116 function icon(string $name, array $attrs = []): string
118 $attrs = array_merge([
119 'class' => 'svg-icon',
120 'data-icon' => $name,
121 'role' => 'presentation',
124 foreach ($attrs as $attrName => $attr) {
125 $attrString .= $attrName . '="' . $attr . '" ';
128 $iconPath = resource_path('icons/' . $name . '.svg');
129 $themeIconPath = theme_path('icons/' . $name . '.svg');
131 if ($themeIconPath && file_exists($themeIconPath)) {
132 $iconPath = $themeIconPath;
133 } else if (!file_exists($iconPath)) {
137 $fileContents = file_get_contents($iconPath);
138 return str_replace('<svg', '<svg' . $attrString, $fileContents);
142 * Generate a url with multiple parameters for sorting purposes.
143 * Works out the logic to set the correct sorting direction
144 * Discards empty parameters and allows overriding.
146 function sortUrl(string $path, array $data, array $overrideData = []): string
148 $queryStringSections = [];
149 $queryData = array_merge($data, $overrideData);
151 // Change sorting direction is already sorted on current attribute
152 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
153 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
154 } elseif (isset($overrideData['sort'])) {
155 $queryData['order'] = 'asc';
158 foreach ($queryData as $name => $value) {
159 $trimmedVal = trim($value);
160 if ($trimmedVal === '') {
163 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
166 if (count($queryStringSections) === 0) {
170 return url($path . '?' . implode('&', $queryStringSections));