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

Initial install of Yii Advanced - Username doesn't exist #35

Open
nathan005 opened this issue Apr 22, 2021 · 2 comments
Open

Initial install of Yii Advanced - Username doesn't exist #35

nathan005 opened this issue Apr 22, 2021 · 2 comments

Comments

@nathan005
Copy link

nathan005 commented Apr 22, 2021

After initial install of the Yii Advanced template (what I'm currently testing)
Your user table replaces the default Yii table.
In this process it renames username to name

this will throw errors in frontend and backend when logging in.
as username is called
ex: backend/views/main.php - line 67
Yii::$app->user->identity->username

and activeRecord - in /common/models/User.php
$this->_user = User::findByUsername($this->username);

These can all be replaced, but not sure why we need to rename username to name

I can update the references by pointing them to name instead of username, but seems like extra work


FYI - I get that it is by design that you are trying to override the Identity class.
However it doesn't seem to happen completely

In my fresh install sjaakp\pluto\models\User did not replace the Identity class.

@rossaddison
Copy link
Contributor

rossaddison commented Apr 24, 2021

Pluto install:
Summary:

  1. Download https://github.com/yiisoft/yii2-app-advanced zip.
  2. Extract to www.
  3. Move 2 console/migrations files out and away to temp folder. These build the old user table which we do not want.
m130524_201442_init.php
m190124_110200_add_verification_token_column_to_user_table.php
  1. Include "sjaakp/yii2-pluto":"*", in composer.json
  2. Run composer update in root folder eg. C:\wamp64\www\yii2-app-advanced-master\>composer update by means of the command prompt/console.
  3. Run init.bat in root folder eg. C:\wamp64\www\yii2-app-advanced-master\>init.bat to generate additional
    files such as the main-local.php files, inter alia.
  4. Create an .htaccess file in C:\wamp64\www\yii2-app-advanced-master\frontend\web> as seen below.
  5. Confirm database settings in new common\config\main-local.php.
  6. Create database yii2advanced with utf8mb4_unicode_ci or if you are retesting this make sure database default yii2advanced is empty.
  7. Insert the following in common/config/main.php: authManager as below
'authManager' => [
                    'class' => 'yii\rbac\DbManager'
],

  1. Confirm yii migrate should give you "No migration has been done before".
  2. Allow yii2 to pickup pluto user table migrations sitting in vendor/sjaakp/yii2-pluto/migrations by including:
    in console/config/main.php and in frontend/config/main.php
'bootstrap' => [
                   'log',
                   'pluto'
                  ],
  1. Include in console/config/main.php and in frontend/config/main.php
'modules' => ['pluto' => [ 'class' => 'sjaakp\pluto\Module',  ]]   
  1. run yii migrate and you should see only 1 migration:
C:\wamp64\www\yii2-app-advanced-master>yii migrate
Yii Migration Tool (based on Yii v2.0.42-dev)

Total 1 new migration to be applied:
        sjaakp\pluto\migrations\m000000_000000_init

Apply the above migration? (yes|no) [no]:
  1. At this stage we now need the auth namespaced migration files contained in https://github.com/rossaddison/yii2-h2h-core/tree/master/console/migrations/auth but before that we need to include the below code in console/config/main.php.
'controllerMap' => [
        'fixture' => [
            'class' => 'yii\console\controllers\FixtureController',
            'namespace' => 'common\fixtures',
          ],
         'migrate-db-namespaced' => [
                'class' => 'yii\console\controllers\MigrateController',
                'migrationNamespaces' => [
                        //sjaakp has NOT been aliased therefore use a double backslash
                        //installs the user table
                        '\\sjaakp\pluto\migrations',
                        
                        //console has been aliased in common/config/bootstrap.php which appears in web/index.php
                        //installs the mysql auth tables and inserts data
                        'console\migrations\auth',
                    
                        //frontend has been aliased in common/config/bootstrap.php which appears in web/index.php
                        //installs the works tables and inserts data
                        ///'frontend\migrations',
                ],
               'color'=>true,
               'comment'=> 'You are migrating the namespaced tables to database connection component db which is your administration database.',
               'db' => 'db',
               'interactive'=>1,
               'migrationPath' => null, // allows to disable not namespaced migration completely
          ],
    ],
  1. Copy the Namespaced auth folder as mentioned above into /console/migrations. Remove the last three files since these contain the roles and permissions that are to be inserted and will probably not be relevant to your situation but can be adapted.
  2. Run yii migrate-db-namespaced at the command prompt which is the command created in 15. above in the ControllerMap.
  3. Bypass the site/login by using pluto/signup in the browser bar to register first user which will be admin.

A few preliminaries...

Make sure

'authManager' => [
                    'class' => 'yii\rbac\DbManager'
],

is installed under common/config/main.php

Make sure

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
# use index.php as index file
DirectoryIndex index.php

is included in the .htaccess file in the web folder under frontend ie. frontend/web.

Double Check...

Moved the files

m130524_201442_init.php and,
m190124_110200_add_verification_token_column_to_user_table

into a temp folder under console/migrations/temp so that the 'yii migrate' command does not pick them up. These two mentioned files are responsible for creating the old user table that you will not use. You can then use 'yii migrate' and this should only pick up the sjaakp\pluto migration.

The answer otherwise is relatively simple. Personal choice. Sjaakp User model/table uses the field 'name' for the user. If you look in the migration file which is in vendor/sjaakp/yii2-pluto/migrations you will see:

$this->createTable('{{%user}}', [
            'id' => $this->primaryKey()->unsigned(),
           'name' => $this->string(60)->notNull()->unique(),

whereas in the advanced template User model/table uses field 'username' for the user. If you look in frontend/console/migrations you will see:

$this->createTable('{{%user}}', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull()->unique(),

Have you removed:

 'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
],

from your frontend/config/main.php and from your backend/config/main.php file. You will not need this setting since you will have the following module setting in each of these files ie.

'modules' => [
            'pluto' => [
                  'class' => 'sjaakp\pluto\Module',
                  'passwordFlags' => ['all' => 'reveal'],
                  'passwordHint' => Yii::t('app','At least eight characters, one uppercase, one digit'),
                  'passwordRegexp' => '/^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$/',
                 /////here the identity class is set to this model
                  'identityClass' => 'sjaakp\pluto\models\User',
                  'firstDefaultRole'=>'My Online Payer',
                  'defaultRole'=>'admin',  
                  //prevent the external guest signing up of users until site is stable by setting fenceMode to true
	          'fenceMode'=>true,
                  'viewOptions' => [
                     'row' => [ 'class' => 'row justify-content-center' ],
                     'col' => [ 'class' => 'col-md-6 col-lg-5' ],
                     'button' => [ 'class' => 'btn btn-success' ],
                     'link' => [ 'class' => 'btn btn-sm btn-secondary' ],
                  ],
             ],
],

Note the identityClass setting above which embraces the User model which has changed username to name.

Ensure you have:

                   'bootstrap' => [
                    'log',
                    'pluto'
                   ],

in your frontend/config/main.php AND in your console/config/main.php in order to initialize or bootstrap the vendor\sjaakp\yii2-pluto\Module at the start of the application via the init() function...according to bootstrapping.

Replace the ONLY TWO occurrences (frontend/views/layouts/main.php AND backend/views/layouts/main.php) of:
Yii::$app->user->identity->username

with
Yii::$app->user->identity->attributes['name']
or
Yii::$app->user->identity->name

The frontend/config/main.php module setting contains this line:

'identityClass' => 'sjaakp\pluto\models\User',

The bootstrap public function in vendor\sjaakp\yii2-pluto\Module:

$app->setComponents([
                'user' => ArrayHelper::merge($app->components['user'], [
                    'identityClass' => $this->identityClass,
                    'loginUrl' => [$this->id . '/default/login'],
                    'on beforeLogin' => [$this, 'beforeLogin']
                ]),
            ]);

You will see the merge command here. As noted in vendor\yiisoft\yii2\helpers\BaseArrayHelper here:

     Merges two or more arrays into one recursively.
     If each array has an element with the same string key value, the latter
     will overwrite the former (different from array_merge_recursive).

So the sjaakp user settings model will overwrite the advanced template even though it "merges" them because they have the same user setting. So there is no need for the components user settings in the frontend and backend ..../config/main.php.

A longwinded explanation but good for posterity I hope.

Tested 25/04/2021 [Wampserver 3.2.4](https://wampserver.aviatechno.net/) Windows 10 Apache 2.4.46 mySql5.7.31 php7.4.9

@nathan005
Copy link
Author

Thanks @rossaddison, I'll run though it on my end as well.
I appreciate the verbose description.
Lots of info I didn't see on the readme!

Will probably be a week before I have time, but I'll document if I have an issues.
(Local install one of my Macs - MAMP 6.3 - NGINX 1.19- PHP 7.4.12 - PGSQL 12)

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