3

在我的插件中,我只有用户名或电子邮件,我必须授权该用户。我发现了下一个问题,但它对我不起作用:

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

            switch ( $params['do'] ) {
                case 'login':
                    // Registering. Works fine.
                    $user = User::newFromName( 'admin' );
                    $user->setEmail( admin@email.com );
                    $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;
            }

        }
    }

此外,另一个问题是直接从数据库获取密码哈希,但这是野蛮行为......
提前致谢!

4

1 回答 1

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( admin@email.com );
                $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;
        }

    }
}
于 2012-11-06T13:44:25.557 回答