namespace app\components\services;
// PHP built-in exception:
use \Throwable;
use Yii;
use yii\db\Exception as YiiDbException;
use app\components\SessionManager;
use app\components\interfaces\SessionInterface;
use app\models\SessionUser;
use app\helpers\ExceptionLogger;
use app\helpers\LogHelper;
use app\helpers\RoleHelper;
/**
* Session User Service
*/
class SessionUserService
{
private bool $_debugMode = true;
private const SESSION_TOKEN_LENGTH = 32;
private const USER_AGENT_COMPARE_LENGTH = 30;
private SessionInterface $_sessionManager;
private ?SessionUser $_cachedSessionUser = null;
private ?string $_currentSessionId = null;
private array $_cachedSessionTokens = [];
public function __construct(?SessionInterface $sessionManager = null)
{
$this->_sessionManager = $sessionManager ?? new SessionManager();
}
public function hasSession(): bool
{
$sessionId = $this->_sessionManager->getId();
if (empty($sessionId))
{
return false;
}
$sessionToken = $this->getSessionToken($sessionId);
if ($sessionToken === null)
{
return false;
}
$key = SessionUser::SESSION_USER_KEY_PREFIX . substr($sessionToken, 0, self::SESSION_TOKEN_LENGTH);
return $this->_sessionManager->has($key);
}
public function writeSession(array $data = [], bool $allowCriticalFieldUpdate = false): bool
{
//$this->_sessionManager->destroy();// Debugging
// If $data array is empty, theirs nothing to write, return false.
if (empty($data))
{
LogHelper::debug('Attempted to write empty session data.');
return false;
}
// Validate 'extra_data' if it exists with validateJsonData().
if (isset($data['extra_data']) && !$this->validateJsonData($data['extra_data']))
{
if ($this->_debugMode)
{
LogHelper::error("Invalid extra_data JSON structure: " . json_encode($data['extra_data']), [
'extra data' => $data['extra_data'],
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
// Validate 'cart_data' if it exists with validateCartData().
if (isset($data['cart_data']) && !$this->validateCartData($data['cart_data']))
{
if ($this->_debugMode)
{
LogHelper::error("Invalid extra_data JSON structure: " . json_encode($data['cart_data']), [
'cart data' => $data['cart_data'],
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
// Retrieve the session ID and session token.
$sessionId = $this->_sessionManager->getId();
if (empty($sessionId))
{
if ($this->_debugMode)
{
LogHelper::debug('No session ID available during writeSession.');
}
return false;
}
// Fetch existing token or generate a new one
$sessionToken = $this->getSessionToken($sessionId);
if ($sessionToken === null)
{
$sessionToken = $this->generateSessionToken();
}
// Remove old session key if token changed or was present
$oldToken = $this->_cachedSessionTokens[$sessionId] ?? null;
if ($oldToken !== null && $oldToken !== $sessionToken)
{
$oldKey = SessionUser::SESSION_USER_KEY_PREFIX . substr($oldToken, 0, self::SESSION_TOKEN_LENGTH);
$this->_sessionManager->remove($oldKey);
}
// Update cached token
$this->_cachedSessionTokens[$sessionId] = $sessionToken;
$sessionTokenKey = substr($sessionToken, 0, self::SESSION_TOKEN_LENGTH);
try
{
// Prepare data to be stored in Yii session and DB
$sessionData = $this->initializeSessionData($sessionId, $sessionToken, $data);
// Store session data in Yii session storage using canonical key
$this->_sessionManager->set(SessionUser::SESSION_USER_KEY_PREFIX . $sessionTokenKey, $sessionData);
// Save or update session data in DB
return $this->saveSession($sessionId, $sessionData, $allowCriticalFieldUpdate);
}
catch (Throwable $e)
{
// Use the ExceptionLogger to handle and log the exception.
ExceptionLogger::log($e, 'sessionUser_writeSession_error_');
return false;
}
}
public function readSession(): ?array
{
//$this->_sessionManager->destroy();// Debugging
$sessionId = $this->_sessionManager->getId();
if ($this->_debugMode)
{
LogHelper::debug("SessionUser::readSession called with session_id: {$sessionId}", [
'session ID' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
if (empty($sessionId))
{
return null;
}
$sessionToken = $this->getSessionToken($sessionId);
if ($sessionToken === null)
{
return null;
}
// Subtract x amount from $sessionToken for validateSessionData() mechanism.
$sessionTokenKey = substr($sessionToken, 0, self::SESSION_TOKEN_LENGTH);
// Read session data from session storage (Yii)
$sessionData = $this->fetchSessionData($sessionTokenKey);
if ($sessionData === null)
{
return null;
}
// Get session record in DB
$sessionUser = $this->getSession($sessionId);
// Validate token match, IP/user-agent, expiration...
if ($sessionUser !== null && !$sessionUser->isExpired() &&
$this->validateSessionData($sessionData, $sessionUser) &&
$this->validateRequest($sessionUser)
)
{
return $sessionData;
}
if ($this->_debugMode)
{
LogHelper::debug("Session validation failed for session_id: {$sessionId}", [
'session ID' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return null;
}
private function fetchSessionData(string $sessionTokenKey): ?array
{
$key = SessionUser::SESSION_USER_KEY_PREFIX . $sessionTokenKey;
try
{
if (!$this->_sessionManager->has($key))
{
if ($this->_debugMode)
{
LogHelper::debug("Session key: {$key} not found in Yii session storage.", [
'key' => $key,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return null;
}
$data = $this->_sessionManager->get($key);
if ($this->_debugMode)
{
LogHelper::debug("Session data fetched from Yii session storage for key: {$key}", [
'key' => $key,
'session manager data' => json_encode($data),
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return $data;
}
catch (Throwable $e)
{
// Use the ExceptionLogger to handle and log the exception.
ExceptionLogger::log($e, 'sessionUser_fetchSessionData_error_');
}
return null;
}
private function saveSession(
string $sessionId,
array $sessionData,
bool $allowCriticalFieldUpdate = false
): bool
{
try
{
$sessionUser = $this->getSession($sessionId);
if ($sessionUser === null)
{
// Create new SessionUser record if missing
$sessionUser = new SessionUser();
$sessionUser->session_id = $sessionId;
$sessionUser->session_token = $sessionData['session_token'];
$sessionUser->user_ip = $sessionData['user_ip'];
$sessionUser->user_agent = $sessionData['user_agent'];
$sessionUser->extra_data = json_encode($sessionData['extra_data'] ?? [], JSON_THROW_ON_ERROR);
$sessionUser->cart_data = json_encode($sessionData['cart_data'] ?? [], JSON_THROW_ON_ERROR);
$sessionUser->expires_at = $sessionData['expires_at'] ?? null;
if (!$sessionUser->save(false))
{
throw new YiiDbException('Failed to create new SessionUser DB record.');
}
}
else
{
$updated = false;
if ($allowCriticalFieldUpdate && $sessionUser->session_id !== null)
{
foreach (['session_token', 'user_ip', 'user_agent'] as $field)
{
if (isset($sessionData[$field]) && $sessionUser->{$field} !== $sessionData[$field])
{
$sessionUser->{$field} = $sessionData[$field];
$updated = true;
}
}
}
$sessionUser->session_token = $sessionData['session_token'];
$sessionUser->user_ip = $sessionData['user_ip'];
$sessionUser->user_agent = $sessionData['user_agent'];
$sessionUser->extra_data = json_encode($sessionData['extra_data'] ?? [], JSON_THROW_ON_ERROR);
$sessionUser->cart_data = json_encode($sessionData['cart_data'] ?? [], JSON_THROW_ON_ERROR);
$sessionUser->expires_at = $sessionData['expires_at'] ?? null;
if ($updated || $sessionUser->isNewRecord)
{
if (!$sessionUser->save(false))
{
throw new YiiDbException('Failed to update user session in the database.');
}
}
}
// Update caches
$this->_cachedSessionUser = $sessionUser;
$this->_cachedSessionTokens[$sessionId] = $sessionUser->session_token;
return true;
}
catch (Throwable $e)
{
ExceptionLogger::log($e, 'sessionUser_saveSession_error_');
}
return false;
}
private function initializeSessionData(string $sessionId, string $sessionToken, array $data = []): array
{
return [
'session_id' => $sessionId,
'session_token' => $sessionToken,
'user_ip' => RoleHelper::userIp(),
'user_agent' => RoleHelper::userAgent(),
'extra_data' => array_merge($data['extra_data'] ?? [], [
// Add any new default values you want to merge into extra_data here.
// Example: 'additional_key' => 'additional_value',
]),
'cart_data' => $data['cart_data'] ?? [],// Accessing directly from the input.
'expires_at' => time() + SessionUser::SESSION_DURATION_SECONDS,
];
}
public function validateRequest(SessionUser $sessionUser): bool
{
$currentIp = RoleHelper::userIp();
$storedIp = $sessionUser->user_ip;
$storedAgent = $sessionUser->user_agent ?? '';
$currentAgent = RoleHelper::userAgent() ?? '';
//$storedAgent = $this->normalizeUserAgent($sessionUser->user_agent ?? '');
//$currentAgent = $this->normalizeUserAgent(RoleHelper::userAgent() ?? '');
if ($this->_debugMode)
{
LogHelper::debug("Stored IP: {$storedAgent}, Current IP: {$currentIp}", [
'stored user IP' => $storedIp,
'current IP' => $currentIp,
/*'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__*/
]);
LogHelper::debug("Stored IP: {$storedAgent}, Current IP: {$currentIp}", [
'stored user_agent' => $storedAgent,
'current user_agent' => $currentAgent
]);
}
if (!$this->ipMatches($storedIp, $currentIp))
{
if ($this->_debugMode)
{
LogHelper::debug("IP mismatch for session {$sessionUser->session_id}." , [
'session' => $sessionUser->session_id,
'stored user IP' => $storedIp,
'current IP' => $currentIp
]);
LogHelper::debug("IP mismatch for session {$sessionUser->session_id}." , [
'session' => $sessionUser->session_id,
'stored user IP' => $storedIp,
'current IP' => $currentIp,
]);
}
return false;
}
$storedAgent = $sessionUser->user_agent ?? '';
// Simplified example: compare only first 30 character
if (strncmp($storedAgent, $currentAgent, self::USER_AGENT_COMPARE_LENGTH) !== 0)
{
if ($this->_debugMode)
{
LogHelper::debug("User-Agent mismatch for session {$sessionUser->session_id}." , [
'session' => $sessionUser->session_id,
'stored user_agent' => $storedAgent,
'current user_agent' => $currentAgent,
'stored agent prefix 30' => substr($storedAgent, 0, 30),
'current agent prefix 30' => substr($currentAgent, 0, 30)
]);
}
return false;
}
return true;
}
private function normalizeUserAgent(string $ua): string
{
// Convert to lowercase for case-insensitive comparison
$uaClean = strtolower($ua);
/**
* Optionally remove digits but keep spaces and letters (tune based on your needs)
* Commented out as versions can matter:
*
$uaClean = preg_replace('/[0-9]/', '', $uaClean);//*/
/**
* Remove excessive whitespaces and normalize spaces
*/
$uaClean = preg_replace('/\s+/', ' ', $uaClean);//*/
/**
* Remove common punctuation that may vary but is insignificant
* Adjust to allow meaningful chars like dots and slashes if you want
*
$uaClean = preg_replace('/[\,;\(\)\[\]]/', '', $uaClean);//*/
// Trim leading/trailing spaces
$uaClean = trim($uaClean);
return $uaClean;
}
private function ipMatches(string $storedIp, string $currentIp): bool
{
// IPv4 subnet matching example
if (filter_var($storedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
&& filter_var($currentIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
)
{
return substr($storedIp, 0, strrpos($storedIp, '.')) === substr($currentIp, 0, strrpos($currentIp, '.'));
}
// Example IPv6 /64 comparison (first 4 blocks)
if (filter_var($storedIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
&& filter_var($currentIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)
)
{
$storedParts = explode(':', $storedIp);
$currentParts = explode(':', $currentIp);
return array_slice($storedParts, 0, 4) === array_slice($currentParts, 0, 4);
}
// Strict equality fallback for IPv6 or others
return $storedIp === $currentIp;
}
private function validateJsonData(array $jsonData): bool
{
if ($this->_debugMode)
{
LogHelper::debug("validateJsonData called with keys: " . implode(', ', array_keys($jsonData)), [
'json data' => implode(', ', array_keys($jsonData)),
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
// TODO Define allowed keys for 'extra_data'.
$allowedKeys = ['last_login_time', 'order_id', 'order_success_url', 'order_canceled_url'];
// Restrict the number of entries in 'extra_data'.
if (count($jsonData) > 10)
{
if ($this->_debugMode)
{
LogHelper::debug('extra_data exceeds maximum number of keys: ' . count($jsonData), [
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
foreach ($jsonData as $key => $value)
{
// Reject unexpected keys strictly
if (!is_string($key) || !in_array($key, $allowedKeys, true))
{
LogHelper::debug("Invalid extra_data key found: {$key}", [
'key' => $key,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
return false;
}
// Reject nested values (arrays/objects not allowed)
if (is_array($value) || is_object($value))
{
if ($this->_debugMode)
{
LogHelper::debug("extra_data value for key {$key} is a nested array/object which is not allowed.", [
'value type' => gettype($value),
'key' => $key,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
// Accept only string or int types
if (!is_string($value) && !is_int($value))
{
if ($this->_debugMode)
{
LogHelper::debug("Invalid type for extra_data key {$key}. Expected string or int, got "
. gettype($value),
[
'value type' => gettype($value),
'key' => $key,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
return false;
}
// Validate string length.
if (is_string($value) && mb_strlen($value) > 255)
{
if ($this->_debugMode)
{
LogHelper::debug("Value for key {$key} exceeds max length of 255 chars.", [
'value' => $value,
'key' => $key,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
}
// Optional: check total serialized length to prevent too large payload
$encoded = json_encode($jsonData);
if ($encoded === false || strlen($encoded) > 4096)
{
if ($this->_debugMode)
{
LogHelper::debug('Encoded extra_data JSON too large or failed to encode: '
. $encoded === false ? 0 : strlen($encoded),
[
'length' => $encoded === false ? 0 : strlen($encoded),
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
return false;
}//*/
return true;
}
private function validateCartData(array $cartData): bool
{
// Configuration: max allowed items in cart
$maxItems = 10;
if (count($cartData) > $maxItems)
{
if ($this->_debugMode)
{
LogHelper::debug("Cart data exceeds maximum: {$maxItems} allowed items: "
. count($cartData),
[
'count' => count($cartData),
'max' => $maxItems,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
return false;
}
foreach ($cartData as $index => $item)
{
if (!is_array($item) || !isset($item['id'], $item['quantity']))
{
if ($this->_debugMode)
{
LogHelper::debug("Cart item at index {$index} missing required keys or invalid format.", [
'item' => $item,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
if (!is_int($item['id']) && !is_string($item['id']))
{
if ($this->_debugMode)
{
LogHelper::debug("Cart item 'id' at index {$index} must be int or string.", [
'id' => $item['id'],
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
$qty = $item['quantity'];
// Validate quantity is positive integer or string of digits
if (!((is_int($qty) && $qty > 0) || (is_string($qty) && ctype_digit($qty) && (int) $qty > 0))
)
{
if ($this->_debugMode)
{
LogHelper::debug("Cart item 'quantity' at index {$index} must be a positive integer.", [
'quantity' => $qty,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
// Optional: reject unexpected extra keys in cart items to avoid abuse
$allowedItemKeys = ['id', 'quantity'];
foreach ($item as $key => $value)
{
if (!in_array($key, $allowedItemKeys, true))
{
if ($this->_debugMode)
{
LogHelper::debug("Unexpected key {$key} in cart item at index {$index}.", [
'item' => $item,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
// Optionally reject nested arrays in item fields (no complex objects)
if (is_array($value) || is_object($value))
{
if ($this->_debugMode)
{
LogHelper::debug("Cart item value for key {$key} at index {$index} is nested array/object.",
[
'value type' => gettype($value),
'item' => $item,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
return false;
}
}//*/
}
// Optional: Check total serialized length of cart_data JSON to prevent too large carts
$encoded = json_encode($cartData);
if ($encoded === false || strlen($encoded) > 8192)
{
if ($this->_debugMode)
{
LogHelper::debug('Encoded extra_data JSON too large or failed to encode: '
. $encoded === false ? 0 : strlen($encoded),
[
'length' => $encoded === false ? 0 : strlen($encoded),
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
return false;
}//*/
return true;
}
public function invalidate(string $sessionId): bool
{
$sessionUser = SessionUser::findOne(['session_id' => $sessionId]);
if ($sessionUser === null)
{
if ($this->_debugMode)
{
LogHelper::debug("Attempted to invalidate non-existing session user with ID: {$sessionId}", [
'user session_id' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
$result = $sessionUser->delete() !== false;
if ($result)
{
$this->_cachedSessionUser = null;
unset($this->_cachedSessionTokens[$sessionId]);
}
if ($this->_debugMode)
{
LogHelper::debug("Invalidate session with ID: {$sessionId}", [
'session ID' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return $result;
}
private function getSession(string $sessionId): ?SessionUser
{
if ($this->_currentSessionId !== $sessionId)
{
if ($this->_debugMode)
{
LogHelper::debug(
"Session ID: {$this->_currentSessionId} changed, clearing cached SessionUser ID: {$sessionId}",
[
'old_session_id' => $this->_currentSessionId,
'new_session_id' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]
);
}
$this->_cachedSessionUser = null;
$this->_currentSessionId = $sessionId;
/**
* Also clear any related caches tied to session ID,
* For example, cached token if you implement.
*/
unset($this->_cachedSessionTokens[$sessionId]);
}
if ($this->_cachedSessionUser !== null)
{
return $this->_cachedSessionUser;
}
if ($this->_debugMode)
{
LogHelper::debug("Querying SessionUser DB record for session ID: {$sessionId}", [
'session_id' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
// Get from SessionUser ActiveRecord
$sessionUser = SessionUser::findOne(['session_id' => $sessionId]);
if ($sessionUser === null)
{
if ($this->_debugMode)
{
LogHelper::debug("No SessionUser DB record found for: {$sessionUser->session_id}", [
'user session_id' => $sessionUser->session_id,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
}
else
{
if ($this->_debugMode)
{
LogHelper::debug("SessionUser DB record found for: {$sessionUser->session_id}", [
'user session_id' => $sessionUser->session_id,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
$this->_cachedSessionUser = $sessionUser;
}
return $sessionUser;
}
private function generateSessionToken(): string
{
return \Yii::$app->security->generateRandomString(64);
}
private function getSessionToken(string $sessionId): ?string
{
if ($this->_currentSessionId !== $sessionId)
{
if ($this->_debugMode)
{
LogHelper::debug("Session ID: {$sessionId} changed, clearing cached session tokens.", [
'old_session_id' => $this->_currentSessionId,
'new_session_id' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
// Clear token cache for old session ID
unset($this->_cachedSessionTokens[$this->_currentSessionId]);
$this->_currentSessionId = $sessionId;
}
if (isset($this->_cachedSessionTokens[$sessionId]))
{
return $this->_cachedSessionTokens[$sessionId];
}
$sessionUser = $this->getSession($sessionId);
if ($sessionUser !== null && !empty($sessionUser->session_token))
{
$this->_cachedSessionTokens[$sessionId] = $sessionUser->session_token;
return $sessionUser->session_token;
}
return null;
}
public function rollSessionToken(string $sessionId): bool
{
$sessionUser = $this->getSession($sessionId);
if ($sessionUser === null)
{
if ($this->_debugMode)
{
LogHelper::debug("No SessionUser record found in rollSessionToken for id: {$sessionId}", [
'session ID' => $sessionId,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return false;
}
$oldTokenKey = substr($sessionUser->session_token, 0, self::SESSION_TOKEN_LENGTH);
$newToken = $this->generateSessionToken();
$newTokenKey = substr($newToken, 0, self::SESSION_TOKEN_LENGTH);
$oldSessionData = $this->_sessionManager->get(SessionUser::SESSION_USER_KEY_PREFIX . $oldTokenKey);
if ($oldSessionData === null)
{
if ($this->_debugMode)
{
LogHelper::debug("No existing session data found during token roll.");
}
return false;
}
$oldSessionData['session_token'] = $newToken;
// Remove old session key and save new
$this->_sessionManager->remove(SessionUser::SESSION_USER_KEY_PREFIX . $oldTokenKey);
$this->_sessionManager->set(SessionUser::SESSION_USER_KEY_PREFIX . $newTokenKey, $oldSessionData);
try
{
$sessionUser->session_token = $newToken;
if (!$sessionUser->save(false, ['session_token']))
{
throw new YiiDbException("Failed to save session token to the database.");
}
return true;
}
catch (Throwable $e)
{
// Use the ExceptionLogger to handle and log the exception.
ExceptionLogger::log($e, 'session_user_service_rollSessionToken_error_');
}
return false;
}
}
code
class BaseController extends Controller
{
protected ?SessionUserService $_sessionUser = null;
protected ?SessionCartService $_sessionCart = null;
public function init()
{
parent::init();
$this->_sessionUser = ServiceLocator::get('sessionUserService');
$this->_sessionCart = ServiceLocator::get('sessionCartService');
if ($this->_sessionUser === null xor $this->_sessionCart === null)
{
throw new RuntimeException("Failed to load SessionUserService xor SessionCartService from ServiceLocator");
}
}
public function beforeAction($action): bool
{
if (!parent::beforeAction($action))
{
return false;
}
/**
* Middleware Enforce Session Security
*/
$sessionId = \Yii::$app->session->getId();
if ($this->_debugMode)
{
LogHelper::debug('Session ID: ' . $sessionId, ['session ID' => $sessionId]);
}
try
{
$sessionData = $this->_sessionUser->readSession();
}
catch (Throwable $e)
{
if ($this->_debugMode)
{
LogHelper::debug('Error reading session data: ' . $e->getMessage(), [
'exception' => $e,
'route' => \Yii::$app->requestedRoute
]);
}
// Optionally handle error (for instance redirect or show maintenance)
return false;
}
if (!$sessionData)
{
// Session invalid or expired - clean up session and related services
$this->_sessionUser->invalidate($sessionId);
$this->_sessionCart->clearCart();
RoleHelper::userLogout(true);
if ($this->_debugMode)
{
\Yii::$app->session->setFlash(
'warning',
\Yii::t('app', 'Your session has expired or was invalidated. Please log in again.')
);
}
\Yii::$app->response->redirect(['site/login'])->send();
return false;
}
return true;
}
}
code
class BaseController extends Controller
{
protected ?SessionUserService $_sessionUser = null;
protected ?SessionCartService $_sessionCart = null;
public function init()
{
parent::init();
$this->_sessionUser = ServiceLocator::get('sessionUserService');
$this->_sessionCart = ServiceLocator::get('sessionCartService');
if ($this->_sessionUser === null xor $this->_sessionCart === null)
{
throw new RuntimeException("Failed to load SessionUserService xor SessionCartService from ServiceLocator");
}
}
public function actionLogout()
{
$userId = Yii::$app->user->id;
$sessionId = Yii::$app->session->id;
$this->_sessionUser->writeSession([
'extra_data' => ['last_login_time' => time()],
// Optionally, you can pass existing cart_data to keep it synced.
]);
$this->_sessionCart->setCartSessionToDatabase($userId);
if (!$sessionUserService->rollSessionToken($sessionId))
{
LogHelper::warning("Failed to roll session token after login", [
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return $this->goHome();
}
}
all I get is a blank page when attempint to login
From your description and the provided logs/code snippets, the blank page at login likely indicates that the script encountered a fatal error or unhandled exception, or output was interrupted (headers sent but no content). Since you have _debugMode = true and detailed logging statements in your SessionUserService, the absence of any visible error output or stack trace suggests the error might happen before rendering or there's a PHP fatal error, suppressed error, or redirect issue.
Let's review potential causes and fixes:
1) Check error output and PHP fatal errors
Make sure PHP error reporting and display errors are enabled in your dev environment:
Check your web server's PHP error logs for fatal errors that stop execution.
Yii may suppress or catch exceptions: check RuntimeException or similar errors from service locator or from writeSession().
2) actionLogout() syntax and code review
Your actionLogout() has a few issues that could cause silent failure:
php
public function actionLogout()
{
$userId = Yii::$app->user->id; // OK
$sessionId = Yii::$app->session->id; // OK, but better getId()
$this->_sessionUser->writeSession([
'extra_data' => ['last_login_time' => time()],
// Optionally, you can pass existing cart_data to keep it synced.
]);
$this->_sessionCart->setCartSessionToDatabase($userId);
if (!$sessionUserService->rollSessionToken($sessionId)) // Problem: $sessionUserService is undefined
{
LogHelper::warning("Failed to roll session token after login", [
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return $this->goHome();
}
Issues:
You're using $sessionUserService->rollSessionToken(...), but $sessionUserService is not defined anywhere in this function. Probably you meant $this->_sessionUser->rollSessionToken($sessionId).
In this logout action, the session ID retrieved via Yii::$app->session->id is not guaranteed to be correct. The better way is to use Yii::$app->session->getId().
You call $this->_sessionUser->writeSession() but don't check return value.
Also, calling rollSessionToken() on logout might not be necessary unless you want to invalidate existing tokens.
Fix:
Update actionLogout() code as:
php
public function actionLogout()
{
$userId = Yii::$app->user->id;
$sessionId = Yii::$app->session->getId();
$this->_sessionUser->writeSession([
'extra_data' => ['last_login_time' => time()],
// Pass cart_data if needed here as well
]);
$this->_sessionCart->setCartSessionToDatabase($userId);
if (!$this->_sessionUser->rollSessionToken($sessionId))
{
LogHelper::warning("Failed to roll session token after logout", [
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return $this->goHome();
}
3) Check for infinite redirect or login loop
In your beforeAction() in BaseController, if the session is invalid or expired, you:
Invalidate session user
Clear cart
Log out user via RoleHelper::userLogout(true)
Redirect to site/login
Make sure:
RoleHelper::userLogout() successfully logs the user out
When redirecting to login, you prevent the beforeAction from failing again (i.e., unauthenticated users must be allowed on the login route).
If beforeAction runs on login route and tries to validate session but session is invalid, it loops infinitely redirecting.
Fix:
In beforeAction(), skip validation for site/login and guest accessible routes:
php
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
$route = \Yii::$app->requestedRoute;
$allowedRoutes = ['site/login', 'site/error']; // add your public routes
if (in_array($route, $allowedRoutes, true)) {
return true;
}
// ... existing session validation follows ...
}
4) Debug the blank page
If you still get blank page:
Add logging or echo or die('here') in actionLogout, and beforeAction to find where it breaks.
Use Yii debugger or Xdebug.
Add try/catch in actionLogout and beforeAction to catch any exception and log it.
Use Yii::$app->session->getId() instead of Yii::$app->session->id
Add exception handling and error display to catch fatal errors
Prevent login redirect loop by skipping session validation on login page
Confirm debug logs for clues on failure during session read/write
Confirm no fatal PHP errors in logs or web server logs
If you apply these fixes and enable full error reporting, you'll likely see either a PHP error or the login page working again instead of blank.
If you want, I can help you refactor your logout/login flow more robustly or add graceful error handling around session service calls. Let me know!
public function setCartSessionToDatabase(int $userId): bool
{
if ($userId <= 0)
{
throw new YiiBaseInvalidArgumentException('User ID must be positive integer.');
}
// validated, secured cart items from session
$guestItems = $this->getCartItems();
if (empty($guestItems))
{
return;
}
$transaction = Yii::$app->db->beginTransaction();
try
{
foreach ($guestItems as $item)
{
$productId = (int) ($item['id'] ?? 0);
$quantity = (int) ($item['quantity'] ?? 0);
if ($productId <= 0 || $quantity <= 0)
{
continue;
}
// Find user's persistent cart item
$cartItem = Cart::find()->userId($userId)->productId($productId)->one();
if ($cartItem)
{
$maxQty = $cartItem->product ? (int) $cartItem->product->quantity : PHP_INT_MAX;
$newQuantity = min($cartItem->quantity + $quantity, $maxQty);
$cartItem->quantity = $newQuantity;
}
else
{
$cartItem = new Cart([
'created_by' => $userId,
'product_id' => $productId,
'quantity' => $quantity,
]);
/**
* Lazy Loading
* Note: The Cart model can access related data from the Product model via its
* relationship. It does NOT rely on fields like `category_title` being stored
* in the Cart database table itself. Instead, Yii2 automatically queries the
* related Product record when `$cart->product` is accessed.
*
* Example Usage:
*
* $cart = new Cart();
*
* // Lazy load Product, no manual query needed.
* $category_title = $cart->product->category_title;
*
* This is made possible through the Cart model's relation:
*
* return $this->hasOne(Product::class, ['id' => 'product_id']);
*
$cart->category_title = $cartItem['category_title'];
$cart->slug = $cartItem['slug'];
$cart->meta_description = $cartItem['meta_description'],//*/
// Set the fallback user ID for blameable behavior in Cart $cart.
$cartItem->fallbackUserId = $userId;
}
$cartItem->total_price = $cartItem->product
? $cartItem->product->price * $cartItem->quantity
: 0;
if (!$cartItem->save(false))
{
throw new YiiDbException('Failed to save cart item during merge.');
}
}
// If all saves are successful, commit the transaction.
$transaction->commit();
// Clear guest cart after merge
$this->clearCart();
}
catch (Throwable $e)
{
// An error occurred; roll back the transaction.
$transaction->rollBack();
// Use the ExceptionLogger to handle and log the exception.
ExceptionLogger::log($e, 'sessionCart_mergeGuestCart_error');
}
}
Can you assist in rewriting this code to a bool
Certainly! Your current method setCartSessionToDatabase is meant to return a boolean but currently it does not explicitly return anything (it returns null in the case of an empty cart array and no explicit return in other cases).
To rewrite this method to explicitly return true on success and false on failure or empty cart, you can:
Make sure all code paths explicitly return a boolean.
Return false if $guestItems is empty or if an error/exception occurs.
Return true if the transaction completes successfully.
Here is the rewritten method with explicit boolean returns:
php
public function setCartSessionToDatabase(int $userId): bool
{
if ($userId <= 0)
{
throw new YiiBaseInvalidArgumentException('User ID must be positive integer.');
}
// validated, secured cart items from session
$guestItems = $this->getCartItems();
if (empty($guestItems))
{
// Nothing to save, treat as successful no-op or false based on expectation
return false;
}
$transaction = Yii::$app->db->beginTransaction();
try
{
foreach ($guestItems as $item)
{
$productId = (int) ($item['id'] ?? 0);
$quantity = (int) ($item['quantity'] ?? 0);
if ($productId <= 0 || $quantity <= 0)
{
continue;
}
// Find user's persistent cart item
$cartItem = Cart::find()->userId($userId)->productId($productId)->one();
if ($cartItem)
{
$maxQty = $cartItem->product ? (int) $cartItem->product->quantity : PHP_INT_MAX;
$newQuantity = min($cartItem->quantity + $quantity, $maxQty);
$cartItem->quantity = $newQuantity;
}
else
{
$cartItem = new Cart([
'created_by' => $userId,
'product_id' => $productId,
'quantity' => $quantity,
]);
// Fallback user id for blameable behavior
$cartItem->fallbackUserId = $userId;
}
$cartItem->total_price = $cartItem->product
? $cartItem->product->price * $cartItem->quantity
: 0;
if (!$cartItem->save(false))
{
throw new YiiDbException('Failed to save cart item during merge.');
}
}
// Commit transaction if all inserts/updates successful
$transaction->commit();
// Clear guest cart after merge
$this->clearCart();
return true; // success
}
catch (Throwable $e)
{
$transaction->rollBack();
ExceptionLogger::log($e, 'sessionCart_mergeGuestCart_error');
return false; // failure
}
}
Explanation of key points:
If getCartItems() returns empty array, return false (or true if you prefer success in no-op, but generally false is better here so caller can know nothing was merged).
In the try-catch block, on failure, roll back and return false.
On success, return true.
Added comments and removed commented-out code for clarity.
If you want the method to return true even if there are no guestItems (just an empty cart), change that return false; to return true;.
Let me know if you want me to adjust it for that behavior!
Yes, that’s a very likely cause of your problem with blank pages or unexpected behavior, especially when dealing with login and other public routes.
Why?
Your BaseController::beforeAction() enforces a session validation on every request handled by controllers that extend BaseController. This includes routes like site/login, site/error, or any other public access pages.
If the session is invalid or expired during a call to, for example, site/login, beforeAction() invalidates the session and redirects back to the login page again, causing an infinite redirect loop or premature exit, which can manifest as a blank page to the user.
How to fix this
Modify your beforeAction() to skip session validation for routes that need to be accessible without a valid session—typically login, error pages, signup, etc.
Example fix
Add a whitelist (allowed public routes) and skip session validation there:
php
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
$route = \Yii::$app->requestedRoute;
// List of routes allowed without session validation
// Customize this list for your app’s public routes
$allowedRoutes = [
'site/login',
'site/error',
'site/signup',
// Add others as needed
];
if (in_array($route, $allowedRoutes, true)) {
// Don't enforce session validation on public routes
return true;
}
// Now proceed with session validation on all other routes
$sessionId = \Yii::$app->session->getId();
if ($this->_debugMode) {
LogHelper::debug('Session ID: ' . $sessionId, ['session ID' => $sessionId]);
}
try {
$sessionData = $this->_sessionUser->readSession();
} catch (Throwable $e) {
if ($this->_debugMode) {
LogHelper::debug('Error reading session data: ' . $e->getMessage(), [
'exception' => $e,
'route' => $route,
]);
}
return false;
}
if (!$sessionData) {
$this->_sessionUser->invalidate($sessionId);
$this->_sessionCart->clearCart();
RoleHelper::userLogout(true);
if ($this->_debugMode) {
\Yii::$app->session->setFlash(
'warning',
\Yii::t('app', 'Your session has expired or was invalidated. Please log in again.')
);
}
\Yii::$app->response->redirect(['site/login'])->send();
return false;
}
return true;
}
Summary
Skip session validation on public routes, especially the login page.
This prevents infinite redirection and blank pages caused by beforeAction().
Once logged in, other routes will have session validation enforced.
Note: Double check that your controllers extend this BaseController. If some don't, they’ll skip this session enforcement.
If you want, I can help you refactor this further or help debug specific login flows with the session service. Just ask!