2

We have a working Yii2 app that was Frankensteinized using Yii1's old version as a base then updated to Yii2. Of course there were a lot of problems and I managed to fix most, however there is a problem that keeps evading me.

As soon as we switch the environment to anything else than DEV, this code isn't executed anymore:

$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
    'class' => 'yii\debug\Module',
    // uncomment the following to add your IP if you are not connecting from localhost.
    //'allowedIPs' => ['127.0.0.1', '::1'],
];

When this isn't executed, we can't log in anymore. I isolated that issue by leaving the DEV environment on and just commenting that bit. I have no idea why it seems like the session or Identity is not saving the user id and just keeps showing back the login page.

I know the login, User and IdentityInterface changed a lot with Yii2 and my dodgy user component/Interface might be one of the causes, but I can't figure out the problem and was wondering if someone would have a strike of "déjà-vu" and recognize why the debug module made it work.

I fixed it temporarily by adding this, in the init() method:

class UserIdentity extends \yii\web\User implements \yii\web\IdentityInterface
{
    function init()
    {
        parent::init();
        $this->id = Yii::$app->session->get('id');
    }
}

This way, Yii::$app->user->id will always have the $_SESSION value in it
.
.
.
.
.
As requested, my complete web.php config code:

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'name' => 'NAME WIL BE HERE',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm' => '@vendor/npm-asset',
    ],
    'layout' => 'column1',
    'components' => [
        'request' => [
            'class' => 'app\components\NTDI_HttpRequest',
            'cookieValidationKey' => 'XXXXXXXXXXXXXXXXXXXX',
        ],
        'session' => [
            'class' => 'yii\web\DbSession',
            'name'=>'sess',
            'timeout'=>3600,
            'db'=>'db',
            'sessionTable'=>'session',
        ],
        'user' => [
            'identityClass' => 'app\components\UserIdentity',
            'class' => 'app\components\UserIdentity',
            'loginUrl' => array('/site/login'),
            'returnUrl' => array('/site/index'),
            'enableAutoLogin' => false,
        ],
        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
        'language' => 'fr_CA',
        'sourceLanguage' => 'en_US',
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => false,
            'showScriptName' => true,
            'rules' => [
                '' => 'site/index',
                'membre' => 'site/login',
                'nip' => 'member/nip',
                'verif' => 'member/verif',
                'vote' => 'member/vote',
                'merci' => 'member/logout',
            ],
        ],
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'fileMap' => [
                        'general' => 'general.php',
                        'landing' => 'landing.php',
                        'vote' => 'vote.php',
                        'app/error' => 'error.php',
                    ],
                ],
            ],
        ],
    ],
    'params' => $params,
];


// #BUG# If this is not set, we can't login anymore and the system keeps asking for a username numnber.. why??
//*
if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        //'allowedIPs' => ['127.0.0.1', '::1'],
    ];
}
//*/

if (YII_ENV_DEV) {
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        // uncomment the following to add your IP if you are not connecting from localhost.
        'allowedIPs' => ['127.0.0.1', '::1', '192.222.216.153', '24.37.138.46'],
    ];
}

return $config;

As requested by @rob006, my UserIdentity code. (sorry it's messy, most of that code was already there and I didn't want to change it too much

NaturalBornCamper
  • 3,675
  • 5
  • 39
  • 58
  • 1
    are you using dektrium-user module ? – Muhammad Omer Aslam Apr 20 '18 at 19:27
  • and the above code is part of main-local.php file? – Muhammad Omer Aslam Apr 20 '18 at 19:30
  • show all the code in main.php or main-local.php – ScaisEdge Apr 20 '18 at 19:32
  • I'm using the basic Yii2 template with the config file named "web.php", I'll add it right away to my question. No idea what is dektrium-user module so I guess we're not using that – NaturalBornCamper Apr 20 '18 at 19:39
  • I found a hacky temporary fix by the way, I added it in my question and I can now login, however it's not very healthy hahaha! – NaturalBornCamper Apr 20 '18 at 20:40
  • Lol ! @NaturalBornCamper you are placing **`CAMPERS`** in the `init()`, you just need to change the condition :D see my answer :D – Muhammad Omer Aslam Apr 20 '18 at 23:44
  • @NaturalBornCamper What is in your `UserIdentity`? You probably messed up something in it. – rob006 Apr 21 '18 at 18:49
  • @MuhammadOmerAslam what are CAMPERS Did you delete your answer? – NaturalBornCamper Apr 23 '18 at 13:12
  • @rob006 yes.. I don't get 100% how the new UserIdentity and User classes work. I'm pretty sure that's the problem too but I didn't want to touch the old code too much and mess it up. It's too long to post here so I'll add a pastebin with all of it – NaturalBornCamper Apr 23 '18 at 13:13
  • my answer was not adressing the actual problem and was a wrong recommendation as pointed out by roob – Muhammad Omer Aslam Apr 23 '18 at 13:41
  • You should read https://www.yiiframework.com/doc/api/2.0/yii-web-user and https://www.yiiframework.com/doc/api/2.0/yii-web-identityinterface - identity and web user are 2 separated components, you should not mix them into one object. I suggest to throw this code away i start from scratch using examples from guide/API and app templates. – rob006 Apr 23 '18 at 13:42
  • I did read it and was very confused because the old login is quite different.. And it would be the best to restart yes, except the project is due soon and we have limited time + this code will be dumped for .net after this project, so I won't do that. I guess I'll leave my hack as is – NaturalBornCamper Apr 23 '18 at 15:25

0 Answers0