Skip to content

Commit e18da08

Browse files
committed
Merge branch 'release/0.4.1'
2 parents 9c696b9 + 17a3267 commit e18da08

26 files changed

Lines changed: 197 additions & 1333 deletions

app/Console/HaveAccess.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@ abstract public function error($string, $verbosity = null);
6969
*/
7070
private function isAllowedPath(string $path): bool
7171
{
72+
$error = 'No valid paths in IMPORT_DIR_WHITELIST, cannot continue.';
7273
$paths = config('importer.import_dir_whitelist');
7374
if (null === $paths) {
74-
$this->warn('No valid paths in IMPORT_DIR_WHITELIST, cannot continue.');
75+
$this->warn($error);
7576
return false;
7677
}
7778
if (is_array($paths) && 0 === count($paths)) {
78-
$this->warn('No valid paths in IMPORT_DIR_WHITELIST, cannot continue.');
79+
$this->warn($error);
7980
return false;
8081
}
8182
if (is_array($paths) && 1 === count($paths) && '' === $paths[0]) {
82-
$this->warn('No valid paths in IMPORT_DIR_WHITELIST, cannot continue.');
83+
$this->warn($error);
8384
return false;
8485
}
8586

app/Http/Controllers/Import/AuthenticateController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
*/
4343
class AuthenticateController extends Controller
4444
{
45+
private const AUTH_ROUTE = '002-authenticate.index';
46+
4547
public function __construct()
4648
{
4749
parent::__construct();
@@ -112,27 +114,27 @@ public function postIndex(Request $request)
112114
$appId = (string) $request->get('spectre_app_id');
113115
$secret = (string) $request->get('spectre_secret');
114116
if ('' === $appId || '' === $secret) {
115-
return redirect(route('002-authenticate.index'))->with(['error' => 'Both fields must be filled in.']);
117+
return redirect(route(self::AUTH_ROUTE))->with(['error' => 'Both fields must be filled in.']);
116118
}
117119
// give to secret manager to store:
118120
SpectreSecretManager::saveAppId($appId);
119121
SpectreSecretManager::saveSecret($secret);
120122

121-
return redirect(route('002-authenticate.index'));
123+
return redirect(route(self::AUTH_ROUTE));
122124
}
123125
if ('nordigen' === $flow) {
124126
$key = $request->get('nordigen_key');
125127
$identifier = $request->get('nordigen_id');
126128
if ('' === $key || '' === $identifier) {
127-
return redirect(route('002-authenticate.index'))->with(['error' => 'Both fields must be filled in.']);
129+
return redirect(route(self::AUTH_ROUTE))->with(['error' => 'Both fields must be filled in.']);
128130
}
129131
// store ID and key in session:
130132
$cookies = [
131133
NordigenSecretManager::saveId($identifier),
132134
NordigenSecretManager::saveKey($key),
133135
];
134136

135-
return redirect(route('002-authenticate.index'))->withCookies($cookies);
137+
return redirect(route(self::AUTH_ROUTE))->withCookies($cookies);
136138
}
137139

138140
throw new ImporterErrorException('Impossible flow exception.');

app/Http/Controllers/Import/ConfigurationController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
*/
6464
class ConfigurationController extends Controller
6565
{
66+
protected const ASSET_ACCOUNTS = 'assets';
67+
protected const LIABILITIES = 'liabilities';
68+
6669
use RestoresConfiguration;
6770

6871
/**
@@ -90,8 +93,8 @@ public function index(Request $request)
9093
$mainTitle = 'Configuration';
9194
$subTitle = 'Configure your import';
9295
$accounts = [
93-
'Asset accounts' => [],
94-
'Liabilities' => [],
96+
self::ASSET_ACCOUNTS => [],
97+
self::LIABILITIES => [],
9598
];
9699
$flow = $request->cookie(Constants::FLOW_COOKIE); // TODO should be from configuration right
97100

@@ -118,7 +121,7 @@ public function index(Request $request)
118121

119122
/** @var Account $account */
120123
foreach ($response as $account) {
121-
$accounts['Asset accounts'][$account->id] = $account;
124+
$accounts[self::ASSET_ACCOUNTS][$account->id] = $account;
122125
}
123126

124127
// also get liabilities
@@ -131,7 +134,7 @@ public function index(Request $request)
131134
$response = $request->get();
132135
/** @var Account $account */
133136
foreach ($response as $account) {
134-
$accounts['Liabilities'][$account->id] = $account;
137+
$accounts[self::LIABILITIES][$account->id] = $account;
135138
}
136139

137140
// possibilities for duplicate detection (unique columns)
@@ -254,7 +257,7 @@ private function mergeNordigenAccountLists(array $nordigen, array $firefly): arr
254257
continue;
255258
}
256259
Log::debug('No special filtering on the Firefly III account list.');
257-
$entry['firefly'] = array_merge($firefly['Asset accounts'] , $firefly['Liabilities']);
260+
$entry['firefly'] = array_merge($firefly[self::ASSET_ACCOUNTS], $firefly[self::LIABILITIES]);
258261
$return[] = $entry;
259262
}
260263
return $return;
@@ -273,7 +276,7 @@ private function filterByIban(array $firefly, string $iban): array
273276
return [];
274277
}
275278
$result = [];
276-
$all = array_merge($firefly['Asset accounts'] ?? [], $firefly['Liabilities'] ?? []);
279+
$all = array_merge($firefly[self::ASSET_ACCOUNTS] ?? [], $firefly[self::LIABILITIES] ?? []);
277280
/** @var Account $account */
278281
foreach ($all as $account) {
279282
if ($iban === $account->iban) {
@@ -294,7 +297,7 @@ private function filterByCurrency(array $firefly, string $currency): array
294297
return [];
295298
}
296299
$result = [];
297-
$all = array_merge($firefly['Asset accounts'] ?? [], $firefly['Liabilities'] ?? []);
300+
$all = array_merge($firefly[self::ASSET_ACCOUNTS] ?? [], $firefly[self::LIABILITIES] ?? []);
298301
/** @var Account $account */
299302
foreach ($all as $account) {
300303
if ($currency === $account->currencyCode) {

app/Http/Controllers/Import/UploadController.php

Lines changed: 106 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Illuminate\Contracts\View\Factory;
3535
use Illuminate\Http\RedirectResponse;
3636
use Illuminate\Http\Request;
37+
use Illuminate\Http\UploadedFile;
3738
use Illuminate\Routing\Redirector;
3839
use Illuminate\Support\MessageBag;
3940
use Illuminate\View\View;
@@ -104,78 +105,15 @@ public function upload(Request $request)
104105
$flow = $request->cookie(Constants::FLOW_COOKIE);
105106
$errors = new MessageBag;
106107

107-
if (null === $csvFile && 'csv' === $flow) {
108-
$errors->add('csv_file', 'No file was uploaded.');
109-
110-
return redirect(route('003-upload.index'))->withErrors($errors);
111-
}
112-
if ('csv' === $flow) {
113-
$errorNumber = $csvFile->getError();
114-
if (0 !== $errorNumber) {
115-
$errors->add('csv_file', $this->getError($errorNumber));
116-
}
117-
118-
119-
// upload the file to a temp directory and use it from there.
120-
if (0 === $errorNumber) {
121-
$content = file_get_contents($csvFile->getPathname());
122-
123-
// https://stackoverflow.com/questions/11066857/detect-eol-type-using-php
124-
// because apparantly there are banks that use "\r" as newline. Looking at the morons of KBC Bank, Belgium.
125-
// This one is for you: 🤦‍♀️
126-
$eol = $this->detectEOL($content);
127-
if ("\r" === $eol) {
128-
Log::error('You bank is dumb. Tell them to fix their CSV files.');
129-
$content = str_replace("\r", "\n", $content);
130-
}
131-
132-
$csvFileName = StorageService::storeContent($content);
133-
session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName);
134-
session()->put(Constants::HAS_UPLOAD, true);
135-
}
136-
}
137-
138-
// if present, and no errors, upload the config file and store it in the session.
139-
if (null !== $configFile) {
140-
Log::debug('Config file is present.');
141-
$errorNumber = $configFile->getError();
142-
if (0 !== $errorNumber) {
143-
$errors->add('config_file', $errorNumber);
144-
}
145-
// upload the file to a temp directory and use it from there.
146-
if (0 === $errorNumber) {
147-
Log::debug('Config file uploaded.');
148-
$configFileName = StorageService::storeContent(file_get_contents($configFile->getPathname()));
108+
// process CSV file (if present)
109+
$errors = $this->processCsvFile($flow, $errors, $csvFile);
149110

150-
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
111+
// process config file (if present)
112+
$errors = $this->processConfigFile($errors, $configFile);
151113

152-
// process the config file
153-
try {
154-
$configuration = ConfigFileProcessor::convertConfigFile($configFileName);
155-
session()->put(Constants::CONFIGURATION, $configuration->toSessionArray());
156-
} catch (ImporterErrorException $e) {
157-
$errors->add('config_file', $e->getMessage());
158-
}
159-
}
160-
}
161-
// if no uploaded config file, read and use the submitted existing file, if any.
162-
$existingFile = (string) $request->get('existing_config');
114+
// process pre-selected file (if present):
115+
$errors = $this->processSelection($errors, (string) $request->get('existing_config'), $configFile);
163116

164-
if (null === $configFile && '' !== $existingFile) {
165-
Log::debug('User selected a config file from the store.');
166-
$disk = Storage::disk('configurations');
167-
$configFileName = StorageService::storeContent($disk->get($existingFile));
168-
169-
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
170-
171-
// process the config file
172-
try {
173-
$configuration = ConfigFileProcessor::convertConfigFile($configFileName);
174-
session()->put(Constants::CONFIGURATION, $configuration->toSessionArray());
175-
} catch (ImporterErrorException $e) {
176-
$errors->add('config_file', $e->getMessage());
177-
}
178-
}
179117

180118
if ($errors->count() > 0) {
181119
return redirect(route('003-upload.index'))->withErrors($errors);
@@ -245,4 +183,103 @@ private function detectEOL(string $string): string
245183
return $curEol;
246184
}
247185

186+
/**
187+
* @return MessageBag
188+
*/
189+
private function processCsvFile(string $flow, MessageBag $errors, UploadedFile|null $file): MessageBag
190+
{
191+
if (null === $file && 'csv' === $flow) {
192+
$errors->add('csv_file', 'No file was uploaded.');
193+
return $errors;
194+
}
195+
if ('csv' === $flow) {
196+
$errorNumber = $file->getError();
197+
if (0 !== $errorNumber) {
198+
$errors->add('csv_file', $this->getError($errorNumber));
199+
}
200+
201+
202+
// upload the file to a temp directory and use it from there.
203+
if (0 === $errorNumber) {
204+
$content = file_get_contents($file->getPathname());
205+
206+
// https://stackoverflow.com/questions/11066857/detect-eol-type-using-php
207+
// because apparantly there are banks that use "\r" as newline. Looking at the morons of KBC Bank, Belgium.
208+
// This one is for you: 🤦‍♀️
209+
$eol = $this->detectEOL($content);
210+
if ("\r" === $eol) {
211+
Log::error('You bank is dumb. Tell them to fix their CSV files.');
212+
$content = str_replace("\r", "\n", $content);
213+
}
214+
215+
$csvFileName = StorageService::storeContent($content);
216+
session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName);
217+
session()->put(Constants::HAS_UPLOAD, true);
218+
}
219+
}
220+
return $errors;
221+
}
222+
223+
/**
224+
* @param MessageBag $errors
225+
* @param UploadedFile|null $file
226+
* @return MessageBag
227+
* @throws ImporterErrorException
228+
*/
229+
private function processConfigFile(MessageBag $errors, UploadedFile|null $file): MessageBag
230+
{
231+
if (count($errors) > 0 || null === $file) {
232+
return $errors;
233+
}
234+
Log::debug('Config file is present.');
235+
$errorNumber = $file->getError();
236+
if (0 !== $errorNumber) {
237+
$errors->add('config_file', $errorNumber);
238+
}
239+
// upload the file to a temp directory and use it from there.
240+
if (0 === $errorNumber) {
241+
Log::debug('Config file uploaded.');
242+
$configFileName = StorageService::storeContent(file_get_contents($file->getPathname()));
243+
244+
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
245+
246+
// process the config file
247+
try {
248+
$configuration = ConfigFileProcessor::convertConfigFile($configFileName);
249+
session()->put(Constants::CONFIGURATION, $configuration->toSessionArray());
250+
} catch (ImporterErrorException $e) {
251+
$errors->add('config_file', $e->getMessage());
252+
}
253+
}
254+
return $errors;
255+
}
256+
257+
/**
258+
* @param MessageBag $errors
259+
* @param string $selection
260+
* @param UploadedFile|null $file
261+
* @return MessageBag
262+
* @throws ImporterErrorException
263+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
264+
*/
265+
private function processSelection(MessageBag $errors, string $selection, UploadedFile|null $file): MessageBag
266+
{
267+
if (null === $file && '' !== $selection) {
268+
Log::debug('User selected a config file from the store.');
269+
$disk = Storage::disk('configurations');
270+
$configFileName = StorageService::storeContent($disk->get($selection));
271+
272+
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
273+
274+
// process the config file
275+
try {
276+
$configuration = ConfigFileProcessor::convertConfigFile($configFileName);
277+
session()->put(Constants::CONFIGURATION, $configuration->toSessionArray());
278+
} catch (ImporterErrorException $e) {
279+
$errors->add('config_file', $e->getMessage());
280+
}
281+
}
282+
return $errors;
283+
}
284+
248285
}

app/Http/Controllers/IndexController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,23 @@ public function index(Request $request): mixed
8282
}
8383

8484
// display to user the method of authentication
85-
$pat = false;
85+
$clientId = (string) config('importer.client_id');
86+
$url = (string) config('importer.url');
87+
$pat = false;
8688
if ('' !== (string) config('importer.access_token')) {
8789
$pat = true;
8890
}
8991
$clientIdWithURL = false;
90-
if ('' !== (string) config('importer.url') && '' !== (string) config('importer.client_id')) {
92+
if ('' !== $url && '' !== $clientId) {
9193
$clientIdWithURL = true;
9294
}
9395
$URLonly = false;
94-
if ('' !== (string) config('importer.url') && '' === (string) config('importer.client_id') && '' === (string) config('importer.access_token')
96+
if ('' !== $url && '' === $clientId && '' === (string) config('importer.access_token')
9597
) {
9698
$URLonly = true;
9799
}
98100
$flexible = false;
99-
if ('' === (string) config('importer.url') && '' === (string) config('importer.client_id')) {
101+
if ('' === $url && '' === $clientId) {
100102
$flexible = true;
101103
}
102104

0 commit comments

Comments
 (0)