Changeset 292


Ignore:
Timestamp:
03/27/12 16:48:55 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Move action logging for addGroup() into DNSDB.pm. See #35

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r290 r292  
    17321732# Add a group
    17331733# Takes a database handle, group name, parent group, hashref for permissions,
    1734 # and optional template-vs-cloneme flag
     1734# and optional template-vs-cloneme flag for the default records
    17351735# Returns a status code and message
    17361736sub addGroup {
     
    17511751  local $dbh->{RaiseError} = 1;
    17521752
    1753   my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE group_name=?");
    1754   my $group_id;
    1755 
    1756 # quick check to start to see if we've already got one
    1757   $sth->execute($groupname);
    1758   ($group_id) = $sth->fetchrow_array;
     1753  my ($group_id) = $dbh->selectrow_array("SELECT group_id FROM groups WHERE group_name=?", undef, ($groupname));
    17591754
    17601755  return ('FAIL', "Group already exists") if $group_id;
     
    17621757  # Wrap all the SQL in a transaction
    17631758  eval {
    1764     $sth = $dbh->prepare("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)");
    1765     $sth->execute($pargroup,$groupname);
    1766 
    1767     my ($groupid) = $dbh->selectrow_array("SELECT group_id FROM groups WHERE group_name=?", undef, ($groupname));
    1768 
    1769 # Permissions
    1770     if ($inherit) {
    1771     } else {
    1772       my @permvals;
    1773       foreach (@permtypes) {
    1774         if (!defined ($permissions->{$_})) {
    1775           push @permvals, 0;
    1776         } else {
    1777           push @permvals, $permissions->{$_};
    1778         }
     1759    $dbh->do("INSERT INTO groups (parent_group_id,group_name) VALUES (?,?)", undef, ($pargroup, $groupname) );
     1760
     1761    my ($groupid) = $dbh->selectrow_array("SELECT currval('groups_group_id_seq')");
     1762
     1763    # We work through the whole set of permissions instead of specifying them so
     1764    # that when we add a new permission, we don't have to change the code anywhere
     1765    # that doesn't explicitly deal with that specific permission.
     1766    my @permvals;
     1767    foreach (@permtypes) {
     1768      if (!defined ($permissions->{$_})) {
     1769        push @permvals, 0;
     1770      } else {
     1771        push @permvals, $permissions->{$_};
    17791772      }
    1780 
    1781       $sth = $dbh->prepare("INSERT INTO permissions (group_id,$permlist) values (?".',?'x($#permtypes+1).")");
    1782       $sth->execute($groupid,@permvals);
    1783 
    1784       $sth = $dbh->prepare("SELECT permission_id FROM permissions WHERE group_id=?");
    1785       $sth->execute($groupid);
    1786       my ($permid) = $sth->fetchrow_array();
    1787 
    1788       $dbh->do("UPDATE groups SET permission_id=$permid WHERE group_id=$groupid");
    1789     } # done permission fiddling
    1790 
    1791 # Default records
     1773    }
     1774    $dbh->do("INSERT INTO permissions (group_id,$permlist) values (?".',?'x($#permtypes+1).")",
     1775        undef, ($groupid, @permvals) );
     1776    my ($permid) = $dbh->selectrow_array("SELECT currval('permissions_permission_id_seq')");
     1777    $dbh->do("UPDATE groups SET permission_id=$permid WHERE group_id=$groupid");
     1778
     1779    # Default records
    17921780    my $sthf = $dbh->prepare("INSERT INTO default_records (group_id,host,type,val,distance,weight,port,ttl) ".
    17931781        "VALUES ($groupid,?,?,?,?,?,?,?)");
     
    18031791      }
    18041792      # And now the reverse records
    1805       $sth2 = $dbh->prepare("SELECT group_id,host,type,val,ttl FROM default_rev_records WHERE group_id=?");
     1793      $sth2 = $dbh->prepare("SELECT host,type,val,ttl FROM default_rev_records WHERE group_id=?");
    18061794      $sth2->execute($pargroup);
    18071795      while (my @clonedata = $sth2->fetchrow_array) {
     
    18231811    }
    18241812
     1813    _log($dbh, (group_id => $pargroup, entry => "Added group $groupname") );
     1814
    18251815    # once we get here, we should have suceeded.
    18261816    $dbh->commit;
     
    18301820    my $msg = $@;
    18311821    eval { $dbh->rollback; };
     1822    if ($config{log_failures}) {
     1823      _log($dbh, (group_id => $pargroup, entry => "Failed to add group $groupname: $msg") );
     1824      $dbh->commit;
     1825    }
    18321826    return ('FAIL',$msg);
    1833   } else {
    1834     return ('OK','OK');
    1835   }
    1836 
     1827  }
     1828
     1829  return ('OK','OK');
    18371830} # end addGroup()
    18381831
  • trunk/dns.cgi

    r291 r292  
    933933    my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup}, \%newperms);
    934934    if ($code eq 'OK') {
    935       logaction(0, $session->param("username"), $webvar{pargroup}, "Added group $webvar{newgroup}");
    936935      if ($alterperms) {
    937936        changepage(page => "grpman", warnmsg =>
     
    941940      }
    942941    } # fallthrough else
    943     logaction(0, $session->param("username"), $webvar{pargroup}, "Failed to add group $webvar{newgroup}: $msg")
    944         if $config{log_failures};
    945942    # no point in doing extra work
    946943    fill_permissions($page, \%newperms);
Note: See TracChangeset for help on using the changeset viewer.