php – 如何为MediaWiki设置外部单点登录?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何为MediaWiki设置外部单点登录?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用ExtAuthDB扩展为MediaWiki设置单点登录.目的是在用户登录主网站时自动从外部用户系统验证用户:www.mysITe.COM. Mediawiki位于子域名:www.wiki.mysite.com.

我按照指南中的说法安装了扩展程序.所有特权都是正确的.但它不起作用.

ExtAuthDB.PHP是:

<?PHP
/**
* Authentication plugin interface. Instantiate a subclass of AuthPlugin
* and set $wgAuth to it to authenticate against some external tool.
*
* The default behavior is not to do anything,and use the local user
* database for all authentication. A subclass can require that all
* accounts authenticate externally,or use it only as a fallback; also
* you can transparently create internal wiki accounts the First time
* someone LOGs in who can be authenticated externally.
*
* This interface is a derivation of AuthJooMLa and might change a bit before 1.4.0 final is done...
*
*/
$wgExtensionCredits['parserhook'][] = array (
'name' => 'ExtAuthDB','author' => 'Alessandra Bilardi','description' => 'Authenticate users about external MysqL database','url' => 'https://www.mediawiki.org/wiki/Extension:ExtAuthDB','version' => '0.1',);

require_once ( "$IP/includes/AuthPlugin.PHP" );
class ExtAuthDB extends AuthPlugin
{

/**
* Add into LocalSettings.PHP the following code: 
*
* MysqL Host Name.
* $wgExtAuthDB_MysqL_Host = '';
* MysqL Username.      
* $wgExtAuthDB_MysqL_Username = '';
* MysqL Password.        
* $wgExtAuthDB_MysqL_Password = '';
* MysqL Database Name.    
* $wgExtAuthDB_MysqL_Database = '';
* MysqL Database Table of users data.
* $wgExtAuthDB_MysqL_Table = '';
* MysqL Database username column label.
* $wgExtAuthDB_MysqL_Login = '';
* MysqL Database login password column label
* $wgExtAuthDB_MysqL_Pswrd = '';
* MysqL Database email column label
* $wgExtAuthDB_MysqL_Email = '';
* MysqL Database user real name column label
* $wgExtAuthDB_MysqL_RealN = '';
* require_once("$IP/extensions/ExtAuthDB/ExtAuthDB.PHP");
* $wgAuth = new ExtAuthDB();
*
* @return Object Database
*/
PRivate function connectToDB()
{
    $db = &amp; Database :: newFromParams(
    $GLOBALS['wgExtAuthDB_MysqL_Host'],$GLOBALS['wgExtAuthDB_MysqL_Username'],$GLOBALS['wgExtAuthDB_MysqL_Password'],$GLOBALS['wgExtAuthDB_MysqL_Database']);

    $this->userTable = $GLOBALS['wgExtAuthDB_MysqL_Table'];
    $this->userLogin = $GLOBALS['wgExtAuthDB_MysqL_Login'];
    $this->userPswrd = $GLOBALS['wgExtAuthDB_MysqL_Pswrd'];//.$GLOBALS['$wgExtAuthDB_MysqL_Salt'];
    $this->userEmail = $GLOBALS['wgExtAuthDB_MysqL_Email'];
    $this->userRealN = $GLOBALS['wgExtAuthDB_MysqL_RealN'];
    wfDebug("ExtAuthDB::connectToDB() : DB Failed to oPEn\n");
    return $db;
}

/**
 * Check whether there exists a user account with the given name.
 * The name will be normalized to MediaWiki's requirements,so
 * you might need to munge it (for instance,for lowercase initial
 * letters).
 *
 * @param $username String: username.
 * @return bool
 * @public
 */
function userExists( $username ) {
    # override this!
    return true;
}

/**
 * Check if a username+password pair is a valid login.
 * The name will be normalized to MediaWiki's requirements,for lowercase initial
 * letters).
 *
 * @param $username String: username.
 * @param $password String: user password.
 * @return bool
 * @public
 */
function authenticate( $username,$password )
{
    $db = $this->connectToDB();
    $hash_password = $db->selectRow($this->userTable,array ($this->userPswrd),array ($this->userLogin => $username ),__METHOD__ );
    if ($password == $hash_password->{$this->userPswrd}) {
        return true;
    }
    return false;
}

/**
 * Set the domain this plugin is supposed to use when authenticating.
 *
 * @param $domain String: authentication domain.
 * @public
 */
function setDomain( $domain ) {

    $this->domain = $domain;
}

/**
 * Check to see if the specific domain is a valid domain.
 *
 * @param $domain String: authentication domain.
 * @return bool
 * @public
 */
function validDomain( $domain ) {
    # Override this!
    return true;
}

/**
 * When a user logs in,optionally fill in preferences and such.
 * For instance,you might pull the email address or real name from the
 * external user database.
 *
 * The User object is passed by reference so it can be modified; don't
 * forget the & on your function declaration.
 *
 * @param User $user
 * @public
 */
function updateUser( &$user )
{
    $db = $this->connectToDB();
    $euser = $db->selectRow($this->userTable,array ( '*' ),array ($this->userLogin => $user->;mName ),__METHOD__ );
    $user->setRealName($euser->{$this->userRealN});
    $user->setEmail($euser->{$this->userEmail});
    $user->mEmailAuthenticated = wfTimestampNow();
    $user->saveSettings();
    //exit;
    # Override this and do something
    return true;
}
function disallowPrefsEditByUser() {
    return array (
        'wpRealName' => true,'wpUserEmail' => true,'wpNick' => true
    );
}

/**
 * Return true if the wiki should create a new local account automatically
 * when asked to login a user who doesn't exist locally but does in the
 * external auth database.
 *
 * If you don't automatically create accounts,you must still create
 * accounts in some way. It's not possible to authenticate without
 * a local account.
 *
 * This is just a question,and shouldn't perform any actions.
 *
 * @return bool
 * @public
 */
function autoCreate() {
    return true;
}

/**
 * Can users change their passwords?
 *
 * @return bool
 */
function allowPasswordChange() {
    return false;
}

/**
 * Set the given password in the authentication database.
 * As a special case,the password may be set to null to request
 * locking the password to an unusable value,with the expectation
 * that it will be set later through a mail reset or other method.
 *
 * Return true if successful.
 *
 * @param $user User object.
 * @param $password String: password.
 * @return bool
 * @public
 */
function setPassword( $user,$password ) {
    return true;
}

/**
 * Update user information in the external authentication database.
 * Return true if successful.
 *
 * @param $user User object.
 * @return bool
 * @public
 */
function updateExternalDB( $user ) {
    $db = $this->connectToDB();
    $euser = $db->selectRow($this->userTable,__METHOD__ );
    $user->setRealName($euser->{$this->userRealN});
    $user->setEmail($euser->{$this->userEmail});
    $user->mEmailAuthenticated = wfTimestampNow();
    $user->saveSettings();
    return true;
}

/**
 * Check to see if external accounts can be created.
 * Return true if external accounts can be created.
 * @return bool
 * @public
 */
function canCreateAccounts() {
    return false;
}

/**
 * Add a user to the external authentication database.
 * Return true if successful.
 *
 * @param User $user - only the name should be assumed valid at this point
 * @param string $password
 * @param string $email
 * @param string $realname
 * @return bool
 * @public
 */
function addUser( $user,$password,$email='',$realname='' ) {
    return false;
}


/**
 * Return true to prevent logins that don't authenticate here from being
 * checked against the local database's password fields.
 *
 * This is just a question,and shouldn't perform any actions.
 *
 * @return bool
 * @public
 */
function strict() {
    return true;
}

/**
 * When creating a user account,you might pull the email address or real name from the
 * external user database.
 *
 * The User object is passed by reference so it can be modified; don't
 * forget the & on your function declaration.
 *
 * @param $user User object.
 * @param $autocreate bool True if user is being autocreated on login
 * @public
 */
function initUser( $user,$autocreate=false ) {
    # Override this to do something.
}

/**
 * If you want to munge the case of an account name before the final
 * check,Now is your chance.
 */
function getcanonicalName( $username ) {
    return $username;
}
}

在LocalSettings.PHP中,我应该添加以下代码

// add ExtAuthDB
// MysqL Host Name.
$wgExtAuthDB_MysqL_Host = 'localhost';
// MysqL Username.
$wgExtAuthDB_MysqL_Username = 'dbuser';
// MysqL Password.
$wgExtAuthDB_MysqL_Password = 'dbpassword';
// MysqL Database Name.
$wgExtAuthDB_MysqL_Database = 'base';
// MysqL Database Table of users data.
$wgExtAuthDB_MysqL_Table = 'members';
// MysqL Database username column label.
$wgExtAuthDB_MysqL_Login = 'username';
// MysqL Database login password column label
$wgExtAuthDB_MysqL_Pswrd = 'password';
$wgExtAuthDB_MysqL_Salt='salt';
// MysqL Database email column label
$wgExtAuthDB_MysqL_Email = 'email';
// MysqL Database user real name column label
$wgExtAuthDB_MysqL_RealN = 'real_name';

require_once("$IP/extensions/ExtAuthDB/ExtAuthDB.PHP");
$wgAuth = new ExtAuthDB();

对不起,我不得不复制完整的脚本,因为我不知道确切的错误在哪里.我的问题是:为什么不起作用?哪里出错了?

编辑:

我的外部用户包括id,username,password,salt,email,real_name.我认为这可能是因为单独的密码和盐字段,所以我尝试手动在ExtAuthDB.PHP文件中实现Salt.不幸的是,它也没有用.然后我评论了这一行.

解决方法

您需要为此扩展程序运行 MediaWiki update script.

从浏览器

如果您无权访问服务器的命令行,请使用web updater运行更新脚本.

从命令行

从命令行,或SSH shell或类似的:

>切换到维护目录!>使用PHP update.PHP命令运行更新脚本!

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何为MediaWiki设置外部单点登录?全部内容,希望文章能够帮你解决php – 如何为MediaWiki设置外部单点登录?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。