2amigos / yii2-usuario

Highly customizable and extensible user management, authentication, and authorization Yii2 extension
https://github.com/2amigos/yii2-usuario
Other
294 stars 142 forks source link

User could not be registered. #266

Closed larry-tx closed 5 years ago

larry-tx commented 5 years ago

What steps will reproduce the problem?

Installed the extension exactly as described in the documentation. Specifically,

  1. Ran composer require 2amigos/yii2-usuario:~1.0
  2. Altered /console/config/main.php to read (in pertinent part):
    'controllerMap'       => [
            'fixture' => [
                'class'     => 'yii\console\controllers\FixtureController',
                'namespace' => 'common\fixtures',
            ],
            'migrate' => [
                'class'               => 'yii\console\controllers\MigrateController',
                'migrationPath' => [
                    '@app/migrations',
                    '@yii/rbac/migrations',
                ],
                'migrationNamespaces' => [
                    'Da\User\Migration',
                    'nemmo\attachments\migrations',
                ],
            ],
        ],
  3. Ran yii migrate --migrationPath=@yii/rbac/migrations. This picked up the migrations needed for yii2-usario as well as those needed by RBAC.
  4. Modified /backend/config/main.php to read (in pertinent part):
    'modules' => [
     'user'        => [
                'class'                   => Da\User\Module::class,
                'enableEmailConfirmation' => false,
                'generatePasswords'       => false,
                'administrators' => ['LarryTX'],
                // 'rememberLoginLifespan'   => 72, 576, 000,
                // ...other configs from here: [Configuration Options](installation/configuration-options.md), e.g.
                // 'generatePasswords' => true,
                // 'switchIdentitySessionKey' => 'myown_usuario_admin_user_key',
            ],
    ],
    'components' => [
    'authManager' => [
                'class' => 'yii\rbac\DbManager', // or use 'yii\rbac\DbManager'
            ],
            'user'        => [
                'identityClass'   => 'app\models\User',
                'enableAutoLogin' => true,
            ],
    ],
  5. Went to <backend URL for my app>/user/registration/register.
  6. Completed the registration form.
  7. Clicked the submit button.

    What is the expected result?

    A record would be entered in the user table of the database.

    What do you get instead?

    The following message was displayed: User could not be registered. I did find the following information in backend/runtime/app.log:

    2018-10-14 04:52:41 [127.0.0.1][-][-][error][usuario] Calling unknown method: yii\swiftmailer\Mailer::sethost()
    in D:\xampp\htdocs\public_html\vendor\2amigos\yii2-usuario\src\User\Traits\MailAwareTrait.php:45
    in D:\xampp\htdocs\public_html\vendor\2amigos\yii2-usuario\src\User\Service\UserRegisterService.php:72
    in D:\xampp\htdocs\public_html\vendor\2amigos\yii2-usuario\src\User\Controller\RegistrationController.php:108

    Since the very cryptic error message (User could not be registered) was the only information that I had to go on, I have no idea whether the SwiftMailer error would be sufficient to prevent registration when i had disabled email confirmation.

It is very clear that whoever assembled this extension had little understanding of the Yii2 advanced template. I had to do a great deal of experimentation to get it work at all in the advanced template. Your documentation should be amended to reflect the following:

  1. Don't bother to make any changes to the common/config/main.php file. They will simply generate a multitude of incomprehensible error messages.Make all your changes in backend/config/main.php and frontend/config/main.php. Completely violates the DRY principles embodied in the common feature of the advanced template, but that's what you get.
  2. Copy the user model from /common/models/User.php to /backend/models/User.php and /frontend/models/User.php. There is no User model in the models directories of the backend and frontend.
  3. You will save yourself a great deal of experimentation and untold grief by overriding the views files placing your overrides in the backend and frontend. Of course, you won't need all the overrides in the frontend.

Anyway, I'm still completely stuck after all this work without the ability to make use of any of the functionality of this extension. Hope someone who understands the advanced template, in use by a majority of those developing in Yii2, can help resolve this show-stopping enigma.

TonisOrmisson commented 5 years ago

Seems to be something wrong with your mailer configuration. Note that enableEmailConfirmation set to false will still sent the e-mail to user about creating an user. Its just not requiring the conformation click.

I have just installed a clean yii2-advanded app from scratch, installed and configured the usuario extension and it seems to work just fine.

larry-tx commented 5 years ago

I'm so happy that worked for you. Now, just how often does anyone ever actually put into production a Website that is perfectly clean and devoid of anything but one extension. That just isn't real. The real world doesn't work like that. A well-designed extension works in an environment with multiple other extensions, many, many, many models and their associated controllers and views, a layout that is different than what is shipped from Github, and so on. A well-designed extension insulates itself from other extensions.

mrbig00 commented 5 years ago

@LarryTX , I have 20+ apps with yii2-usuario in production and I did not had any problems.

And I have to agree with @TonisOrmisson, most likely this error is generated by the mailer configuration. Please disable the mail sending functionality and try again (and if this does not help, please post your config files also).

tonydspaniard commented 5 years ago

@LarryTX thank you very much for such detailed issued. Usuario is actually not doing anything with Mailer but using the mailer component that is already configured on your Yii application. The only thing it overrides (in case the developer requires to do so as we did in many apps) is the mail parameters (https://github.com/2amigos/yii2-usuario/blob/master/src/User/Service/MailService.php)

The MailService is instantiated by Yii2's di at all times, thus, injecting dynamically the mailer by using getMailer() (https://github.com/2amigos/yii2-usuario/blob/master/src/User/Factory/MailFactory.php#L136).

That error reminds me an issue I had when configuring the mailer component Yii, as the mailer uses a transport and is the transport the one that dynamically uses "set" . $config to the SwiftMailer's transport. So the component must be set as:

'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'localhost',
                'username' => 'username',
                'password' => 'password',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],
        // ...
    ],

The problem I had was that I did this:

'components' => [
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
             'host' => 'localhost', // <--- That was my mistake
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'username' => 'username',
                'password' => 'password',
                'port' => '587',
                'encryption' => 'tls',
            ],
        ],
        // ...
    ],

Do you mind checking if that was the issue? Thanks in advance

tonydspaniard commented 5 years ago

Closed by no reaction to the last answer. Hope that was the issue and the solution.