How to add super user via code

(sushilbajimaya) #1

I want to implement Piwik inside my PHP application. And if someone logs into his/her profile, s/he should also be automatically logged into Piwik and the stats should be available. Thats my general requirement.

To do this, I created an API for user login. It is as follows:

class Piwik_Login_API 
	static private $instance = null;
	static public function getInstance()
		if (self::$instance == null)
			$c = __CLASS__;
			self::$instance = new $c();
		return self::$instance;
    public function logme($userLogin, $md5Password)
      $login = $userLogin;
      $password = $md5Password;
      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.");
      //$objLogin = get
      $authenticated = $this->authenticate($login, $password);
      if($authenticated === false)
        return false; //echo Piwik_Translate('Login_LoginPasswordNotCorrect');
      return $authenticated;

    protected function authenticate($login, $md5Password)
		$tokenAuth = Piwik_UsersManager_API::getInstance()->getTokenAuth($login, $md5Password);

		$auth = Zend_Registry::get('auth');

		$authResult = $auth->authenticate();
			return false;//return Piwik_Translate('Login_LoginPasswordNotCorrect');

		$authCookieName = Zend_Registry::get('config')->General->login_cookie_name;
		$authCookieExpiry = time() + Zend_Registry::get('config')->General->login_cookie_expire;
		$authCookiePath = Zend_Registry::get('config')->General->login_cookie_path;
		$cookie = new Piwik_Cookie($authCookieName, $authCookieExpiry, $authCookiePath);
		$cookie->set('login', $login);
		$cookie->set('token_auth', $authResult->getTokenAuth());

        return $authResult->getTokenAuth();


I copied few functions from the controller and made the API based upon them and tweaked them a little bit to suit my requirement.

So, I wrote a code to call this login method and automatically log in the user. The code is as follows:

$request = new Piwik_API_Request('method=Login.logme
    $token = $request->process();
    if($token !== false)
        $request = new Piwik_API_Request('method=SitesManager.addSite 
        $result = $request->process();

I have already initialized Piwik in my application, so it should not be a major concern.

This page always gives the following error:

	<error message="You can't access this resource as it requires a 'superuser' access." /> 

I found something in this post:

I did what is explained there but it didn’t help either.

I have manually added the user ‘sushil’ as superuser.

Can anybody please help me with this? If I pass this step, then I think I can easily integrate Piwik into my application.

Thanks in advance for the help.

(Matthieu Aubry) #2

in the API request string, you must put the token_auth parameter (to authenticate API and widgets), not md5Password (only used for the API to create/update users). See examples:

(sushilbajimaya) #3

Thanks for the reply. But, if you check my code closely, the function logme in Login_API, it returns the token when the login is successful. I have passed the same token with the second api request. But still, it doesn’t work.

Do you have any other solution? Thanks in advance.


(sushilbajimaya) #4

Seems like nobody is interested in replying my post. Well, I found a solution myself for that.

I suppose we cannot create super user at all. Even if we give super user permission to a particular user, it is not identified by the system as the super user. The system identifies the user defined in config file as the super user only (it is defined in the code somewhere).

So, the workaround for this solution is to add token of the admin super user while creating a site or a user using API.

The APIs return values in XML format. So, to check the return value of the API, I have used SimpleXML. At some places, I have manually stripped the return var using substr.