Quantcast
Channel: DataTables 1.10 — DataTables forums
Viewing all articles
Browse latest Browse all 2366

Robust audit logging

$
0
0

I wanted to add something to my logging so that could quickly see at a glance what the actual changes to the data were.

There's an example of logging changes here: https://editor.datatables.net/manual/php/events#Logging-changes

And a related discussion here: https://datatables.net/forums/discussion/44932

I do an additional step to compare the $prevValues and the $values and store the difference in two different fields in the database.

I add each of these events to the instance of Editor:

    // Pre functions
    ->on( 'preEdit', function ( $editor, $id, $values ) {
        getPrevValues($editor->db(), $editor->table()[0], $id);
    } )
    ->on( 'preRemove', function ( $editor, $id, $values ) {
        getPrevValues($editor->db(), $editor->table()[0], $id);
    } )
    
    // Post functions
    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), $editor->table()[0], 'create', $id, $values );
    } ) 
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), $editor->table()[0], 'edit', $id, $values );
    } )
    ->on( 'postRemove', function ( $editor, $id, $values ) {
        logChange( $editor->db(), $editor->table()[0], 'delete', $id, $values );
    } )

And then I use array_intersect_key() along with array_diff_assoc() (in PHP) to find the difference between the two and store that in separate fields:

$prevValues = [];

function getPrevValues ( $db, $table, $id ) {
    global $prevValues;
    $prevValues = $db->select( $table, '*', [ 'id' => $id ] )->fetch();
}

function logChange ( $db, $table, $action, $id, $values ) {
    global $prevValues;

    switch ($action) {
        case "create":
            $old_values = [];
            $new_values = $values;
            break;
        case "edit":
            $old_values = array_intersect_key(array_diff_assoc($prevValues,$values),array_diff_assoc($values,$prevValues));
            $new_values = array_intersect_key(array_diff_assoc($values,$prevValues),array_diff_assoc($prevValues,$values));
            break;
        case "delete":
            $old_values = $prevValues;
            $new_values = [];
            break;
    }    
    
    
    if (!empty($old_values) || !empty($new_values) ) {
        
        $db->insert( 'ws_log', [
            'user' => $_POST['currentUser'],
            'when' => date('c'),
            'table' => $table,
            'row' => $id,
            'action' => $action,
            'same_values' => json_encode($prevValues),
            'old_values' => json_encode($old_values),
            'new_values' => json_encode($new_values)
            ]);
    }   
}

Now at a glance I can see 1) who made a change, 2) when it was done, 3) what table and row was it done on, 4) what type of action it was (create, edit, or delete), and 5) what the specific changes to the data were. I also have the prev_values in case I'd like to dig deeper.


Viewing all articles
Browse latest Browse all 2366

Trending Articles