]> BookStack Code Mirror - bookstack/blobdiff - app/Settings/SettingService.php
Updated styles to use logical properties/values
[bookstack] / app / Settings / SettingService.php
index c9491e3eeb4b4c949248680b9d43b1a0e45c3f59..1c053b3848ea779d480adea01834dce0059d629d 100644 (file)
@@ -4,10 +4,8 @@ use Illuminate\Contracts\Cache\Repository as Cache;
 
 /**
  * Class SettingService
- *
  * The settings are a simple key-value database store.
- *
- * @package BookStack\Services
+ * For non-authenticated users, user settings are stored via the session instead.
  */
 class SettingService
 {
@@ -52,6 +50,19 @@ class SettingService
         return $formatted;
     }
 
+    /**
+     * Get a value from the session instead of the main store option.
+     * @param $key
+     * @param bool $default
+     * @return mixed
+     */
+    protected function getFromSession($key, $default = false)
+    {
+        $value = session()->get($key, $default);
+        $formatted = $this->formatValue($value, $default);
+        return $formatted;
+    }
+
     /**
      * Get a user-specific setting from the database or cache.
      * @param \BookStack\Auth\User $user
@@ -61,9 +72,23 @@ class SettingService
      */
     public function getUser($user, $key, $default = false)
     {
+        if ($user->isDefault()) {
+            return $this->getFromSession($key, $default);
+        }
         return $this->get($this->userKey($user->id, $key), $default);
     }
 
+    /**
+     * Get a value for the current logged-in user.
+     * @param $key
+     * @param bool $default
+     * @return bool|string
+     */
+    public function getForCurrentUser($key, $default = false)
+    {
+        return $this->getUser(user(), $key, $default);
+    }
+
     /**
      * Gets a setting value from the cache or database.
      * Looks at the system defaults if not cached or in database.
@@ -73,12 +98,6 @@ class SettingService
      */
     protected function getValueFromStore($key, $default)
     {
-        // Check for an overriding value
-        $overrideValue = $this->getOverrideValue($key);
-        if ($overrideValue !== null) {
-            return $overrideValue;
-        }
-
         // Check the cache
         $cacheKey = $this->cachePrefix . $key;
         $cacheVal = $this->cache->get($cacheKey, null);
@@ -180,6 +199,9 @@ class SettingService
      */
     public function putUser($user, $key, $value)
     {
+        if ($user->isDefault()) {
+            return session()->put($key, $value);
+        }
         return $this->put($this->userKey($user->id, $key), $value);
     }
 
@@ -227,20 +249,4 @@ class SettingService
     {
         return $this->setting->where('setting_key', '=', $key)->first();
     }
-
-
-    /**
-     * Returns an override value for a setting based on certain app conditions.
-     * Used where certain configuration options overrule others.
-     * Returns null if no override value is available.
-     * @param $key
-     * @return bool|null
-     */
-    protected function getOverrideValue($key)
-    {
-        if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {
-            return false;
-        }
-        return null;
-    }
 }