bfinlay / laravel-excel-seeder

Seed your database with Laravel using Excel and CSV files
Other
38 stars 8 forks source link

How to encrypt passwords? #16

Closed alquialrium closed 1 year ago

alquialrium commented 1 year ago

I installed the package and loaded the users from excel without problems, but I don't know how to encrypt the password field.

Watching the documentation, I did the following adding it to UserSeeder:

`<?php

namespace Database\Seeders;

use App\Models\User;
use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class UserSeeder extends SpreadsheetSeeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
     public function run()
    {
        User::create([
            'name' => 'Administrador',
            'username' => 'Admin',
            'password' => Bcrypt('password'),
            'user_type' => 'Admin',
        ]);
    }

    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeders/users.xlsx';
        $set->hashable = ['password'];
    }
}`

My DatabaseSeeder file:

` <?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            SpreadsheetSeeder::class,
        ]);
        $this->call(PermissionSeeder::class);
    }
}`

But I get that error: Could not check compatibility between Database\Seeders\DatabaseSeeder::settings(Database\Seeders\SpreadsheetSeederSettings $set) and bfinlay\SpreadsheetSeeder\SpreadsheetSeeder::settings(bfinlay\SpreadsheetSeeder\SpreadsheetSeederSettings $set), because class Database\Seeders\SpreadsheetSeederSettings is not available

I will like to use Bcrypt(), I don't know which type uses hashable

I put all in the DatabaseSeeder and same:

` <?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class DatabaseSeeder extends SpreadsheetSeeder
{
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeders/users.xlsx';
        $set->hashable = ['password'];
    }
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            SpreadsheetSeeder::class,
        ]);
        $this->call(UserSeeder::class);
    }
}

`

bfinlay commented 1 year ago

Just saw your question.

Hashable uses the Hash facade in Laravel: https://github.com/bfinlay/laravel-excel-seeder#hashable. If you follow the documentation and the link to the Laravel documentation, it explains that Laravel uses Bcrypt by default and you will find more documentation on how to configure Bcrypt settings (or how to change to a different driver).

Hashable $hashable (array ['password'])

This is an array of column names in the data source that should be hashed using Laravel's Hash facade.

The hashing algorithm is configured in config/hashing.php per https://laravel.com/docs/master/hashing

Note: this setting is currently global and applies to all files or worksheets that are processed. All columns with the specified name in all files or worksheets will have hashing applied. To apply differently to different files, process files with separate Seeder instances.

Example: ['password']

Default: []

When you use SpreadsheetSeeder, the seeder creates rows in the database so you cannot use a model to create rows. So this will not work:

        User::create([
            'name' => 'Administrador',
            'username' => 'Admin',
            'password' => Bcrypt('password'),
            'user_type' => 'Admin',
        ]);

Instead, set this up as two classes following this documentation: https://github.com/bfinlay/laravel-excel-seeder#basic-usage

UsersSeeder:

use bfinlay\SpreadsheetSeeder\SpreadsheetSeeder;

class UserSeeder extends SpreadsheetSeeder
{    
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function settings(SpreadsheetSeederSettings $set)
    {
        $set->file = '/database/seeders/users.xlsx';
        $set->hashable = ['password'];
    }
}

Then edit your DatabaseSeeder to run the Spreadsheet Seeder:

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            UserSeeder::class,
        ]);
    }
}