1 <?php namespace BookStack\Settings;
3 use Illuminate\Contracts\Cache\Repository as Cache;
8 * The settings are a simple key-value database store.
10 * @package BookStack\Services
17 protected $localCache = [];
19 protected $cachePrefix = 'setting-';
22 * SettingService constructor.
23 * @param Setting $setting
26 public function __construct(Setting $setting, Cache $cache)
28 $this->setting = $setting;
29 $this->cache = $cache;
33 * Gets a setting from the database,
34 * If not found, Returns default, Which is false by default.
36 * @param string|bool $default
39 public function get($key, $default = false)
41 if ($default === false) {
42 $default = config('setting-defaults.' . $key, false);
45 if (isset($this->localCache[$key])) {
46 return $this->localCache[$key];
49 $value = $this->getValueFromStore($key, $default);
50 $formatted = $this->formatValue($value, $default);
51 $this->localCache[$key] = $formatted;
56 * Get a user-specific setting from the database or cache.
57 * @param \BookStack\Auth\User $user
59 * @param bool $default
62 public function getUser($user, $key, $default = false)
64 if ($user->isDefault()) {
65 return session()->get($key, $default);
67 return $this->get($this->userKey($user->id, $key), $default);
71 * Get a value for the current logged-in user.
73 * @param bool $default
76 public function getForCurrentUser($key, $default = false)
78 return $this->getUser(user(), $key, $default);
82 * Gets a setting value from the cache or database.
83 * Looks at the system defaults if not cached or in database.
88 protected function getValueFromStore($key, $default)
90 // Check for an overriding value
91 $overrideValue = $this->getOverrideValue($key);
92 if ($overrideValue !== null) {
93 return $overrideValue;
97 $cacheKey = $this->cachePrefix . $key;
98 $cacheVal = $this->cache->get($cacheKey, null);
99 if ($cacheVal !== null) {
103 // Check the database
104 $settingObject = $this->getSettingObjectByKey($key);
105 if ($settingObject !== null) {
106 $value = $settingObject->value;
107 $this->cache->forever($cacheKey, $value);
115 * Clear an item from the cache completely.
118 protected function clearFromCache($key)
120 $cacheKey = $this->cachePrefix . $key;
121 $this->cache->forget($cacheKey);
122 if (isset($this->localCache[$key])) {
123 unset($this->localCache[$key]);
128 * Format a settings value
133 protected function formatValue($value, $default)
135 // Change string booleans to actual booleans
136 if ($value === 'true') {
139 if ($value === 'false') {
143 // Set to default if empty
151 * Checks if a setting exists.
155 public function has($key)
157 $setting = $this->getSettingObjectByKey($key);
158 return $setting !== null;
162 * Check if a user setting is in the database.
166 public function hasUser($key)
168 return $this->has($this->userKey($key));
172 * Add a setting to the database.
177 public function put($key, $value)
179 $setting = $this->setting->firstOrNew([
180 'setting_key' => $key
182 $setting->value = $value;
184 $this->clearFromCache($key);
189 * Put a user-specific setting into the database.
190 * @param \BookStack\Auth\User $user
195 public function putUser($user, $key, $value)
197 if ($user->isDefault()) {
198 return session()->put($key, $value);
200 return $this->put($this->userKey($user->id, $key), $value);
204 * Convert a setting key into a user-specific key.
208 protected function userKey($userId, $key = '')
210 return 'user:' . $userId . ':' . $key;
214 * Removes a setting from the database.
218 public function remove($key)
220 $setting = $this->getSettingObjectByKey($key);
224 $this->clearFromCache($key);
229 * Delete settings for a given user id.
233 public function deleteUserSettings($userId)
235 return $this->setting->where('setting_key', 'like', $this->userKey($userId) . '%')->delete();
239 * Gets a setting model from the database for the given key.
243 protected function getSettingObjectByKey($key)
245 return $this->setting->where('setting_key', '=', $key)->first();
250 * Returns an override value for a setting based on certain app conditions.
251 * Used where certain configuration options overrule others.
252 * Returns null if no override value is available.
256 protected function getOverrideValue($key)
258 if ($key === 'registration-enabled' && config('auth.method') === 'ldap') {