2012-08-29 128 views
0

当我用admin用户运行代码时,模块返回它应该的内容。但是,当我用普通用户运行它时,出现403错误。该模块从AJAX调用返回数据。我已经尝试添加'访问回调'=>'user_access');exoticlang_chat_logger_menu()函数。我会很感激你可能有的任何指针。Drupal模块权限

感谢您的帮助

的AJAX调用:

jQuery.ajax({ 
    type: 'POST', 
    url: '/chatlog', 
    success: exoticlangAjaxCompleted, 
    data:'messageLog=' + privateMessageLogJson, 
    dataType: 'json' 
}); 

模块代码:

function exoticlang_chat_logger_init(){ 
drupal_add_js('misc/jquery.form.js'); 
drupal_add_library('system', 'drupal.ajax'); 
} 

function exoticlang_chat_logger_permission() { 
    return array(
    'Save chat data' => array(
     'title' => t('Save ExoticLang Chat Data'), 
     'description' => t('Send private message on chat close') 
    ), 
); 
} 

/** 
* Implementation of hook_menu(). 
*/ 

function exoticlang_chat_logger_menu() { 
    $items = array(); 
    $items['chatlog'] = array(
    'type'    => MENU_CALLBACK, 
    'page callback' => 'exoticlang_chat_log_ajax', 
    'access arguments' => 'Save chat data'); 
    //'access callback' => 'user_access'); 
    return $items; 
} 

function exoticlang_chat_logger_ajax(){ 
    $messageLog=stripslashes($_POST['messageLog']); 

    $chatLog= 'Drupal has processed this. Message log is: '.$messageLog; 
    $chatLog=str_replace('":"{[{','":[{',$chatLog); 
    $chatLog=str_replace(',,',',',$chatLog); 
    $chatLog=str_replace('"}"','"}',$chatLog); 
    $chatLog=str_replace('"}]}"','"}]',$chatLog); 

    echo json_encode(array('messageLog' => $chatLog)); 
// echo $chatLog; 

    echo print_r(privatemsg_new_thread(array(user_load(1)), 'The subject', 'The body text')); 
    drupal_exit(); 
} 

回答

2

access arguments需求是一个数组:

$items['chatlog'] = array(
    'type'    => MENU_CALLBACK, 
    'page callback' => 'exoticlang_chat_log_ajax', 
    'access arguments' => array('Save chat data') 
); 
+0

那是问题!万分感谢。但有一个问题 - 如果访问参数使用了错误的语法,为什么它对管理员用户有效? –

+0

默认的访问回调是'user_access()',它给了管理员用户一个免费的通行证......基本上没有为该用户进行访问检查 – Clive