- Timestamp:
- 03/31/11 18:01:43 (14 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r83 r87 240 240 # Update an ACL entry 241 241 # Takes a db handle, type, owner-id, and hashref for the changed permissions. 242 ##fixme: Must handle case of changing object's permissions from inherited to custom242 ##fixme: Must handle case of changing object's permissions from custom to inherited 243 243 sub changePermissions { 244 244 my $dbh = shift; … … 246 246 my $id = shift; 247 247 my $newperms = shift; 248 my $inherit = shift || 0; 248 249 249 250 my $failmsg = ''; 250 251 251 # see if we're switching from inherited to custom 252 my $sth = $dbh->prepare("SELECT (u.permission_id=g.permission_id) AS was_inherited,u.permission_id". 252 # see if we're switching from inherited to custom. for bonus points, 253 # snag the permid and parent permid anyway, since we'll need the permid 254 # to set/alter custom perms, and both if we're switching from custom to 255 # inherited. 256 my $sth = $dbh->prepare("SELECT (u.permission_id=g.permission_id) AS was_inherited,u.permission_id,g.permission_id". 253 257 " FROM ".($type eq 'user' ? 'users' : 'groups')." u ". 254 258 " JOIN groups g ON u.".($type eq 'user' ? '' : 'parent_')."group_id=g.group_id ". … … 256 260 $sth->execute($id); 257 261 258 my ($wasinherited,$permid ) = $sth->fetchrow_array;262 my ($wasinherited,$permid,$parpermid) = $sth->fetchrow_array; 259 263 260 264 # hack phtoui … … 268 272 # Wrap all the SQL in a transaction 269 273 eval { 270 if ($wasinherited) { 274 if ($inherit) { 275 276 $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='t',permission_id=? ". 277 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($parpermid, $id) ); 278 $dbh->do("DELETE FROM permissions WHERE permission_id=?", undef, ($permid) ); 279 280 } else { 281 282 if ($wasinherited) { # munge new permission entry in if we're switching from inherited perms 271 283 ##fixme: need to add semirecursive bit to properly munge inherited permission ID on subgroups and users 272 $dbh->do("INSERT INTO permissions ($permlist) ". 273 "SELECT $permlist FROM permissions WHERE permission_id=?", undef, ($permid) ); 274 #$sth = $dbh->prepare($sql); 275 #$sth->execute($permid); 276 $sth = $dbh->prepare("SELECT permission_id FROM ".($type eq 'user' ? 'users' : 'groups'). 277 " WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?"); 278 $sth->execute($id); 279 ($permid) = $sth->fetchrow_array; 280 $dbh->do("UPDATE permissions SET ".($type eq 'user' ? 'user' : 'group')."_id=? ". 281 "WHERE permission_id=?", undef, ($id,$permid) ); 282 $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET permission_id=? ". 283 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($permid,$id) ); 284 } 285 foreach (@permtypes) { 286 if (defined ($newperms->{$_})) { 287 $dbh->do("UPDATE permissions SET $_=? WHERE permission_id=?", undef, ($newperms->{$_},$permid) ); 288 #$sth->execute($newperms->{$_},$permid); 284 # ... if'n'when we have groups with fully inherited permissions. 285 # SQL is coo 286 $dbh->do("INSERT INTO permissions ($permlist,".($type eq 'user' ? 'user' : 'group')."_id) ". 287 "SELECT $permlist,? FROM permissions WHERE permission_id=?", undef, ($id,$permid) ); 288 ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions ". 289 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($id) ); 290 $dbh->do("UPDATE ".($type eq 'user' ? 'users' : 'groups')." SET inherit_perm='f',permission_id=? ". 291 "WHERE ".($type eq 'user' ? 'user' : 'group')."_id=?", undef, ($permid, $id) ); 289 292 } 290 } 293 294 # and now set the permissions we were passed 295 foreach (@permtypes) { 296 if (defined ($newperms->{$_})) { 297 $dbh->do("UPDATE permissions SET $_=? WHERE permission_id=?", undef, ($newperms->{$_},$permid) ); 298 } 299 } 300 301 } # (inherited->)? custom 291 302 292 303 $dbh->commit; … … 295 306 my $msg = $@; 296 307 eval { $dbh->rollback; }; 297 return ('FAIL',"$failmsg: $msg ");308 return ('FAIL',"$failmsg: $msg ($permid)"); 298 309 } else { 299 310 return ('OK',$permid); … … 533 544 "VALUES ($groupid,?,?,?,?,?,?,?)"); 534 545 if ($inherit) { 535 ##fixme: fixme! 546 # Duplicate records from parent. Actually relying on inherited records feels 547 # very fragile, and it would be problematic to roll over at a later time. 536 548 my $sth2 = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?"); 549 $sth2->execute($pargroup); 537 550 while (my @clonedata = $sth2->fetchrow_array) { 538 551 $sth->execute(@clonedata); … … 665 678 666 679 ## DNSDB::addUser() 667 # 680 # Add a user. 681 # Takes a DB handle, username, group ID, password, state (active/inactive). 682 # Optionally accepts: 683 # user type (user/admin) - defaults to user 684 # permissions string - defaults to inherit from group 685 # three valid forms: 686 # i - Inherit permissions 687 # c:<user_id> - Clone permissions from <user_id> 688 # C:<permission list> - Set these specific permissions 689 # first name - defaults to username 690 # last name - defaults to blank 691 # phone - defaults to blank (could put other data within column def) 692 # Returns (OK,OK) on success, (FAIL,<message>) on failure 668 693 sub addUser { 669 694 $errstr = ''; 670 695 my $dbh = shift; 671 return ('FAIL',"Need database handle") if !$dbh;672 696 my $username = shift; 673 return ('FAIL',"Missing username") if !defined($username);674 697 my $group = shift; 675 return ('FAIL',"Missing group") if !defined($group);676 698 my $pass = shift; 677 return ('FAIL',"Missing password") if !defined($pass);678 699 my $state = shift; 679 return ('FAIL',"Need account status") if !defined($state); 700 701 return ('FAIL',"Missing one or more required entries") if !defined($state); 680 702 681 703 my $type = shift || 'u'; # create limited users by default - fwiw, not sure yet how this will interact with ACLs … … 701 723 local $dbh->{RaiseError} = 1; 702 724 725 my $failmsg; 703 726 # Wrap all the SQL in a transaction 704 727 eval { 705 # insert the user... 706 my $sth = $dbh->prepare("INSERT INTO users (group_id,username,password,firstname,lastname,phone,type,status) ". 707 "VALUES (?,?,?,?,?,?,?,?)"); 708 $sth->execute($group,$username,unix_md5_crypt($pass),$fname,$lname,$phone,$type,$state); 728 # insert the user... note we set inherited perms by default since 729 # it's simple and cleans up some other bits of state 730 my $sth = $dbh->prepare("INSERT INTO users ". 731 "(group_id,username,password,firstname,lastname,phone,type,status,permission_id,inherit_perm) ". 732 "VALUES (?,?,?,?,?,?,?,?,(SELECT permission_id FROM permissions WHERE group_id=?),'t')"); 733 $sth->execute($group,$username,unix_md5_crypt($pass),$fname,$lname,$phone,$type,$state,$group); 709 734 710 735 # get the ID... … … 713 738 ($user_id) = $sth->fetchrow_array(); 714 739 740 # Permissions! Gotta set'em all! 741 die "Invalid permission string $permstring" 742 if $permstring !~ /^(?: 743 i # inherit 744 |c:\d+ # clone 745 # custom. no, the leading , is not a typo 746 |C:(?:,(?:group|user|domain|record|self)_(?:edit|create|delete))+ 747 )$/x; 748 # bleh. I'd call another function to do my dirty work, but we're in the middle of a transaction already. 749 if ($permstring ne 'i') { 750 # for cloned or custom permissions, we have to create a new permissions entry. 751 my $clonesrc = $group; 752 if ($permstring =~ /^c:(\d+)/) { $clonesrc = $1; } 753 $dbh->do("INSERT INTO permissions ($permlist,user_id) ". 754 "SELECT $permlist,? FROM permissions WHERE permission_id=". 755 "(SELECT permission_id FROM permissions WHERE ".($permstring =~ /^c:/ ? 'user' : 'group')."_id=?)", 756 undef, ($user_id,$clonesrc) ); 757 $dbh->do("UPDATE users SET permission_id=". 758 "(SELECT permission_id FROM permissions WHERE user_id=?) ". 759 "WHERE user_id=?", undef, ($user_id, $user_id) ); 760 } 761 if ($permstring =~ /^C:/) { 762 # finally for custom permissions, we set the passed-in permissions (and unset 763 # any that might have been brought in by the clone operation above) 764 my ($permid) = $dbh->selectrow_array("SELECT permission_id FROM permissions WHERE user_id=?", 765 undef, ($user_id) ); 766 foreach (@permtypes) { 767 if ($permstring =~ /,$_/) { 768 $dbh->do("UPDATE permissions SET $_='t' WHERE permission_id=?", undef, ($permid) ); 769 } else { 770 $dbh->do("UPDATE permissions SET $_='f' WHERE permission_id=?", undef, ($permid) ); 771 } 772 } 773 } 774 775 $dbh->do("UPDATE users SET inherit_perm='n' WHERE user_id=?", undef, ($user_id) ); 776 715 777 ##fixme: add another table to hold name/email for log table? 716 die "dying horribly\n";778 #die "dying horribly ($permstring, $user_id)"; 717 779 718 780 # once we get here, we should have suceeded. … … 723 785 my $msg = $@; 724 786 eval { $dbh->rollback; }; 725 return ('FAIL',$msg );787 return ('FAIL',$msg." $failmsg"); 726 788 } else { 727 789 return ('OK',$user_id); … … 762 824 my $pass = shift; 763 825 my $state = shift; 764 my $type = shift ;826 my $type = shift || 'u'; 765 827 my $fname = shift || $username; 766 828 my $lname = shift || ''; … … 1136 1198 my $id = shift; 1137 1199 1138 return "FAIL", "wakka wakka";1139 1200 my $sth = $dbh->prepare("DELETE FROM ".($defrec eq 'y' ? 'default_' : '')."records WHERE record_id=?"); 1140 1201 $sth->execute($id); -
trunk/dns.cgi
r83 r87 7 7 # Last update by $Author$ 8 8 ### 9 # Copyright (C) 2008 ,2009- Kris Deugau <kdeugau@deepnet.cx>9 # Copyright (C) 2008-2011 - Kris Deugau <kdeugau@deepnet.cx> 10 10 11 11 use strict; … … 261 261 $sortorder = $session->param($webvar{page}.'order'); 262 262 263 ##work264 263 # set up the headers 265 264 my @cols = ('host', 'type', 'val', 'distance', 'weight', 'port', 'ttl'); … … 275 274 $page->param(defrec => $webvar{defrec}); 276 275 if ($webvar{defrec} eq 'y') { 277 ##fixme: hardcoded group278 276 showdomain('y',$curgroup); 279 277 } else { … … 291 289 if ($webvar{recact} eq 'new') { 292 290 293 $page->param(todo => "Add record to");291 $page->param(todo => "Add record"); 294 292 $page->param(recact => "add"); 295 293 $page->param(parentid => $webvar{parentid}); … … 324 322 $page->param(errmsg => $msg); 325 323 $page->param(wastrying => "adding"); 326 $page->param(todo => "Add record to");324 $page->param(todo => "Add record"); 327 325 $page->param(recact => "add"); 328 326 $page->param(parentid => $webvar{parentid}); … … 613 611 614 612 my $alterperms = 0; # flag iff we need to force custom permissions due to user's current access limits 615 my %newperms; 613 614 my %newperms; # we're going to prefill the existing permissions, so we can change them. 615 getPermissions($dbh, 'user', $webvar{uid}, \%newperms); 616 616 617 617 if ($webvar{pass1} ne $webvar{pass2}) { … … 642 642 $permstring = 'C:'; 643 643 foreach (@permtypes) { 644 $newperms{$_} = 0; 645 $newperms{$_} = 1 if $webvar{$_} eq 'on'; 646 if ($permissions{admin}) { 644 if ($permissions{admin} || $permissions{$_}) { 647 645 $permstring .= ",$_" if defined($webvar{$_}) && $webvar{$_} eq 'on'; 648 } else { 649 $permstring .= ",$_" if $permissions{$_} && defined($webvar{$_}) && $webvar{$_} eq 'on'; 646 $newperms{$_} = (defined($webvar{$_}) && $webvar{$_} eq 'on' ? 1 : 0); 650 647 } 651 648 } … … 653 650 } elsif ($permissions{admin} && $webvar{perms_type} eq 'clone') { 654 651 $permstring = "c:$webvar{clonesrc}"; 652 getPermissions($dbh, 'user', $webvar{clonesrc}, \%newperms); 655 653 $page->param(perm_clone => 1); 656 654 } else { … … 671 669 ##fixme - need to actually get a correct permission set to pass in here, 672 670 # also a flag to revert custom permissions to inherited 673 ##work 674 ($code,$msg) = changePermissions($dbh, 'user', $webvar{uid}, \%newperms); 671 ($code,$msg) = changePermissions($dbh, 'user', $webvar{uid}, \%newperms, ($permstring eq 'i')); 675 672 } 676 673 } … … 694 691 $page->param(action => $webvar{action}); 695 692 $page->param(set_permgroup => 1); 693 if ($webvar{perms_type} eq 'inherit') { # set permission class radio 694 $page->param(perm_inherit => 1); 695 } elsif ($webvar{perms_type} eq 'clone') { 696 $page->param(perm_clone => 1); 697 } else { 698 $page->param(perm_custom => 1); 699 } 696 700 $page->param(uname => $webvar{uname}); 697 701 $page->param(fname => $webvar{fname}); … … 724 728 $page->param(fname => $userinfo->{firstname}); 725 729 $page->param(lname => $userinfo->{lastname}); 730 $page->param(set_permgroup => 1); 726 731 if ($userinfo->{inherit_perm}) { 727 732 $page->param(perm_inherit => 1); … … 729 734 $page->param(perm_custom => 1); 730 735 } 731 736 ##work 732 737 # } elsif ($webvar{action} eq 'update') { 733 738 } else { … … 1043 1048 } 1044 1049 1050 print "<pre>\n"; 1045 1051 foreach (@debugbits) { print; } 1052 print "</pre>\n"; 1046 1053 1047 1054 # spit it out … … 1239 1246 $sth->execute; 1240 1247 1248 # shut up some warnings, but don't stomp on caller's state 1249 local $webvar{clonesrc} = 0 if !defined($webvar{clonesrc}); 1250 1241 1251 my @clonesrc; 1242 1252 while (my ($username,$uid) = $sth->fetchrow_array) { … … 1276 1286 $page->param(lastoffs => int (($count-1)/$perpage)); 1277 1287 } 1288 } else { 1289 $page->param(onepage => 1); 1278 1290 } 1279 1291 } … … 1374 1386 } # end listdomains() 1375 1387 1388 1376 1389 sub listgroups { 1377 1390 -
trunk/dns.sql
r86 r87 251 251 SELECT pg_catalog.setval('default_records_record_id_seq', 8, true); 252 252 SELECT pg_catalog.setval('domains_domain_id_seq', 1, false); 253 SELECT pg_catalog.setval('groups_group_id_seq', 2, true);254 SELECT pg_catalog.setval('permissions_permission_id_seq', 3, true);253 SELECT pg_catalog.setval('groups_group_id_seq', 1, true); 254 SELECT pg_catalog.setval('permissions_permission_id_seq', 2, true); 255 255 SELECT pg_catalog.setval('records_record_id_seq', 1, false); 256 256 SELECT pg_catalog.setval('users_user_id_seq', 2, false); -
trunk/templates/fpnla.tmpl
r73 r87 3 3 <TMPL_IF navnext><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=<TMPL_VAR NAME=nextoffs><TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>">Next<img src="images/fwd.png" alt="[ Next ]" /></a><TMPL_ELSE>Next<img src="images/fwd.png" alt="[ Next ]" /></TMPL_IF> 4 4 <TMPL_IF navlast><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=<TMPL_VAR NAME=lastoffs><TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>">Last<img src="images/ffwd.png" alt="[ Last ]" /></a><TMPL_ELSE>Last<img src="images/ffwd.png" alt="[ Last ]" /></TMPL_IF> 5 <TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=all<TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>">All</a><TMPL_ELSE>< a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=0<TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>"><TMPL_VAR NAME=perpage> per page</a></TMPL_IF>5 <TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=all<TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>">All</a><TMPL_ELSE><TMPL_UNLESS onepage><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=<TMPL_VAR NAME=curpage>&offset=0<TMPL_IF id>&id=<TMPL_VAR NAME=id></TMPL_IF><TMPL_IF defrec>&defrec=<TMPL_VAR NAME=defrec></TMPL_IF>"><TMPL_VAR NAME=perpage> per page</a></TMPL_UNLESS></TMPL_IF> -
trunk/templates/record.tmpl
r38 r87 54 54 </tr> 55 55 <tr class="datalinelight"> 56 <td colspan="2" align="center"><input type="submit" value=" Add record" /></td>56 <td colspan="2" align="center"><input type="submit" value=" <TMPL_VAR NAME=todo> " /></td> 57 57 </tr> 58 58 </table>
Note:
See TracChangeset
for help on using the changeset viewer.