1

I use magento like API. In login controller I set into session user parameter. Session are stored in files. It work well, when I login like customer or admin. But when I will loged in after admin/customer, it will be created new session file with parameters $_SESSION['admin']['user'] and $_SESSION['customer_base']['user']. I want to store every session for loged in admin/customer in separate file.

public function loginAction()
{
    if (!in_array($this->getRequest()->getMethod(), ['POST'])) {
        return $this->api_helper->responseError(null, 'Only POST method allowed', 405);
    }
    $requestBody = json_decode($this->getRequest()->getRawBody());
    if ($this->api_helper->checkSecurityKey($this->getRequest()->getHeader('Security-Key'))) {
        return $this->api_helper->responseError(null, 'Security Key is Invalid');
    }
    /** @var $session Mage_Admin_Model_Session */
    $session = Mage::getSingleton('admin/session');
    $user = $session->login($requestBody->username, $requestBody->password);
    if (!$user || !$user->getId()) {
        /** @var $session Mage_Customer_Model_Session */
        $session = Mage::getSingleton('customer/session');
        try {
            $session->login($requestBody->email, $requestBody->password);
        } catch (Exception $e) {
            return $this->api_helper->responseError(null, $e->getMessage());
        }
        $user = $session->getCustomer();
    }
    $session->setData('user', $user->getData());
    return $this->api_helper->response(['session_id' => $session->getSessionId()]);
}

I check session using custome method:

public function checkIsLogin($sessionId)
{
   if ($this->checkSecurityKey(Mage::app()->getRequest()->getHeader('Security-Key'))) {
        $this->responseError(null, 'Security Key is Invalid');
        return false;
    }
   if ($sessionId == null) {
        $this->responseError(null, $message = 'Forbidden', 403);
        return false;
    }
    $sessionFilePath = Mage::getBaseDir('session') . DS . 'sess_' . $sessionId;
    $sessionFile = file_get_contents($sessionFilePath);
    if (!$sessionFile) {
        $this->responseError(null, $message = 'Not found sessionFile', 403);
        return false;
    }
    $currentSession = session_encode();
    session_decode($sessionFile);
    $sessionData = $_SESSION;
    session_decode($currentSession);
    if(array_key_exists('user', $sessionData['admin']) || array_key_exists('user', $sessionData['customer_base'])){
       $user = $sessionData['admin']['user'] ?? $sessionData['customer_base']['user'];
    }
    if (!isset($user)) {
        $this->responseError(null, $message = 'Not found user', 403);
        return false;
    }
    return $user;
}

Thanks for help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.