我在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?
答案 0 :(得分:2)
在我看来,你真的完全错误地解决了这个问题。如果您想从另一个访问服务器/网页,则不要使用iFrame并按照您的方式进行操作。这是黑客攻击,这个漏洞很可能会在未来的版本中修复。
您应该做的是连接数据库,只收集用户表中的信息。
没有黑客攻击,没有“诡计”,不会停止工作,也不会更安全。
答案 1 :(得分: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"}';
}
}