use Exception;
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
+use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Config\ConfigManager;
use Shaarli\Formatter\BookmarkMarkdownFormatter;
/** @var bool true for logged in users. Default value to retrieve private bookmarks. */
protected $isLoggedIn;
- /** @var bool Allow datastore alteration from not logged in users. */
- protected $anonymousPermission = false;
-
/**
* @inheritDoc
*/
} else {
try {
$this->bookmarks = $this->bookmarksIO->read();
- } catch (EmptyDataStoreException $e) {
+ } catch (EmptyDataStoreException|DatastoreNotInitializedException $e) {
$this->bookmarks = new BookmarkArray();
+
if ($this->isLoggedIn) {
- $this->save();
+ // Datastore file does not exists, we initialize it with default bookmarks.
+ if ($e instanceof DatastoreNotInitializedException) {
+ $this->initialize();
+ } else {
+ $this->save();
+ }
}
}
*/
public function set($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function add($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function addOrSet($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function remove($bookmark, $save = true)
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
throw new Exception(t('You\'re not authorized to alter the datastore'));
}
if (! $bookmark instanceof Bookmark) {
*/
public function save()
{
- if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) {
+ if (true !== $this->isLoggedIn) {
// TODO: raise an Exception instead
die('You are not authorized to change the database.');
}
{
$initializer = new BookmarkInitializer($this);
$initializer->initialize();
- }
- public function enableAnonymousPermission(): void
- {
- $this->anonymousPermission = true;
- }
-
- public function disableAnonymousPermission(): void
- {
- $this->anonymousPermission = false;
+ if (true === $this->isLoggedIn) {
+ $this->save();
+ }
}
/**
namespace Shaarli\Bookmark;
+use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Bookmark\Exception\NotWritableDataStoreException;
use Shaarli\Config\ConfigManager;
*
* @return BookmarkArray instance
*
- * @throws NotWritableDataStoreException Data couldn't be loaded
- * @throws EmptyDataStoreException Datastore doesn't exist
+ * @throws NotWritableDataStoreException Data couldn't be loaded
+ * @throws EmptyDataStoreException Datastore file exists but does not contain any bookmark
+ * @throws DatastoreNotInitializedException File does not exists
*/
public function read()
{
if (! file_exists($this->datastore)) {
- throw new EmptyDataStoreException();
+ throw new DatastoreNotInitializedException();
}
if (!is_writable($this->datastore)) {
* Class BookmarkInitializer
*
* This class is used to initialized default bookmarks after a fresh install of Shaarli.
- * It is no longer call when the data store is empty,
- * because user might want to delete default bookmarks after the install.
+ * It should be only called if the datastore file does not exist(users might want to delete the default bookmarks).
*
* To prevent data corruption, it does not overwrite existing bookmarks,
* even though there should not be any.
*/
public function initialize()
{
- $this->bookmarkService->enableAnonymousPermission();
-
$bookmark = new Bookmark();
$bookmark->setTitle(t('My secret stuff... - Pastebin.com'));
$bookmark->setUrl('http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=');
));
$bookmark->setTagsString('opensource software');
$this->bookmarkService->add($bookmark, false);
-
- $this->bookmarkService->save();
-
- $this->bookmarkService->disableAnonymousPermission();
}
}
use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
use Shaarli\Bookmark\Exception\NotWritableDataStoreException;
use Shaarli\Config\ConfigManager;
-use Shaarli\Exceptions\IOException;
use Shaarli\History;
/**
* Creates the default database after a fresh install.
*/
public function initialize();
-
- /**
- * Allow to write the datastore from anonymous session (not logged in).
- *
- * This covers a few specific use cases, such as datastore initialization,
- * but it should be used carefully as it can lead to security issues.
- */
- public function enableAnonymousPermission();
-
- /**
- * Disable anonymous permission.
- */
- public function disableAnonymousPermission();
}
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Bookmark\Exception;
+
+class DatastoreNotInitializedException extends \Exception
+{
+
+}
namespace Shaarli\Front\Controller\Visitor;
use Shaarli\ApplicationUtils;
-use Shaarli\Bookmark\BookmarkFilter;
use Shaarli\Container\ShaarliContainer;
use Shaarli\Front\Exception\AlreadyInstalledException;
use Shaarli\Front\Exception\ResourcePermissionException;
return $response->write($this->render('error'));
}
- if ($this->container->bookmarkService->count(BookmarkFilter::$ALL) === 0) {
- $this->container->bookmarkService->initialize();
- }
-
$this->container->sessionManager->setSessionParameter(
SessionManager::KEY_SUCCESS_MESSAGES,
[t('Shaarli is now configured. Please login and start shaaring your bookmarks!')]
namespace Shaarli\Bookmark;
use PHPUnit\Framework\TestCase;
-use ReferenceLinkDB;
use Shaarli\Config\ConfigManager;
use Shaarli\History;
}
/**
- * Test initialize() with an empty data store.
+ * Test initialize() with a data store containing bookmarks.
*/
- public function testInitializeEmptyDataStore()
+ public function testInitializeNotEmptyDataStore(): void
{
$refDB = new \ReferenceLinkDB();
$refDB->write(self::$testDatastore);
);
$this->assertFalse($bookmark->isPrivate());
+ $this->bookmarkService->save();
+
// Reload from file
$this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
$this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
}
/**
- * Test initialize() with a data store containing bookmarks.
+ * Test initialize() with an a non existent datastore file .
*/
- public function testInitializeNotEmptyDataStore()
+ public function testInitializeNonExistentDataStore(): void
{
+ $this->conf->set('resource.datastore', static::$testDatastore . '_empty');
+ $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
+
$this->initializer->initialize();
$this->assertEquals(2, $this->bookmarkService->count());
;
$this->container->conf->expects(static::once())->method('write');
- $this->container->bookmarkService->expects(static::once())->method('count')->willReturn(0);
- $this->container->bookmarkService->expects(static::once())->method('initialize');
-
$this->container->sessionManager
->expects(static::once())
->method('setSessionParameter')