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

Handle DB session callback custom fields before session closed #17188

Merged
merged 19 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.17 under development
------------------------

- Bug #9438, #13740, #15037: Handle DB session callback custom fields before session closed (lubosdz)
- Bug #17185: Fixed `AssetManager` timestamp appending when a file is published manually (GHopperMSK)
- Bug #17156: Fixes PHP 7.2 warning when a data provider has no data as a parameter for a GridView (evilito)
- Bug #17083: Fixed `yii\validators\EmailValidator::$checkDNS` tells that every domain is correct on alpine linux (mikk150)
Expand Down
40 changes: 36 additions & 4 deletions framework/web/DbSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class DbSession extends MultiFieldSession
*/
public $sessionTable = '{{%session}}';

/**
* @var array Session fields to be written into session table columns
lubosdz marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.0.17
*/
protected $fields = [];

/**
* Initializes the DbSession component.
Expand Down Expand Up @@ -136,6 +141,19 @@ public function regenerateID($deleteOldSession = false)
}
}

/**
* Ends the current session and store session data.
* @since 2.0.17
*/
public function close()
lubosdz marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->getIsActive()) {
// prepare writeCallback fields before session closes
$this->fields = $this->composeFields();
YII_DEBUG ? session_write_close() : @session_write_close();
}
}

/**
* Session read handler.
* @internal Do not call this method directly.
Expand Down Expand Up @@ -169,14 +187,28 @@ public function writeSession($id, $data)
// exception must be caught in session write handler
// https://secure.php.net/manual/en/function.session-set-save-handler.php#refsect1-function.session-set-save-handler-notes
try {
$fields = $this->composeFields($id, $data);
$fields = $this->typecastFields($fields);
$this->db->createCommand()->upsert($this->sessionTable, $fields)->execute();
// ensure backwards compatability (fixed #9438)
if ($this->writeCallback && !$this->fields) {
$this->fields = $this->composeFields();
}
// ensure data consistency
lubosdz marked this conversation as resolved.
Show resolved Hide resolved
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->fields = $this->typecastFields($this->fields);
$this->db->createCommand()->upsert($this->sessionTable, $this->fields)->execute();
$this->fields = [];
} catch (\Exception $e) {
Yii::$app->errorHandler->handleException($e);
return false;
}

return true;
}

Expand Down
29 changes: 9 additions & 20 deletions framework/web/MultiFieldSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,19 @@ public function getUseCustomStorage()

/**
* Composes storage field set for session writing.
* @param string $id session id
* @param string $data session data
* @param string $id Optional session id
* @param string $data Optional session data
* @return array storage fields
*/
protected function composeFields($id, $data)
protected function composeFields($id = null, $data = null)
{
$fields = [
'data' => $data,
];
if ($this->writeCallback !== null) {
$fields = array_merge(
$fields,
call_user_func($this->writeCallback, $this)
);
if (!is_string($fields['data'])) {
$_SESSION = $fields['data'];
$fields['data'] = session_encode();
}
$fields = $this->writeCallback ? call_user_func($this->writeCallback, $this) : [];
if($id !== null){
lubosdz marked this conversation as resolved.
Show resolved Hide resolved
$fields['id'] = $id;
}
if($data !== null){
$fields['data'] = $data;
}
// ensure 'id' and 'expire' are never affected by [[writeCallback]]
$fields = array_merge($fields, [
'id' => $id,
'expire' => time() + $this->getTimeout(),
]);
return $fields;
}

Expand Down