Changeset 24


Ignore:
Timestamp:
10/30/09 17:52:55 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

checkpoint

  • group management more or less functional
  • user management partially functional
  • misc minor tweaks and normalizations
Location:
trunk
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r23 r24  
    2626        &addDomain &delDomain &domainName
    2727        &addGroup &delGroup &getChildren &groupName
     28        &addUser &delUser
    2829        &getSOA &getRecLine &getDomRecs
    2930        &addRec &updateRec &delRec
     
    3738                &addDomain &delDomain &domainName
    3839                &addGroup &delGroup &getChildren &groupName
     40                &addUser &delUser
    3941                &getSOA &getRecLine &getDomRecs
    4042                &addRec &updateRec &delRec
     
    379381## DNSDB::getChildren()
    380382# Get a list of all groups whose parent^n is group <n>
    381 # Takes a database handle, group ID, and reference to an array to put the group IDs in
     383# Takes a database handle, group ID, reference to an array to put the group IDs in,
     384# and an optional flag to return only immediate children or all children-of-children
     385# default to returning all children
    382386# Calls itself
    383387sub getChildren {
     
    386390  my $rootgroup = shift;
    387391  my $groupdest = shift;
     392  my $immed = shift || 'all';
    388393
    389394  # special break for default group;  otherwise we get stuck.
    390395  if ($rootgroup == 1) {
    391396    # by definition, group 1 is the Root Of All Groups
    392     my $sth = $dbh->prepare("SELECT group_id FROM groups");
     397    my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)".
     398        ($immed ne 'all' ? " AND parent_group_id=1" : ''));
    393399    $sth->execute;
    394     my @grouplist;
    395400    while (my @this = $sth->fetchrow_array) {
    396401      push @$groupdest, @this;
     
    403408    while (my ($group) = $sth->fetchrow_array) {
    404409      push @$groupdest, $group;
    405       getChildren($dbh,$group,$groupdest);
     410      getChildren($dbh,$group,$groupdest) if $immed eq 'all';
    406411    }
    407412  }
     
    423428  return $groupname if $groupname;
    424429} # end groupName
     430
     431
     432## DNSDB::addUser()
     433#
     434sub addUser {
     435  $errstr = '';
     436  my $dbh = shift;
     437  return ('FAIL',"Need database handle") if !$dbh;
     438  my $username = shift;
     439  return ('FAIL',"Missing username") if !defined($username);
     440  my $group = shift;
     441  return ('FAIL',"Missing group") if !defined($group);
     442  my $pass = shift;
     443  return ('FAIL',"Missing password") if !defined($pass);
     444
     445  my $state = shift;
     446  return ('FAIL',"Need account status") if !defined($state);
     447  my $fname = shift || '';
     448  my $lname = shift || '';
     449
     450  my $user_id;
     451
     452  # Allow transactions, and raise an exception on errors so we can catch it later.
     453  # Use local to make sure these get "reset" properly on exiting this block
     454  local $dbh->{AutoCommit} = 0;
     455  local $dbh->{RaiseError} = 1;
     456
     457  # Wrap all the SQL in a transaction
     458  eval {
     459    # insert the user...
     460    my $sth = $dbh->prepare("INSERT INTO users (email,group_id,password,status) VALUES (?,?,?,?)");
     461    $sth->execute($username,$group,$pass,$state);
     462die "user fail\n";
     463
     464    # get the ID...
     465    $sth = $dbh->prepare("select user_id from users where username=?");
     466    $sth->execute($username);
     467    ($user_id) = $sth->fetchrow_array();
     468
     469    # once we get here, we should have suceeded.
     470    $dbh->commit;
     471  }; # end eval
     472
     473  if ($@) {
     474    my $msg = $@;
     475    eval { $dbh->rollback; };
     476    return ('FAIL',$msg);
     477  } else {
     478    return ('OK',$user_id);
     479  }
     480} # end addUser
     481
     482
     483## DNSDB::delUser()
     484#
     485sub delUser {
     486} # end delUser
    425487
    426488
     
    574636
    575637  my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
    576   my $vallist = "$id,'$host',$rectype,'$val',$ttl";
     638  my $vallen = "?,?,?,?,?";
     639  my @vallist = ($id,$host,$rectype,$val,$ttl);
    577640
    578641  my $dist;
     
    581644    return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
    582645    $fields .= ",distance";
    583     $vallist .= ",$dist";
     646    $vallen .= ",?";
     647    push @vallist, $dist;
    584648  }
    585649  my $weight;
    586650  my $port;
    587651  if ($rectype == $reverse_typemap{SRV}) {
     652    # check for _service._protocol.  NB:  RFC2782 does not say "MUST"...  nor "SHOULD"...
     653    # it just says (paraphrased) "... is prepended with _ to prevent DNS collisions"
     654    return ('FAIL',"SRV records must begin with _service._protocol")
     655        if $host !~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-z0-9-]+/;
    588656    $weight = shift;
    589657    $port = shift;
    590658    return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
    591659    $fields .= ",weight,port";
    592     $vallist .= ",$weight,$port";
    593   }
    594 
    595   my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
     660    $vallen .= ",?,?";
     661    push @vallist, ($weight,$port);
     662  }
     663
     664  my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)";
     665##fixme: use array for values, replace "vallist" with series of ?,?,? etc
    596666# something is bugging me about this...
    597667#warn "DEBUG: $sql";
    598668  my $sth = $dbh->prepare($sql);
    599   $sth->execute;
     669  $sth->execute(@vallist);
    600670
    601671  return ('FAIL',$sth->errstr) if $sth->err;
  • trunk/dns.cgi

    r23 r24  
    198198      changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
    199199    } else {
    200       $page->param(failed => 1);
    201       $page->param(errmsg => $msg);
     200
     201      $page->param(failed       => 1);
     202      $page->param(errmsg       => $msg);
     203      $page->param(wastrying    => "adding");
     204      $page->param(todo         => "Add record to");
     205      $page->param(recact       => "add");
     206      $page->param(parentid     => $webvar{parentid});
     207      $page->param(defrec       => $webvar{defrec});
     208      $page->param(id           => $webvar{id});
    202209      fill_recdata();   # populate the form... er, mostly.
    203210    }
     
    246253    $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
    247254  } else {
     255    $page->param(parentid => $webvar{parentid});
     256#    $page->param(id => $webvar{id});
    248257    $page->param(dohere => domainName($dbh,$webvar{parentid}));
    249258  }
     
    353362# redirect to dns.cgi?etc&page=reclist
    354363    changepage(page => "reclist", id => $msg);
    355     $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
    356     showdomain(0,$msg);
    357 ##work
     364#    $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
     365#    showdomain(0,$msg);
    358366  } else {
    359367# oooh, yeah, this is supposed to be a redirect.  er, maybe.  whee.
     368##fixme: session ID
    360369    $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
    361370    $page->param(add_failed => 1);
     
    416425  }
    417426  $page->param(delgroupname => groupName($dbh, $webvar{id}));
     427
     428} elsif ($webvar{page} eq 'useradmin') {
     429
     430  list_users();
     431  $page->param(curpage => $webvar{page});
     432
     433} elsif ($webvar{page} eq 'newuser') {
     434
     435  # foo?
     436  fill_actypelist();
     437
     438} elsif ($webvar{page} eq 'adduser') {
     439
     440  my ($code,$msg);
     441 
     442  if ($webvar{pass1} ne $webvar{pass2}) {
     443    $code = 'FAIL';
     444    $msg = "Passwords don't match";
     445  } else {
     446    ($code,$msg) = addUser($dbh,$webvar{username}, $webvar{group}, $webvar{pass1},
     447        ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{fname}, $webvar{lname});
     448  }
     449
     450# hokay, a bit of magic to decide which page we hit.
     451  if ($code eq 'OK') {
     452# redirect to dns.cgi?etc&page=reclist
     453    changepage(page => "useradmin");
     454#    $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
     455#    showdomain(0,$msg);
     456##work
     457  } else {
     458# oooh, yeah, this is supposed to be a redirect.  er, maybe.  whee.
     459#    $page = HTML::Template->new(filename => "$templatedir/newuser.tmpl");
     460    $page->param(add_failed => 1);
     461    $page->param(username => $webvar{username});
     462    $page->param(fname => $webvar{fname});
     463    $page->param(lname => $webvar{lname});
     464    $page->param(pass1 => $webvar{pass1});
     465    $page->param(pass2 => $webvar{pass2});
     466    $page->param(errmsg => $msg);
     467    fill_actypelist();
     468  }
     469
     470  $page->param(add_failed => 1);
    418471}
    419472
     
    421474# start output here so we can redirect pages.
    422475print "Content-type: text/html\n\n", $header->output;
    423 
    424 foreach (@debugbits) { print; }
    425476
    426477##common bits
     
    429480  $page->param(groupname => groupName($dbh,$curgroup));
    430481
     482  # group tree.  should go elsewhere, probably
     483  my $tmpgrplist = fill_grptree($logingroup,$curgroup);
     484  $page->param(grptree => $tmpgrplist);
     485
    431486  # stuff for menu group change.  nb: this is icky.
    432487  fill_grouplist("grouplist");
    433488  $page->param(whereami => $ENV{REQUEST_URI});
    434489}
     490
     491foreach (@debugbits) { print; }
    435492
    436493# spit it out
     
    458515
    459516exit 0;
     517
     518
     519sub fill_grptree {
     520  my $root = shift;
     521  my $cur = shift;
     522
     523  my @childlist;
     524
     525  my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
     526  getChildren($dbh,$root,\@childlist,'immediate');
     527  return if $#childlist == -1;
     528  my @grouplist;
     529  foreach (@childlist) {
     530    my %row;
     531    $row{grpname} = groupName($dbh,$_);
     532    $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
     533    $row{subs} = fill_grptree($_,$cur);
     534    push @grouplist, \%row;
     535  }
     536  $grptree->param(treelvl => \@grouplist);
     537  return $grptree->output;
     538}
    460539
    461540
     
    575654  $page->param(ttl      => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
    576655}
     656
     657
     658sub fill_actypelist {
     659  my @actypes;
     660
     661  my %row1 = (actypeval => 'u', actypename => 'user');
     662  $row1{typesel} = 1 if $webvar{accttype} eq 'u';
     663  push @actypes, \%row1;
     664
     665  my %row2 = (actypeval => 'S', actypename => 'superuser');
     666  $row2{typesel} = 1 if $webvar{accttype} eq 'S';
     667  push @actypes, \%row2;
     668
     669  $page->param(actypelist       => \@actypes);
     670}
     671
    577672
    578673sub fill_fpnla {
     
    713808  $page->param("$template_var" => \@grouplist);
    714809
    715 }
     810} # end fill_grouplist()
     811
     812sub list_users {
     813  my $sth = $dbh->prepare("select count(*) from users where group_id=?");
     814  $sth->execute($curgroup);
     815  my ($count) = ($sth->fetchrow_array);
     816
     817# fill page count and first-previous-next-last-all bits
     818##fixme - hardcoded group bit
     819  fill_pgcount($count,"users",'');
     820  fill_fpnla($count);
     821
     822  my @userlist;
     823  $sth = $dbh->prepare("SELECT u.user_id, u.email, u.firstname, u.lastname, u.type, g.group_name, u.status ".
     824        "FROM users u ".
     825        "INNER JOIN groups g ON u.group_id=g.group_id ".
     826        "WHERE u.group_id=?".
     827        ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
     828  $sth->execute($curgroup);
     829
     830  my $rownum = 0;
     831  while (my @data = $sth->fetchrow_array) {
     832    my %row;
     833    $row{userid} = $data[0];
     834    $row{username} = $data[1];
     835    $row{userfull} = "$data[2] $data[3]";
     836    $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
     837    $row{usergroup} = $data[5];
     838    $row{mkactive} = $data[6];
     839    $row{bg} = ($rownum++)%2;
     840    $row{sid} = $sid;
     841    push @userlist, \%row;
     842  }
     843  $page->param(usertable => \@userlist);
     844}
  • trunk/templates/dns.css

    r2 r24  
    11#debug {
    22        background-color: #990066;
     3        padding: 2px;
    34}
    45
     
    7677        font-size: 10px;
    7778}
     79ul {
     80        margin-left: 10px;
     81        padding: 0px;
     82        list-style-type:none;
     83}
    7884
    7985/* general classes */
  • trunk/templates/menu.tmpl

    r20 r24  
    1717<hr>
    1818<a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=grpman">Manage groups</a><br />
    19  <TMPL_VAR NAME=groupname>
     19<TMPL_VAR NAME=grptree>
     20<!-- hmm:  <TMPL_VAR NAME=groupname> -->
    2021<hr>
    2122<a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=dnsq">DNS Query</a><br />
  • trunk/templates/newdomain.tmpl

    r20 r24  
    33<TMPL_INCLUDE NAME="menu.tmpl">
    44
    5 <td>
     5<td align="center">
    66<TMPL_IF add_failed><span class="errmsg">Error adding domain <TMPL_VAR NAME=domain>: <TMPL_VAR
    77NAME=errmsg></span></TMPL_IF>
     
    1212<input type="hidden" name="newdomain" value="yes">
    1313
    14 <table border=0 width="70%" bgcolor="white">
     14<table border=0 width="450" bgcolor="white">
    1515<tr><td>
    16     <table border=0 cellspacing=0 cellpadding=2 width="100%">
     16    <table border=0 cellspacing=2 cellpadding=2 width="100%">
    1717        <tr bgcolor="#cccccc"><td colspan=2 align="center">Add Domain</td></tr>
    1818
    1919        <tr bgcolor="#eeeeee">
    20                 <td>Domain Name:</td>
     20                <td width="50%">Domain Name:</td>
    2121                <td align="left"><input type="text" name="domain" value="<TMPL_VAR NAME=domain>"></td>
    2222        </tr>
     
    2929        </tr>
    3030        <tr bgcolor="#eeeeee">
    31                 <td></td><td><input type=checkbox name=makeactive checked>Make domain active on next DNS propagation</td>
     31                <td>Make domain active on next DNS propagation</td><td><input type=checkbox name=makeactive checked></td>
    3232        </tr>
    33         <tr><td colspan=2 align="center"><input type="submit" value="add"></td></tr>
     33        <tr><td colspan=2 align="center"><input type="submit" value="Add domain"></td></tr>
    3434<tr><td>tmp note:  radio button select "group template" vs "clone domain"?</td></tr>
    3535    </table>
  • trunk/templates/newgrp.tmpl

    r20 r24  
    2828                </select></td>
    2929        </tr>
    30         <tr><td colspan=2 align="center"><input type="submit" value="add"></td></tr>
     30        <tr><td colspan=2 align="center"><input type="submit" value="Add group"></td></tr>
    3131<tr><td colspan=2>tmp note:  radio button select "group template" vs "clone group"?</td></tr>
    3232    </table>
Note: See TracChangeset for help on using the changeset viewer.