Yii2: Yii 2.0.17 + Yiii2-mongodb ~2.1.0 Flash message does no work

Created on 19 Apr 2019  ·  16Comments  ·  Source: yiisoft/yii2

If using MongoDB as session storage, Flash messages do not work.

'session' => [
            'name' => 'backend',
            'class' => 'yii\mongodb\Session',
            'sessionCollection' => 'BackendSessions',
],
/**
     * Changing user password
     *
     * @param string $id User id
     * @return mixed
     * @throws NotFoundHttpException
     */
    public function actionChangePassword($id)
    {
        $model = User::findOne($id);
        if (empty($model)) {
            throw new NotFoundHttpException("User specified not found!");
        }

        $password = User::generateRandomPassword();
        $model->setPassword($password);
        $model->generateAuthKey();

        if ($model->save(false)) {
            \Yii::$app->session->setFlash('success', 'New password was set: "'.$password.'"');
        } else {
            \Yii::$app->session->setFlash('error', 'There was an error changing password!');
        }

        return $this->redirect(['view', 'id' => $id]);
    }

Message is list after redirect. It works if I stop storing sessions in MongoDB.

bug

Most helpful comment

Mongodb Session class not extends DbSession.

All 16 comments

The problem is here in latest framework version. It works well with 2.0.15.1.

I've translated issue into English so community can participate as a whole.

If the version of MongoDB was not changed, then it seems the problem is somewhere in the framework. Since you have a clear way to reproduce it please do bisect to find out exact commit that broke it. Here's how: https://git-scm.com/docs/git-bisect or in Russian: https://git-scm.com/book/ru/v1/Инструменты-Git-Отладка-с-помощью-Git

This problem after commit:

Class yii\mongodb\Session extends MultiFieldSession

Function MultiFieldSession::composeFields was changed.
If you return the old feature code, it works fine.

The session ID changes from one page to another.

..

db.BackendSessions.remove({});
WriteResult({ "nRemoved" : 4 })

refresh site page by F5

db.BackendSessions.find().pretty();
{
"_id" : ObjectId("5d79f9882893100f3029bfa4"),
"id" : "3racnq6iq3gtm8ma3has4nueva",
"data" : "__flash|a:0:{}__id|s:24:\"5a65db240a64100202b7ca65\";"
}

refresh site page by F5

db.BackendSessions.find().pretty();
{
"_id" : ObjectId("5d79f9882893100f3029bfa4"),
"id" : "04gl55gs0ecj22q61tu3s9l475",
"data" : "__flash|a:0:{}__id|s:24:\"5a65db240a64100202b7ca65\";"
}

refresh site page by F5

db.BackendSessions.find().pretty();
{
"_id" : ObjectId("5d79f9882893100f3029bfa4"),
"id" : "ilcs8pkjetisbie47p0h524adb",
"data" : "__flash|a:0:{}__id|s:24:\"5a65db240a64100202b7ca65\";"
}

...

Does PHP write it into MongoDB at all?

Снимок экрана от 2019-09-12 12-17-06

This log for each page..

@lubosdz any idea on why would it break?

@ailmanki thanks, that would be awesome.

Mongodb Session class not extends DbSession.

Mongodb Session class not extends DbSession.

So is the issue solved ?

I reckon yii2-mongo sessionWrite has not been updated accordingly after bugfix - compare:
https://github.com/yiisoft/yii2-mongodb/blob/master/src/Session.php#L137
https://github.com/yiisoft/yii2/blob/master/framework/web/DbSession.php#L186

Unfortunatelly I dont have mongo setup ..

Edit:
Related mongo issue #279 - see e.g. failing build.

So is the issue solved ?

No.

Fixed in PR #285.

CI fail not related to this PR - see build log - PHP 5.5. not pre-installed (is PHP 5.5 officially supported or can this be removed?).

@webspec2012 can you test with dev-master of the yiisoft/yii2-mongo?

The `dev-master ' branch works correctly!

Was this page helpful?
0 / 5 - 0 ratings