Changeset 295


Ignore:
Timestamp:
03/29/12 17:06:10 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Move entity-relationship reference hashes up into the same area
as other (semi)globals in DNSDB.pm since I was starting to add
an accidental duplicate of %par_tbl.
Move action logging for bulk domain change to DNSDB.pm: (See #35)

  • Clean up dns.cgi to match previous changes to zoneStatus() and delZone()
  • Less not-quite-identical code in dns.cgi can now be factored down
  • Updated changeGroup() with action logging and more error handling
  • Tweak logging and messages in delZone()
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r294 r295  
    154154my %userdata;
    155155
     156# Entity-relationship reference hashes.
     157my %par_tbl = (
     158                group   => 'groups',
     159                user    => 'users',
     160                defrec  => 'default_records',
     161                defrevrec       => 'default_rev_records',
     162                domain  => 'domains',
     163                revzone => 'revzones',
     164                record  => 'records'
     165        );
     166my %id_col = (
     167                group   => 'group_id',
     168                user    => 'user_id',
     169                defrec  => 'record_id',
     170                defrevrec       => 'record_id',
     171                domain  => 'domain_id',
     172                revzone => 'rdns_id',
     173                record  => 'record_id'
     174        );
     175my %par_col = (
     176                group   => 'parent_group_id',
     177                user    => 'group_id',
     178                defrec  => 'group_id',
     179                defrevrec       => 'group_id',
     180                domain  => 'group_id',
     181                revzone => 'group_id',
     182                record  => 'domain_id'
     183        );
     184my %par_type = (
     185                group   => 'group',
     186                user    => 'group',
     187                defrec  => 'group',
     188                defrevrec       => 'group',
     189                domain  => 'group',
     190                revzone => 'group',
     191                record  => 'domain'
     192        );
    156193
    157194##
     
    12311268  #return ('FAIL', "Missing
    12321269
     1270  return ('FAIL', "Can't change the group of a $type")
     1271        unless grep /^$type$/, ('domain','revzone','user','group');     # could be extended for defrecs?
     1272
     1273  # Collect some names for logging and messages
     1274  my $entname;
    12331275  if ($type eq 'domain') {
    1234     $dbh->do("UPDATE domains SET group_id=? WHERE domain_id=?", undef, ($newgrp, $id))
    1235         or return ('FAIL','Group change failed: '.$dbh->errstr);
     1276    $entname = domainName($dbh, $id);
     1277  } elsif ($type eq 'revzone') {
     1278    $entname = revName($dbh, $id);
    12361279  } elsif ($type eq 'user') {
    1237     $dbh->do("UPDATE users SET group_id=? WHERE user_id=?", undef, ($newgrp, $id))
    1238         or return ('FAIL','Group change failed: '.$dbh->errstr);
     1280    $entname = userFullName($dbh, $id, '%u');
    12391281  } elsif ($type eq 'group') {
    1240     $dbh->do("UPDATE groups SET parent_group_id=? WHERE group_id=?", undef, ($newgrp, $id))
    1241         or return ('FAIL','Group change failed: '.$dbh->errstr);
    1242   }
    1243   return ('OK','OK');
     1282    $entname = groupName($dbh, $id);
     1283  }
     1284
     1285  my ($oldgid) = $dbh->selectrow_array("SELECT group_id FROM $par_tbl{$type} WHERE $id_col{$type}=?",
     1286        undef, ($id));
     1287  my $oldgname = groupName($dbh, $oldgid);
     1288  my $newgname = groupName($dbh, $newgrp);
     1289
     1290  return ('FAIL', "Can't move things into a group that doesn't exist") if !$newgname;
     1291
     1292  return ('WARN', "Nothing to do, new group is the same as the old group") if $oldgid == $newgrp;
     1293
     1294  # Allow transactions, and raise an exception on errors so we can catch it later.
     1295  # Use local to make sure these get "reset" properly on exiting this block
     1296  local $dbh->{AutoCommit} = 0;
     1297  local $dbh->{RaiseError} = 1;
     1298
     1299  eval {
     1300    $dbh->do("UPDATE $par_tbl{$type} SET group_id=? WHERE $id_col{$type}=?", undef, ($newgrp, $id));
     1301    # Log the change in both the old and new groups
     1302    _log($dbh, (group_id => $oldgid, entry => "Moved $type $entname from $oldgname to $newgname"));
     1303    _log($dbh, (group_id => $newgrp, entry => "Moved $type $entname from $oldgname to $newgname"));
     1304    $dbh->commit;
     1305  };
     1306  if ($@) {
     1307    my $msg = $@;
     1308    eval { $dbh->rollback; };
     1309    if ($config{log_failures}) {
     1310      _log($dbh, (group_id => $oldgid, entry => "Error moving $type $entname to $newgname: $msg"));
     1311      $dbh->commit;     # since we enabled transactions earlier
     1312    }
     1313    return ('FAIL',"Error moving $type $entname to $newgname: $msg");
     1314  }
     1315
     1316  return ('OK',"Moved $type $entname from $oldgname to $newgname");
    12441317} # end changeGroup()
    12451318
     
    14091482    $msg = $@;
    14101483    eval { $dbh->rollback; };
    1411     $loghash{entry} = "Delete $zone: $failmsg: $msg";
    1412     _log($dbh, %loghash) if $config{log_failures};
    1413     $dbh->commit;       # since we enabled transactions earlier
    1414     return ('FAIL',"Delete $zone: $failmsg: $msg");
     1484    $loghash{entry} = "Error deleting $zone: $msg ($failmsg)";
     1485    if ($config{log_failures}) {
     1486      _log($dbh, %loghash);
     1487      $dbh->commit;     # since we enabled transactions earlier
     1488    }
     1489    return ('FAIL', $loghash{entry});
    14151490  } else {
    1416     return ('OK',$msg);
     1491    return ('OK', $msg);
    14171492  }
    14181493
     
    27512826  return ('OK',$logdata{entry});
    27522827} # end delRec()
    2753 
    2754 
    2755   # Reference hashes.
    2756 my %par_tbl = (
    2757                 group   => 'groups',
    2758                 user    => 'users',
    2759                 defrec  => 'default_records',
    2760                 defrevrec       => 'default_rev_records',
    2761                 domain  => 'domains',
    2762                 revzone => 'revzones',
    2763                 record  => 'records'
    2764         );
    2765 my %id_col = (
    2766                 group   => 'group_id',
    2767                 user    => 'user_id',
    2768                 defrec  => 'record_id',
    2769                 defrevrec       => 'record_id',
    2770                 domain  => 'domain_id',
    2771                 revzone => 'rdns_id',
    2772                 record  => 'record_id'
    2773         );
    2774 my %par_col = (
    2775                 group   => 'parent_group_id',
    2776                 user    => 'group_id',
    2777                 defrec  => 'group_id',
    2778                 defrevrec       => 'group_id',
    2779                 domain  => 'group_id',
    2780                 revzone => 'group_id',
    2781                 record  => 'domain_id'
    2782         );
    2783 my %par_type = (
    2784                 group   => 'group',
    2785                 user    => 'group',
    2786                 defrec  => 'group',
    2787                 defrevrec       => 'group',
    2788                 domain  => 'group',
    2789                 revzone => 'group',
    2790                 record  => 'domain'
    2791         );
    27922828
    27932829
  • trunk/dns.cgi

    r294 r295  
    10851085  }
    10861086
     1087  # per-action scope checks
    10871088  if ($webvar{bulkaction} eq 'move') {
    10881089    changepage(page => "domlist", errmsg => "You are not permitted to bulk-move domains")
     
    10901091    my $newgname = groupName($dbh,$webvar{destgroup});
    10911092    $page->param(action => "Move to group $newgname");
    1092     my @bulkresults;
    1093     # nngh.  due to alpha-sorting on the previous page, we can't use domid-numeric
    1094     # order here, and since we don't have the domain names until we go around this
    1095     # loop, we can't alpha-sort them here.  :(
    1096     foreach (keys %webvar) {
    1097       my %row;
    1098       next unless $_ =~ /^dom_\d+$/;
    1099       # second security check - does the user have permission to meddle with this domain?
    1100       if (!check_scope(id => $webvar{$_}, type => 'domain')) {
    1101         $row{domerr} = "You are not permitted to make changes to the requested domain";
    1102         $row{domain} = $webvar{$_};
    1103         push @bulkresults, \%row;
    1104         next;
    1105       }
    1106       $row{domain} = domainName($dbh,$webvar{$_});
    1107       my ($code, $msg) = changeGroup($dbh, 'domain', $webvar{$_}, $webvar{destgroup});
    1108       if ($code eq 'OK') {
    1109         logaction($webvar{$_}, $session->param("username"),
    1110                 parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
    1111                 "Moved domain ".domainName($dbh, $webvar{$_})." to group $newgname");
    1112         $row{domok} = ($code eq 'OK');
    1113       } else {
    1114         logaction($webvar{$_}, $session->param("username"),
    1115                 parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
    1116                 "Failed to move domain ".domainName($dbh, $webvar{$_})." to group $newgname: $msg")
    1117                 if $config{log_failures};
    1118       }
    1119       $row{domerr} = $msg;
    1120       push @bulkresults, \%row;
    1121     }
    1122     $page->param(bulkresults => \@bulkresults);
    1123 
    11241093  } elsif ($webvar{bulkaction} eq 'deactivate' || $webvar{bulkaction} eq 'activate') {
    11251094    changepage(page => "domlist", errmsg => "You are not permitted to bulk-$webvar{bulkaction} domains")
    11261095        unless ($permissions{admin} || $permissions{domain_edit});
    11271096    $page->param(action => "$webvar{bulkaction} domains");
    1128     my @bulkresults;
    1129     foreach (keys %webvar) {
    1130       my %row;
    1131       next unless $_ =~ /^dom_\d+$/;
    1132       # second security check - does the user have permission to meddle with this domain?
    1133       if (!check_scope(id => $webvar{$_}, type => 'domain')) {
    1134         $row{domerr} = "You are not permitted to make changes to the requested domain";
    1135         $row{domain} = $webvar{$_};
    1136         push @bulkresults, \%row;
    1137         next;
    1138       }
    1139       $row{domain} = domainName($dbh,$webvar{$_});
    1140 ##fixme:  error handling on status change
    1141       my $stat = zoneStatus($dbh,$webvar{$_},($webvar{bulkaction} eq 'activate' ? 'domon' : 'domoff'));
    1142       logaction($webvar{$_}, $session->param("username"),
    1143         parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
    1144         "Changed domain ".domainName($dbh, $webvar{$_})." state to ".($stat ? 'active' : 'inactive'));
    1145       $row{domok} = 1;
    1146 #      $row{domok} = ($code eq 'OK');
    1147 #      $row{domerr} = $msg;
    1148       push @bulkresults, \%row;
    1149     }
    1150     $page->param(bulkresults => \@bulkresults);
    1151 
    11521097  } elsif ($webvar{bulkaction} eq 'delete') {
    11531098    changepage(page => "domlist", errmsg => "You are not permitted to bulk-delete domains")
    11541099        unless ($permissions{admin} || $permissions{domain_delete});
    11551100    $page->param(action => "$webvar{bulkaction} domains");
    1156     my @bulkresults;
    1157     foreach (keys %webvar) {
    1158       my %row;
    1159       next unless $_ =~ /^dom_\d+$/;
    1160       # second security check - does the user have permission to meddle with this domain?
    1161       if (!check_scope(id => $webvar{$_}, type => 'domain')) {
    1162         $row{domerr} = "You are not permitted to make changes to the requested domain";
    1163         $row{domain} = $webvar{$_};
    1164         push @bulkresults, \%row;
    1165         next;
    1166       }
    1167       $row{domain} = domainName($dbh,$webvar{$_});
    1168       my $pargroup = parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec}));
    1169       my $dom = domainName($dbh, $webvar{$_});
    1170       my ($code, $msg) = delZone($dbh, $webvar{$_}, 'n');
    1171       if ($code eq 'OK') {
    1172         logaction($webvar{$_}, $session->param("username"), $pargroup, "Deleted domain $dom");
    1173         $row{domok} = ($code eq 'OK');
    1174       } else {
    1175         logaction($webvar{$_}, $session->param("username"), $pargroup, "Failed to delete domain $dom: $msg")
    1176                 if $config{log_failures};
    1177       }
     1101  } else {
     1102    # unknown action, bypass actually doing anything.  it should not be possible in
     1103    # normal operations, and anyone who meddles with the URL gets what they deserve.
     1104    goto DONEBULK;
     1105  } # move/(de)activate/delete if()
     1106
     1107  my @bulkresults;
     1108  # nngh.  due to alpha-sorting on the previous page, we can't use domid-numeric
     1109  # order here, and since we don't have the domain names until we go around this
     1110  # loop, we can't alpha-sort them here.  :(
     1111  foreach (keys %webvar) {
     1112    my %row;
     1113    next unless $_ =~ /^dom_\d+$/;
     1114    # second security check - does the user have permission to meddle with this domain?
     1115    if (!check_scope(id => $webvar{$_}, type => 'domain')) {
     1116      $row{domerr} = "You are not permitted to make changes to the requested domain";
     1117      $row{domain} = $webvar{$_};
     1118      push @bulkresults, \%row;
     1119      next;
     1120    }
     1121    $row{domain} = domainName($dbh,$webvar{$_});
     1122
     1123    # Do the $webvar{bulkaction}
     1124    my ($code, $msg);
     1125    ($code, $msg) = changeGroup($dbh, 'domain', $webvar{$_}, $webvar{destgroup})
     1126        if $webvar{bulkaction} eq 'move';
     1127    if ($webvar{bulkaction} eq 'deactivate' || $webvar{bulkaction} eq 'activate') {
     1128      my $stat = zoneStatus($dbh,$webvar{$_},'n',($webvar{bulkaction} eq 'activate' ? 'domon' : 'domoff'));
     1129      $code = (defined($stat) ? 'OK' : 'FAIL');
     1130      $msg = (defined($stat) ? $DNSDB::resultstr : $DNSDB::errstr);
     1131    }
     1132    ($code, $msg) = delZone($dbh, $webvar{$_}, 'n')
     1133        if $webvar{bulkaction} eq 'delete';
     1134
     1135    # Set the result output from the action
     1136    if ($code eq 'OK') {
     1137      $row{domok} = $msg;
     1138    } elsif ($code eq 'WARN') {
     1139      $row{domwarn} = $msg;
     1140    } else {
    11781141      $row{domerr} = $msg;
    1179       push @bulkresults, \%row;
    1180     }
    1181     $page->param(bulkresults => \@bulkresults);
    1182 
    1183   } # move/(de)activate/delete if()
    1184 
    1185   # not going to handle the unknown $webvar{action} else;  it should not be possible in normal
    1186   # operations, and anyone who meddles with the URL gets what they deserve.
     1142    }
     1143    push @bulkresults, \%row;
     1144
     1145  } # foreach (keys %webvar)
     1146  $page->param(bulkresults => \@bulkresults);
    11871147
    11881148  # Yes, this is a GOTO target.  PTHBTTT.
  • trunk/templates/bulkchange.tmpl

    r113 r295  
    1010<tr class="datalinelight">
    1111        <td><TMPL_VAR NAME=domain></td>
    12 <TMPL_IF domok> <td>OK</td>
    13 <TMPL_ELSE><TMPL_IF domwarn>    <td class="warn">Import OK but:<br />
    14 <TMPL_VAR NAME=domwarn></td>
    15 <TMPL_ELSE>     <td class="err">Failed: <TMPL_VAR NAME=domerr></td>
     12<TMPL_IF domok> <td><TMPL_VAR NAME=domok></td>
     13<TMPL_ELSE><TMPL_IF domwarn>    <td class="warn"><TMPL_VAR NAME=domwarn></td>
     14<TMPL_ELSE>     <td class="err"><TMPL_VAR NAME=domerr></td>
    1615</TMPL_IF></TMPL_IF>
    1716</tr>
    1817</TMPL_LOOP>
    1918</table>
    20 <TMPL_VAR NAME=foobar>
    2119</td>
    2220</tr>
Note: See TracChangeset for help on using the changeset viewer.