3 use BookStack\Auth\Permissions\PermissionApplicator;
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.
59 * Checks a generic role permission or, if an ownable model is passed in, it will
60 * check against the given entity model, taking into account entity-level permissions.
62 function userCan(string $permission, Model $ownable = null): bool
64 if ($ownable === null) {
65 return user() && user()->can($permission);
68 // Check permission on ownable item
69 $permissions = app(PermissionApplicator::class);
71 return $permissions->checkOwnableUserAccess($ownable, $permission);
75 * Check if the current user can perform the given action on any items in the system.
76 * Can be provided the class name of an entity to filter ability to that specific entity type.
78 function userCanOnAny(string $action, string $entityClass = ''): bool
80 $permissions = app(PermissionApplicator::class);
82 return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
86 * Helper to access system settings.
88 * @return mixed|SettingService
90 function setting(string $key = null, $default = null)
92 $settingService = resolve(SettingService::class);
95 return $settingService;
98 return $settingService->get($key, $default);
102 * Get a path to a theme resource.
103 * Returns null if a theme is not configured and
104 * therefore a full path is not available for use.
106 function theme_path(string $path = ''): ?string
108 $theme = config('view.theme');
114 return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
118 * Get fetch an SVG icon as a string.
119 * Checks for icons defined within a custom theme before defaulting back
120 * to the 'resources/assets/icons' folder.
122 * Returns an empty string if icon file not found.
124 function icon(string $name, array $attrs = []): string
126 $attrs = array_merge([
127 'class' => 'svg-icon',
128 'data-icon' => $name,
129 'role' => 'presentation',
132 foreach ($attrs as $attrName => $attr) {
133 $attrString .= $attrName . '="' . $attr . '" ';
136 $iconPath = resource_path('icons/' . $name . '.svg');
137 $themeIconPath = theme_path('icons/' . $name . '.svg');
139 if ($themeIconPath && file_exists($themeIconPath)) {
140 $iconPath = $themeIconPath;
141 } elseif (!file_exists($iconPath)) {
145 $fileContents = file_get_contents($iconPath);
147 return str_replace('<svg', '<svg' . $attrString, $fileContents);
151 * Generate a url with multiple parameters for sorting purposes.
152 * Works out the logic to set the correct sorting direction
153 * Discards empty parameters and allows overriding.
155 function sortUrl(string $path, array $data, array $overrideData = []): string
157 $queryStringSections = [];
158 $queryData = array_merge($data, $overrideData);
160 // Change sorting direction is already sorted on current attribute
161 if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) {
162 $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc';
163 } elseif (isset($overrideData['sort'])) {
164 $queryData['order'] = 'asc';
167 foreach ($queryData as $name => $value) {
168 $trimmedVal = trim($value);
169 if ($trimmedVal === '') {
172 $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal);
175 if (count($queryStringSections) === 0) {
179 return url($path . '?' . implode('&', $queryStringSections));