0

在 Joomla 源代码中,我找到了一个方法 caled onUserAuthenticate,在 API 中找不到(通过谷歌),但它的功能类似于onLoginUser ...所以,在登录/密码检查后,我需要通过运行更多代码这个功能。结果,我有true/false- 根据它我需要完全设置用户的授权。即使用户的登录名/密码正确,但我的代码返回 false -> 授权失败...

我正在尝试类似的东西:

functionon UserAuthenticate($credentials,$options,&$response){

  jimport('joomla.user.helper');

  $username=mysql_real_escape_string($credentials['username']);
  $password=mysql_real_escape_string(md5($credentials['password']));

  //my code returns $result

  if($result!=NULL){
    $response->status=JAUTHENTICATE_STATUS_SUCCESS;
    $response->error_message='';
  }
  else{
    $response->status=JAUTHENTICATE_STATUS_FAILURE;
    $response->error_message=JText::_('JGLOBAL_AUTH_INVALID_PASS');
  }
}
4

3 回答 3

1

onUserAuthenticate 是一个事件而不是一个方法。您使用插件来侦听 Joomla 事件,在这种情况下,通常用户插件会侦听此事件。当事件发生时,您的代码将运行。 http://docs.joomla.org/Plugin

于 2012-10-04T15:25:16.577 回答
0

你可以试试这个自定义登录表单 -

$app = JFactory::getApplication();
$data = array();
$data['return']     = '';
$data['username']   = JRequest::getVar('username', '', 'method', 'username');
$data['password']   = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);

// Get the log in options.
$options = array();

// Get the log in credentials.
$credentials = array();
$credentials['username'] = $data['username'];
$credentials['password'] = $data['password'];

// Perform the log in.
$error = $app->login($credentials, $options);

if (!JError::isError($error)) {
    $response->status=JAUTHENTICATE_STATUS_SUCCESS;
    $response->error_message='';                    
}else{
    $response->status=JAUTHENTICATE_STATUS_FAILURE;
    $response->error_message=JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
于 2012-10-04T14:37:46.100 回答
0

如果您想对功能“onUserAuthenticate”进行身份验证解决方案,您应该自己检查用户凭据是否有效并且您使用以下代码执行此操作:

   function onUserAuthenticate($credentials, $options, &$response)
   {
    $response->type = 'Joomla';
    // Joomla does not like blank passwords
    if (empty($credentials['password'])) {
      $response->status = JAuthentication::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;

        $response->status = JAuthentication::STATUS_SUCCESS;
        $response->error_message = '';

        print_r("You login correct Sir");
        die();        

      } else {
        print_r("you enter wrong credential");
        die();

        $response->status = JAuthentication::STATUS_FAILURE;
        $response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');

      }

    } else {

    print_r("you enter blank credential");
    die();
      $response->status = JAuthentication::STATUS_FAILURE;
      $response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');

    }
      return true;
   }
于 2012-11-18T09:16:25.917 回答