【问题标题】:Joomla 2.5 authentication Plugin Fatal error: Call to a member function get() on a non-objectJoomla 2.5 身份验证插件致命错误:在非对象上调用成员函数 get()
【发布时间】:2012-10-09 17:48:36
【问题描述】:

我编写了一个简单的身份验证插件,它使用 SOAP 网络服务来检查用户名和密码。效果很好。

我想在 joomla 的管理员中设置一些参数,例如 SOAP 密码。所以我在 xml 中添加了参数,它在管理员中显示得很好。当我尝试获取 php 的值时,我得到:

致命错误:在非对象上调用成员函数 get()

所以我对比了其他的认证,我的做法一模一样....我不明白为什么会这样。

这是插件的代码:

public function __construct() {
    $nusoap = JPATH_SITE . '/plugins/authentication/ers/nusoap/lib/nusoap.php';
    if (! file_exists ( $nusoap )){
                $response->error_message = "No such file";
        return;
            }
    require_once ($nusoap);

}



function onUserAuthenticate($credentials, $options, &$response)
{



        //Without defaults (the plugin crashes on the first get() bellow)
        $webservice = $this->params->get('webservice', '');
        $group      = $this->params->get('group', '');
        $whitepaw   = $this->params->get('whitepaw', '');



        JRequest::checkToken() or die( 'Invalid Token' );
        // For JLog
        $response->type = 'ERS SOAP Webservice';

            // MyCompany does not like blank passwords (So does Joomla ;))
    if (empty($credentials['password'])) {
        $response->status = JAuthentication::STATUS_FAILURE;
        $response->error_message = JText::_('JGLOBAL_AUTH_EMPTY_PASS_NOT_ALLOWED');
        return false;
    }

    if (empty($credentials['username'])) {
        $response->status = JAuthentication::STATUS_FAILURE;
        $response->error_message = JText::_('Please enter a username');
        return false;
    }

           // Add a user to joomla
                function addJoomlaUser($name, $username, $password, $email, $group) {

                            $data = array(
                                "name"=>$name,
                                "username"=>$username,
                                "password"=>$password,
                                "password2"=>$password,
                                "email"=>$email,
                                "block"=>0,
                                "groups"=>array("1","2", $group) // the uer is added into the group "public" and "registered" as well as a group of the user's choice.

                            );

                            $user = clone(JFactory::getUser());
                            //Write to database
                            if(!$user->bind($data)) {
                                throw new Exception("Could not bind data. Error: " . $user->getError());
                            }
                            if (!$user->save()) {
                                throw new Exception("Could not save user. Error: " . $user->getError());
                            }

                        return $user->id;
                }


            // Pour supprimer le cache du web-service
            ini_set('soap.wsdl_cache_enabled', 0);

            // Nouveau Client SOAP
            try {

                // Nouvelle instance de la classe soapClient
                $client = new SoapClient($webservice, array('trace' => true));


                $username = $credentials['username'];
                $password = $credentials['password'];



                $result = $client->CheckLogin(array('whitepaw'=>$whitepaw, 'username'=>$username, 'password'=>$password));

                if($result->isInDB){


                        $name = $result->fname.' '.$result->lname;
                        $email = $result->email;

                        $response->error_message = $username.'<br>'.$password.'<br>'.$name.'<br>'.$email."<br><br>".
                                "<b>Request :</b><br>".htmlentities($client->__getLastRequest())."<br><br>".
                                "<b>RESPONSE :</b><br>".htmlentities($client->__getLastResponse())."<br><br>";

                        if(!$result->email == '' || empty ($result)) {
                            //Todo: check if the user is already in joomla db
                            $user_id = addJoomlaUser($name, $username, $password, $email,$group);
                            $response->status = JAuthentication::STATUS_SUCCESS;
                            //for testing purposes
                            $response->error_message = $user_id;
                      } else {
                           $response->error_message = "The webservice did not return data".$email.'did you see it?';

                       }

                } else {
                    $response->status = JAuthentication::STATUS_FAILURE;
                    $response->error_message = 'You do not have yet an account in <a href="http://my.ersnet.org">myers</a>. Please register.<br>';
                    $response->error_message .= $result->isInDB;

                }
                } catch (Exception $fault) {
                    $response->error_message = $fault->getMessage();
                }




}

}

【问题讨论】:

    标签: php joomla joomla2.5


    【解决方案1】:

    由于你有自己的构造函数,你需要像这样调用父构造函数:

    public function __construct(& $subject, $params = array()) {
        $nusoap = JPATH_SITE . '/plugins/authentication/ers/nusoap/lib/nusoap.php';
        if (! file_exists ( $nusoap )){
                    $response->error_message = "No such file";
            return;
                }
        require_once ($nusoap);
        // call the parent constructor
        parent::__construct($subject, $params);
    }
    

    父构造函数是设置$this-&gt;params 对象的位置,因此如果您不调用它,则永远不会设置$this-&gt;params。这就是为什么您会收到错误消息说 params 不是对象。

    【讨论】:

    • 谢谢。我理解你回答的逻辑。我已经尝试过,但现在它要求我缺少论点......为什么会这样?在我的构造函数中,我没有。
    • Warning: Missing argument 1 for JPlugin::__construct(), called in /home/forum/public_html/plugins/authentication/ers/ers.php on line 30 and defined in /home/forum/public_html/libraries/joomla/plugin/plugin.php on line 55 Fatal error: Call to a member function attach() on a non-object in /home/forum/public_html/libraries/joomla/event/event.php on line 39 但是如果我删除构造函数并在插件方法中调用 lib nusoap 一切都很好...... @MrCode
    • @Wiglaf 编辑了我的答案,它需要一个 $subject 参数。
    • Fatal error: Call to a member function attach() on a non-object in /home/forum/public_html/libraries/joomla/event/event.php on line 39 可能还有一个?
    猜你喜欢
    • 2013-06-12
    • 2012-09-26
    • 1970-01-01
    • 2013-11-25
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    • 2012-05-14
    • 2013-11-19
    相关资源
    最近更新 更多