Automatically login AND default dashboard to specific SiteID

Hello,

The piwik documentation noted:

Some Piwik users automatically provide their CMS customers with Piwik statistics. If you want to provide a one-click automatic login to Piwik for your users, you can pass their login & the md5 string of their password in the URL parameters:

/pathpiwik/index.php?module=Login&action=logme&login=your_login&password=your_MD5_password

This will log the user (create a cookie in their browser) and redirect to the index.php on succesful login. We advise to do this authentication over HTTPS. Note that this authentication method doesn’t work for the Super User.

You can also specify the URL to redirect after a successful login, if different from the default Piwik dashboard page:

/pathpiwik/index.php?module=Login&action=logme&login=your_login&password=your_MD5_pwd&url=piwik.mycompany.com/subpath/

However, I am having problems getting the default dashboard that is loaded after logging in, be for a specific SiteID. I’ve tried a variety of variations with the url= parameter in the URL string, and just haven’t been able to make it work. Maybe I’m missing something though, any help is appreciated.

Thanks,
jcb

So for example, I’ve tried:

http://foo/index.php?module=Login&action=logme&login=foo&password=76ce4bb5ca1d468ae24f96b54cd1f471&url=http://foo/index.php?module=CoreHome&action=index&period=month&date=today&idSite=5

But it just seems to log me out and not work. Though it works if I don’t try appending the &url variable onto the string. I’ve looked through the Login module and it’s Controller.php related class methods, and I don’t really see why it isn’t working. I must be missing something stupid…

Any ideas? All I’m looking to do is automatically log into a certain Site ID.

Well, if anyone’s interested, I solved my little problem by making some minor changes to Controller.php’s Piwik_Login_Controller::logme()

function logme()
{
  $login = Piwik_Common::getRequestVar('login', null, 'string');
  $password = Piwik_Common::getRequestVar('password', null, 'string');
  $site_id = Piwik_Common::getRequestVar('siteId', null, 'string');  // reference siteId in URL string
  $currentUrl = 'index.php';
  $urlToRedirect = Piwik_Common::getRequestVar('url', $currentUrl, 'string');
  $urlToRedirect = htmlspecialchars_decode($urlToRedirect);

  if(strlen($password) != 32)
  {
    throw new Exception("The password parameter is expected to be a MD5 hash of the password.");
  }

  if($login == Zend_Registry::get('config')->superuser->login)
  {
    throw new Exception("The Super User cannot be authenticated using this URL.");
  }
  
  /* hardcoded URL referencing the passed siteId... messy, but works. */
  $url = "index.php?module=CoreHome&action=index&idSite=$site_id&period=day&date=today#module=Dashboard&action=embeddedIndex&idSite=$site_id&period=day&date=today";  

  $authenticated = $this->authenticateAndRedirect($login, $password, $url);
  if($authenticated === false)
  {
    echo Piwik_Translate('Login_LoginPasswordNotCorrect');
  }

}

So with this, you can append a siteId onto the typical action=logme request and upon login you will be directed to the siteId you specified. Not sure why I had such a problem trying to get it working with just using the &url parameter, but … maybe this will help someone else out there.

Thanks again for the great project.

Cheers,
jcb

Instead of hacking the Controller, try encoding your url when you pass it as a parameter.