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

Fix sessionWrite callback fields #285

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.1.8 under development
-----------------------

- no changes in this release.
- Bug #285: Fix `sessionWrite` callback fields (related to https://github.com/yiisoft/yii2/issues/17559 and https://github.com/yiisoft/yii2/pull/17188) (lubosdz)


2.1.7 March 30, 2018
Expand Down
31 changes: 30 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Session extends MultiFieldSession
*/
public $sessionCollection = 'session';

/**
* @var array Session fields to be written into session table columns
* @since 2.1.8
*/
protected $fields = [];

/**
* Initializes the Session component.
Expand Down Expand Up @@ -139,11 +144,35 @@ public function writeSession($id, $data)
// exception must be caught in session write handler
// http://us.php.net/manual/en/function.session-set-save-handler.php
try {

// ensure backwards compatability, related to:
// https://github.com/yiisoft/yii2/pull/17188
// https://github.com/yiisoft/yii2/pull/17559
if ($this->writeCallback && !$this->fields) {
$this->fields = $this->composeFields();
}

// ensure data consistency
if (!isset($this->fields['data'])) {
$this->fields['data'] = $data;
} else {
$_SESSION = $this->fields['data'];
}

// ensure 'id' and 'expire' are never affected by [[writeCallback]]
$this->fields = array_merge($this->fields, [
'id' => $id,
'expire' => time() + $this->getTimeout(),
]);

$this->db->getCollection($this->sessionCollection)->update(
['id' => $id],
$this->composeFields($id, $data),
$this->fields,
['upsert' => true]
);

$this->fields = [];

} catch (\Exception $e) {
Yii::$app->errorHandler->handleException($e);
return false;
Expand Down