0

I have an email configured in the services.admin.key that has the email of the admin of the app.

The app only has one admin. Do you know the best approach so that someone who has access to the code from Git could run a command to configure the user? For example, if its a table with three columns:

name: admin
email: the email configured in`services.admin.key`
password: sometestpass

How can I allow someone who has access to the project to run some command to create one user with these details? Do you know what the best approach for this is? I doubt how to approach this properly if it should be with a seeder or another approach?

For example, I have this method on the user model that I use in some views to check if the current user is an admin.

 public function isAdministrator ()
 {
     if ($this->email == config('services.admin.key')) 
     {
            return true;
     }

     return false;
 }
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
OsG
  • 21
  • 1
  • 8
  • The most popular approach is to have a boolean column on the user table like is_admin. Then you do something similar like $this->is_admin – Diego Dieh Oct 25 '21 at 22:20
  • Thanks but in this case it should exist only one admin that should be configured before using the project. So that column would be null for all users unless for one user, the admin that should be configured before. – OsG Oct 25 '21 at 22:23
  • If you are sure that you want go that way, the best way to create that user is a migration. You can read the config in order to get its email and password. Migrations only runs once, so there you will have only one admin as you wish. – Diego Dieh Oct 25 '21 at 22:51
  • 1
    Thanks, but the migration is only to create the columns of the table right? It can be also used to create that initial admin user? Thanks! – OsG Oct 25 '21 at 23:03
  • Yes! It can be used to add/update/remove data from the database too. – Diego Dieh Oct 25 '21 at 23:36

1 Answers1

1

If you talk about fresh project on local database, it would be nice to get ready seeder for admin user.(not remote database). let's say I got access to git repo I could clone project, and run composer install for dependencies and than PHP command php artisan migrate --seed it would be good to have seeder ready for creating admin. To make seeder you need just php artisan make:seed AdminSeeder and after write in AdminSeeder file something like:

User::create(['name' => 'admin', 'email' => config('services.admin.key')),'password' => bcrypt('sometestpass'),]);

than in Database/seeders/DatabaseSeeder.php write $this->call(AdminSeeder::class); it's all. now when someone get fresh project it does 3 commands to make everything ready.

  1. composer install
  2. php artisan migrate --seed
  3. php artisan key:generate

hope I understood right way what you needed.

  • Thanks!! Can you just explain better why that "php artisan key:generate" is necessary? – OsG Oct 26 '21 at 10:40
  • Better explains here in https://laravel.com/docs/7.x/installation but in short : If the application key is not set, your user sessions and other encrypted data will not be secure! – Nikoloz Gvritishvili Oct 26 '21 at 11:51