2012-08-29 126 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