Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documetation for developer to create user with service #479

Open
cgsmith opened this issue Oct 19, 2022 · 6 comments
Open

Update documetation for developer to create user with service #479

cgsmith opened this issue Oct 19, 2022 · 6 comments

Comments

@cgsmith
Copy link
Contributor

cgsmith commented Oct 19, 2022

What steps will reproduce the problem?

When following the guide for creating a user on a fresh install the process errors. This is based off the yii2-advanced-app

  1. Install yii2-advanced-app
  2. Install 2amigos/yii2-usuario & follow guide
  3. Try the migration listed and you will receive the error
<?php

use yii\db\Migration;

/**
 * Class m221019_020522_first_admin
 */
class m221019_020522_first_admin extends Migration
{
    public function safeUp()
    {
        $auth = Yii::$app->authManager;

        // create a role named "administrator"
        $administratorRole = $auth->createRole('admin');
        $administratorRole->description = 'Administrator';
        $auth->add($administratorRole);

        // create permission for certain tasks
        $permission = $auth->createPermission('user-management');
        $permission->description = 'User Management';
        $auth->add($permission);

        // let administrators do user management
        $auth->addChild($administratorRole, $auth->getPermission('user-management'));

        // create user "admin" with password "verysecret"
        $user = new \Da\User\Model\User([
                                            'scenario' => 'create',
                                            'email' => "[email protected]",
                                            'username' => "admin2",
                                            'password' => "verysecret"  // >6 characters!
                                        ]);
        $user->confirmed_at = time();
        $user->save();

        // assign role to our admin-user
        $auth->assign($administratorRole, $user->id);
    }

    public function safeDown()
    {
        $auth = Yii::$app->authManager;

        // delete permission
        $auth->remove($auth->getPermission('user-management'));

        // delete admin-user and administrator role
        $administratorRole = $auth->getRole("administrator");
        $user = \Da\User\Model\User::findOne(['username'=>"admin"]);
        $auth->revoke($administratorRole, $user->id);
        $user->delete();
        $auth->remove($administratorRole);
    }
}

What is the expected result?

User should be created without an error.

What do you get instead?

First there is an error about $this->module->enableGdprCompliance fails. Then I18N fails if the line in User is commented out.

@TonisOrmisson
Copy link
Contributor

You should not be creating an user like that. You should be using UserCreateService to create a new user. Via what you do - its not a bug.

Have a look at here for an example to create an user and assign a role
https://github.com/2amigos/yii2-usuario/blob/master/src/User/Command/CreateController.php#L48

@cgsmith
Copy link
Contributor Author

cgsmith commented Oct 19, 2022

This documentation should probably be updated then.

@cgsmith cgsmith changed the title Creating first user from console errors Update documetation for developer to create user with service Oct 19, 2022
@TonisOrmisson
Copy link
Contributor

Yup, the documentation is weird for that section, and has mistakes in it. I don't think we should be referring to a migration approach specifically for creating an user programmatically. Should be an example without the migration context and have something like https://github.com/2amigos/yii2-usuario/blob/master/src/User/Command/CreateController.php#L48

I ran the above example on my dev, bud did not get errors on migrate/up, but /down (has mistakes). Anyway the documentation for sure needs a change here.

@cgsmith
Copy link
Contributor Author

cgsmith commented Nov 2, 2022

Hi Tonis, I still have an issue even when running

$user = new User();

Exception: Attempt to read property "enableGdprCompliance" on null (/app/yii2-usuario/src/User/Model/User.php:167)
#0 /app/yii2-usuario/src/User/Model/User.php(167): yii\base\ErrorHandler->handleError(2, 'Attempt to read...', '/app/yii2-usuar...', 167)
#1 /app/vendor/yiisoft/yii2/base/Component.php(739): Da\User\Model\User->behaviors()
#2 /app/vendor/yiisoft/yii2/base/Component.php(520): yii\base\Component->ensureBehaviors()
#3 /app/common/models/User.php(18): yii\base\Component->on('beforeRegister', Object(Closure))
#4 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): common\models\User->init()
#5 /app/console/migrations/m221019_020522_first_admin.php(15): yii\base\BaseObject->__construct()
#6 /app/vendor/yiisoft/yii2/db/Migration.php(114): m221019_020522_first_admin->safeUp()
#7 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(758): yii\db\Migration->up()
#8 /app/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(216): yii\console\controllers\BaseMigrateController->migrateUp('m221019_020522_...')
#9 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#10 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#11 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#12 /app/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('', Array)
#13 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\console\Controller->runAction('', Array)
#14 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('migrate', Array)
#15 /app/vendor/yiisoft/yii2/console/Application.php(147): yii\console\Application->runAction('migrate', Array)
#16 /app/vendor/yiisoft/yii2/base/Application.php(384): yii\console\Application->handleRequest(Object(yii\console\Request))
#17 /app/yii(23): yii\base\Application->run()
#18 {main}

@TonisOrmisson
Copy link
Contributor

How does your user module config look like?

@cgsmith
Copy link
Contributor Author

cgsmith commented Nov 2, 2022

@TonisOrmisson ah that is it. I had not put this in common/config/main.php

<?php
return [
    'modules' => [
        'user' => [
            'class' => Da\User\Module::class,
            'administrators' => ['admin'],
        ],
    ]
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants