]> BookStack Code Mirror - bookstack/blobdiff - app/helpers.php
Major permission naming refactor and database migration cleanup
[bookstack] / app / helpers.php
index f25a8f765d4e7694d92b276e36a2e4a0b96c356f..fea67a09a4c8d047ac52617a1831f5bee0ef33bf 100644 (file)
@@ -1,10 +1,10 @@
 <?php
 
-if (! function_exists('versioned_asset')) {
+if (!function_exists('versioned_asset')) {
     /**
      * Get the path to a versioned file.
      *
-     * @param  string  $file
+     * @param  string $file
      * @return string
      *
      * @throws \InvalidArgumentException
@@ -27,4 +27,35 @@ if (! function_exists('versioned_asset')) {
 
         throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
     }
-}
\ No newline at end of file
+}
+
+/**
+ * Check if the current user has a permission.
+ * If an ownable element is passed in the jointPermissions are checked against
+ * that particular item.
+ * @param $permission
+ * @param \BookStack\Ownable $ownable
+ * @return mixed
+ */
+function userCan($permission, \BookStack\Ownable $ownable = null)
+{
+    if ($ownable === null) {
+        return auth()->user() && auth()->user()->can($permission);
+    }
+
+    // Check permission on ownable item
+    $permissionService = app('BookStack\Services\PermissionService');
+    return $permissionService->checkEntityUserAccess($ownable, $permission);
+}
+
+/**
+ * Helper to access system settings.
+ * @param $key
+ * @param bool $default
+ * @return mixed
+ */
+function setting($key, $default = false)
+{
+    $settingService = app('BookStack\Services\SettingService');
+    return $settingService->get($key, $default);
+}