0

When deploying my application there is of course always an admin user.

How can I create such an admin user as a first user without any interaction ...

  • ... by means of SQL?
  • ... using a Yii2-migration?
WeSee
  • 3,158
  • 2
  • 30
  • 58
  • 1
    the simplest way is the migration .. once you have create the tables you need you can populate with the Initial data necessary for the proper functioning of the application – ScaisEdge Feb 28 '17 at 19:18
  • @scaisEdge How can the password of the ```admin``` user be set in a migration? – WeSee Feb 28 '17 at 19:19
  • 1
    you can use a default password and let the user the possibility for change the password once installed .. – ScaisEdge Feb 28 '17 at 19:20
  • @scaisEdge: Is there a command to create a new user with a password? I don't want to have an user interaction here. If I can specify a default password that's fine, too. – WeSee Feb 28 '17 at 19:24
  • just to avoid misunderstandings Normally the creation of user (and password) for an application are managed by application itself .. the user (and password) for application are not the user/passwordc for db .. so the creation of user/password in migration can use the same class and function for create an application user. hope this is clear .. – ScaisEdge Feb 28 '17 at 19:28
  • I just saw ```User::create()``` creates a user with a given plain text password. This is what I was looking for. And if the user is called "admin" and this is specified in ```config/web.php``` the initial admin user is created. – WeSee Feb 28 '17 at 19:38
  • well seems you have found what's you need .. – ScaisEdge Feb 28 '17 at 19:40
  • @scaisEdge thanks for your help. – WeSee Feb 28 '17 at 19:41

1 Answers1

2

Found it. There is an easy way to do this with Yii2 builtin migrations.

In Yii2-user there are some hooks we can use to create users.

This code has to be inserted in a migration. after creating a new migration ./yii migrate/create, preferably after creating initial tables in the database:

use yii\db\Transaction;
use app\models\user\User;

public function safeUp()
{
    $transaction = $this->getDb()->beginTransaction();
    $user = \Yii::createObject([
        'class'    => User::className(),
        'scenario' => 'create',
        'email'    => 'admin',
        'username' => 'admin@example.com',
        'password' => 'mysecret',
    ]);
    if (!$user->insert(false)) {
        $transaction->rollBack();
        return false;
    }
    $user->confirm();
    $transaction->commit();
}

The skeleton code can be found in ./migrations/....

Don't forget to add database config parameters in ./config/db.php and the user module in ./config/console.php

WeSee
  • 3,158
  • 2
  • 30
  • 58
  • how we can use this code, if importing from excel.csv – Joshi Apr 29 '20 at 20:43
  • @Joshi Take the transaction and put it in your controller. Works and is quite easy. – WeSee Apr 29 '20 at 20:47
  • Thanks. can you elaborate little more, how I can do it. It will be quite helpful. if you want I can create a new question and share the link. – Joshi Apr 29 '20 at 20:50
  • A new quedtion is a good idea. Here are some hints: actionUpload -> transaction -> try -> create user per csv-line -> commit if no error -> catch for rollback. – WeSee Apr 29 '20 at 21:03
  • I get some idea what you are saying, like for example for action upload, can I upload CSV file with upload interface in a form? BTW - I have created the question link - https://stackoverflow.com/questions/61511927/yii2-import-multiple-users-from-csv-file – Joshi Apr 29 '20 at 21:23