aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/BookmarkFileService.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/bookmark/BookmarkFileService.php')
-rw-r--r--application/bookmark/BookmarkFileService.php36
1 files changed, 17 insertions, 19 deletions
diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php
index 6e04f3b7..b3a90ed4 100644
--- a/application/bookmark/BookmarkFileService.php
+++ b/application/bookmark/BookmarkFileService.php
@@ -6,6 +6,7 @@ namespace Shaarli\Bookmark;
6 6
7use Exception; 7use Exception;
8use Shaarli\Bookmark\Exception\BookmarkNotFoundException; 8use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
9use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
9use Shaarli\Bookmark\Exception\EmptyDataStoreException; 10use Shaarli\Bookmark\Exception\EmptyDataStoreException;
10use Shaarli\Config\ConfigManager; 11use Shaarli\Config\ConfigManager;
11use Shaarli\Formatter\BookmarkMarkdownFormatter; 12use Shaarli\Formatter\BookmarkMarkdownFormatter;
@@ -46,9 +47,6 @@ class BookmarkFileService implements BookmarkServiceInterface
46 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ 47 /** @var bool true for logged in users. Default value to retrieve private bookmarks. */
47 protected $isLoggedIn; 48 protected $isLoggedIn;
48 49
49 /** @var bool Allow datastore alteration from not logged in users. */
50 protected $anonymousPermission = false;
51
52 /** 50 /**
53 * @inheritDoc 51 * @inheritDoc
54 */ 52 */
@@ -65,10 +63,16 @@ class BookmarkFileService implements BookmarkServiceInterface
65 } else { 63 } else {
66 try { 64 try {
67 $this->bookmarks = $this->bookmarksIO->read(); 65 $this->bookmarks = $this->bookmarksIO->read();
68 } catch (EmptyDataStoreException $e) { 66 } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) {
69 $this->bookmarks = new BookmarkArray(); 67 $this->bookmarks = new BookmarkArray();
68
70 if ($this->isLoggedIn) { 69 if ($this->isLoggedIn) {
71 $this->save(); 70 // Datastore file does not exists, we initialize it with default bookmarks.
71 if ($e instanceof DatastoreNotInitializedException) {
72 $this->initialize();
73 } else {
74 $this->save();
75 }
72 } 76 }
73 } 77 }
74 78
@@ -157,7 +161,7 @@ class BookmarkFileService implements BookmarkServiceInterface
157 */ 161 */
158 public function set($bookmark, $save = true) 162 public function set($bookmark, $save = true)
159 { 163 {
160 if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { 164 if (true !== $this->isLoggedIn) {
161 throw new Exception(t('You\'re not authorized to alter the datastore')); 165 throw new Exception(t('You\'re not authorized to alter the datastore'));
162 } 166 }
163 if (! $bookmark instanceof Bookmark) { 167 if (! $bookmark instanceof Bookmark) {
@@ -182,7 +186,7 @@ class BookmarkFileService implements BookmarkServiceInterface
182 */ 186 */
183 public function add($bookmark, $save = true) 187 public function add($bookmark, $save = true)
184 { 188 {
185 if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { 189 if (true !== $this->isLoggedIn) {
186 throw new Exception(t('You\'re not authorized to alter the datastore')); 190 throw new Exception(t('You\'re not authorized to alter the datastore'));
187 } 191 }
188 if (! $bookmark instanceof Bookmark) { 192 if (! $bookmark instanceof Bookmark) {
@@ -207,7 +211,7 @@ class BookmarkFileService implements BookmarkServiceInterface
207 */ 211 */
208 public function addOrSet($bookmark, $save = true) 212 public function addOrSet($bookmark, $save = true)
209 { 213 {
210 if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { 214 if (true !== $this->isLoggedIn) {
211 throw new Exception(t('You\'re not authorized to alter the datastore')); 215 throw new Exception(t('You\'re not authorized to alter the datastore'));
212 } 216 }
213 if (! $bookmark instanceof Bookmark) { 217 if (! $bookmark instanceof Bookmark) {
@@ -224,7 +228,7 @@ class BookmarkFileService implements BookmarkServiceInterface
224 */ 228 */
225 public function remove($bookmark, $save = true) 229 public function remove($bookmark, $save = true)
226 { 230 {
227 if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { 231 if (true !== $this->isLoggedIn) {
228 throw new Exception(t('You\'re not authorized to alter the datastore')); 232 throw new Exception(t('You\'re not authorized to alter the datastore'));
229 } 233 }
230 if (! $bookmark instanceof Bookmark) { 234 if (! $bookmark instanceof Bookmark) {
@@ -277,7 +281,7 @@ class BookmarkFileService implements BookmarkServiceInterface
277 */ 281 */
278 public function save() 282 public function save()
279 { 283 {
280 if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { 284 if (true !== $this->isLoggedIn) {
281 // TODO: raise an Exception instead 285 // TODO: raise an Exception instead
282 die('You are not authorized to change the database.'); 286 die('You are not authorized to change the database.');
283 } 287 }
@@ -359,16 +363,10 @@ class BookmarkFileService implements BookmarkServiceInterface
359 { 363 {
360 $initializer = new BookmarkInitializer($this); 364 $initializer = new BookmarkInitializer($this);
361 $initializer->initialize(); 365 $initializer->initialize();
362 }
363 366
364 public function enableAnonymousPermission(): void 367 if (true === $this->isLoggedIn) {
365 { 368 $this->save();
366 $this->anonymousPermission = true; 369 }
367 }
368
369 public function disableAnonymousPermission(): void
370 {
371 $this->anonymousPermission = false;
372 } 370 }
373 371
374 /** 372 /**