Skip to content

Commit

Permalink
Merge pull request #74 from simonhamp/feature/password-hashing
Browse files Browse the repository at this point in the history
Feature/password hashing
  • Loading branch information
simonhamp committed Aug 14, 2023
2 parents c976287 + 8b6edaf commit 7fc3386
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/js/tool.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"/js/tool.js": "/js/tool.js",
"/css/tool.css": "/css/tool.css"
"/js/tool.js": "/js/tool.js"
}
12 changes: 12 additions & 0 deletions resources/js/pages/Configure.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@
<optgroup label="Custom - same for all">
<option value="custom">Single value</option>
</optgroup>

<optgroup label="Custom - different for each row">
<option value="random">Randomly-generated value</option>
</optgroup>
</SelectControl>

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

<label class="flex items-center space-x-2" v-if="mappings[field.attribute] === 'random'">
<span>Length</span>
<input v-model="random[field.attribute]"
class="form-control form-input form-input-bordered">
</label>

<draggable
v-model="modifiers[field.attribute]"
handle=".handle"
Expand Down Expand Up @@ -189,6 +199,7 @@ export default {
mappings: this.config?.mappings || {},
values: this.config?.values || {},
modifiers: this.config?.modifiers || {},
random: this.config?.random || {},
saving: false,
};
},
Expand Down Expand Up @@ -262,6 +273,7 @@ export default {
values: this.values,
modifiers: this.modifiers,
file: this.file,
random: this.random,
};
Nova.request()
Expand Down
2 changes: 2 additions & 0 deletions src/Concerns/HasModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SimonHamp\LaravelNovaCsvImport\Modifiers\DefaultValue;
use SimonHamp\LaravelNovaCsvImport\Modifiers\ExcelDate;
use SimonHamp\LaravelNovaCsvImport\Modifiers\Hash;
use SimonHamp\LaravelNovaCsvImport\Modifiers\Password;
use SimonHamp\LaravelNovaCsvImport\Modifiers\Prefix;
use SimonHamp\LaravelNovaCsvImport\Modifiers\Str as StrModifier;
use SimonHamp\LaravelNovaCsvImport\Modifiers\Suffix;
Expand All @@ -28,6 +29,7 @@ protected function bootHasModifiers()
new ExcelDate,
new StrModifier,
new Hash,
new Password,
new Prefix,
new Suffix,
);
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function storeConfig(NovaRequest $request)
'resource' => $request->input('resource'),
'mappings' => $request->input('mappings'),
'values' => $request->input('values'),
'random' => $request->input('random'),
'modifiers' => collect($request->input('modifiers'))
->map(function ($modifiers) {
return collect($modifiers)
Expand Down Expand Up @@ -109,6 +110,7 @@ public function preview(NovaRequest $request, string $file): Response
$import = $this->importer
->setAttributeMap($columns = $config['mappings'])
->setCustomValues($config['values'])
->setRandomStringSettings($config['random'])
->setMeta($config['meta'])
->setModifiers($config['modifiers'])
->toCollection($this->getFilePath($file), $this->getDisk())
Expand Down Expand Up @@ -157,6 +159,7 @@ public function import(ImportNovaRequest $request)
->setModelClass($model_class)
->setMeta($config['meta'])
->setCustomValues($config['values'])
->setRandomStringSettings($config['random'])
->setModifiers($config['modifiers'])
->import($path, $this->getDisk());

Expand Down
19 changes: 19 additions & 0 deletions src/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SimonHamp\LaravelNovaCsvImport;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Laravel\Nova\Resource;
use Maatwebsite\Excel\Concerns\Importable;
Expand Down Expand Up @@ -114,6 +115,22 @@ public function setMeta(array $meta): self
return $this;
}

public function getRandomStringSettings($key = null)
{
if ($key) {
return $this->random_string_settings[$key] ?? '';
}

return $this->random_string_settings;
}

public function setRandomStringSettings(array $map): self
{
$this->random_string_settings = $map;

return $this;
}

public function getCustomValues($key = null)
{
if ($key) {
Expand Down Expand Up @@ -162,6 +179,8 @@ protected function getFieldValue(array $row, string $mapping, string $attribute)
return $row[$mapping];
} elseif (Str::startsWith($mapping, 'meta')) {
return $this->getMeta(Str::remove('@meta.', "@{$mapping}"));
} elseif ($mapping === 'random') {
return Str::random($this->getRandomStringSettings($attribute));
} elseif ($mapping === 'custom') {
return $this->getCustomValues($attribute);
}
Expand Down
42 changes: 42 additions & 0 deletions src/Modifiers/Password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace SimonHamp\LaravelNovaCsvImport\Modifiers;

use Illuminate\Support\Facades\Hash;
use SimonHamp\LaravelNovaCsvImport\Contracts\Modifier;
use Illuminate\Support\Str;

class Password implements Modifier
{
public function title(): string
{
return 'Password hash value';
}

public function description(): string
{
return 'Treat the the value as a password and securely hash it using the chosen hashing algorithm.<br>
Your default hashing algorithm is: <b>'. Hash::getDefaultDriver() . '</b>';
}

public function settings(): array
{
return [
'algorithm' => [
'type' => 'select',
'options' => [
'bcrypt',
'argon',
'argon2id'
],
'help' => 'See the <a href="https://laravel.com/docs/hashing" target="_blank">Laravel documentation</a>
for more info on password hashing',
],
];
}

public function handle($value = null, array $settings = []): string
{
return Hash::driver($settings['algorithm'])->make($value);
}
}

0 comments on commit 7fc3386

Please sign in to comment.