From 3b4141eb4d4c081f9b5b350d435e7a964ff871e6 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 16 Jul 2018 21:17:32 +0200 Subject: [PATCH] Prepare for embedded mode --- .gitignore | 1 + README.md | 9 ++++++--- app.php | 13 +++++++++++-- middleware/ApiKeyAuthMiddleware.php | 2 +- middleware/SessionAuthMiddleware.php | 4 ++-- services/ApplicationService.php | 10 +++++++++- services/DatabaseService.php | 2 +- services/DemoDataGeneratorService.php | 2 +- services/LocalizationService.php | 2 +- services/StockService.php | 2 +- views/openapiui.blade.php | 4 ++-- 11 files changed, 36 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 305dfcdd..9946de1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /public/node_modules /vendor /.release +embedded.txt \ No newline at end of file diff --git a/README.md b/README.md index 35cb36a9..cfe88d49 100644 --- a/README.md +++ b/README.md @@ -59,13 +59,16 @@ There is no plugin included for any service, see the reference implementation in ### Database migrations Database schema migration is automatically done when visiting the root (`/`) route (click on the logo in the left upper edge). -### Demo mode -When the file `data/demo.txt` exists, the application will work in a demo mode which means authentication is disabled and some demo data will be generated during the database schema migration. - ### Adding your own CSS or JS without to have to modify the application itself - When the file `data/custom_js.html` exists, the contents of the file will be added just before `` (end of body) on every page - When the file `data/custom_css.html` exists, the contents of the file will be added just before `` (end of head) on every page +### Demo mode +When the file `data/demo.txt` exists, the application will work in a demo mode which means authentication is disabled and some demo data will be generated during the database schema migration. + +### Embedded mode +When the file `/embedded.txt` exists, it must contain a valid and writable path which will be used as the data directory instead of `data/` and authentication will be disabled (used in [grocy-desktop](https://github.com/berrnd/grocy-desktop)). + ## Screenshots #### Dashboard ![Dashboard](https://github.com/berrnd/grocy/raw/master/publication_assets/dashboard.png "Dashboard") diff --git a/app.php b/app.php index 6308f3e5..7e2c48db 100644 --- a/app.php +++ b/app.php @@ -6,8 +6,17 @@ use \Psr\Http\Message\ResponseInterface as Response; use \Grocy\Helpers\UrlManager; use \Grocy\Controllers\LoginController; +if (file_exists(__DIR__ . '/embedded.txt')) +{ + define('DATAPATH', file_get_contents(__DIR__ . '/embedded.txt')); +} +else +{ + define('DATAPATH', __DIR__ . '/data'); +} + require_once __DIR__ . '/vendor/autoload.php'; -require_once __DIR__ . '/data/config.php'; +require_once DATAPATH . '/config.php'; require_once __DIR__ . '/config-dist.php'; //For not in own config defined values we use the default ones // Setup base application @@ -18,7 +27,7 @@ $appContainer = new \Slim\Container([ ], 'view' => function($container) { - return new \Slim\Views\Blade(__DIR__ . '/views', __DIR__ . '/data/viewcache'); + return new \Slim\Views\Blade(__DIR__ . '/views', DATAPATH . '/viewcache'); }, 'LoginControllerInstance' => function($container) { diff --git a/middleware/ApiKeyAuthMiddleware.php b/middleware/ApiKeyAuthMiddleware.php index 3e28dfea..cc7cbf32 100644 --- a/middleware/ApiKeyAuthMiddleware.php +++ b/middleware/ApiKeyAuthMiddleware.php @@ -22,7 +22,7 @@ class ApiKeyAuthMiddleware extends BaseMiddleware $route = $request->getAttribute('route'); $routeName = $route->getName(); - if ($this->ApplicationService->IsDemoInstallation()) + if ($this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation()) { $response = $next($request, $response); } diff --git a/middleware/SessionAuthMiddleware.php b/middleware/SessionAuthMiddleware.php index 5a454365..0037b3e3 100644 --- a/middleware/SessionAuthMiddleware.php +++ b/middleware/SessionAuthMiddleware.php @@ -19,9 +19,9 @@ class SessionAuthMiddleware extends BaseMiddleware $route = $request->getAttribute('route'); $routeName = $route->getName(); - if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation()) + if ($routeName === 'root' || $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation()) { - define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation()); + define('AUTHENTICATED', $this->ApplicationService->IsDemoInstallation() || $this->ApplicationService->IsEmbeddedInstallation()); $response = $next($request, $response); } else diff --git a/services/ApplicationService.php b/services/ApplicationService.php index 3be2e60f..d92c4acd 100644 --- a/services/ApplicationService.php +++ b/services/ApplicationService.php @@ -9,7 +9,15 @@ class ApplicationService extends BaseService */ public function IsDemoInstallation() { - return file_exists(__DIR__ . '/../data/demo.txt'); + return file_exists(DATAPATH . '/demo.txt'); + } + + /** + * @return boolean + */ + public function IsEmbeddedInstallation() + { + return file_exists(__DIR__ . '/../embedded.txt'); } private $InstalledVersion; diff --git a/services/DatabaseService.php b/services/DatabaseService.php index f23d3412..f3d25102 100644 --- a/services/DatabaseService.php +++ b/services/DatabaseService.php @@ -14,7 +14,7 @@ class DatabaseService { if ($this->DbConnectionRaw == null) { - $pdo = new \PDO('sqlite:' . __DIR__ . '/../data/grocy.db'); + $pdo = new \PDO('sqlite:' . DATAPATH . '/grocy.db'); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->DbConnectionRaw = $pdo; } diff --git a/services/DemoDataGeneratorService.php b/services/DemoDataGeneratorService.php index d2644bec..6f26f946 100644 --- a/services/DemoDataGeneratorService.php +++ b/services/DemoDataGeneratorService.php @@ -123,7 +123,7 @@ class DemoDataGeneratorService extends BaseService public function RecreateDemo() { - unlink(__DIR__ . '/../data/grocy.db'); + unlink(DATAPATH . '/grocy.db'); $this->PopulateDemoData(); } } diff --git a/services/LocalizationService.php b/services/LocalizationService.php index 5dbe1a48..f8b6f40e 100644 --- a/services/LocalizationService.php +++ b/services/LocalizationService.php @@ -36,7 +36,7 @@ class LocalizationService private function LogMissingLocalization(string $culture, string $text) { - $file = __DIR__ . "/../data/missing_translations_$culture.json"; + $file = DATAPATH . "/missing_translations_$culture.json"; $missingTranslations = array(); if (file_exists($file)) diff --git a/services/StockService.php b/services/StockService.php index 28375b63..4b419944 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -222,7 +222,7 @@ class StockService extends BaseService throw new \Exception('No barcode lookup plugin defined'); } - $path = __DIR__ . "/../data/plugins/$pluginName.php"; + $path = DATAPATH . "/plugins/$pluginName.php"; if (file_exists($path)) { require_once $path; diff --git a/views/openapiui.blade.php b/views/openapiui.blade.php index 6280af1f..b7bbd850 100644 --- a/views/openapiui.blade.php +++ b/views/openapiui.blade.php @@ -30,8 +30,8 @@ - @if(file_exists(__DIR__ . '/../../data/add_before_end_body.html')) - @php include __DIR__ . '/../../data/add_before_end_body.html' @endphp + @if(file_exists(DATAPATH . '/add_before_end_body.html')) + @php include DATAPATH . '/add_before_end_body.html' @endphp @endif