在我的插件中,我只有用户名或电子邮件,我必须授权该用户。我发现了下一期,但它对我不起作用:
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;
}
}
}
另外,另一个问题是直接从DB获取密码哈希,但这是野蛮的...
提前谢谢!
答案 0 :(得分: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;
}
}
}