Piwik auto-login, specifying a website


#1

Hi everyone,

I have set-up a one-click Piwik autologin for the statistics of a hosting service. The problem is that many users have more than one site, so I would like to be able to force which one to display in Piwik.

For example if a user has a “website1” and a “website2”, and he clicks on the “statistics” link of “website2”, he will get the “website1” stats for now. I would like to force “website2”.

Is there an easy way to do this, if possible by passing the website name or url (and not the idSite)?

Many thanks in advance,

Smaon


(Matthieu Aubry) #2

This can be set in the User preferences for each user


#3

Thanks for your answer, but it would be much more useful for me to be able to specify the website in the URL, even if it involve to use the idSite parameter (from the piwik db)…


#4

Well, I actually modified 2 Piwik files:

plugins/Login/Controller.php


	function logme()
	{
		self::checkForceSslLogin();

		$password = Piwik_Common::getRequestVar('password', null, 'string');
		if(strlen($password) != 32)
		{
			throw new Exception(Piwik_TranslateException('Login_ExceptionPasswordMD5HashExpected'));
		}

		$login = Piwik_Common::getRequestVar('login', null, 'string');
		if($login == Zend_Registry::get('config')->superuser->login)
		{			
			throw new Exception(Piwik_TranslateException('Login_ExceptionInvalidSuperUserAuthenticationMethod', array("logme")));
		}

		$currentUrl = 'index.php';

		//Added by smaon, 31 march 2011
		$idSite = Piwik_Common::getRequestVar('idSite', -1, 'string');
		if($idSite != -1)
			$currentUrl = "index.php?idSite=".$idSite;
		
		$urlToRedirect = Piwik_Common::getRequestVar('url', $currentUrl, 'string');
		$urlToRedirect = Piwik_Common::unsanitizeInputValue($urlToRedirect);

		$this->authenticateAndRedirect($login, $password, false, $urlToRedirect);
	}

And plugins/CoreHome


	function redirectToCoreHomeIndex()
	{
		$defaultReport = Piwik_UsersManager_API::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), Piwik_UsersManager_API::PREFERENCE_DEFAULT_REPORT);
		$module = 'CoreHome';
		$action = 'index';

		//Added by smaon, 31 march 2011
		$idSite = Piwik_Common::getRequestVar('idSite', -1, 'string');

		
		// User preference: default report to load is the All Websites dashboard
		if($defaultReport == 'MultiSites' 
			&& Piwik_PluginsManager::getInstance()->isPluginActivated('MultiSites'))
		{
			$module = 'MultiSites';
		}
		if($defaultReport == Piwik::getLoginPluginName())
		{
			$module = Piwik::getLoginPluginName();
		}
		
		if($idSite == -1) //Added by smaon, there is certainly a way to do this nicer
			parent::redirectToIndex($module, $action);
		else
			parent::redirectToIndex($module, $action, $idSite);
	}

Now I can call the Piwik auto-login with idSite specify in the url. I think this could be useful for more people.

Cheers,

Smaon


(Matthieu Aubry) #5

Thanks for the suggestion and patch, this was fixed in Allow 'logme' automatic login mechanism to specify the idSite to load after logging in · Issue #2286 · matomo-org/matomo · GitHub


(vipsoft) #6

fwiw logme() takes an optional ‘url’ parameter to redirect after login, which is a lot more flexible than simply ‘idSite’.