Skip to content

Commit

Permalink
Merge pull request #712 from USGCRP/MoreAuditFixes
Browse files Browse the repository at this point in the history
Fixed more sources of audit user/note errors.
  • Loading branch information
zacharylandes committed Nov 18, 2019
2 parents aab4912 + ecb3c53 commit dbfd70b
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ used for production instances.
1. Install [docker compose](https://docs.docker.com/compose/install/)
1. run `docker build -t gcis .`
- Go get some tea.
1. run `mkdir -p files/assets-back`
1. Setup postgres *alone* first
1. If you have an existing postgres container, run `docker rm gcis_postgres_1`
1. Download the latest [public content release](https://github.com/USGCRP/gcis/releases)
1. Untar the files
1. the `schema` file should be moved to `./db/docker/2_schema.sql`
1. the `content_1` file should be moved to `./db/docker/3_content_1.sql`
1. the `content_2` file should be moved to `./db/docker/4_content_2.sql`
- if you want an empty GCIS instance, only copy the schema & content 1.
1. run `docker-compose up postgres`
1. run `docker-compose up postgres &`
- Refresh your tea.
1. after the previous command finishes loading, exit the command
1. after the previous command finishes loading, run `docker-compose stop`
1. Start the full docker set
1. run `docker-compose up`
1. GCIS should be available at `127.0.0.1` with all content available
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '2'
version: '3'
services:
nginx:
image: nginx:latest
Expand All @@ -18,6 +18,8 @@ services:
- PGHOST=postgres
- PGUSER=gcisops
- MOJO_MODE=development
volumes:
- ./files:/var/local/www

postgres:
image: postgres:9.6
Expand Down
4 changes: 2 additions & 2 deletions lib/Tuba/Activity.pm
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ sub update_rel {
for my $id ($c->param('delete_publication')) {
next unless $id;
Tuba::DB::Object::Methodology::Manager->delete_objects(
{ activity_identifier => $activity->identifier,
publication_id => $id });
where => [{activity_identifier => $activity->identifier, publication_id => $id}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Tuba/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ sub update_rel {
}

for my $id ($c->param('delete_table')) {
ArrayTableMaps->delete_objects({ table_identifier => $id, array_identifier => $object->identifier });
ArrayTableMaps->delete_objects(
where => [{table_identifier => $id, array_identifier => $object->identifier}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}

Expand Down
10 changes: 6 additions & 4 deletions lib/Tuba/Controller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,13 @@ PUT files.

sub put_files {
my $c = shift;

my $file = Mojo::Upload->new(asset => Mojo::Asset::File->new->add_chunk($c->req->body));
$file->filename($c->stash("filename") || 'asset');
my $obj = $c->_this_object or return $c->reply->not_found;
my $pub = $obj->get_publication(autocreate => 1);
my $tfile = $pub->upload_file(c => $c, upload => $file) or do {

my $tfile = $pub->upload_file(c => $c, upload => $file, audit_user => $c->user, audit_note => $c->audit_note) or do {
return $c->render(status => 500, text => $pub->error);
};
$tfile->generate_thumbnail();
Expand Down Expand Up @@ -1219,8 +1221,8 @@ sub update_rel {
for my $id ($c->param("delete_$dwhat")) {
next unless $id;
$mwhat->delete_objects(
{ "${dwhat}_identifier" => $id,
publication_id => $pub->id });
where => [{"${dwhat}_identifier" => $id, publication_id => $pub->id}],
audit_user => $c->user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}
}
Expand Down Expand Up @@ -1404,7 +1406,7 @@ sub remove {
or return $c->redirect_with_error('update_form',"couldn't find $json->{replacement}");
@replacement = (replacement => $rpl);
}
$object->delete(audit_user => $c->user, @replacement)
$object->delete(audit_user => $c->user, audit_note => $c->audit_note, @replacement)
or return $c->redirect_with_error('update_form', $object->error);
return $c->render(text => 'ok');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Tuba/DB/Mixin/Object/Publication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ sub upload_file {
};
$tfile->size($file->size);
$pub->add_files($tfile);
$pub->save(audit_user => $c->user);
$pub->save(audit_user => $c->user, audit_note => $c->audit_note);
$tfile->meta->error_mode('return');
$tfile->save(audit_user => $c->user) or do {
$tfile->save(audit_user => $c->user, audit_note => $c->audit_note) or do {
$pub->error($tfile->error);
return;
};
Expand Down
48 changes: 48 additions & 0 deletions lib/Tuba/DB/Object/Manager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,53 @@ sub dbgrep {
}


sub delete_objects
{
# Get the arguments.
#
my $object = shift;
my %args = @_;

# Get the database and database handle objects.
#
my $db = $object->object_class->init_db;
my $dbh = $db->retain_dbh;

# Add the database and database handle to the args.
#
$args{db} = $db;
$args{dbh} = $dbh;

# Remove the audit user and audit note arguments. Throw an error if there is
# no audit user argument.
#
my $audit_user = delete $args{audit_user} or die "missing audit_user for $object";
my $audit_note = delete $args{audit_note};

# Do the database transaction.
#
my $count= 0;

$db->do_transaction( sub {
# Add the audit user and audit note.
#
$dbh->do("set local audit.username = ?",{},$audit_user);
$dbh->do("set local audit.note = ?",{},$audit_note) if $audit_note;

# Call the super-class delete_objects method with the arguments.
#
$count = $object->SUPER::delete_objects(%args);
}
) or do {
# Record an error if the transaction didn't succeed.
#
$object->error($db->error) unless $object->error;
};

# Return the object count result from the parent delete_objects method.
#
return $count;
}


1;
18 changes: 14 additions & 4 deletions lib/Tuba/Figure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,23 @@ sub update_rel {
}

my $report_identifier = $c->stash('report_identifier');
my @delete_images = $c->param('delete_image');

my @delete_images;

@delete_images = $c->param('delete_image') if $c->param('delete_image');

if (my $nother = $json->{delete_image_identifier}) {
push @delete_images, $nother;
}
for my $id (@delete_images) {
ImageFigureMaps->delete_objects({ image_identifier => $id, figure_identifier => $object->identifier, report_identifier => $report_identifier });
$c->flash(message => 'Saved changes');

if (0 < scalar @delete_images) {
for my $id (@delete_images) {
ImageFigureMaps->delete_objects(
where => [{figure_identifier => $object->identifier,
report_identifier => $report_identifier}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}
}

return $c->SUPER::update_rel(@_);
Expand Down
4 changes: 3 additions & 1 deletion lib/Tuba/Image.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ sub update_rel {
}

for my $id ($c->param('delete_figure')) {
ImageFigureMaps->delete_objects({ figure_identifier => $id, image_identifier => $object->identifier });
ImageFigureMaps->delete_objects(
where => [{figure_identifier => $id, image_identifier => $object->identifier}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Tuba/Methodology.pm
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ sub update_rel {
for my $id ($c->param('delete_publication')) {
next unless $id;
Methodology->delete_objects(
{ activity_identifier => $activity->identifier,
publication_id => $id });
where => [{activity_identifier => $activity->identifier, publication_id => $id}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}

Expand Down
14 changes: 8 additions & 6 deletions lib/Tuba/Organization.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ sub update_rel {
if (my $pub_id = $json->{delete_publication} || $c->param('delete_publication')) {
my $con_id = $json->{contributor_id} || $c->param('contributor_id');
die "person does not match contributor" unless Contributor->new(id => $con_id)->load->organization_identifier eq $org->identifier;
PublicationContributorMaps->delete_objects({
contributor_id => $con_id,
publication_id => $pub_id,
}) or return $c->update_error("Failed to remove publication");
PublicationContributorMaps->delete_objects(
where => [{ contributor_id => $con_id, publication_id => $pub_id }],
audit_user => $c->audit_user, audit_note => $c->audit_note)
or return $c->update_error("Failed to remove publication");
} elsif (my $id = $json->{delete_contributor} || $c->param('delete_contributor')) {
die "person does not match contributor" unless Contributor->new(id => $id)->load->organization_identifier eq $org->identifier;
Contributors->delete_objects({ id => $id })
or return $c->update_error("Failed to remove contributor");
Contributors->delete_objects(
where => [{ id => $id }],
audit_user => $c->audit_user, audit_note => $c->audit_note)
or return $c->update_error("Failed to remove contributor");
$c->flash(info => "Saved changes.");
}

Expand Down
14 changes: 8 additions & 6 deletions lib/Tuba/Person.pm
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,16 @@ sub update_rel {
if (my $pub_id = $json->{delete_publication} || $c->param('delete_publication')) {
my $con_id = $json->{contributor_id} || $c->param('contributor_id');
die "person does not match contributor" unless Contributor->new(id => $con_id)->load->person_id == $person->id;
PublicationContributorMaps->delete_objects({
contributor_id => $con_id,
publication_id => $pub_id,
}) or return $c->update_error("Failed to remove publication");
PublicationContributorMaps->delete_objects(
where => [{contributor_id => $con_id, publication_id => $pub_id}],
audit_user => $c->audit_user, audit_note => $c->audit_note)
or return $c->update_error("Failed to remove publication");
} elsif (my $id = $json->{delete_contributor} || $c->param('delete_contributor')) {
die "person does not match contributor" unless Contributor->new(id => $id)->load->person_id == $person->id;
Contributors->delete_objects({ id => $id })
or return $c->update_error("Failed to remove contributor");
Contributors->delete_objects(
where => [{ id => $id }],
audit_user => $c->audit_user, audit_note => $c->audit_note)
or return $c->update_error("Failed to remove contributor");
$c->flash(info => "Saved changes.");
}

Expand Down
6 changes: 5 additions & 1 deletion lib/Tuba/Table.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ sub update_rel {

my $report_identifier = $c->stash('report_identifier');
for my $id (grep { defined && length } $c->param('delete_array')) {
ArrayTableMaps->delete_objects({ array_identifier => $id, table_identifier => $object->identifier, report_identifier => $report_identifier });
ArrayTableMaps->delete_objects(
where => [{array_identifier => $id,
table_identifier => $object->identifier,
report_identifier => $report_identifier}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
my $array = Array->new(identifier => $id);
$array->load;
if (!@{ $array->tables }) {
Expand Down

0 comments on commit dbfd70b

Please sign in to comment.