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($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.
38 function user() : User
40 return auth()->user() ?: User::getDefault();
44 * Check if current user is a signed in user.
47 function signedInUser() : bool
49 return auth()->user() && !auth()->user()->isDefault();
53 * Check if the current user has general access.
56 function hasAppAccess() : bool
58 return !auth()->guest() || setting('app-public');
62 * Check if the current user has a permission.
63 * If an ownable element is passed in the jointPermissions are checked against
64 * that particular item.
65 * @param string $permission
66 * @param Ownable $ownable
69 function userCan(string $permission, Ownable $ownable = null) : bool
71 if ($ownable === null) {
72 return user() && user()->can($permission);
75 // Check permission on ownable item
76 $permissionService = app(PermissionService::class);
77 return $permissionService->checkOwnableUserAccess($ownable, $permission);
81 * Check if the current user has the given permission
82 * on any item in the system.
83 * @param string $permission
84 * @param string|null $entityClass
87 function userCanOnAny(string $permission, string $entityClass = null) : bool
89 $permissionService = app(PermissionService::class);
90 return $permissionService->checkUserHasPermissionOnAnything($permission, $entityClass);
94 * Helper to access system settings.
96 * @param bool $default
97 * @return bool|string|SettingService
99 function setting($key = null, $default = false)
101 $settingService = resolve(SettingService::class);
103 return $settingService;
105 return $settingService->get($key, $default);
109 * Get a path to a theme resource.
110 * @param string $path
113 function theme_path($path = '') : string
115 $theme = config('view.theme');
120 return base_path('themes/' . $theme .($path ? DIRECTORY_SEPARATOR.$path : $path));
124 * Get fetch an SVG icon as a string.
125 * Checks for icons defined within a custom theme before defaulting back
126 * to the 'resources/assets/icons' folder.
128 * Returns an empty string if icon file not found.
130 * @param array $attrs
133 function icon($name, $attrs = [])
135 $attrs = array_merge([
136 'class' => 'svg-icon',
137 'data-icon' => $name,
138 'role' => 'presentation',
141 foreach ($attrs as $attrName => $attr) {
142 $attrString .= $attrName . '="' . $attr . '" ';
145 $iconPath = resource_path('assets/icons/' . $name . '.svg');
146 $themeIconPath = theme_path('icons/' . $name . '.svg');
147 if ($themeIconPath && file_exists($themeIconPath)) {
148 $iconPath = $themeIconPath;
149 } else if (!file_exists($iconPath)) {
153 $fileContents = file_get_contents($iconPath);
154 return str_replace('<svg', '<svg' . $attrString, $fileContents);
158 * Generate a url with multiple parameters for sorting purposes.
159 * Works out the logic to set the correct sorting direction
160 * Discards empty parameters and allows overriding.
163 * @param array $overrideData
166 function sortUrl($path, $data, $overrideData = [])
168 $queryStringSections = [];
169 $queryData = array_merge($data, $overrideData);
171 // Change sorting direction is already sorted on current attribute
172 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
173 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
175 $queryData['order'] = 'asc';
178 foreach ($queryData as $name => $value) {
179 $trimmedVal = trim($value);
180 if ($trimmedVal === '') {
183 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
186 if (count($queryStringSections) === 0) {
190 return url($path . '?' . implode('&', $queryStringSections));