Changeset 531 for trunk/cgi-bin


Ignore:
Timestamp:
10/29/12 18:09:30 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Clean up and move most SQL for allocation update into IPDB.pm. See #34.

Location:
trunk/cgi-bin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/IPDB.pm

    r530 r531  
    3030        &getTypeList
    3131        &getParent &getRoutedCity
    32         &allocateBlock &deleteBlock &getBlockData
     32        &allocateBlock &updateBlock &deleteBlock &getBlockData
    3333        &getNodeList &getNodeName &getNodeInfo
    3434        &mailNotify
     
    4444                &getTypeList
    4545                &getParent &getRoutedCity
    46                 &allocateBlock &deleteBlock &getBlockData
     46                &allocateBlock &updateBlock &deleteBlock &getBlockData
    4747                &getNodeList &getNodeName &getNodeInfo
    4848                &mailNotify
     
    826826
    827827
     828## IPDB::updateBlock()
     829# Update an allocation
     830# Takes all allocation fields in a hash
     831sub updateBlock {
     832  my $dbh = shift;
     833  my %args = @_;
     834
     835  return ('FAIL', 'Missing block to update') if !$args{block};
     836
     837  # do it all in a transaction
     838  local $dbh->{AutoCommit} = 0;
     839  local $dbh->{RaiseError} = 1;
     840
     841  my @fieldlist;
     842  my @vallist;
     843  foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata') {
     844    if ($args{$_}) {
     845      push @fieldlist, $_;
     846      push @vallist, $args{$_};
     847    }
     848  }
     849
     850  my $updtable = 'allocations';
     851  my $keyfield = 'cidr';
     852  if ($args{alloctype} =~ /^(.)i$/) {
     853    $updtable = 'poolips';
     854    $keyfield = 'ip';
     855  } else {
     856## fixme:  there's got to be a better way...
     857    if ($args{swip}) {
     858      if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
     859        $args{swip} = 'y';
     860      } else {
     861        $args{swip} = 'n';
     862      }
     863    }
     864    foreach ('type', 'swip') {
     865      if ($args{$_}) {
     866        push @fieldlist, $_;
     867        push @vallist, $args{$_};
     868      }
     869    }
     870  }
     871
     872  return ('FAIL', 'No fields to update') if !@fieldlist;
     873
     874  push @vallist, $args{block};
     875  my $sql = "UPDATE $updtable SET ";
     876  $sql .= join " = ?, ", @fieldlist;
     877  $sql .= " = ? WHERE $keyfield = ?";
     878
     879  eval {
     880    # do the update
     881    $dbh->do($sql, undef, @vallist);
     882
     883    if ($args{node}) {
     884      # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
     885      $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($args{block}) );
     886      $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{block}, $args{node}) );
     887    }
     888
     889    $dbh->commit;
     890  };
     891  if ($@) {
     892    my $msg = $@;
     893    $dbh->rollback;
     894    return ('FAIL', $msg);
     895  }
     896  return 0;
     897} # end updateBlock()
     898
     899
    828900## IPDB::deleteBlock()
    829901# Removes an allocation from the database, including deleting IPs
  • trunk/cgi-bin/main.cgi

    r530 r531  
    797797  }
    798798
    799   # Check to see if we can update restricted data
    800   my $privdata = '';
    801   if ($IPDBacl{$authuser} =~ /s/) {
    802     $privdata = ",privdata='$webvar{privdata}'";
    803   }
    804 
    805799  # Make sure incoming data is in correct format - custID among other things.
    806800  return if !validateInput;
    807801
    808   # SQL transaction wrapper
    809   eval {
    810     # Relatively simple SQL transaction here.
    811     my $sql;
    812     if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) {
    813       $sql = "UPDATE poolips SET custid='$webvar{custid}',".
    814         "city=?,description=?,notes=?,".
    815         "circuitid='$webvar{circid}',".
    816         "$privdata where ip='$webvar{block}'";
    817     } else {
    818       $sql = "UPDATE allocations SET custid='$webvar{custid}',".
    819         "city=?,description=?,notes=?,".
    820         "circuitid='$webvar{circid}'$privdata,".
    821         "type='$webvar{alloctype}',".
    822         "swip='".($webvar{swip} eq 'on' ? 'y' : 'n')."' ".
    823         "where cidr='$webvar{block}'";
    824     }
    825     # Log the details of the change.
    826     syslog "debug", $sql;
    827     $sth = $ip_dbh->prepare($sql);
    828     $sth->execute($webvar{city}, $webvar{desc}, $webvar{notes});
    829 ## node hack
    830     if ($webvar{node}) {
    831       # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
    832       $ip_dbh->do("DELETE FROM noderef WHERE block='$webvar{block}'");
    833       $sth = $ip_dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)");
    834       $sth->execute($webvar{block},$webvar{node});
    835     }
    836 ## end node hack
    837     $ip_dbh->commit;
    838   };
    839   if ($@) {
    840     my $msg = $@;
    841     eval { $ip_dbh->rollback; };
     802  my %updargs = (
     803        custid          => $webvar{custid},
     804        city            => $webvar{city},
     805        description     => $webvar{desc},
     806        notes           => $webvar{notes},
     807        circuitid       => $webvar{circid},
     808        block           => $webvar{block},
     809        type            => $webvar{alloctype},
     810        );
     811
     812  # Semioptional values
     813  $updargs{privdata} = $webvar{privdata} if $IPDBacl{$authuser} =~ /s/;
     814  $updargs{node} = $webvar{node} if $webvar{node};
     815
     816  my ($code,$msg) = updateBlock($ip_dbh, %updargs);
     817
     818  if ($code eq 'FAIL') {
    842819    syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'";
    843820    $page->param(err => "Could not update block/IP $webvar{block}: $msg");
     
    847824  # If we get here, the operation succeeded.
    848825  syslog "notice", "$authuser updated $webvar{block}";
     826##fixme:  log details of the change?  old way is in the .debug stream anyway.
    849827##fixme:  need to wedge something in to allow "update:field" notifications
    850828## hmm.  how to tell what changed?  O_o
Note: See TracChangeset for help on using the changeset viewer.