Changeset 90 for trunk/DNSDB.pm


Ignore:
Timestamp:
04/12/11 14:46:22 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint

  • Comment-docu-tweak on addUser()
  • Disallow blank usernames on creation
  • Add docucomment on updateUser()
  • Update getRecLine() to handle long records, switch to more compact $dbh->do() instead of explicit prepare/execute/fetch
  • Update getDomRecs() to handle long records. Clean up SQL formatting.
  • Update addRec() to handle long records
  • Update updateRec() to handle long records
  • Add fixme reminder to handle VegaDNS encrypted passwords (hex-coded MD5)
  • Use getRecData() instead of local SQL for "Edit record" page
  • Log user add and update actions
  • Remember to set $permissions{admin} to 1 on update if user type is "superuser"
  • Uncomment page=deluser segment; this was not integrated with the rest of the user add/update pages
  • HTML-comment "Customer ID" (uid) on log pages; having trouble seeing a use-case
  • Nitpick tweaks on record add/edit page
  • Use HTML::Template's HTML-escaping on the record value on record add/edit page - required for eg SPF records with quotation marks
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r87 r90  
    690690#   last name                   - defaults to blank
    691691#   phone                       - defaults to blank (could put other data within column def)
    692 # Returns (OK,OK) on success, (FAIL,<message>) on failure
     692# Returns (OK,<uid>) on success, (FAIL,<message>) on failure
    693693sub addUser {
    694694  $errstr = '';
     
    699699  my $state = shift;
    700700
    701   return ('FAIL',"Missing one or more required entries") if !defined($state);
     701  return ('FAIL', "Missing one or more required entries") if !defined($state);
     702  return ('FAIL', "Username must not be blank") if !$username;
    702703
    703704  my $type = shift || 'u';      # create limited users by default - fwiw, not sure yet how this will interact with ACLs
     
    816817
    817818## DNSDB:: updateUser()
    818 #
     819# Update general data about user
    819820sub updateUser {
    820821  my $dbh = shift;
     
    10201021  my $id = shift;
    10211022
    1022   my $sql = "SELECT record_id,host,type,val,distance,weight,port,ttl".
    1023         (($def eq 'def' or $def eq 'y') ? ',group_id FROM default_' : ',domain_id FROM ').
    1024         "records WHERE record_id=?";
    1025   my $sth = $dbh->prepare($sql);
    1026   $sth->execute($id);
    1027 
    1028   my ($recid,$host,$rtype,$val,$distance,$weight,$port,$ttl,$parid) = $sth->fetchrow_array();
    1029 
    1030   if ($sth->err) {
     1023  my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata".
     1024        (($def eq 'def' or $def eq 'y') ? ',r.group_id FROM default_' : ',r.domain_id FROM ').
     1025        "records r LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id WHERE record_id=?";
     1026  my $ret = $dbh->selectrow_hashref($sql, undef, ($id) ) or warn $dbh->errstr;
     1027
     1028  if ($dbh->err) {
    10311029    $errstr = $DBI::errstr;
    10321030    return undef;
    10331031  }
    1034   my %ret;
    1035   $ret{recid}   = $recid;
    1036   $ret{host}    = $host;
    1037   $ret{type}    = $rtype;
    1038   $ret{val}     = $val;
    1039   $ret{distance}= $distance;
    1040   $ret{weight}  = $weight;
    1041   $ret{port}    = $port;
    1042   $ret{ttl}     = $ttl;
    1043   $ret{parid}   = $parid;
    1044 
    1045   return %ret;
     1032
     1033  $ret->{val} = $ret->{recdata} if $ret->{longrec_id};  # put the long data in the real value space
     1034  delete $ret->{longrec_id};    # remove these since they shouldn't be exposed - the caller
     1035  delete $ret->{recdata};       # should not care about "long records" vs normal ones.
     1036
     1037  return $ret;
    10461038}
    10471039
     
    10651057  my $direction = shift || 'ASC';
    10661058
    1067   my $sql = "SELECT record_id,host,type,val,distance,weight,port,ttl FROM ";
    1068   if ($type eq 'def' or $type eq 'y') {
    1069     $sql .= " default_records where group_id=$id";
     1059  $type = 'y' if $type eq 'def';
     1060
     1061  my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM ";
     1062  $sql .= "default_" if $type eq 'y';
     1063  $sql .= "records r ";
     1064  $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";
     1065  if ($type eq 'y') {
     1066    $sql .= "WHERE r.group_id=?";
    10701067  } else {
    1071     $sql .= " records where domain_id=$id";
    1072   }
    1073   $sql .= " and not type=$reverse_typemap{SOA} order by $order $direction";
     1068    $sql .= "WHERE r.domain_id=?";
     1069  }
     1070  $sql .= " AND NOT r.type=$reverse_typemap{SOA} ORDER BY r.$order $direction";
    10741071##fixme:  need to set nstart properly (offset is not internally multiplied with limit)
    1075   $sql .= " limit $nrecs offset ".($nstart*$nrecs) if $nstart ne 'all';
    1076 
    1077   my $sth = $dbh->prepare($sql);
    1078   $sth->execute;
     1072  $sql .= " LIMIT $nrecs OFFSET ".($nstart*$nrecs) if $nstart ne 'all';
     1073
     1074  my $sth = $dbh->prepare($sql) or warn $dbh->errstr;
     1075  $sth->execute($id) or warn "$sql: ".$sth->errstr;
    10791076
    10801077  my @retbase;
    10811078  while (my $ref = $sth->fetchrow_hashref()) {
     1079    $ref->{val} = $ref->{recdata} if $ref->{longrec_id};        # put the long data in the real value space
     1080    delete $ref->{longrec_id};  # remove these since they shouldn't be exposed - the caller
     1081    delete $ref->{recdata};     # should not care about "long records" vs normal ones.
    10821082    push @retbase, $ref;
    10831083  }
     
    11331133  }
    11341134
    1135   my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)";
    1136 ##fixme: use array for values, replace "vallist" with series of ?,?,? etc
    1137 # something is bugging me about this...
    1138 #warn "DEBUG: $sql";
    1139   my $sth = $dbh->prepare($sql);
    1140   $sth->execute(@vallist);
    1141 
    1142   return ('FAIL',$sth->errstr) if $sth->err;
     1135  # Allow transactions, and raise an exception on errors so we can catch it later.
     1136  # Use local to make sure these get "reset" properly on exiting this block
     1137  local $dbh->{AutoCommit} = 0;
     1138  local $dbh->{RaiseError} = 1;
     1139
     1140  eval {
     1141    if (length($val) > 100 ) {
     1142      # extralong records get an entry in a separate table.
     1143      $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );
     1144      my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM longrecs WHERE recdata=?", undef, ($val) );
     1145      $fields .= ",longrec_id";
     1146      $vallen .= ",?";
     1147      push @vallist, $longid;
     1148      $vallist[3] = ''; # so we don't barf when we insert the main record
     1149    }
     1150    $dbh->do("INSERT INTO ".($defrec eq 'y' ? 'default_' : '')."records ($fields) VALUES ($vallen)",
     1151        undef, @vallist);
     1152    $dbh->commit;
     1153  };
     1154  if ($@) {
     1155    my $msg = $@;
     1156    eval { $dbh->rollback; };
     1157    return ('FAIL',$msg);
     1158  }
    11431159
    11441160  return ('OK','OK');
     1161
    11451162} # end addRec()
    11461163
     
    11791196  }
    11801197
    1181   my $sth = $dbh->prepare("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
    1182         "SET host=?,type=?,val=?,ttl=?,distance=?,weight=?,port=? ".
    1183         "WHERE record_id=?");
    1184   $sth->execute($host,$type,$val,$ttl,$dist,$weight,$port,$id);
    1185 
    1186   return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;
     1198#  my $sql = "SELECT r.record_id,r.host,r.type,r.val,r.distance,r.weight,r.port,r.ttl,r.longrec_id,l.recdata FROM ";
     1199#  $sql .= "default_" if $type eq 'y';
     1200#  $sql .= "records r ";
     1201#  $sql .= "LEFT OUTER JOIN longrecs l ON r.longrec_id=l.longrec_id ";
     1202
     1203  # get the long record ID, if any
     1204  my ($longid) = $dbh->selectrow_array("SELECT longrec_id FROM ".($defrec eq 'y' ? 'default_' : '')."records ".
     1205        "WHERE record_id=?", undef, ($id) );
     1206
     1207  local $dbh->{AutoCommit} = 0;
     1208  local $dbh->{RaiseError} = 1;
     1209
     1210  eval {
     1211    # there's really no tidy way to squash this down.  :/
     1212    if (length($val) > 100) {
     1213      if ($longid) {
     1214        $dbh->do("UPDATE longrecs SET recdata=? WHERE longrec_id=?", undef, ($val, $longid) );
     1215      } else {
     1216##fixme:  has to be a better way to be sure we get the right recid back once inserted...
     1217        $dbh->do("INSERT INTO longrecs (recdata) VALUES (?)", undef, ($val) );
     1218        my ($newlongid) = $dbh->selectrow_array("SELECT currval('longrecs_longrec_id_seq')");
     1219        $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=? ".
     1220                "WHERE record_id=?", undef, ('', $newlongid, $id) );
     1221      }
     1222    } else {
     1223      if ($longid) {
     1224        $dbh->do("DELETE FROM longrecs WHERE longrec_id=?", undef, ($longid) );
     1225        $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=?,longrec_id=NULL ".
     1226                "WHERE record_id=?", undef, ($val, $id) );
     1227      } else {
     1228        $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records SET val=? ".
     1229                "WHERE record_id=?", undef, ($val, $id) );
     1230      }
     1231    }
     1232
     1233    $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
     1234        "SET host=?,type=?,ttl=?,distance=?,weight=?,port=? ".
     1235        "WHERE record_id=?", undef, ($host, $type, $ttl, $dist, $weight, $port, $id) );
     1236
     1237  };
     1238  if ($@) {
     1239    my $msg = $@;
     1240    $dbh->rollback;
     1241    return ('FAIL', $msg);
     1242  }
     1243#  return ('FAIL',$sth->errstr."<br>\n$errstr<br>\n") if $sth->err;
    11871244
    11881245  return ('OK','OK');
Note: See TracChangeset for help on using the changeset viewer.