Programmatically magento admin connection doesn't work

Viewed 367

I want to log a user programmatically to magento admin. The admin page is in an iframe and it have to redirect automatically to the admin dashboard without authentication. I used a code found in a ancient post and it matches with the magento core source. The code is :

umask(0);
$app = Mage::app('default');

Mage::getSingleton('core/session', array('name' => 'adminhtml'));

// supply username
$user = Mage::getModel('core/factory')->getModel('admin/user')->loadByUsername($loginadmin);

  if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
         Mage::getSingleton('adminhtml/url')->renewSecretUrls();
  }

  $session = Mage::getSingleton('admin/session');
  $session->setIsFirstVisit(false);
  $session->setUser($user);
  $session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));

  if ($session->isLoggedIn()) {
      //Redirection vers le dashboard
       $url = "index.php/admico/dashboard";
       header('Location: '.$url);
  }

When I var_dump()the data, the user exists and it have all the informations like firstname, id, etc and it's all correct. The code go inside the last if and redirect to 'index.php/admico/dashboard' so the $session is correctly logged in. But, anyway, the frontpage display the connection form as if the session was not logged in and not the dashboard of the admin.

Can someone help me to figure out what's wrong please ?

2 Answers
Related