0

我从 2 天开始就对这个问题感到恼火。

我正在使用MODx Revolution 2.2.5(传统)并且想从外部服务器登录到 modx 只是为了获取一些用户详细信息。

1)我知道 runprocessor 方法只有在我登录到管理器时才有效(不幸的是,这是我知道登录用户的唯一方法)所以我尝试了 IFRAME 方法来避免(跨脚本)它工作得很好但我不能由于同样的问题,跨域访问策略,使用 javascript 从 IFRAME 读取数据。

当我尝试使用 CURL 等其他方法发布数据时,Ajax 使用

header("Access-Control-Allow-Origin: *"); 

我能够登录(我看到 $response->response['success'] == 1)但无法访问任何数据,它说

Fatal error: Call to a member function get() on a non-object

下面是我正在使用的代码片段

if(isset($_POST) && count($_POST)){
    $c = array(
        'username' => $_POST['username'],
        'password' => $_POST['password']
    );
    $response = $modx->runProcessor('security/login',$c);
    if($response->response['success'] == 1){
        $user['id'] = $modx->user->get('id');
                $profile = $modx->user->getOne('Profile');
        $user['fullname'] = $profile->get('fullname');
        $user['email'] = $profile->get('email');
        echo json_encode($user);
    }else{
        echo json_encode($response->response); 
    }
}

2)我可以使用登录片段,但它不会返回我期望的输出。我们已经准备好了站点,并且我们已经在使用登录插件,所以我什至无法修改登录插件以响应预期的数据

我如何使用 api 或任何其他方法登录 modx ?

4

2 回答 2

2

在我看来,你真的完全错误地攻击了这个问题。如果您想从另一个访问服务器/网页,则不要使用 iFrame 并按照您的方式进行操作。这就是黑客攻击,这个漏洞很可能会在未来的版本中得到修复。

您应该做的是连接到数据库并从用户表中收集信息。

没有黑客攻击,没有“技巧”,不会停止工作并且更安全。

于 2012-12-23T20:55:10.587 回答
0

好吧,我今天整理了这个,下面是完美运行的完整版本。注意

header("Access-Control-Allow-Origin: http://www.xyz.com");

使用上述 CORS 规范,您可以允许 2 个服务器进行通信。

header("Access-Control-Allow-Origin: http://www.xyz.com");
if(isset($_POST['username']) && isset($_POST['password'])){

// get username and password from POST array
$username = $modx->sanitizeString($_POST['username']);
$password = $modx->sanitizeString($_POST['password']);
if(trim($username) != "" and trim($password) != ""){
    // Load lexicons to show proper error messages
    if (!isset($modx->lexicon) || !is_object($modx->lexicon)) {
        $modx->getService('lexicon','modLexicon');
    }
    $modx->lexicon->load('login');

    $loginContext= isset ($scriptProperties['login_context']) ? $scriptProperties['login_context'] :
    $modx->context->get('key');

    $addContexts= isset ($scriptProperties['add_contexts']) && !empty($scriptProperties['add_contexts']) ? explode(',', $scriptProperties['add_contexts']) : array();

    $mgrEvents = ($loginContext == 'mgr');

    $givenPassword = $password;

    /** @var $user modUser */
    $user= $modx->getObjectGraph('modUser', '{"Profile":{},"UserSettings":{}}', array ('modUser.username' => $username));

    if (!$user) {
        $ru = $modx->invokeEvent("OnUserNotFound", array(
            'user' => &$user,
            'username' => $username,
            'password' => $password,
            'attributes' => array(
                'loginContext' => $loginContext,
            )
        ));

        if (!empty($ru)) {
            foreach ($ru as $obj) {
                if (is_object($obj) && $obj instanceof modUser) {
                    $user = $obj;
                    break;
                }
            }
        }

        if (!is_object($user) || !($user instanceof modUser)) {
            //echo "cant locate account";
            echo $modx->toJSON($modx->error->failure($modx->lexicon('login_cannot_locate_account')));
            exit;
        }
    }

    if (!$user->get('active')) {
        //echo "inactivated accout";
        echo $modx->toJSON($modx->error->failure($modx->lexicon('login_user_inactive')));
            exit;
        }

    if (!$user->passwordMatches($givenPassword)) {
        if (!array_key_exists('login_failed', $_SESSION)) {
            $_SESSION['login_failed'] = 0;
        }
        if ($_SESSION['login_failed'] == 0) {
            $flc = ((integer) $user->Profile->get('failedlogincount')) + 1;
            $user->Profile->set('failedlogincount', $flc);
            $user->Profile->save();
            $_SESSION['login_failed']++;
        } else {
            $_SESSION['login_failed'] = 0;
        }
        //echo "wrong username pass";
        echo $modx->toJSON($modx->error->failure($modx->lexicon('login_username_password_incorrect')));
            exit;
        }

        $fullname =  $user->Profile->get('fullname');
        echo '{"success":true,"message":"Welcome '.$fullname.'!"}';
 }else{
        echo '{"success":false,"message":"Please enter username and password"}';
 }

}

于 2012-12-23T19:16:52.327 回答