Skip to content

Commit 7fc3386

Browse files
authored
Merge pull request #74 from simonhamp/feature/password-hashing
Feature/password hashing
2 parents c976287 + 8b6edaf commit 7fc3386

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

dist/js/tool.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mix-manifest.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"/js/tool.js": "/js/tool.js",
3-
"/css/tool.css": "/css/tool.css"
2+
"/js/tool.js": "/js/tool.js"
43
}

resources/js/pages/Configure.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,21 @@
102102
<optgroup label="Custom - same for all">
103103
<option value="custom">Single value</option>
104104
</optgroup>
105+
106+
<optgroup label="Custom - different for each row">
107+
<option value="random">Randomly-generated value</option>
108+
</optgroup>
105109
</SelectControl>
106110

107111
<input v-model="values[field.attribute]" v-if="mappings[field.attribute] === 'custom'"
108112
class="form-control form-input form-input-bordered">
109113

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+
110120
<draggable
111121
v-model="modifiers[field.attribute]"
112122
handle=".handle"
@@ -189,6 +199,7 @@ export default {
189199
mappings: this.config?.mappings || {},
190200
values: this.config?.values || {},
191201
modifiers: this.config?.modifiers || {},
202+
random: this.config?.random || {},
192203
saving: false,
193204
};
194205
},
@@ -262,6 +273,7 @@ export default {
262273
values: this.values,
263274
modifiers: this.modifiers,
264275
file: this.file,
276+
random: this.random,
265277
};
266278
267279
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
@@ -79,6 +79,7 @@ public function storeConfig(NovaRequest $request)
7979
'resource' => $request->input('resource'),
8080
'mappings' => $request->input('mappings'),
8181
'values' => $request->input('values'),
82+
'random' => $request->input('random'),
8283
'modifiers' => collect($request->input('modifiers'))
8384
->map(function ($modifiers) {
8485
return collect($modifiers)
@@ -109,6 +110,7 @@ public function preview(NovaRequest $request, string $file): Response
109110
$import = $this->importer
110111
->setAttributeMap($columns = $config['mappings'])
111112
->setCustomValues($config['values'])
113+
->setRandomStringSettings($config['random'])
112114
->setMeta($config['meta'])
113115
->setModifiers($config['modifiers'])
114116
->toCollection($this->getFilePath($file), $this->getDisk())
@@ -157,6 +159,7 @@ public function import(ImportNovaRequest $request)
157159
->setModelClass($model_class)
158160
->setMeta($config['meta'])
159161
->setCustomValues($config['values'])
162+
->setRandomStringSettings($config['random'])
160163
->setModifiers($config['modifiers'])
161164
->import($path, $this->getDisk());
162165

src/Importer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SimonHamp\LaravelNovaCsvImport;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Hash;
67
use Illuminate\Support\Str;
78
use Laravel\Nova\Resource;
89
use Maatwebsite\Excel\Concerns\Importable;
@@ -114,6 +115,22 @@ public function setMeta(array $meta): self
114115
return $this;
115116
}
116117

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+
117134
public function getCustomValues($key = null)
118135
{
119136
if ($key) {
@@ -162,6 +179,8 @@ protected function getFieldValue(array $row, string $mapping, string $attribute)
162179
return $row[$mapping];
163180
} elseif (Str::startsWith($mapping, 'meta')) {
164181
return $this->getMeta(Str::remove('@meta.', "@{$mapping}"));
182+
} elseif ($mapping === 'random') {
183+
return Str::random($this->getRandomStringSettings($attribute));
165184
} elseif ($mapping === 'custom') {
166185
return $this->getCustomValues($attribute);
167186
}

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)