Changeset 57 for trunk/dns.cgi


Ignore:
Timestamp:
01/13/10 17:46:21 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint, cleanup and normalization (still, again)

  • fiddle searchsubs handling so it really goes away if you uncheck it. maybe. With a perfect browser. In the light of a full harvest moon while 13 virgins do the chicken dance on top of five maypoles with ....
  • Collect global $webvar{action} actions into one logical segment
  • Shuffle $webvar{page} segments to be close to logically similar blocks
  • Get logging stubbed and mostly fleshed out
  • Fix fumbled group selection in various lists
  • Remove a stale template
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dns.cgi

    r55 r57  
    7171$session->param($webvar{page}.'filter', $webvar{filter}) if $webvar{filter};
    7272$webvar{searchsubs} =~ s/^n ?// if $webvar{searchsubs};
    73 $session->param($webvar{page}.'searchsubs', $webvar{searchsubs}) if $webvar{searchsubs};
     73$session->param($webvar{page}.'searchsubs', $webvar{searchsubs}) if defined($webvar{searchsubs});
    7474
    7575my $startwith = $session->param($webvar{page}.'startwith');
     
    7777my $searchsubs = $session->param($webvar{page}.'searchsubs');
    7878
    79 
    80 
    81 if (0) {
    82 # filter, starts with, search subgroups
    83 $session->param('filter', "$webvar{page}:filter$webvar{filter}") if defined($webvar{filter});
    84 my $filter = $session->param('filter');
    85 $filter = ($filter =~ /^$webvar{page}:filter(.+)$/ ? $1 : '');
    86 
    87 $session->param('startwith', "$webvar{page}:startwith$webvar{startwith}") if defined($webvar{startwith});
    88 my $startwith = $session->param('startwith');
    89 $startwith = ($startwith =~ /^$webvar{page}:startwith(.+)$/ ? $1 : '');
    90 
    91 $webvar{searchsubs} =~ s/^n ?// if $webvar{searchsubs};
    92 $session->param('searchsubs', "$webvar{page}:searchsubs$webvar{searchsubs}") if defined($webvar{searchsubs});
    93 my $searchsubs = $session->param('searchsubs');
    94 $searchsubs = ($searchsubs =~ /^$webvar{page}:searchsubs(.+)$/ ? $1 : '');
    95 }
    96 
    97 
     79push @debugbits, "foo<br>\n" if defined($webvar{searchsubs});
    9880
    9981# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
    100 
    101 if ($webvar{action} && $webvar{action} eq 'chgroup') {
    102   # fiddle session-stored group data
    103   # magic incantation to... uhhh...
    104   $session->param('curgroup', $webvar{group});
    105   $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
    106 }
    10782
    10883my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
     
    158133    exit;
    159134
    160   }
    161 }
     135  } elsif ($webvar{action} eq 'chgroup') {
     136    # fiddle session-stored group data
     137    # magic incantation to... uhhh...
     138    $session->param('curgroup', $webvar{group});
     139    $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
     140  }
     141} # handle global webvar{action}s
     142
    162143
    163144## Default page is a login page
    164145#my $page;      # to be initialized as an HTML::Template entity sooner or later
    165 
    166146
    167147
     
    197177} elsif ($webvar{page} eq 'newdomain') {
    198178
     179  # hmm.  nothing to do here?
     180  # - group list is filled by the same bit that fills the group list in the menu
     181
     182} elsif ($webvar{page} eq 'adddomain') {
     183
     184  my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
     185
     186  if ($code eq 'OK') {
     187    logaction($msg, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
     188    changepage(page => "reclist", id => $msg);
     189  } else {
     190    changepage(page => "newdomain", add_failed => 1, domain => $webvar{domain}, errmsg => $msg);
     191  }
    199192
    200193} elsif ($webvar{page} eq 'deldom') {
     
    211204
    212205  } elsif ($webvar{del} eq 'ok') {
     206    my $pargroup = parentID($webvar{id}, 'dom', 'group');
    213207    my ($code,$msg) = delDomain($dbh, $webvar{id});
    214208    if ($code ne 'OK') {
     
    218212      listdomains($curgroup);
    219213    } else {
    220       # success.  go back to the domain list, do not pass "GO"
    221       changepage(page => "domlist");
     214##fixme: need domain to put in log message
     215     logaction($webvar{id}, $session->param("username"), $pargroup, "Deleted domain $webvar{domain}");
     216     changepage(page => "domlist");
    222217    }
    223218  } else {
     
    277272##fixme:  need more magic to get proper group - if domain was fiddled
    278273# from search-subgroups listing, may not be "current" group
    279       logaction($webvar{parentid}, $session->param("username"), $webvar{group},
    280         "Added '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl}");
     274
     275# SELECT group_id FROM domains WHERE domain_id=?;
     276# sth->execute($webvar{parentid});
     277##log
     278      if ($webvar{defrec} eq 'y') {
     279        logaction(0, $session->param("username"), $webvar{parentid},
     280                "Added default record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl}");
     281      } else {
     282        logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
     283                "Added record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl}");
     284      }
    281285      changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
    282286    } else {
     
    300304    $page->param(id             => $webvar{id});
    301305    $page->param(defrec         => $webvar{defrec});
     306##fixme: SQL does not belong!
    302307    my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
    303308        ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
     
    321326##fixme:  need more magic to get proper group - if domain was fiddled
    322327# from search-subgroups listing, may not be "current" group
    323       logaction($webvar{parentid}, $session->param("username"), $webvar{group},
    324         "Updated '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl}");
     328
     329# SELECT d.group_id FROM domains d INNER JOIN records r ON d.domain_id=r.domain_id WHERE r.record_id=?
     330# $sth->execute($webvar{id});
     331##log
     332      if ($webvar{defrec} eq 'y') {
     333        logaction(0, $session->param("username"), $webvar{parentid},
     334                "Updated default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
     335      } else {
     336        logaction($webvar{parentid}, $session->param("username"), parentID($webvar{id}, 'rec', 'group'),
     337                "Updated record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
     338      }
    325339      changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
    326340    } else {
     
    345359  }
    346360
    347 } elsif ($webvar{page} eq 'newrec') {
    348   push @debugbits, "whee!\n";
    349 
    350   # populate most fields as needed.  (eg, type list.)
    351   stdrecs();
    352 
    353 } elsif ($webvar{page} eq 'addrec') {
    354 
    355   my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
    356   if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
    357     push @recargs, $webvar{distance};
    358     if ($webvar{type} == $reverse_typemap{SRV}) {
    359       push @recargs, $webvar{weight};
    360       push @recargs, $webvar{port};
    361     }
    362   }
    363 # wtf?
    364 #  push @recargs,
    365   my ($code,$msg) = addRec(@recargs);
    366 
    367   if ($code eq 'OK') {
    368 ##fixme:  need more magic to get proper group - if domain was fiddled
    369 # from search-subgroups listing, may not be "current" group
    370     logaction($webvar{parentid}, $session->param("username"), $webvar{group},
    371         "Added '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl}");
    372     showdomain($webvar{defrec},$webvar{parentid});
    373 # NB: should **really** redirect here, in case of reload.  >_<  eyowch.
    374   } else {
    375     $page->param(add_failed => 1);
    376     $page->param(errmsg => $msg);
    377     stdrecs($webvar{type});     # populate the form... er, mostly.
    378     $page->param(name => $webvar{name});
    379     $page->param(address => $webvar{address});
    380     $page->param(distance => $webvar{distance})
    381         if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
    382     $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
    383     $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
    384   }
    385 
    386   $page->param(defrec => $webvar{defrec});
    387 
    388 } elsif ($webvar{page} eq 'conf_del') {
    389 
    390   $page->param(id => $webvar{id});
    391   $page->param(defrec => $webvar{defrec});
    392 
    393   my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
    394 
    395361} elsif ($webvar{page} eq 'delrec') {
    396362
     
    406372    $page->param(recval => $rec{val});
    407373  } elsif ($webvar{del} eq 'ok') {
     374# get groupid of deleted record before we try to delete it
     375    my $pargrp = parentID($webvar{id}, 'rec', 'group', $webvar{defrec});
    408376    my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
    409377    if ($code ne 'OK') {
    410378## need to find failure mode
     379      #logaction(
    411380      $page->param(del_failed => 1);
    412381      $page->param(errmsg => $msg);
    413382      showdomain($webvar{defrec}, $webvar{parentid});
    414383    } else {
     384##log
    415385      changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
    416386    }
     
    441411    fillsoa($webvar{defrec},$webvar{id});
    442412  } else {
     413
     414##fixme!  need to set group ID properly here
     415# SELECT group_id FROM domains WHERE domain_id=?
     416# $sth->execute($webvar{id});
     417##log
    443418    logaction(0, $session->param("username"), $webvar{group},
    444419        "Updated SOA (ns $webvar{prins}, contact $webvar{contact}, refresh $webvar{refresh},".
    445420        " retry $webvar{retry}, expire $webvar{expire}, minTTL $webvar{minttl}, TTL $webvar{ttl}");
    446421    changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
    447     $page->param(update_failed => 0);
    448 ##fixme!  need to set group ID properly here
     422#    $page->param(update_failed => 0);
    449423#    showdomain('y',1);
    450   }
    451 
    452 } elsif ($webvar{page} eq 'adddomain') {
    453 
    454   my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
    455 
    456   if ($code eq 'OK') {
    457     logaction($msg, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
    458     changepage(page => "reclist", id => $msg);
    459   } else {
    460 # oooh, yeah, this is supposed to be a redirect.  er, maybe.  whee.
    461 ##fixme: session ID
    462     $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
    463     $page->param(add_failed => 1);
    464     $page->param(domain => $webvar{domain});
    465     $page->param(errmsg => $msg);
    466424  }
    467425
     
    477435        # not gonna provide the 4th param: template-or-clone flag, just yet
    478436    my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
    479     if $code eq 'OK' {
     437    if ($code eq 'OK') {
    480438      logaction(0, $session->param("username"), $webvar{pargroup}, "Added group $webvar{newgroup}");
    481439      changepage(page => "grpman");
     
    504462
    505463  } elsif ($webvar{del} eq 'ok') {
     464    my $deleteme = groupName($dbh,$webvar{id}); # get this before we delete it...
    506465    my ($code,$msg) = delGroup($dbh, $webvar{id});
    507 push @debugbits, groupName($dbh, $webvar{id});
    508466    if ($code ne 'OK') {
    509467# need to find failure mode
     
    513471      listgroups();
    514472    } else {
    515 ##fixem: need more magic to correctly determine the group this action happens in
    516       logaction(0, $session->param("username"), $webvar{curgroup}, "Deleted group $webvar{id}");
     473##fixme: need to clean up log when deleting a major container
     474      logaction(0, $session->param("username"), $webvar{curgroup}, "Deleted group $deleteme");
    517475      # success.  go back to the domain list, do not pass "GO"
    518476      changepage(page => "grpman");
     
    554512# hokay, a bit of magic to decide which page we hit.
    555513  if ($code eq 'OK') {
     514##log
    556515    logaction(0, $session->param("username"), $webvar{group},
    557516        "Added user $webvar{uname} ($webvar{fname} $webvar{lname})");
     
    579538    $page->param(user => userFullName($dbh,$webvar{id}));
    580539  } elsif ($webvar{del} eq 'ok') {
     540##fixme: find group id user is in (for logging) *before* we delete the user
    581541    my ($code,$msg) = delUser($dbh, $webvar{id});
    582542    if ($code ne 'OK') {
     
    587547    } else {
    588548      # success.  go back to the domain list, do not pass "GO"
     549##log
     550      logaction(0, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
    589551      changepage(page => "useradmin");
    590552    }
     
    670632  $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit});     # eww.
    671633  $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
    672 ##work
    673634
    674635##fixme: check group too?
     
    844805}
    845806
    846 
    847807sub changepage {
    848808  my %params = @_;      # think this works the way I want...
     
    860820  exit;
    861821} # end changepage
    862 
    863822
    864823sub fillsoa {
     
    932891}
    933892
    934 
    935893# fill in record type list on add/update/edit record template
    936894sub fill_rectypes {
     
    966924}
    967925
    968 
    969926sub fill_actypelist {
    970927  my @actypes;
     
    980937  $page->param(actypelist       => \@actypes);
    981938}
    982 
    983939
    984940sub fill_fpnla {
     
    1010966} # end fill_fpnla()
    1011967
    1012 
    1013968sub fill_pgcount {
    1014969  my $pgcount = shift;
     
    1025980} # end fill_pgcount()
    1026981
    1027 
    1028982sub listdomains {
    1029983
    1030984##fixme:  $logingroup or $curgroup?
    1031985  my @childgroups;
    1032   getChildren($dbh, $logingroup, \@childgroups, 'all') if $searchsubs;
     986  #getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
    1033987  my $childlist = join(',',@childgroups);
    1034988
    1035   my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
     989  my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
    1036990        ($startwith ? " AND domain ~* '^[$startwith]'" : '').
    1037991        ($filter ? " AND domain ~* '$filter'" : '');
     
    10761030  $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
    10771031        " INNER JOIN groups ON domains.group_id=groups.group_id".
    1078         " WHERE domains.group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
     1032        " WHERE domains.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
    10791033##fixme:  don't do variable subs in SQL, use placeholders and params in ->execute()
    10801034        ($startwith ? " AND domain ~* '^[$startwith]'" : '').
     
    11021056} # end listdomains()
    11031057
    1104 
    11051058sub listgroups {
    11061059
     
    11751128} # end listgroups()
    11761129
    1177 
    11781130sub fill_grouplist {
    11791131  my $template_var = shift;
     
    12041156} # end fill_grouplist()
    12051157
    1206 
    12071158sub list_users {
    12081159
     
    12781229  $page->param(usertable => \@userlist);
    12791230} # end list_users()
    1280 
    12811231
    12821232# Generate all of the glop necessary to add or not the appropriate marker/flag for
     
    13121262} # end fill_colheads()
    13131263
    1314 
    13151264sub logaction {
    1316   my ($domid,$username,$groupid,$entry);
     1265  my ($domid,$username,$groupid,$entry) = shift;
    13171266
    13181267  my $sth = dbh->prepare("SELECT user_id, firstname || ' ' || lastname FROM users WHERE username=?");
     
    13221271  $sth = $dbh->prepare("INSERT INTO log (domain_id,user_id,group_id,email,name,entry) ".
    13231272        "VALUES (?,?,?,?,?,?)");
    1324   $sth->execute($domid,$userid,$groupid,$username,$fullname,$entry);
     1273  $sth->execute($domid,$user_id,$groupid,$username,$fullname,$entry);
    13251274
    13261275} # end logaction()
     1276
     1277#sub parentID($dbh, $id, $idtype, $partype[, $defrec])
     1278sub parentID {
     1279  my $id = shift;
     1280  my $idtype = shift;
     1281  my $partype = shift;
     1282  my $defrec = shift || '';
     1283
     1284  my $sql = '';
     1285
     1286  if ($idtype eq 'dom') {
     1287    return $id if $defrec eq 'y';
     1288    $sql = "SELECT group_id FROM domains WHERE domain_id=?";
     1289  } elsif ($idtype eq 'rec') {
     1290    if ($defrec && $defrec eq 'y') {
     1291    } else {
     1292      return
     1293      $sql = "SELECT d.group_id FROM domains d".
     1294        " INNER JOIN records r ON d.domain_id=r.domain_id".
     1295        " WHERE r.record_id=?";
     1296    }
     1297  } elsif ($idtype eq 'group') {
     1298    $sql = "SELECT parent_group_id FROM groups WHERE group_id=?";
     1299  } elsif ($idtype eq 'user') {
     1300    $sql = "SELECT group_id FROM users WHERE user_id=?";
     1301  } else {
     1302    return "FOO", "BAR";  # can't get here.... we think.
     1303  }
     1304}
Note: See TracChangeset for help on using the changeset viewer.