| 1 | package DBIx::Class::Schema::AuditLog::Structure; | ||||||||||||||||||||||||||||
| 2 | 12 | $DBIx::Class::Schema::AuditLog::Structure::VERSION = '0.6.2'; | |||||||||||||||||||||||||||
| 3 | 12 | ▶ | use base qw/DBIx::Class::Schema/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 12 | require DBIx::Class::Schema::AuditLog::Structure::Action; | |||||||||||||||||||||||||||
| 9 | 12 | require DBIx::Class::Schema::AuditLog::Structure::AuditedTable; | |||||||||||||||||||||||||||
| 10 | 12 | require DBIx::Class::Schema::AuditLog::Structure::Change; | |||||||||||||||||||||||||||
| 11 | 12 | require DBIx::Class::Schema::AuditLog::Structure::Changeset; | |||||||||||||||||||||||||||
| 12 | 12 | require DBIx::Class::Schema::AuditLog::Structure::Field; | |||||||||||||||||||||||||||
| 13 | 12 | require DBIx::Class::Schema::AuditLog::Structure::User; | |||||||||||||||||||||||||||
| 14 | 12 | require DBIx::Class::Schema::AuditLog::Structure::View; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 12 | __PACKAGE__->mk_group_accessors( simple => '_current_changeset_container' ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub _current_changeset { | ||||||||||||||||||||||||||||
| 19 | 85 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_current_changeset: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 85 | my $ref = $self->_current_changeset_container; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 85 | ▶ | return $ref && $ref->{changeset}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$ref && $ref->{'changeset'}
MC/DC100%2/2
$ref && $ref->{'changeset'}
$ref
$ref->{'changeset'}
Truth table100%3/3
A && B
A= $refB= $ref->{'changeset'}
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub current_changeset { | ||||||||||||||||||||||||||||
| 27 | 56 | ▶ | my ( $self, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
current_changeset: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 56 | ▶ | $self->throw_exception('Cannot set changeset manually. Use txn_do.') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if @args; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # we only want to create a changeset if the action (insert/update/delete) | ||||||||||||||||||||||||||||
| 33 | # is being run from txn_do -- the txn_do method in | ||||||||||||||||||||||||||||
| 34 | # DBIx::Class::Schema::AuditLog sets local | ||||||||||||||||||||||||||||
| 35 | # _current_changeset_container->{changeset} & | ||||||||||||||||||||||||||||
| 36 | # _current_changeset_container->{args} variables in the scope | ||||||||||||||||||||||||||||
| 37 | # of each transaction | ||||||||||||||||||||||||||||
| 38 | 56 | ▶ | if ( defined $self->_current_changeset_container | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->_current_changeset_container and defined $self->_current_changeset_container->{"changeset"}
MC/DC0%0/2
defined $self->_current_changeset_container and defined $self->_current_changeset_container->{"changeset"}
defined $self->_current_changeset_container
defined $self->_current_changeset_container->{"changeset"}
Truth table33%1/3
A and B
A= defined $self->_current_changeset_containerB= defined $self->_current_changeset_container->{"changeset"}
| |||||||||||||||||||||||||||||
| 39 | && defined $self->_current_changeset_container->{changeset} ) | ||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 56 | my $id = $self->_current_changeset; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 56 | ▶ | unless ($id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | my $changeset = $self->audit_log_create_changeset( | ||||||||||||||||||||||||||||
| 46 | 27 | $self->_current_changeset_container->{args} ); | |||||||||||||||||||||||||||
| 47 | 27 | $self->_current_changeset_container->{changeset} = $changeset->id; | |||||||||||||||||||||||||||
| 48 | 27 | $id = $changeset->id; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 56 | return $id; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | return; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub audit_log_create_changeset { | ||||||||||||||||||||||||||||
| 59 | 27 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
audit_log_create_changeset: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 27 | my $changeset_data = shift; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 27 | my ( $changeset, $user ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 27 | ▶ | my $user_id = $changeset_data->{user_id} || $changeset_data->{user}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$changeset_data->{'user_id'} || $changeset_data->{'user'}
MC/DC100%2/2
$changeset_data->{'user_id'} || $changeset_data->{'user'}
$changeset_data->{'user_id'}
$changeset_data->{'user'}
Truth table100%3/3
A || B
A= $changeset_data->{'user_id'}B= $changeset_data->{'user'}
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 27 | ▶ | if ( $user_id ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 16 | $user = $self->resultset('AuditLogUser') | |||||||||||||||||||||||||||
| 68 | ->find_or_create( { name => $user_id } ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | $changeset = $user->create_related( 'Changeset', | ||||||||||||||||||||||||||||
| 71 | 16 | { description => $changeset_data->{description} } ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | $changeset = $self->resultset('AuditLogChangeset') | ||||||||||||||||||||||||||||
| 75 | 11 | ->create( { description => $changeset_data->{description} } ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 27 | return $changeset; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub audit_log_create_action { | ||||||||||||||||||||||||||||
| 83 | 56 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
audit_log_create_action: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 56 | my $action_data = shift; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 56 | my $changeset = $self->current_changeset; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 56 | ▶ | if ($changeset) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | my $table = $self->resultset('AuditLogAuditedTable') | ||||||||||||||||||||||||||||
| 90 | 56 | ->find_or_create( { name => $action_data->{table} } ); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | return ( | ||||||||||||||||||||||||||||
| 93 | $self->resultset('AuditLogChangeset')->find($changeset) | ||||||||||||||||||||||||||||
| 94 | ->create_related( | ||||||||||||||||||||||||||||
| 95 | 'Action', | ||||||||||||||||||||||||||||
| 96 | { audited_row => $action_data->{row}, | ||||||||||||||||||||||||||||
| 97 | audited_table_id => $table->id, | ||||||||||||||||||||||||||||
| 98 | action_type => $action_data->{action_type}, | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 56 | ), | |||||||||||||||||||||||||||
| 101 | $table | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub get_changes { | ||||||||||||||||||||||||||||
| 109 | 12 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_changes: called CC=12 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 12 | my $options = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 12 | my $audited_row = $options->{id}; | |||||||||||||||||||||||||||
| 113 | 12 | ▶ | my $change_order = $options->{change_order} || 'desc'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'change_order'} || 'desc'
MC/DC0%0/1
$options->{'change_order'} || 'desc'
$options->{'change_order'}
Truth table50%1/2
A
A= $options->{'change_order'}
| |||||||||||||||||||||||||||||
| 114 | 12 | my $field_name = $options->{field}; | |||||||||||||||||||||||||||
| 115 | 12 | my $table_name = $options->{table}; | |||||||||||||||||||||||||||
| 116 | 12 | my $timestamp = $options->{created_on}; | |||||||||||||||||||||||||||
| 117 | my $action_types = $options->{action_types} | ||||||||||||||||||||||||||||
| 118 | 12 | ▶ | || [ 'insert', 'update', 'delete' ]; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'action_types'} || ['insert', 'update', 'delete']
MC/DC0%0/1
$options->{'action_types'} || ['insert', 'update', 'delete']
$options->{'action_types'}
Truth table50%1/2
A
A= $options->{'action_types'}
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # row and table are required for all changes | ||||||||||||||||||||||||||||
| 121 | 12 | ▶ | return if !$audited_row || !$table_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$audited_row and $table_name
MC/DC0%0/2
$audited_row and $table_name
$audited_row
$table_name
Truth table33%1/3
A and B
A= $audited_rowB= $table_name
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 12 | ▶ | my $table_criteria = { prefetch => 'Field' } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | if $field_name; | ||||||||||||||||||||||||||||
| 125 | 12 | my $table = $self->resultset('AuditLogAuditedTable') | |||||||||||||||||||||||||||
| 126 | ->find( { name => $table_name }, $table_criteria ); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # cannot get changes if the specified table hasn't been logged | ||||||||||||||||||||||||||||
| 129 | 12 | ▶ | return unless defined $table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 12 | ▶ | my $field = $table->find_related( 'Field', { name => $field_name } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | if $field_name; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 12 | my $changeset_criteria = {}; | |||||||||||||||||||||||||||
| 135 | 12 | ▶ | $changeset_criteria->{'me.created_on'} = $timestamp if $timestamp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 12 | my $changesets = $self->resultset('AuditLogChangeset')->search_rs( | |||||||||||||||||||||||||||
| 137 | $changeset_criteria | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 12 | my $actions = $changesets->search_related( | |||||||||||||||||||||||||||
| 141 | 'Action', | ||||||||||||||||||||||||||||
| 142 | { 'Action.audited_table_id' => $table->id, | ||||||||||||||||||||||||||||
| 143 | 'Action.audited_row' => $audited_row, | ||||||||||||||||||||||||||||
| 144 | 'Action.action_type' => $action_types, | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 12 | ▶ | if ( $actions != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | # if field is passed and the passed field wasn't found in the Field | ||||||||||||||||||||||||||||
| 150 | # table set field id to -1 to ensure a $changes object with ->count = | ||||||||||||||||||||||||||||
| 151 | # 0 is returned | ||||||||||||||||||||||||||||
| 152 | 12 | my $criteria = {}; | |||||||||||||||||||||||||||
| 153 | 12 | ▶ | if ( $field_name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 12 | ▶ | $criteria->{field_id} = $field ? $field->id : -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 12 | my $changes = $actions->search_related_rs( | |||||||||||||||||||||||||||
| 158 | 'Change', | ||||||||||||||||||||||||||||
| 159 | $criteria, | ||||||||||||||||||||||||||||
| 160 | { order_by => { "-$change_order" => 'me.id' }, | ||||||||||||||||||||||||||||
| 161 | prefetch => [{ 'Action' => { 'Changeset' => 'User' } }, { 'Field' => 'AuditedTable' }], | ||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||
| 164 | 12 | return $changes; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | return; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 12 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =pod | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head1 NAME | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | DBIx::Class::Schema::AuditLog::Structure | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head1 VERSION | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | version 0.6.2 | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 current_changeset | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Returns the changeset that is currently in process. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | This is localized to the scope of each transaction. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head2 audit_log_create_changeset | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | Creates a new Changeset for audited Actions. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Will create a new Audit Log User if ncessary. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 audit_log_create_action | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Creates a related Action for the current Changeset. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Also will create an AuditedTable for the new action if | ||||||||||||||||||||||||||||
| 204 | it doesn't already exist. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head2 get_changes | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Returns DBIC resultset of audit changes. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Takes a passed options hashref. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Required: | ||||||||||||||||||||||||||||
| 213 | id: row id from the table that was audited | ||||||||||||||||||||||||||||
| 214 | table: name of the table that was audited | ||||||||||||||||||||||||||||
| 215 | this must include the schema name | ||||||||||||||||||||||||||||
| 216 | for databases that have multiple schemas | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | Optional: | ||||||||||||||||||||||||||||
| 219 | action_types: array ref of action types: [ delete, insert, update ] | ||||||||||||||||||||||||||||
| 220 | change_order: sets the order to return the results, either asc, or desc | ||||||||||||||||||||||||||||
| 221 | defaults to desc | ||||||||||||||||||||||||||||
| 222 | field: name of the field that was audited | ||||||||||||||||||||||||||||
| 223 | created_on: timestamp of the changeset to search by | ||||||||||||||||||||||||||||
| 224 | takes a standard dbic where clause for a field, | ||||||||||||||||||||||||||||
| 225 | eg: | ||||||||||||||||||||||||||||
| 226 | '2012-07-09-15.25.18' | ||||||||||||||||||||||||||||
| 227 | or | ||||||||||||||||||||||||||||
| 228 | { '>=' , '2012-07-09-15.25.18; } | ||||||||||||||||||||||||||||
| 229 | the timestamp must already be in the format that the | ||||||||||||||||||||||||||||
| 230 | database stores in | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Mark Jubenville <ioncache@cpan.org> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | This software is copyright (c) 2012 by Mark Jubenville. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 241 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||