2012-11-03 95 views
3

在我的插件中,我只有用戶名或電子郵件,我必須授權該用戶。我發現下一個問題,但它並沒有爲我工作:如何在沒有密碼的情況下授權MediaWiki 1.19中的用戶?

class ApiPlugin extends ApiBase { 
     public function execute() { 
      $params = $this->extractRequestParams(); 

      switch ($params['do']) { 
       case 'login': 
        // Registering. Works fine. 
        $user = User::newFromName('admin'); 
        $user->setEmail([email protected]); 
        $user->setRealName('admin'); 
        $uid = $user->idForName(); 

        if ($uid === 0) { 
         $user->addToDatabase(); 
         $user->setPassword(generate_password()); 
         $user->saveSettings(); 
        } 
        $ssu = new SiteStatsUpdate(0, 0, 0, 0, 1); 
        $ssu->doUpdate(); 

        if ($user->isLoggedIn()) $user->doLogout(); 

        //Logging in. 
        $id = User::idFromName('admin'); 
        $user->setID($id); 
        $user->loadFromId(); 

        $user->setToken(); 
        $user->saveSettings(); 

        wfSetupSession(); 
        $user->setCookies(); 
        break; 
      } 

     } 
    } 

此外,另一個問題是越來越密碼哈希直接從數據庫,但它是野蠻...
提前感謝!

+0

_ How does it does not work?另外,您可能想要提供更多的上下文。我假設這個代碼來自[UserLoadFromSession](http://www.mediawiki.org/wiki/Manual:Hooks/UserLoadFromSession)鉤子? –

+0

它是基於API的插件。增加了一些代碼。 >它不起作用? 調用'api.php?action = ApiPlugin&do = login'後,我仍然以匿名身份登錄。 –

+0

嗯,那很奇怪。你有沒有收到新的cookies? –

回答

2

幾天後,我試圖分開代碼和...利潤!

class ApiPlugin extends ApiBase { 
    public function execute() { 
     $params = $this->extractRequestParams(); 

     switch ($params['do']) { 
      case 'register': 
       // Registering. Works fine. 
       $user = User::newFromName('admin'); 
       $user->setEmail([email protected]); 
       $user->setRealName('admin'); 
       $uid = $user->idForName(); 

       if ($uid === 0) { 
        $user->addToDatabase(); 
        $user->setPassword(generate_password()); 
        $user->saveSettings(); 
       } 
       $ssu = new SiteStatsUpdate(0, 0, 0, 0, 1); 
       $ssu->doUpdate(); 
      case 'authorise': 
       if ($user->isLoggedIn()) $user->doLogout(); 
       wfSetupSession(); 
       //Logging in. 
       $id = User::idFromName('admin'); 
       $user->setID($id); 
       $user->loadFromId(); 

       $user->setToken(); 
       $user->saveSettings(); 

       $user->setCookies(); 
       break; 
     } 

    } 
} 
相關問題