Changeset 117 for trunk/DNSDB.pm


Ignore:
Timestamp:
09/01/11 15:55:00 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Add getParents (untested) and isParent (tested) subs
Add some supporting hashes for entity -> parent(entity)

relationships in the database - private to DNSDB.pm

Rename tmp_ruri to uri_self for clarity and reuse
Move uri_self munging from ##common area so that more

subs can use it

Update group tree to change the current group by clicking

the group name. Working comments need to be cleaned up
and choose-a-group dropdown removed from the menu

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r116 r117  
    3434        &getSOA &getRecLine &getDomRecs &getRecCount
    3535        &addRec &updateRec &delRec
    36         &isParent
     36        &getParents
     37        &isParent
    3738        &domStatus &importAXFR
    3839        &export
     
    5253                &getSOA &getRecLine &getDomRecs &getRecCount
    5354                &addRec &updateRec &delRec
    54                 &isParent
     55                &getParents
     56                &isParent
    5557                &domStatus &importAXFR
    5658                &export
     
    13221324
    13231325
     1326  # Reference hashes.
     1327  my %par_tbl = (
     1328                group   => 'groups',
     1329                user    => 'users',
     1330                defrec  => 'default_records',
     1331                domain  => 'domains',
     1332                record  => 'records'
     1333        );
     1334  my %id_col = (
     1335                group   => 'group_id',
     1336                user    => 'user_id',
     1337                defrec  => 'record_id',
     1338                domain  => 'domain_id',
     1339                record  => 'record_id'
     1340        );
     1341  my %par_col = (
     1342                group   => 'parent_group_id',
     1343                user    => 'group_id',
     1344                defrec  => 'group_id',
     1345                domain  => 'group_id',
     1346                record  => 'domain_id'
     1347        );
     1348  my %par_type = (
     1349                group   => 'group',
     1350                user    => 'group',
     1351                defrec  => 'group',
     1352                domain  => 'group',
     1353                record  => 'domain'
     1354        );
     1355
    13241356## DNSDB::getParents()
    13251357# Find out which entities are parent to the requested id
     
    13291361  my $id = shift;
    13301362  my $type = shift;
    1331 
    1332   # Get immediate parent
    1333   $par_col = 'group_id';
    1334 
    1335   if ($type eq 'user') {
    1336     $table = 'users';
    1337     $id_col = 'user_id';
    1338   }
    1339   if ($type eq 'defrec') {
    1340     $table = 'default_records';
    1341     $id_col = 'record_id';
    1342   }
    1343   if ($type eq 'domain') {
    1344     $table = 'domains';
    1345     $id_col = 'domain_id';
    1346   }
    1347   if ($type eq 'record') {
    1348     $table = 'records';
    1349     $id_col = 'record_id';
    1350     $par_col = 'domain_id';
    1351   }
    1352 
    1353 $dbh->do("SELECT $par_col FROM $table WHERE $id_col = ?",
     1363  my $depth = shift || 'all';   # valid values:  'all', 'immed', <int> (stop at this group ID)
     1364
     1365  my @parlist;
     1366
     1367  while (1) {
     1368    my $result = $dbh->selectrow_hashref("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
     1369        undef, ($id) );
     1370    unshift @parlist, ($result->{$par_col{$type}} => $par_type{$type});
     1371    last if $result->{$par_col{$type}} == 1;    # group 1 is its own parent
     1372    $type = $par_type{$type};
     1373    $id = $result->{$par_col{$type}};
     1374  }
     1375
     1376  return \@parlist;
    13541377
    13551378} # end getParents()
     1379
     1380
     1381## DNSDB::isParent()
     1382# Returns true if $id1 is a parent of $id2, false otherwise
     1383sub isParent {
     1384  my $dbh = shift;
     1385  my $id1 = shift;
     1386  my $type1 = shift;
     1387  my $id2 = shift;
     1388  my $type2 = shift;
     1389##todo:  immediate, secondary, full (default)
     1390
     1391  # Return false on impossible relations
     1392  return 0 if $type1 eq 'record';       # nothing may be a child of a record
     1393  return 0 if $type1 eq 'defrec';       # nothing may be a child of a record
     1394  return 0 if $type1 eq 'user';         # nothing may be child of a user
     1395  return 0 if $type1 eq 'domain' && $type2 ne 'record'; # domain may not be a parent of anything other than a record
     1396
     1397  # group 1 is the ultimate root parent
     1398  return 1 if $type1 eq 'group' && $id1 == 1;
     1399
     1400# almost the same loop as getParents() above
     1401  my $id = $id2;
     1402  my $type = $type2;
     1403  my $foundparent = 0;
     1404my $tmp = 0;
     1405  while (1) {
     1406my $sql = "SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?";
     1407    my $result = $dbh->selectrow_hashref($sql,
     1408        undef, ($id) ) or warn $dbh->errstr." $sql";
     1409    if ($result->{$par_col{$type}} == $id1) {
     1410      $foundparent = 1;
     1411      last;
     1412    }
     1413    # group 1 is its own parent.  need this here more to break strange loops than for detecting a parent
     1414    last if $result->{$par_col{$type}} == 1;
     1415    $type = $par_type{$type};
     1416    $id = $result->{$par_col{$type}};
     1417last if $tmp++ > 10;
     1418  }
     1419
     1420  return $foundparent;
     1421} # end isParent()
    13561422
    13571423
Note: See TracChangeset for help on using the changeset viewer.