1 <?php namespace BookStack\Settings;
3 use Illuminate\Contracts\Cache\Repository as Cache;
7 * The settings are a simple key-value database store.
8 * For non-authenticated users, user settings are stored via the session instead.
15 protected $localCache = [];
17 protected $cachePrefix = 'setting-';
20 * SettingService constructor.
21 * @param Setting $setting
24 public function __construct(Setting $setting, Cache $cache)
26 $this->setting = $setting;
27 $this->cache = $cache;
31 * Gets a setting from the database,
32 * If not found, Returns default, Which is false by default.
34 * @param string|bool $default
37 public function get($key, $default = false)
39 if ($default === false) {
40 $default = config('setting-defaults.' . $key, false);
43 if (isset($this->localCache[$key])) {
44 return $this->localCache[$key];
47 $value = $this->getValueFromStore($key, $default);
48 $formatted = $this->formatValue($value, $default);
49 $this->localCache[$key] = $formatted;
54 * Get a value from the session instead of the main store option.
56 * @param bool $default
59 protected function getFromSession($key, $default = false)
61 $value = session()->get($key, $default);
62 $formatted = $this->formatValue($value, $default);
67 * Get a user-specific setting from the database or cache.
68 * @param \BookStack\Auth\User $user
70 * @param bool $default
73 public function getUser($user, $key, $default = false)
75 if ($user->isDefault()) {
76 return $this->getFromSession($key, $default);
78 return $this->get($this->userKey($user->id, $key), $default);
82 * Get a value for the current logged-in user.
84 * @param bool $default
87 public function getForCurrentUser($key, $default = false)
89 return $this->getUser(user(), $key, $default);
93 * Gets a setting value from the cache or database.
94 * Looks at the system defaults if not cached or in database.
99 protected function getValueFromStore($key, $default)
101 // Check for an overriding value
102 $overrideValue = $this->getOverrideValue($key);
103 if ($overrideValue !== null) {
104 return $overrideValue;
108 $cacheKey = $this->cachePrefix . $key;
109 $cacheVal = $this->cache->get($cacheKey, null);
110 if ($cacheVal !== null) {
114 // Check the database
115 $settingObject = $this->getSettingObjectByKey($key);
116 if ($settingObject !== null) {
117 $value = $settingObject->value;
118 $this->cache->forever($cacheKey, $value);
126 * Clear an item from the cache completely.
129 protected function clearFromCache($key)
131 $cacheKey = $this->cachePrefix . $key;
132 $this->cache->forget($cacheKey);
133 if (isset($this->localCache[$key])) {
134 unset($this->localCache[$key]);
139 * Format a settings value
144 protected function formatValue($value, $default)
146 // Change string booleans to actual booleans
147 if ($value === 'true') {
150 if ($value === 'false') {
154 // Set to default if empty
162 * Checks if a setting exists.
166 public function has($key)
168 $setting = $this->getSettingObjectByKey($key);
169 return $setting !== null;
173 * Check if a user setting is in the database.
177 public function hasUser($key)
179 return $this->has($this->userKey($key));
183 * Add a setting to the database.
188 public function put($key, $value)
190 $setting = $this->setting->firstOrNew([
191 'setting_key' => $key
193 $setting->value = $value;
195 $this->clearFromCache($key);
200 * Put a user-specific setting into the database.
201 * @param \BookStack\Auth\User $user
206 public function putUser($user, $key, $value)
208 if ($user->isDefault()) {
209 return session()->put($key, $value);
211 return $this->put($this->userKey($user->id, $key), $value);
215 * Convert a setting key into a user-specific key.
219 protected function userKey($userId, $key = '')
221 return 'user:' . $userId . ':' . $key;
225 * Removes a setting from the database.
229 public function remove($key)
231 $setting = $this->getSettingObjectByKey($key);
235 $this->clearFromCache($key);
240 * Delete settings for a given user id.
244 public function deleteUserSettings($userId)
246 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
250 * Gets a setting model from the database for the given key.
254 protected function getSettingObjectByKey($key)
256 return $this->setting->where('setting_key', '=', $key)->first();
261 * Returns an override value for a setting based on certain app conditions.
262 * Used where certain configuration options overrule others.
263 * Returns null if no override value is available.
267 protected function getOverrideValue($key)
269 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {