Skip to content

Commit 8b6edaf

Browse files
committed
Refactor
1 parent 08a69de commit 8b6edaf

File tree

6 files changed

+78
-37
lines changed

6 files changed

+78
-37
lines changed

resources/js/pages/Configure.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,22 @@
101101

102102
<optgroup label="Custom - same for all">
103103
<option value="custom">Single value</option>
104-
<option value="custom.laravel_random_password">Random Password</option>
104+
</optgroup>
105+
106+
<optgroup label="Custom - different for each row">
107+
<option value="random">Randomly-generated value</option>
105108
</optgroup>
106109
</SelectControl>
107110

108111
<input v-model="values[field.attribute]" v-if="mappings[field.attribute] === 'custom'"
109112
class="form-control form-input form-input-bordered">
110113

114+
<label class="flex items-center space-x-2" v-if="mappings[field.attribute] === 'random'">
115+
<span>Length</span>
116+
<input v-model="random[field.attribute]"
117+
class="form-control form-input form-input-bordered">
118+
</label>
119+
111120
<draggable
112121
v-model="modifiers[field.attribute]"
113122
handle=".handle"
@@ -190,6 +199,7 @@ export default {
190199
mappings: this.config?.mappings || {},
191200
values: this.config?.values || {},
192201
modifiers: this.config?.modifiers || {},
202+
random: this.config?.random || {},
193203
saving: false,
194204
};
195205
},
@@ -263,6 +273,7 @@ export default {
263273
values: this.values,
264274
modifiers: this.modifiers,
265275
file: this.file,
276+
random: this.random,
266277
};
267278
268279
Nova.request()

src/Concerns/HasModifiers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SimonHamp\LaravelNovaCsvImport\Modifiers\DefaultValue;
1010
use SimonHamp\LaravelNovaCsvImport\Modifiers\ExcelDate;
1111
use SimonHamp\LaravelNovaCsvImport\Modifiers\Hash;
12+
use SimonHamp\LaravelNovaCsvImport\Modifiers\Password;
1213
use SimonHamp\LaravelNovaCsvImport\Modifiers\Prefix;
1314
use SimonHamp\LaravelNovaCsvImport\Modifiers\Str as StrModifier;
1415
use SimonHamp\LaravelNovaCsvImport\Modifiers\Suffix;
@@ -28,6 +29,7 @@ protected function bootHasModifiers()
2829
new ExcelDate,
2930
new StrModifier,
3031
new Hash,
32+
new Password,
3133
new Prefix,
3234
new Suffix,
3335
);

src/Http/Controllers/ImportController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function storeConfig(NovaRequest $request)
7878
'resource' => $request->input('resource'),
7979
'mappings' => $request->input('mappings'),
8080
'values' => $request->input('values'),
81+
'random' => $request->input('random'),
8182
'modifiers' => collect($request->input('modifiers'))
8283
->map(function ($modifiers) {
8384
return collect($modifiers)
@@ -108,6 +109,7 @@ public function preview(NovaRequest $request, string $file): Response
108109
$import = $this->importer
109110
->setAttributeMap($columns = $config['mappings'])
110111
->setCustomValues($config['values'])
112+
->setRandomStringSettings($config['random'])
111113
->setMeta($config['meta'])
112114
->setModifiers($config['modifiers'])
113115
->toCollection($this->getFilePath($file), $this->getDisk())
@@ -154,6 +156,7 @@ public function import(NovaRequest $request)
154156
->setModelClass($model_class)
155157
->setMeta($config['meta'])
156158
->setCustomValues($config['values'])
159+
->setRandomStringSettings($config['random'])
157160
->setModifiers($config['modifiers'])
158161
->import($path, $this->getDisk());
159162

src/Importer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ public function setMeta(array $meta): self
115115
return $this;
116116
}
117117

118+
public function getRandomStringSettings($key = null)
119+
{
120+
if ($key) {
121+
return $this->random_string_settings[$key] ?? '';
122+
}
123+
124+
return $this->random_string_settings;
125+
}
126+
127+
public function setRandomStringSettings(array $map): self
128+
{
129+
$this->random_string_settings = $map;
130+
131+
return $this;
132+
}
133+
118134
public function getCustomValues($key = null)
119135
{
120136
if ($key) {
@@ -163,8 +179,8 @@ protected function getFieldValue(array $row, string $mapping, string $attribute)
163179
return $row[$mapping];
164180
} elseif (Str::startsWith($mapping, 'meta')) {
165181
return $this->getMeta(Str::remove('@meta.', "@{$mapping}"));
166-
} elseif ($mapping === 'custom.laravel_random_password') {
167-
return Hash::make(Str::random(16));
182+
} elseif ($mapping === 'random') {
183+
return Str::random($this->getRandomStringSettings($attribute));
168184
} elseif ($mapping === 'custom') {
169185
return $this->getCustomValues($attribute);
170186
}

src/Modifiers/Hash.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace SimonHamp\LaravelNovaCsvImport\Modifiers;
44

5-
use Exception;
6-
use Illuminate\Support\Facades\Hash as LaravelHashFascade;
75
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier;
8-
use Illuminate\Support\Str;
96

107
class Hash implements Modifier
118
{
@@ -22,42 +19,12 @@ public function description(): string
2219
public function settings(): array
2320
{
2421
return [
25-
'algorithm' => array_merge(
26-
[
27-
'laravel.password',
28-
'laravel.bcrypt',
29-
'laravel.argon',
30-
'laravel.argon2id'
31-
],
32-
hash_algos()
33-
),
22+
'algorithm' => hash_algos(),
3423
];
3524
}
3625

3726
public function handle($value = null, array $settings = []): string
3827
{
39-
if (Str::startsWith($settings['algorithm'], 'laravel.')) {
40-
return $this->handleLaravelHash($value, $settings);
41-
}
42-
4328
return hash($settings['algorithm'], $value);
4429
}
45-
46-
public function handleLaravelHash($value = null, array $settings = []): string
47-
{
48-
switch ($settings['algorithm']) {
49-
case 'laravel.password':
50-
return LaravelHashFascade::make($value);
51-
break;
52-
case 'laravel.bcrypt':
53-
return LaravelHashFascade::driver('bcrypt')->make($value);
54-
break;
55-
case 'laravel.argon':
56-
return LaravelHashFascade::driver('argon')->make($value);
57-
break;
58-
case 'laravel.argon2id':
59-
return LaravelHashFascade::driver('argon2id')->make($value);
60-
break;
61-
}
62-
}
6330
}

src/Modifiers/Password.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace SimonHamp\LaravelNovaCsvImport\Modifiers;
4+
5+
use Illuminate\Support\Facades\Hash;
6+
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier;
7+
use Illuminate\Support\Str;
8+
9+
class Password implements Modifier
10+
{
11+
public function title(): string
12+
{
13+
return 'Password hash value';
14+
}
15+
16+
public function description(): string
17+
{
18+
return 'Treat the the value as a password and securely hash it using the chosen hashing algorithm.<br>
19+
Your default hashing algorithm is: <b>'. Hash::getDefaultDriver() . '</b>';
20+
}
21+
22+
public function settings(): array
23+
{
24+
return [
25+
'algorithm' => [
26+
'type' => 'select',
27+
'options' => [
28+
'bcrypt',
29+
'argon',
30+
'argon2id'
31+
],
32+
'help' => 'See the <a href="https://laravel.com/docs/hashing" target="_blank">Laravel documentation</a>
33+
for more info on password hashing',
34+
],
35+
];
36+
}
37+
38+
public function handle($value = null, array $settings = []): string
39+
{
40+
return Hash::driver($settings['algorithm'])->make($value);
41+
}
42+
}

0 commit comments

Comments
 (0)