【发布时间】:2011-10-22 08:49:18
【问题描述】:
我的目标是从我的外部应用程序检查 Joomla username 和 password 是否有效。用户不必登录系统,只要他们的帐户存在即可。 我决定基于 Joomla 身份验证 (JOOMLA_PATH/plugins/authentication/joomla) 创建自己的身份验证插件。我只是改了名字:
<?php
/**
* @version $Id: joomla.php 21097 2011-04-07 15:38:03Z dextercowley $
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
/**
* Joomla Authentication plugin
*
* @package Joomla.Plugin
* @subpackage Authentication.Webservice
* @since 1.5
*/
class plgAuthenticationWebservice extends JPlugin
{
/**
* This method should handle any authentication and report back to the subject
*
* @access public
* @param array Array holding the user credentials
* @param array Array of extra options
* @param object Authentication response object
* @return boolean
* @since 1.5
*/
function onUserAuthenticate($credentials, $options, &$response)
{
jimport('joomla.user.helper');
$response->type = 'Webservice';
// Joomla does not like blank passwords
if (empty($credentials['password'])) {
$response->status = JAUTHENTICATE_STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
return false;
}
// Initialise variables.
$conditions = '';
// Get a database object
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('id, password');
$query->from('#__users');
$query->where('username=' . $db->Quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadObject();
if ($result) {
$parts = explode(':', $result->password);
$crypt = $parts[0];
$salt = @$parts[1];
$testcrypt = JUserHelper::getCryptedPassword($credentials['password'], $salt);
if ($crypt == $testcrypt) {
$user = JUser::getInstance($result->id); // Bring this in line with the rest of the system
$response->email = $user->email;
$response->fullname = $user->name;
if (JFactory::getApplication()->isAdmin()) {
$response->language = $user->getParam('admin_language');
}
else {
$response->language = $user->getParam('language');
}
$response->status = JAUTHENTICATE_STATUS_SUCCESS;
$response->error_message = '';
} else {
$response->status = JAUTHENTICATE_STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
} else {
$response->status = JAUTHENTICATE_STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
}
}
}
我在我的插件中添加了一个文件来访问身份验证,我称之为 test_auth.php,它是这样的:
<?php
define('_JEXEC', 1 );
define('JPATH_BASE', 'C:\xampp\htdocs\joomla');
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
include("Webservice.php");
$credentials = array(
'username' => 'test',
'password' => 'test');
$options = array();
$response = array();
$auth = new plgAuthenticationWebservice();
$auth->onUserAuthenticate($credentials, $options, &$response);
var_dump($response);
但是当我调用它时,它会出现以下错误:
警告:缺少 JPlugin::__construct() 的参数 1,调用 C:\xampp\htdocs\joomla\plugins\authentication\Webservice\test_auth.php 在第 25 行并定义在 C:\xampp\htdocs\joomla\libraries\joomla\plugin\plugin.php 第 57 行
致命错误:在非对象上调用成员函数 attach() C:\xampp\htdocs\joomla\libraries\joomla\base\observer.php 第 41 行
我做错了什么? 我想我可以将所有 php 脚本放在外部并独立于 joomla 并使用 require_once(JPATH_BASE .DS.'includes'.DS.'defines.php') 等。 或者我可以编写一个插件,使用扩展管理器安装它,并且不会为不可用的 joomla 框架而苦恼。但实际上如果我省略了defines.php和framework.php,它就行不通了。
我认为在 Joomla 1.7 中创建插件的指南会有所帮助。
【问题讨论】:
-
您在外部应用程序中使用什么语言?它在不同的主机上吗?你知道 Joomla 代币吗?
-
看到这里完全相同的问题:stackoverflow.com/questions/2176595/…
-
@WooDzu 我的外部应用程序与 Joomla 位于同一主机上,为此我使用 PHP。我不知道 Joomla 代币。这还在 joomla 1.7 中使用吗?
标签: joomla joomla-extensions joomla1.7