]> BookStack Code Mirror - bookstack/blob - app/App/helpers.php
Merge pull request #5626 from BookStackApp/rubentalstra-development
[bookstack] / app / App / helpers.php
1 <?php
2
3 use BookStack\App\AppVersion;
4 use BookStack\App\Model;
5 use BookStack\Facades\Theme;
6 use BookStack\Permissions\PermissionApplicator;
7 use BookStack\Settings\SettingService;
8 use BookStack\Users\Models\User;
9
10 /**
11  * Get the path to a versioned file.
12  *
13  * @throws Exception
14  */
15 function versioned_asset(string $file = ''): string
16 {
17     $version = AppVersion::get();
18
19     $additional = '';
20     if (config('app.env') === 'development') {
21         $additional = sha1_file(public_path($file));
22     }
23
24     $path = $file . '?version=' . urlencode($version) . $additional;
25
26     return url($path);
27 }
28
29 /**
30  * Helper method to get the current User.
31  * Defaults to public 'Guest' user if not logged in.
32  */
33 function user(): User
34 {
35     return auth()->user() ?: User::getGuest();
36 }
37
38 /**
39  * Check if the current user has a permission. If an ownable element
40  * is passed in the jointPermissions are checked against that particular item.
41  */
42 function userCan(string $permission, ?Model $ownable = null): bool
43 {
44     if (is_null($ownable)) {
45         return user()->can($permission);
46     }
47
48     // Check permission on ownable item
49     $permissions = app()->make(PermissionApplicator::class);
50
51     return $permissions->checkOwnableUserAccess($ownable, $permission);
52 }
53
54 /**
55  * Check if the current user can perform the given action on any items in the system.
56  * Can be provided the class name of an entity to filter ability to that specific entity type.
57  */
58 function userCanOnAny(string $action, string $entityClass = ''): bool
59 {
60     $permissions = app()->make(PermissionApplicator::class);
61
62     return $permissions->checkUserHasEntityPermissionOnAny($action, $entityClass);
63 }
64
65 /**
66  * Helper to access system settings.
67  *
68  * @return mixed|SettingService
69  */
70 function setting(?string $key = null, mixed $default = null): mixed
71 {
72     $settingService = app()->make(SettingService::class);
73
74     if (is_null($key)) {
75         return $settingService;
76     }
77
78     return $settingService->get($key, $default);
79 }
80
81 /**
82  * Get a path to a theme resource.
83  * Returns null if a theme is not configured and
84  * therefore a full path is not available for use.
85  */
86 function theme_path(string $path = ''): ?string
87 {
88     $theme = Theme::getTheme();
89     if (!$theme) {
90         return null;
91     }
92
93     return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
94 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.