diff --git a/app/Http/Controllers/Import/IndexController.php b/app/Http/Controllers/Import/IndexController.php index 4dad6d55ca..e94a563d1e 100644 --- a/app/Http/Controllers/Import/IndexController.php +++ b/app/Http/Controllers/Import/IndexController.php @@ -26,9 +26,10 @@ use FireflyIII\Exceptions\FireflyException; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Import\Prerequisites\PrerequisitesInterface; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; +use FireflyIII\Repositories\User\UserRepositoryInterface; +use Log; use View; - /** * Class FileController. */ @@ -37,6 +38,9 @@ class IndexController extends Controller /** @var ImportJobRepositoryInterface */ public $repository; + /** @var UserRepositoryInterface */ + public $userRepository; + /** * */ @@ -48,7 +52,8 @@ class IndexController extends Controller function ($request, $next) { app('view')->share('mainTitleIcon', 'fa-archive'); app('view')->share('title', trans('firefly.import_index_title')); - $this->repository = app(ImportJobRepositoryInterface::class); + $this->repository = app(ImportJobRepositoryInterface::class); + $this->userRepository = app(UserRepositoryInterface::class); return $next($request); } @@ -66,6 +71,8 @@ class IndexController extends Controller */ public function create(string $importProvider) { + // can only create "fake" for demo user. + if ( !(bool)config('app.debug') && !(bool)config(sprintf('import.enabled.%s', $importProvider)) === true @@ -124,34 +131,59 @@ class IndexController extends Controller */ public function index() { - // get all import routines: - /** @var array $config */ - $config = config('import.enabled'); - $providers = []; - foreach ($config as $name => $enabled) { - if ($enabled || (bool)config('app.debug') || \in_array(config('app.env'), ['demo', 'testing'])) { - $providers[$name] = []; - } - } - - // has prereq or config? - foreach (array_keys($providers) as $name) { - $providers[$name]['has_prereq'] = (bool)config('import.has_prereq.' . $name); - $providers[$name]['has_config'] = (bool)config('import.has_config.' . $name); - $class = (string)config('import.prerequisites.' . $name); - $result = false; - if ($class !== '' && class_exists($class)) { - /** @var PrerequisitesInterface $object */ - $object = app($class); - $object->setUser(auth()->user()); - $result = $object->isComplete(); - } - $providers[$name]['prereq_complete'] = $result; - } - + $providers = $this->getProviders(); $subTitle = trans('import.index_breadcrumb'); $subTitleIcon = 'fa-home'; return view('import.index', compact('subTitle', 'subTitleIcon', 'providers')); } + + /** + * @return array + */ + private function getProviders(): array + { + // get and filter all import routines: + /** @var array $config */ + $providerNames = array_keys(config('import.enabled')); + $providers = []; + $isDemoUser = $this->userRepository->hasRole(auth()->user(), 'demo'); + foreach ($providerNames as $providerName) { + Log::debug(sprintf('Now with provider %s', $providerName)); + // only consider enabled providers + $enabled = (bool)config(sprintf('import.enabled.%s', $providerName)); + $allowedForDemo = (bool)config(sprintf('import.allowed_for_demo.%s', $providerName)); + $allowedForUser = (bool)config(sprintf('import.allowed_for_user.%s', $providerName)); + if ($enabled === false) { + Log::debug('Provider is not enabled. NEXT!'); + continue; + } + + if ($isDemoUser === true && $allowedForDemo === false) { + Log::debug('User is demo and this provider is not allowed for demo user. NEXT!'); + continue; + } + if ($isDemoUser === false && $allowedForUser === false) { + Log::debug('User is not demo and this provider is not allowed for such users. NEXT!'); + continue; + } + + $providers[$providerName] = [ + 'has_prereq' => (bool)config('import.has_prereq.' . $providerName), + 'has_config' => (bool)config('import.has_config.' . $providerName), + ]; + $class = (string)config(sprintf('import.prerequisites.%s', $providerName)); + $result = false; + if ($class !== '' && class_exists($class)) { + Log::debug('Will not check prerequisites.'); + /** @var PrerequisitesInterface $object */ + $object = app($class); + $object->setUser(auth()->user()); + $result = $object->isComplete(); + } + $providers[$providerName]['prereq_complete'] = $result; + } + + return $providers; + } } diff --git a/app/Http/Controllers/Import/JobStatusController.php b/app/Http/Controllers/Import/JobStatusController.php index c6a3ae0bad..bbfaed422a 100644 --- a/app/Http/Controllers/Import/JobStatusController.php +++ b/app/Http/Controllers/Import/JobStatusController.php @@ -117,12 +117,9 @@ class JobStatusController extends Controller { // catch impossible status: $allowed = ['ready_to_run', 'need_job_config']; - // todo remove error and running. if (null !== $importJob && !\in_array($importJob->status, $allowed, true)) { Log::error('Job is not ready.'); - - // kill the job: $this->repository->setStatus($importJob, 'error'); return response()->json(['status' => 'NOK', 'message' => 'JobStatusController::start expects status "ready_to_run".']); @@ -138,9 +135,6 @@ class JobStatusController extends Controller // @codeCoverageIgnoreEnd } - // set job to be running: - $this->repository->setStatus($importJob, 'running'); - /** @var RoutineInterface $routine */ $routine = app($className); $routine->setImportJob($importJob); diff --git a/app/Import/Routine/FakeRoutine.php b/app/Import/Routine/FakeRoutine.php index a40bf40010..bd609706c5 100644 --- a/app/Import/Routine/FakeRoutine.php +++ b/app/Import/Routine/FakeRoutine.php @@ -49,7 +49,7 @@ class FakeRoutine implements RoutineInterface * "ahoy": will log some nonsense and then drop job into status:"need_job_config" to force it back to the job config routine. * "final": will do some logging, sleep for 10 seconds and then finish. Generates 5 random transactions. * - * @return bool + * @return void * @throws FireflyException */ public function run(): void @@ -77,7 +77,7 @@ class FakeRoutine implements RoutineInterface /** @var StageAhoyHandler $handler */ $handler = app(StageAhoyHandler::class); $handler->run(); - $this->repository->setStatus($this->importJob, 'ready_to_run'); + $this->repository->setStatus($this->importJob, 'need_job_config'); $this->repository->setStage($this->importJob, 'final'); break; case 'final': diff --git a/app/Import/Routine/FileRoutine.php b/app/Import/Routine/FileRoutine.php index 98de371df0..db228ac494 100644 --- a/app/Import/Routine/FileRoutine.php +++ b/app/Import/Routine/FileRoutine.php @@ -62,7 +62,7 @@ class FileRoutine implements RoutineInterface return; } - throw new FireflyException(sprintf('Import routine cannot handle stage "%s"', $this->importJob->stage)); // @codeCoverageIgnore + throw new FireflyException(sprintf('Import routine cannot handle status "%s"', $this->importJob->status)); // @codeCoverageIgnore } /** diff --git a/app/Support/Binder/ImportProvider.php b/app/Support/Binder/ImportProvider.php index 3e82d332d5..b06cff4dc1 100644 --- a/app/Support/Binder/ImportProvider.php +++ b/app/Support/Binder/ImportProvider.php @@ -40,14 +40,8 @@ class ImportProvider implements BinderInterface */ public static function routeBinder(string $value, Route $route): string { - $providers = (array)config('import.enabled'); - $allowed = []; - foreach ($providers as $name => $enabled) { - if ($enabled || (bool)config('app.debug') === true) { - $allowed[] = $name; - } - } - if (\in_array($value, $allowed, true)) { + $providers = array_keys((array)config('import.enabled')); + if (\in_array($value, $providers, true)) { return $value; } throw new NotFoundHttpException; diff --git a/config/import.php b/config/import.php index 9b022e7abc..07df95bec1 100644 --- a/config/import.php +++ b/config/import.php @@ -22,30 +22,45 @@ declare(strict_types=1); -use FireflyIII\Import\Configuration\BunqConfigurator; -use FireflyIII\Import\Configuration\SpectreConfigurator; use FireflyIII\Import\JobConfiguration\FakeJobConfiguration; use FireflyIII\Import\JobConfiguration\FileJobConfiguration; -use FireflyIII\Import\Prerequisites\BunqPrerequisites; use FireflyIII\Import\Prerequisites\FakePrerequisites; -use FireflyIII\Import\Prerequisites\SpectrePrerequisites; -use FireflyIII\Import\Routine\BunqRoutine; use FireflyIII\Import\Routine\FakeRoutine; use FireflyIII\Import\Routine\FileRoutine; -use FireflyIII\Import\Routine\SpectreRoutine; use FireflyIII\Support\Import\Routine\File\CSVProcessor; return [ - 'enabled' => [ + // these import providers are available: + 'enabled' => [ + 'fake' => true, + 'file' => true, + 'bunq' => false, + 'spectre' => false, + 'plaid' => false, + 'quovo' => false, + 'yodlee' => false, + ], + // demo user can use these import providers (when enabled): + 'allowed_for_demo' => [ + 'fake' => true, + 'file' => false, + 'bunq' => false, + 'spectre' => false, + 'plaid' => false, + 'quovo' => false, + 'yodlee' => false, + ], + // a normal user user can use these import providers (when enabled): + 'allowed_for_user' => [ 'fake' => false, 'file' => true, 'bunq' => true, 'spectre' => true, - 'plaid' => false, - 'quovo' => false, - 'yodlee' => false, + 'plaid' => true, + 'quovo' => true, + 'yodlee' => true, ], - 'has_prereq' => [ + 'has_prereq' => [ 'fake' => true, 'file' => false, 'bunq' => true, @@ -54,16 +69,16 @@ return [ 'quovo' => true, 'yodlee' => true, ], - 'prerequisites' => [ + 'prerequisites' => [ 'fake' => FakePrerequisites::class, 'file' => false, - 'bunq' => BunqPrerequisites::class, - 'spectre' => SpectrePrerequisites::class, + 'bunq' => false, + 'spectre' => false, 'plaid' => false, 'quovo' => false, 'yodlee' => false, ], - 'has_config' => [ + 'has_config' => [ 'fake' => true, 'file' => true, 'bunq' => true, @@ -72,26 +87,27 @@ return [ 'quovo' => true, 'yodlee' => true, ], - 'configuration' => [ + 'configuration' => [ 'fake' => FakeJobConfiguration::class, 'file' => FileJobConfiguration::class, - 'bunq' => BunqConfigurator::class, - 'spectre' => SpectreConfigurator::class, + 'bunq' => false, + 'spectre' => false, 'plaid' => false, 'quovo' => false, 'yodlee' => false, ], - 'routine' => [ + 'routine' => [ 'fake' => FakeRoutine::class, 'file' => FileRoutine::class, - 'bunq' => BunqRoutine::class, - 'spectre' => SpectreRoutine::class, + 'bunq' => false, + 'spectre' => false, 'plaid' => false, 'quovo' => false, 'yodlee' => false, ], - 'options' => [ + 'options' => [ + 'fake' => [], 'file' => [ 'import_formats' => ['csv'], // mt940 'default_import_format' => 'csv', @@ -100,29 +116,17 @@ return [ ], ], 'bunq' => [ - 'server' => 'sandbox.public.api.bunq.com', // sandbox.public.api.bunq.com - api.bunq.com - 'version' => 'v1', + 'live' => [ + 'server' => 'api.bunq.com', + 'version' => 'v1', + ], + 'sandbox' => [ + 'server' => 'sandbox.public.api.bunq.com', // sandbox.public.api.bunq.com - api.bunq.com + 'version' => 'v1', + ], ], 'spectre' => [ 'server' => 'www.saltedge.com', ], ], - 'default_config' => [ - 'file' => [ - 'has-config-file' => true, - 'auto-start' => false, - ], - 'bunq' => [ - 'has-config-file' => false, - 'auto-start' => true, - ], - 'spectre' => [ - 'has-config-file' => false, - 'auto-start' => true, - ], - 'plaid' => [ - 'has-config-file' => false, - 'auto-start' => true, - ], - ], ]; diff --git a/resources/views/import/index.twig b/resources/views/import/index.twig index caeddb4b93..b52d97f261 100644 --- a/resources/views/import/index.twig +++ b/resources/views/import/index.twig @@ -31,6 +31,7 @@