用戶操作記錄
From Joomla! Documentation
如何使用新功能「用戶操作記錄」
本系統是 "Recording Action Logs" project from GSoC 2016一文的實踐。本系統可以建立稽核用的網站操作記錄,並且可以微調來顯示後台操作。擴充套件可以套入這個系統,增加一些客製化訊息,或是套用標準的CRUD操作。
在這個教學中,您會學到如何設定這個新功能。
注意:只有 Super User 才有存取用戶操作記錄的權限。
元件
用戶操作記錄控制台
在管理後台中移至 會員 → 用戶操作記錄
在畫面中您可以看到,Super User 可以查看全站所有的操作,並且也可以檢視、匯出、刪除或是清除記錄。
要知道更多,請見:
外掛
系統 - 用戶操作記錄
啟用之後,這個外掛可以設定要刪除多久之前的記錄
操作記錄
啟用之後,外掛可以記錄核心擴充套件的操作
隱私權 - 用戶操作記錄
啟用之後,外掛可以匯出用戶請求隱私權相關的操作記錄。
模組
用戶操作記錄 - 最新模組
這個模組會在管理區顯示一個近期的操作列表。
注意: 在升級到之時,預設是不會顯示模組,要讓模組顯示在控制台,請訪問 Extensions → Manage → Manage → Enable the System - User Actions Log plugin ,然後到 Extensions → Modules → Administrator → New → Action Logs-Latest
亦請參見: 最近操作記錄模組(管理者介面)
如何掛勾擴充套件到系統
歡迎編輯這個章節來改進或是修正內容
Component Installation Script
新增擴充套件到資料表 (#__action_logs_extensions),使之能夠出現在會員操作記錄的設定介面。
$extension = 'com_mycomponent';
$db = Factory::getDbo();
$db->setQuery(' INSERT into #__action_logs_extensions (extension) VALUES ('.$db->Quote($extension).') ' );
try {
// If it fails, it will throw a RuntimeException
$result = $db->execute();
} catch (RuntimeException $e) {
Factory::getApplication()->enqueueMessage($e->getMessage());
return false;
}
新增擴充套件設定到資料表(#__action_log_config),您的操作會被記錄下來。
$logConf = new stdClass();
$logConf->id = 0;
$logConf->type_title = 'transaction';
$logConf->type_alias = $extension;
$logConf->id_holder = 'id';
$logConf->title_holder = 'trans_desc';
$logConf->table_name = '#__mycomponent_transaction';
$logConf->text_prefix = 'COM_MYCOMPONENT_TRANSACTION';
try {
// If it fails, it will throw a RuntimeException
// Insert the object into the table.
$result = Factory::getDbo()->insertObject('#__action_log_config', $logConf);
} catch (RuntimeException $e) {
Factory::getApplication()->enqueueMessage($e->getMessage());
return false;
}
當然,最好能做檢查以確保重複的資料已經存在
元件 Helper
在範例中,元件 helper 是用來表現儲存的操作記錄。
/**
* Record transaction details in log record
* @param object $user Saves getting the current user again.
* @param int $tran_id The transaction id just created or updated
* @param int $id Passed id reference from the form to identify if new record
* @return boolean True
*/
public static function recordActionLog($user = null, $tran_id = 0, $id = 0)
{
// get the component details such as the id
$extension = MycomponentHelper::getExtensionDetails('com_mycomponent');
// get the transaction details for use in the log for easy reference
$tran = MycomponentHelper::getTransaction($tran_id);
$con_type = "transaction";
if ($id === 0) { $type = 'New '; } else { $type = 'Update '; }
$message = array();
$message['action'] = $con_type;
$message['type'] = $type . $tran->tran_type . ' - '.$tran->tran_desc . ' $' . $tran->tran_amount;
$message['id'] = $tran->id;
$message['title'] = $extension->name;
$message['extension_name'] = $extension->name;
$message['itemlink'] = "index.php?option=com_mycomponent&task=transaction.edit&id=".$tran->id;
$message['userid'] = $user->id;
$message['username'] = $user->username;
$message['accountlink'] = "index.php?option=com_users&task=user.edit&id=".$user->id;
$messages = array($message);
$messageLanguageKey = Text::_('COM_MYCOMPONENT_TRANSACTION_LINK');
$context = $extension->name.'.'.$con_type;
$fmodel = MycomponentHelper::getForeignModel('Actionlog', 'ActionlogsModel');
$fmodel->addLog($messages, $messageLanguageKey, $context, $user->id);
return true;
}
/**
* Get the Model from another component for use
* @param string $name The model name. Optional. Default to my own for safety.
* @param string $prefix The class prefix. Optional
* @param array $config Configuration array for model. Optional
* @return object The model
*/
public function getForeignModel($name = 'Transaction', $prefix = 'MycomponentModel', $config = array('ignore_request' => true))
{
\Joomla\CMS\MVC\Model\ItemModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModelActionlog');
$fmodel = \Joomla\CMS\MVC\Model\ItemModel::getInstance($name, $prefix, $config);
return $fmodel;
}
前台翻譯表單
此時,基礎已經完成,我們需要觸發流程。我們會捕捉和建立或更新相關的資訊,並且我們建立名為transactionform.php的model。它在 Save method 中,我們可以用來捕捉操作記錄。
// So the code above this point checks and does what it should do and then after the successful save of the record, we check for the parameter setting to see if logging is required, we pass key elements to recordActionLog.
$table = $this->getTable();
if ($table->save($data) === true) {
/* ---------------------------------------------------------------- */
// trigger transaction log if required
$act_log = $params->get('act_log', 0);
if ($act_log && $table->id) {
// gather information and log in a new action log record
MycomponentHelper::recordActionLog($user, $table->id, $data['id']);
}
/* ---------------------------------------------------------------- */
return $table->id;
} else {
return false;
}
語言檔案
最後,為了要讓 Joomla! 管理區的操作記錄監聽用戶行為,我們會在en-GB.com_mycomponent.ini語言檔案中,設定一些資料的關鍵元素以用於顯示。
COM_MYCOMPONENT_TRANSACTION_LINK="用戶 <a href='{accountlink}'>{username}</a> 建立一個項目 ( <a href='{itemlink}'>{type}</a> )"
希望這個有幫助