答案 0 :(得分:0)
这很简单。创建一个名为activity的模型并创建一个日志方法:
$this->activity->log("{$this->curr_user->name} logged in.");
我个人使用短代码和开关语句(如果你有很多活动,可能会变得非常笨重 - 不确定是否推荐):
$this->activity->log('page_access', array('uri' => uri_string()));
或
$this->activity->log('modified', array('table' => $table, 'id' => $id));
这是我的活动模型:
class User_activity_model extends CI_Model {
/**
* Admin-only log types
*
* @var array
*/
private $admin_types = array('page_access', 'user_registered', 'logged_in', 'login_failed', 'login_failed_sa');
/**
* Main user activity logging function
*
* @param string $action
* @param array $arr Additional attributes per case
* @return void
*/
public function log($action, $arr = array()) {
switch ($action) {
default:
return;
case 'backup_created':
$username = $this->curr_user->details()->username;
$msg = "{$username} created a backup {$arr['filename']} at {time}.";
break;
case 'backup_restored':
$username = $this->curr_user->details()->username;
$msg = "{$username} restored backup {$arr['filename']} at {time}.";
break;
case 'user_registered':
$msg = "{$arr['first_name']} {$arr['last_name']} registered with username: {$arr['username']}.";
break;
case 'added':
$username = $this->curr_user->details()->username;
$msg = "{$username} added item {$arr['id']} to the {$arr['table']} table.";
break;
case 'modified':
$username = $this->curr_user->details()->username;
$msg = "{$username} modified item {$arr['id']} in the {$arr['table']} table.";
break;
case 'deleted':
$username = $this->curr_user->details()->username;
$msg = "{$username} deleted item {$arr['id']} from the {$arr['table']} table.";
break;
case 'published':
$username = $this->curr_user->details()->username;
$msg = "{$username} published item {$arr['id']} from the {$arr['table']} table.";
break;
case 'unpublished':
$username = $this->curr_user->details()->username;
$msg = "{$username} unpublished item {$arr['id']} from the {$arr['table']} table.";
break;
case 'logged_in':
$username = $this->curr_user->details()->username;
$ip = $this->input->ip_address();
$msg = "{$username} logged in at {time} from IP {$ip}.";
break;
case 'login_failed':
$ip = $this->input->ip_address();
$msg = "Someone tried to login with username {$arr['username']} at {time} from IP {$ip}.";
break;
case 'login_failed_sa':
$ip = $this->input->ip_address();
$msg = "Someone tried to login with social auth provider {$arr['provider']} at {time} from IP {$ip}.";
break;
case 'page_access':
if ($this->ion_auth->logged_in()) {
$identity = $this->curr_user->details()->username;
} else {
$identity = "Someone (IP: {$this->input->ip_address()})";
}
$msg = "{$identity} tried to access a page they didn't have permission for: {$arr['uri']}.";
break;
case 'logout':
$username = $this->curr_user->details()->username;
$msg = "{$username} logged out at {time}.";
break;
}
$data = array(
'action' => $action,
'type' => in_array($action, $this->admin_types) ? 'admin' : 'all',
'message' => $msg
);
$this->add_info();
$this->db->insert('user_activity', $data);
}
/**
* Adds identifier information to the insert query
*
* @return void
*/
public function add_info() {
$this->db->set('time', 'NOW()', false);
$this->db->set('ip', $this->input->ip_address());
if (!empty($this->curr_user->id())) {
$this->db->set('user_id', $this->curr_user->id());
}
}
/**
* Generates log array
*
* Format:
*
* array(
* [date]
* array(
* [0] = message1
* [1] = message2
*
* @param boolean $admin_only Show all logs?
* @return boolean|object Logs object, FALSE otherwise
*/
public function get_logs($admin_only = true) {
if (!$admin_only) {
$this->db->where('type !=', 'admin');
}
$this->db->order_by('time', 'DESC');
$query = $this->db->get('user_activity');
if ($query->num_rows() < 1) {
return false;
}
$rows = $query->result();
$data = new \stdClass();
foreach ($rows as $row) {
// replace {time} with timezone converted time
$time = $this->timezone->convert($row->time);
$row->message = str_replace('{time}', $time, $row->message);
// date Y-m-d acts as key for messages on that day
$key = date('Y-m-d', strtotime($time));
$data->{$key}[] = $row;
}
return $data;
}
/**
* Truncates user_activity table
*
* @return boolean
*/
public function delete_logs() {
return $this->db->truncate('user_activity');
}
/**
* Stub for automate hook to add 'added' activity
*
* @param array $data
*/
public function automate_activity_add($data) {
$this->activity->log('added', array('table' => $data['table'], 'id' => $data['id']));
}
/**
* Stub for automate hook to add 'modified' activity
*
* @param array $data
*/
public function automate_activity_modify($data) {
$this->activity->log('modified', array('table' => $data['table'], 'id' => $data['id']));
}
}