group_defs.php 38.9 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23
#
24 25 26 27 28
#
# A cache of groups to avoid lookups. Indexed by gid_idx;
#
$group_cache = array();

29 30
class Group
{
31
    var	$group;
32
    var $project;
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47
    #
    # Constructor by lookup on unique index.
    #
    function Group($gid_idx) {
	$safe_gid_idx = addslashes($gid_idx);

	$query_result =
	    DBQueryWarn("select * from groups ".
			"where gid_idx='$safe_gid_idx'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->group = NULL;
	    return;
	}
48
	$this->group   = mysql_fetch_array($query_result);
49
	$this->project =  null;
50 51 52 53 54 55 56 57 58
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->group);
    }

    # Lookup by gid_idx.
    function Lookup($gid_idx) {
59 60 61 62 63 64
	global $group_cache;

        # Look in cache first
	if (array_key_exists("$gid_idx", $group_cache))
	    return $group_cache["$gid_idx"];
	
65 66
	$foo = new Group($gid_idx);

67 68 69 70 71 72
	if (! $foo->IsValid())
	    return null;

	# Insert into cache.
	$group_cache["$gid_idx"] = $foo;
	return $foo;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }

    # Backwards compatable lookup by pid,gid. Will eventually flush this.
    function LookupByPidGid($pid, $gid) {
	$safe_pid = addslashes($pid);
	$safe_gid = addslashes($gid);

	$query_result =
	    DBQueryWarn("select gid_idx from groups ".
			"where pid='$safe_pid' and gid='$safe_gid'");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	$idx = $row['gid_idx'];

90
	return Group::Lookup($idx);	
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    }
    
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

	$gid_idx = $this->gid_idx();

	$query_result =
	    DBQueryWarn("select * from groups where gid_idx='$gid_idx'");
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->group = NULL;
	    return -1;
	}
109
	$this->group = mysql_fetch_array($query_result);
110 111 112
	return 0;
    }

113 114 115 116 117 118 119 120 121 122 123 124
    #
    # Class function to create a new Project Group.
    #
    function Create($project, $uid, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "newgroup");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
125
	    $errors[] = "Transient error(1); please try again later.";
126 127 128 129
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
130
	    $errors[] = "Transient error(2); please try again later.";
131 132 133 134
	    return null;
	}

	# Add these. Maybe caller should do this?
135
	$args["project"]  = $project->pid_idx();
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	fwrite($fp, "<group>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</group>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	# Note: running as the user for mkgroup and modgroups underneath.
	$unix_gid  = $project->unix_gid();
	$retval = SUEXEC($uid, $unix_gid, "webnewgroup $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
154
		$errors[] = "Transient error(3, $retval); please try again later.";
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
171
		    $errors[] = "Transient error(4, $retval); please try again later.";
172 173 174 175 176 177 178 179 180 181 182 183
	    }
	    return null;
	}

        #
        # Parse the last line of output. Ick.
        #
	unset($matches);
	
	if (!preg_match("/^GROUP\s+([^\/]+)\/(\d+)\s+/",
			$suexec_output_array[count($suexec_output_array)-1],
			$matches)) {
184
	    $errors[] = "Transient error(5); please try again later.";
185 186 187 188 189 190
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    return null;
	}
	$group = $matches[2];
	$newgroup = Group::Lookup($group);
	if (! $newgroup) {
191
	    $errors[] = "Transient error(6); please try again later.";
192 193 194 195 196 197 198 199 200
	    TBERROR("Could not lookup new group $group", 0);
	    return null;
	}
	# Unlink this here, so that the file is left behind in case of error.
	# We can then create the group by hand from the xmlfile, if desired.
	unlink($xmlname);
	return $newgroup; 
    }

201 202 203 204 205 206 207 208 209 210 211 212
    #
    # Class function to edit group membership.
    #
    function EditGroup($group, $uid, $args, &$errors) {
	global $suexec_output, $suexec_output_array;

        #
        # Generate a temporary file and write in the XML goo.
        #
	$xmlname = tempnam("/tmp", "editgroup");
	if (! $xmlname) {
	    TBERROR("Could not create temporary filename", 0);
213
	    $errors[] = "Transient error(1); please try again later.";
214 215 216 217
	    return null;
	}
	if (! ($fp = fopen($xmlname, "w"))) {
	    TBERROR("Could not open temp file $xmlname", 0);
218
	    $errors[] = "Transient error(2); please try again later.";
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	    return null;
	}

	# Add these. Maybe caller should do this?
	$args["group"]  = $group->gid_idx();

	fwrite($fp, "<group>\n");
	foreach ($args as $name => $value) {
	    fwrite($fp, "<attribute name=\"$name\">");
	    fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	    fwrite($fp, "</attribute>\n");
	}
	fwrite($fp, "</group>\n");
	fclose($fp);
	chmod($xmlname, 0666);

	# Grab the unix GID for running scripts.
	$unix_gid = $group->unix_gid();

	$retval = SUEXEC($uid, $unix_gid, "webeditgroup $xmlname",
			 SUEXEC_ACTION_IGNORE);

	if ($retval) {
	    if ($retval < 0) {
243
		$errors[] = "Transient error(3, $retval); please try again later.";
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
		SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	    }
	    else {
		# unlink($xmlname);
		if (count($suexec_output_array)) {
		    for ($i = 0; $i < count($suexec_output_array); $i++) {
			$line = $suexec_output_array[$i];
			if (preg_match("/^([-\w]+):\s*(.*)$/",
				       $line, $matches)) {
			    $errors[$matches[1]] = $matches[2];
			}
			else
			    $errors[] = $line;
		    }
		}
		else
260
		    $errors[] = "Transient error(4, $retval); please try again later.";
261 262 263 264 265 266 267 268 269 270 271
	    }
	    return null;
	}
	# There are no return value(s) to parse at the end of the output.

	# Unlink this here, so that the file is left behind in case of error.
	# We can then edit the group by hand from the xmlfile, if desired.
	unlink($xmlname);
	return true;
    }

272 273 274 275 276 277 278 279 280 281 282 283
    #
    # Load the project for a group lazily.
    #
    function LoadProject() {
	$pid_idx = $this->pid_idx();

	if (! ($project = Project::Lookup($pid_idx))) {
	    TBERROR("Group::LoadProject: Could not load project $pid_idx!", 1);
	}
	$this->project = $project;
	return 0;
    }
284 285 286 287 288 289 290 291 292 293 294
    function Project() {
	if (! $this->project) {
	    $this->LoadProject();
	}
	return $this->project;
    }
    
    #
    # Return user object for leader.
    #
    function GetLeader() {
295
	$leader_idx = $this->leader_idx();
296

297 298
	if (! ($leader = User::Lookup($leader_idx))) {
	    TBERROR("Could not find user object for $leader_idx", 1);
299 300 301 302
	}
	return $leader;
    }

303 304 305 306 307 308 309
    #
    # Is this group the default project group. Returns boolean.
    #
    function IsProjectGroup() {
	return $this->pid_idx() == $this->gid_idx();
    }

310
    #
311
    # Project permission checks.
312
    #
313 314 315
    #	returns 0 if not allowed.
    #   returns 1 if allowed.
    # 
316
    function AccessCheck($user, $access_type) {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	global $TB_PROJECT_READINFO;
	global $TB_PROJECT_MAKEGROUP;
	global $TB_PROJECT_EDITGROUP;
	global $TB_PROJECT_GROUPGRABUSERS;
	global $TB_PROJECT_BESTOWGROUPROOT;
	global $TB_PROJECT_DELGROUP;
	global $TB_PROJECT_LEADGROUP;
	global $TB_PROJECT_ADDUSER;
	global $TB_PROJECT_DELUSER;
	global $TB_PROJECT_MAKEOSID;
	global $TB_PROJECT_DELOSID;
	global $TB_PROJECT_MAKEIMAGEID;
	global $TB_PROJECT_DELIMAGEID;
	global $TB_PROJECT_CREATEEXPT;
	global $TB_PROJECT_MIN;
	global $TB_PROJECT_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_LOCALROOT;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_PROJROOT;
        # Min declaration.
	$mintrust = $TB_PROJECT_READINFO;

	$pid = $this->pid();
	$gid = $this->gid();
	$uid = $user->uid();

	if ($access_type < $TB_PROJECT_MIN ||
	    $access_type > $TB_PROJECT_MAX) {
	    TBERROR("Invalid access type: $access_type!", 1);
	}
 
        #
        # Admins do whatever they want!
        # 
	if (ISADMIN()) {
	    return 1;
	}

	if ($access_type == $TB_PROJECT_READINFO) {
357 358 359 360 361 362 363
            #
            # Group root in the project can see any group.
	    #
	    if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			   $TBDB_TRUST_GROUPROOT)) {
		return 1;
	    }
364 365 366 367
	    $mintrust = $TBDB_TRUST_USER;
	}
	elseif ($access_type == $TB_PROJECT_MAKEGROUP ||
		$access_type == $TB_PROJECT_DELGROUP) {
368 369 370 371 372 373 374
            if ($access_type == $TB_PROJECT_DELGROUP) {
                $project = $this->Project();
                $leader  = $this->GetLeader();
		if ($user->SameUser($leader)) {
                    return 1;
                }
            }
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	    $mintrust = $TBDB_TRUST_GROUPROOT;
	}
	elseif ($access_type == $TB_PROJECT_LEADGROUP) {
            #
            # Allow mere user (in default group) to lead a subgroup.
            # 
	    $mintrust = $TBDB_TRUST_USER;
	}
	elseif ($access_type == $TB_PROJECT_MAKEOSID ||
		$access_type == $TB_PROJECT_MAKEIMAGEID ||
		$access_type == $TB_PROJECT_CREATEEXPT) {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}
	elseif ($access_type == $TB_PROJECT_ADDUSER ||
		$access_type == $TB_PROJECT_EDITGROUP) {
	    #
	    # If user is project_root or group_root in default group, 
	    # allow them to add/edit/remove users in any group.
	    #
	    if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			   $TBDB_TRUST_GROUPROOT)) {
		return 1;
	    }
	    #
	    # Otherwise, editing a group requires group_root 
	    # in that group.
	    #	
	    $mintrust = $TBDB_TRUST_GROUPROOT;
	}
	elseif ($access_type == $TB_PROJECT_BESTOWGROUPROOT) {
            #
	    # If user is project_root, 
	    # allow them to bestow group_root in any group.
 	    #
	    if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			   $TBDB_TRUST_PROJROOT)) {
		return 1;
	    }
	    
	    if ($gid == $pid)  {
	        #
	        # Only project_root can bestow group_root in default group, 
	        # and we already established that they are not project_root,
		# so fail.
	        #
		return 0;
	    }
	    else {
	        #
	        # Non-default group.
	        # group_root in default group may bestow group_root.
	        #
	        if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			       $TBDB_TRUST_GROUPROOT)) {
		    return 1;
		}

                #
	        # group_root in the group in question may also bestow
		# group_root.
	        #
		$mintrust = $TBDB_TRUST_GROUPROOT;
	    }
	}
	elseif ($access_type == $TB_PROJECT_GROUPGRABUSERS) {
	    #
	    # Only project_root or group_root in default group
	    # may grab (involuntarily add) users into groups.
	    #
	    $gid = $pid;
	    $mintrust = $TBDB_TRUST_GROUPROOT;
	}
	elseif ($access_type == $TB_PROJECT_DELUSER) {
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
            #
            # Only project leaders can delete group root in the main
            # group, group roots cannot delete each other, but that
            # cannot be tested here, so we have to have a seprate
            # CanDeleteUser() user function that wraps this check.
            #
            # In subgroups, group_root can delete each other. Hmm.
            #
            if ($pid != $gid) {
	        if (TBMinTrust(TBGrpTrust($uid, $pid, $pid),
			       $TBDB_TRUST_GROUPROOT)) {
		    return 1;
		}
            }
	    $mintrust = $TBDB_TRUST_GROUPROOT;
463 464 465 466 467 468
	}
	else {
	    TBERROR("Unexpected access type: $access_type!", 1);
	}

	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
469 470
    }

471 472 473 474 475 476 477 478 479 480
    # Can the user delete the target user. 
    function CanDeleteUser($user, $target_user) {
        global $TB_PROJECT_DELUSER;

        if ($this->IsProjectGroup()) {
            return $this->Project()->CanDeleteUser($user, $target_user);
        }
        return $this->AccessCheck($user, $TB_PROJECT_DELUSER);
    }

481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    #
    # Return a users trust within the group.
    #
    function UserTrust($user) {
	global $TBDB_TRUST_NONE;
	
	$uid_idx = $user->uid_idx();
	$pid_idx = $this->pid_idx();
	$gid_idx = $this->gid_idx();

	$query_result =
	    DBQueryFatal("select trust from group_membership ".
			 "where uid_idx='$uid_idx' and ".
			 "      pid_idx='$pid_idx' and gid_idx='$gid_idx'");

        #
        # No membership is the same as no trust. True? Maybe an error instead?
        # 
	if (mysql_num_rows($query_result) == 0) {
	    return $TBDB_TRUST_NONE;
	}
	$row = mysql_fetch_array($query_result);
503
	$trust_string = $row["trust"];
504 505 506 507

	# Convert string to number.      
	return TBTrustConvert($trust_string);
    }
508

509 510 511 512 513 514 515 516
    # accessors
    function field($name) {
	return (is_null($this->group) ? -1 : $this->group[$name]);
    }
    function pid()	        { return $this->field("pid"); }
    function gid()	        { return $this->field("gid"); }
    function pid_idx()          { return $this->field("pid_idx"); }
    function gid_idx()          { return $this->field("gid_idx"); }
517
    function uuid()             { return $this->field("gid_uuid"); }
518
    function leader()           { return $this->field("leader"); }
519
    function leader_idx()       { return $this->field("leader_idx"); }
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    function created()          { return $this->field("created"); }
    function description()      { return $this->field("description"); }
    function unix_gid()         { return $this->field("unix_gid"); }
    function unix_name()        { return $this->field("unix_name"); }
    function expt_count()       { return $this->field("expt_count"); }
    function expt_last()        { return $this->field("expt_last"); }
    function wikiname()         { return $this->field("wikiname"); }
    function mailman_password() { return $this->field("mailman_password"); }


    #
    # Class function to create new group and return object.
    #
    function NewGroup($project, $gid, $leader, $description, $unix_name) {
	global $TBBASE, $TBMAIL_APPROVAL, $TBMAIL_AUDIT, $TBMAIL_WWW;
	global $MIN_UNIX_GID;

537
	$starting_gid = $MIN_UNIX_GID;
538 539
	$ending_gid   = 50000;

540 541 542 543 544 545 546 547 548 549 550
        #
        # Check that we can guarantee uniqueness of the unix group name.
        # 
	$query_result =
	    DBQueryFatal("select gid from groups ".
			 "where unix_name='$unix_name'");

	if (mysql_num_rows($query_result)) {
	    TBERROR("Could not form a unique Unix group name: $unix_name!", 1);
	}

551 552 553 554 555 556 557 558 559 560 561 562
	# Every group gets a new unique index.
	$gid_idx = TBGetUniqueIndex('next_gid');

	# If project is not defined, then creating initial project group.
	if (! $project) {
	    $pid = $gid;
	    $pid_idx = $gid_idx;
	}
	else {
	    $pid = $project->pid();
	    $pid_idx = $project->pid_idx();
	}
563
	$uuid = NewUUID();
564 565 566 567 568 569 570 571
	
	# Get me an unused unix id. Nice query, eh? Basically, find 
	# unused numbers by looking at existing numbers plus one, and check
	# to see if that number is taken. 
	$query_result =
	    DBQueryWarn("select g.unix_gid + 1 as start from groups as g ".
			"left outer join groups as r on ".
			"  g.unix_gid + 1 = r.unix_gid ".
572
			"where g.unix_gid>=$starting_gid and ".
573
			"      g.unix_gid<$ending_gid and ".
574 575 576 577 578 579 580 581 582 583 584 585
			"      r.unix_gid is null limit 1");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    TBERROR("Could not find an unused unix_gid!", 0);
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	$unix_gid = $row[0];

	if (!DBQueryWarn("insert into groups set ".
			 " pid='$pid', gid='$gid', ".
			 " leader='" . $leader->uid() . "'," .
586
			 " leader_idx='" . $leader->uid_idx() . "'," .
587 588 589
			 " created=now(), ".
			 " description='$description', ".
			 " unix_name='$unix_name', ".
590
			 " gid_uuid='$uuid', ".
591 592 593 594 595 596
			 " gid_idx=$gid_idx, ".
			 " pid_idx=$pid_idx, ".
			 " unix_gid=$unix_gid")) {
	    return null;
	}

597 598 599 600
	if (! DBQueryWarn("insert into group_stats ".
			  "  (pid,gid,gid_idx,pid_idx,gid_uuid) ".
			  "values ('$pid', '$gid', $gid_idx, ".
			  "        $pid_idx, '$uuid')")) {
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	    DBQueryFatal("delete from groups where gid_idx='$gid_idx'");
	    return null;
	}
	$newgroup = Group::Lookup($gid_idx);
	if (! $newgroup)
	    return null;

	return $newgroup;
    }

    #
    # Delete a group.
    #
    function Delete() {
	$gid_idx = $this->gid_idx();

	DBQueryWarn("delete from group_stats where gid_idx='$gid_idx'");
	DBQueryWarn("delete from groups where gid_idx='$gid_idx'");
	return 0;
    }

    #
    # This is strictly for initialization of a testbed.
    #
625
    function Initialize($uid) {
Leigh Stoller's avatar
Leigh Stoller committed
626 627
	global $TBOPSPID;
	
628 629 630 631 632 633
	$emulabgroup = Group::LookupByPidGid($TBOPSPID, $TBOPSPID);

	if (! $emulabgroup) {
	    TBERROR("Group::Initialize: Could not find $TBOPSPID!", 1);
	}

634
	$user = User::Lookup($uid);
635 636 637 638 639

	if (! $user) {
	    TBERROR("Group::Initialize: Could not find user $uid!", 1);
	}

640 641
	if (!$emulabgroup->IsMember($user, $ignore) &&
	    $emulabgroup->AddNewMember($user) < 0) {
642 643 644 645 646 647 648 649 650 651 652 653 654
	    TBERROR("Group::Initialize: Could not add $uid to $TBOPSPID!", 1);
	}

	# Inline approval; usually done with modgroups.
	DBQueryFatal("update group_membership set date_approved=now(), ".
		     "  trust='" . TBDB_TRUSTSTRING_GROUPROOT . "' ".
		     "where uid='$uid' and pid='$TBOPSPID'");
	return 0;
    }

    #
    # Add *new* member to group; starts out with trust=none.
    #
655
    function AddNewMember($user, $trust = TBDB_TRUSTSTRING_NONE) {
656 657 658 659 660 661 662 663 664 665 666 667
	$uid     = $user->uid();
	$uid_idx = $user->uid_idx();
	$pid     = $this->pid();
	$pid_idx = $this->pid_idx();
	$gid     = $this->gid();
	$gid_idx = $this->gid_idx();
	    
	if (!DBQueryWarn("insert into group_membership ".
			 "(uid, uid_idx, gid, gid_idx, pid, pid_idx, ".
			 " trust, date_applied) ".
			 "values ('$uid', '$uid_idx', '$gid', '$gid_idx', ".
			 "        '$pid', '$pid_idx', ".
668
			 "        '$trust', now())")) {
669 670 671 672 673
	    return -1;
	}
	return 0;
    }

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    #
    # And a delete function.
    #
    function DeleteMember($user) {
	$uid     = $user->uid();
	$uid_idx = $user->uid_idx();
	$pid     = $this->pid();
	$pid_idx = $this->pid_idx();
	$gid     = $this->gid();
	$gid_idx = $this->gid_idx();

	DBQueryFatal("delete from group_membership ".
		     "where uid_idx='$uid_idx' and pid_idx='$pid_idx' and ".
		     "      gid_idx='$gid_idx'");
	
	return 0;
    }

692 693 694
    #
    # Notify leaders of new (and verified) group member.
    #
695
    function NewMemberNotify($user, $nag = false) {
696
	global $TBWWW, $APTBASE, $TBMAILTAG,
697
	       $TBMAIL_APPROVAL, $TBMAIL_AUDIT, $TBMAIL_WWW, $TBMAIL_NOREPLY;
698 699 700 701
	
	if (! $this->project) {
	    $this->LoadProject();
	}
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
        $uid_idx = $user->uid_idx();
        $pid_idx = $this->project()->pid_idx();
        $gid_idx = $this->gid_idx();
        
        #
        # In nag mode, we want to check the last time we nagged, to
        # avoid annoying the project leader too much. We do this locked
        # to avoid double click sends.
        #
        if ($nag) {
            DBQueryFatal("lock tables group_membership write");
            $membership = $this->MemberShipInfo($user);
            $date_approved = $membership["date_approved"];
            $date_nagged   = $membership["date_nagged"];

            # Nothing to do.
            if ($date_approved) {
                DBQueryFatal("unlock tables");
                return 1;
            }
            # Nagged recently?
            if ($date_nagged) {
                $nagged = strtotime($date_nagged);
                if (time() - $nagged < 3600 * 24) {
                    DBQueryFatal("unlock tables");
                    return 1;
                }
            }
            # Okay, we want to nag. Update the table first.
            DBQueryFatal("update group_membership set date_nagged=now() ".
                         "where uid_idx='$uid_idx' and ".
                         "      pid_idx='$pid_idx' and ".
                         "      gid_idx='$gid_idx'");
            DBQueryFatal("unlock tables");
        }
737 738
	$project	= $this->project;
	$pid            = $project->pid();
Leigh Stoller's avatar
Leigh Stoller committed
739
	$gid            = $this->gid();
740
	$leader		= $project->GetLeader();
741 742 743
	$leader_name	= $leader->name();
	$leader_email	= $leader->email();
	$leader_uid	= $leader->uid();
744
	$allleaders	= $this->LeaderMailList();
745 746 747 748 749 750 751 752 753 754 755 756 757
	$joining_uid    = $user->uid();
	$usr_title	= $user->title();
	$usr_name	= $user->name();
	$usr_affil	= $user->affil();
	$usr_email	= $user->email();
	$usr_addr	= $user->addr();
	$usr_addr2	= $user->addr2();
	$usr_city	= $user->city();
	$usr_state	= $user->state();
	$usr_zip	= $user->zip();
	$usr_country	= $user->country();
	$usr_phone	= $user->phone();
	$usr_URL	= $user->URL();
758 759
	$url            = $project->wwwBase();
	
760
	if ($project->isEmulab() ||
761 762
            $project->isAPT() || $project->isCloud() ||
            $project->isPNet() || $project->isPowder()) {
763
            if ($project->isEmulab()) {
764
                $url .= "/portal";
765
            }
766 767
	    $url .= "/approveuser.php?uid=$joining_uid&pid=$pid";
	    
768 769 770 771
            $message =
                "$usr_name is trying to join your group $gid ".
                "in project $pid.\n";
	    $instructions =
772 773 774 775 776
		"You can approve or reject this user:\n\n".
		"Approve:  ${url}&action=approve\n".
		"or\n".
		"Deny:     ${url}&action=deny\n";
	    $from = $project->ApprovalEmailAddress();
777 778 779 780 781 782 783 784
            $fields =
                "Name:            $usr_name\n".
                "Login ID:        $joining_uid\n".
                "Email:           $usr_email\n" .
                "Affiliation:     $usr_affil\n".
                "City:            $usr_city\n".
                "State:           $usr_state\n".
                "Country:         $usr_country\n";
785 786
	}
	else {
787 788 789 790 791 792 793
            $message =
                "$usr_name is trying to join your group $gid ".
                "in project $pid.\n";
	    $instructions =
	      "Please return to $url,\n".
	      "log in, and select the 'New User Approval' page to enter your\n".
	      "decision regarding $usr_name's membership in your project.\n\n";
794
	    $from = "$usr_name '$joining_uid' <$usr_email>";
795 796 797 798 799 800 801 802 803 804 805 806 807 808
            $fields = 
                "Name:            $usr_name\n".
                "Emulab ID:       $joining_uid\n".
                "Email:           $usr_email\n" .
                "Affiliation:     $usr_affil\n".
                "User URL:        $usr_URL\n".
                "Job Title:       $usr_title\n".
                "Address 1:       $usr_addr\n".
                "Address 2:       $usr_addr2\n" .
                "City:            $usr_city\n".
                "State:           $usr_state\n".
                "ZIP/Postal Code: $usr_zip\n".
                "Country:         $usr_country\n".
                "Phone:           $usr_phone\n";
809
	}
810 811 812 813
        # Make sure this goes out with the right subject tag,
        $oldmailtag = $TBMAILTAG;
        $TBMAILTAG  = $project->EmailTag();
        
814
	TBMAIL("$leader_name '$leader_uid' <$leader_email>",
815 816 817 818 819 820 821 822 823 824 825 826 827
               "$joining_uid $pid Project Join Request",
               $message . 
               "\n".
               "Contact Info:\n".
               $fields . 
               "\n".
               $instructions .
               "\n".
               "Thanks!\n",
               "From: $from\n".
               "Cc: $allleaders\n".
               "Bcc: $TBMAIL_AUDIT\n".
               "Errors-To: $TBMAIL_WWW");
828

829
        $TBMAILTAG = $oldmailtag;
830 831 832
	return 0;
    }

833 834 835
    #
    # Check if user is a member of this group.
    #
836 837 838
    function IsMember($user, &$approved) {
	global $TBDB_TRUST_USER;
	
839 840 841 842 843 844 845 846 847
	$uid     = $user->uid();
	$uid_idx = $user->uid_idx();
	$gid     = $this->gid();
	$gid_idx = $this->gid_idx();

	$query_result =
	    DBQueryFatal("select trust from group_membership ".
			 "where uid_idx='$uid_idx' and gid_idx='$gid_idx'");

848 849 850 851 852 853 854 855 856 857 858 859
	if (mysql_num_rows($query_result) == 0) {
	    $approved = 0;
	    return 0;
	}

	$row      = mysql_fetch_row($query_result);
	$trust    = $row[0];
	$approved = TBMinTrust($trust, $TBDB_TRUST_USER);

	return 1;
    }

860 861 862
    #
    # Return list of approved members in this group. 
    #
863
    function &MemberList($exclude_leader = 1, $include_trustnone = 0) {
864 865 866 867 868 869 870 871 872 873 874 875
	$gid_idx    = $this->gid_idx();
	$pid_idx    = $this->pid_idx();
	$trust_none = TBDB_TRUSTSTRING_NONE;
	$leader     = $this->GetLeader();
	$result     = array();

	$query_result =
	    DBQueryFatal("select m.uid_idx,m.trust ".
			 "   from group_membership as m ".
			 "left join groups as g on ".
			 "     g.pid=m.pid and g.gid=m.gid ".
			 "where m.pid_idx='$pid_idx' and ".
876 877 878
			 "      m.gid_idx='$gid_idx' ".
                         (!$include_trustnone ?
                          "and m.trust!='$trust_none' " : " ").
Leigh Stoller's avatar
Leigh Stoller committed
879
			 "order by m.uid");
880 881 882 883 884 885 886 887

	while ($row = mysql_fetch_array($query_result)) {
	    $uid_idx = $row["uid_idx"];
	    $trust   = $row["trust"];

	    if ($exclude_leader && $leader->uid_idx() == $uid_idx)
		continue;

Leigh Stoller's avatar
Leigh Stoller committed
888
	    if (! ($user = User::Lookup($uid_idx))) {
889 890 891 892 893 894
		TBERROR("Group::MemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
	    # So caller can get this.
	    $user->SetTempData($trust);
	    
Leigh Stoller's avatar
Leigh Stoller committed
895
	    $result["$uid_idx"] = $user;
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
	}
	return $result;
    }

    #
    # Grab the user list from the project. These are the people who can be
    # added. Do not include people in the above list, obviously! Do not
    # include members that have not been approved to main group either! This
    # will force them to go through the approval page first.
    #
    function NonMemberList() {
	$gid_idx    = $this->gid_idx();
	$pid_idx    = $this->pid_idx();
	$trust_none = TBDB_TRUSTSTRING_NONE;
	$result     = array();
	
	$query_result =
	    DBQueryFatal("select m.uid_idx from group_membership as m ".
			 "left join group_membership as a on ".
			 "     a.uid_idx=m.uid_idx and ".
			 "     a.pid_idx=m.pid_idx and a.gid_idx='$gid_idx' ".
			 "where m.pid_idx='$pid_idx' and ".
			 "      m.gid_idx=m.pid_idx and a.uid_idx is NULL ".
Leigh Stoller's avatar
Leigh Stoller committed
919 920
			 "      and m.trust!='$trust_none' ".
			 "order by m.uid");
921 922 923 924

	while ($row = mysql_fetch_array($query_result)) {
	    $uid_idx = $row["uid_idx"];

925
	    if (! ($user = User::Lookup($uid_idx))) {
926 927 928
		TBERROR("Group::NonMemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
929
	    $result[] = $user;
930 931 932 933
	}
	return $result;
    }

934 935 936 937
    #
    # Change the leader for a group.
    #
    function ChangeLeader($leader) {
938 939 940
	$idx     = $this->gid_idx();
	$uid     = $leader->uid();
	$uid_idx = $leader->uid_idx();
941

942
	DBQueryFatal("update groups set leader='$uid',leader_idx='$uid_idx' ".
943 944 945
		     "where gid_idx='$idx'");

	$this->group["leader"] = $uid;
946
	$this->group["leader_idx"] = $uid_idx;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	return 0;
    }

    #
    # Trust consistency.
    #
    function CheckTrustConsistency($user, $newtrust, $fail) {
	global $TBDB_TRUST_USER;
	
	$uid = $user->uid();
	$pid = $this->pid();
	$gid = $this->gid();
	$uid_idx = $user->uid_idx();
	$pid_idx = $this->pid_idx();
	$gid_idx = $this->gid_idx();
	$trust_none = TBDB_TRUSTSTRING_NONE;
963
	$project = $this->Project();
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
	
        # 
        # set $newtrustisroot to 1 if attempting to set a rootful trust,
        # 0 otherwise.
        #
	$newtrustisroot = TBTrustConvert($newtrust) > $TBDB_TRUST_USER ? 1 : 0;

        #
        # If changing subgroup trust level, then compare levels.
        # A user may not have root privs in the project and user privs
        # in the subgroup; it makes no sense to do that and can violate trust.
        #
	if ($pid_idx != $gid_idx) {
            #
            # Setting non-default "sub"group.
	    # Verify that if user has root in project,
	    # we are setting a rootful trust for him in 
	    # the subgroup as well.
	    #
	    $projtrustisroot =
984
		($project->UserTrust($user) > $TBDB_TRUST_USER ? 1 : 0);
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

	    if ($projtrustisroot > $newtrustisroot) {
		if (!$fail)
		    return 0;
		
		TBERROR("User $uid may not have a root trust level in ".
			"the default group of $pid, ".
			"yet be non-root in subgroup $gid!", 1);
	    }
	}
	else {
            #
	    # Setting default group.
	    # Do not verify anything (yet.)
	    #
	    $projtrustisroot = $newtrustisroot;
	}

        #
        # Get all the subgroups not equal to the subgroup being changed.
        # 
	$query_result =
	    DBQueryFatal("select trust,gid from group_membership ".
			 "where uid_idx='$uid_idx' and ".
			 "      pid_idx='$pid_idx' and ".
			 "      gid_idx!=pid_idx and ".
			 "      gid_idx!='$gid_idx' and ".
			 "      trust!='$trust_none'");

	while ($row = mysql_fetch_array($query_result)) {
	    $grptrust = $row[0];
	    $ogid     = $row[1];
	
  	    # 
	    # Get what the users trust level is in the 
	    # current subgroup we are looking at.
	    #
	    $grptrustisroot = 
		TBTrustConvert($grptrust) > $TBDB_TRUST_USER ? 1 : 0;

 	    #
	    # If users trust level is higher in the default group than in the
	    # subgroup we are looking at, this is wrong.
 	    #
	    if ($projtrustisroot > $grptrustisroot) {
	        if (!$fail)
		    return 0;

		TBERROR("User $uid may not have a root trust level in ".
			"the default group of $pid, ".
			"yet be non-root in subgroup $ogid!", 1);
	    }

	    if ($pid_idx != $gid_idx) {
                #
	        # Iff we are modifying a subgroup, 
	        # Make sure that the trust we are setting is as
	        # rootful as the trust we already have set in
	        # every other subgroup.
	        # 
		if ($newtrustisroot != $grptrustisroot) { 
		    if (!$fail)
			return 0;
		    
		    TBERROR("User $uid may not mix root and ".
			    "non-root trust levels in ".
			    "different subgroups of $pid!", 1);
		}
	    }
	}
	return 1;
    }

    #
1059
    # Hmm, this is really a group_membership query. Needs different treatment.
1060
    #
1061
    function MemberShipInfo($user) {
1062 1063 1064 1065
	$uid_idx = $user->uid_idx();
	$gid_idx = $this->gid_idx();

	$query_result =
1066
	    DBQueryFatal("select trust,date_applied,date_approved,date_nagged ".
1067 1068 1069 1070 1071 1072 1073 1074
			 "  from group_membership ".
			 "where uid_idx='$uid_idx' and gid_idx='$gid_idx'");

	if (! mysql_num_rows($query_result)) {
	    TBERROR("Group::MemberShipInfo: ".
		    "Lookup failed for $uid_idx/$gid_idx", 1);
	}
	$row      = mysql_fetch_row($query_result);
1075 1076 1077 1078 1079
        $result   = array("trust"         => $row[0],
                          "date_applied"  => $row[1],
                          "date_approved" => $row[2],
                          "date_nagged"   => $row[3]);
	return $result;
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    }

    #
    # Return mail addresses for project_root and group_root people.
    #
    function LeaderMailList() {
	$gid_idx = $this->gid_idx();
	$pid_idx = $this->pid_idx();

	# Constants.
	$trust_group  = TBDB_TRUSTSTRING_GROUPROOT;
	$trust_project= TBDB_TRUSTSTRING_PROJROOT;

	$query_result =
1094
	    DBQueryFatal("select distinct usr_name,u.uid,usr_email,trust ".
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
			 "   from users as u ".
			 "left join group_membership as gm on ".
			 "     gm.uid_idx=u.uid_idx ".
			 "where (trust='$trust_project' and ".
			 "       pid_idx='$pid_idx') or ".
			 "      (trust='$trust_group' and ".
			 "       pid_idx='$pid_idx' and gid_idx='$gid_idx') ".
			 "order by trust DESC, usr_name");
  
	if (mysql_num_rows($query_result) == 0) {
	    return "";
	}
	
	$mailstr="";
	while ($row = mysql_fetch_array($query_result)) {
	    if ($mailstr != "")
		$mailstr .= ", ";
	    
1113 1114
	    $mailstr .= '"' . $row["usr_name"] . " (". $row["uid"] . ")\" <" .
		$row["usr_email"] . ">";
1115
	}
1116
	return $mailstr;
1117
    }
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

    function Show() {
	global $OURDOMAIN;
	global $MAILMANSUPPORT;

	echo "<center>
               <h3>Group Profile</h3>
              </center>
              <table align=center border=1>\n";

	$pid         = $this->pid();
	$gid         = $this->gid();
	$gid_idx     = $this->gid_idx();
	$created     = $this->created();
	$leader	     = $this->leader();
	$description = $this->description();
	$expt_count  = $this->expt_count();
	$expt_last   = $this->expt_last();
	$unix_gid    = $this->unix_gid();
	$unix_name   = $this->unix_name();
1138
	$uuid        = $this->uuid();
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

	if ($this->IsProjectGroup()) 
	    $mail = "$pid" . "-users@" . $OURDOMAIN;
	else
	    $mail = "$pid-$gid" . "-users@" . $OURDOMAIN;

	if (!$expt_last) {
	    $expt_last = "&nbsp;";
	}

	if (! ($leader_user = $this->GetLeader())) {
	    TBERROR("Could not lookup object for user $leader", 1);
	}
	$showuser_url = CreateURL("showuser", $leader_user);

	echo "<tr>
                  <td>GID: </td>
                  <td class=\"left\">
                   <a href='showgroup.php3?pid=$pid&gid=$gid'>$gid ($gid_idx)".
	          "</a></td>
              </tr>\n";
    
	echo "<tr>
                  <td>PID: </td>
                  <td class=\"left\">
                    <a href='showproject.php3?pid=$pid'>$pid</a></td>
              </tr>\n";
    
	echo "<tr>
                  <td>Description: </td>
                  <td class=\"left\">$description</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Unix GID: </td>
                  <td class=\"left\">$unix_gid</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Unix Group Name: </td>
                  <td class=\"left\">$unix_name</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Group Leader: </td>
                  <td class=\"left\">
                     <a href='$showuser_url'>$leader</a></td>
              </tr>\n";
    
	if ($MAILMANSUPPORT) {
	    $mmurl   = "gotommlist.php3?pid=$pid&gid=$gid";

	    echo "<tr>
                      <td>Email List:</td>
                      <td class=\"left\">
                          <a href='$mmurl'>$mail</a> ";

	    if (ISADMIN()) {
		$mmurl .= "&wantadmin=1";
		echo "<a href='$mmurl'>(admin page)</a>";
	    }
	    echo "    </td>
                  </tr>\n";
	}
	else {
	    echo "<tr>
                      <td>Email List: </td>
                      <td class=\"left\">$mail</td>
                  </tr>\n";
	}

1210 1211 1212 1213 1214 1215 1216
	if (ISADMIN()) {
	    echo "<tr>
                    <td>UUID: </td>
                    <td class=left>$uuid</td>
                  </tr>\n";
	}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	echo "<tr>
                  <td>Created: </td>
                  <td class=\"left\">$created</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Experiments Created:</td>
                  <td class=\"left\">$expt_count</td>
              </tr>\n";
    
	echo "<tr>
                  <td>Date of last experiment:</td>
                  <td class=\"left\">$expt_last</td>
              </tr>\n";
    
	echo "</table>\n";
    }

    #
    # Pretty display of group members.
    #
1238
    function ShowMembers($this_user = null) {
1239 1240
	$gid_idx = $this->gid_idx();
	$pid_idx = $this->pid_idx();
1241
	$project = $this->Project();
1242
        global $TB_PROJECT_DELUSER;
1243 1244 1245 1246 1247 1248 1249 1250

	$query_result =
	    DBQueryFatal("select uid_idx,trust from group_membership ".
			 "where pid_idx='$pid_idx' and gid_idx='$gid_idx'");
    
	if (! mysql_num_rows($query_result)) {
	    return;
	}
1251 1252 1253
	$showdel =
            ($pid_idx == $gid_idx && $this_user &&
             $this->AccessCheck($this_user, $TB_PROJECT_DELUSER) ? 1 : 0);
1254
	$projgrp  = $this->IsProjectGroup();
1255 1256 1257 1258 1259 1260 1261 1262

	echo "<center>\n";
	if ($projgrp)
	    echo "<h3>Project Members</h3>\n";
	else
	    echo "<h3>Group Members</h3>\n";

	echo "</center>
1263 1264
              <table align=center border=1 cellpadding=1
                     id='userlist' cellspacing=2>\n";
1265

1266
	echo "<thead class='sort'>";
1267 1268
	echo "<tr>
                  <th>Name</th>\n";
1269
	echo "<th>Affiliation</th>\n";
1270 1271 1272 1273 1274 1275 1276 1277
	if (! $projgrp) {
	    echo "<th>Email</th>\n";
	}
	echo "    <th>UID</th>
                  <th>Privs</th>\n";
	if ($showdel) {
	    echo "<th>Remove</th>\n";
	}
1278
	echo "</tr></thead>\n";
1279 1280 1281 1282 1283 1284

	while ($row = mysql_fetch_array($query_result)) {
	    $uid_idx = $row["uid_idx"];
	    $trust   = $row["trust"];

	    if (! ($target_user = User::Lookup($uid_idx))) {
1285
		TBERROR("Could not lookup object for user $uid_idx", 1);
1286 1287
	    }
	    $usr_name     = $target_user->name();
1288
	    $usr_affil    = $target_user->affil_abbrev();
1289 1290 1291
	    $usr_email    = $target_user->email();
	    $usr_uid      = $target_user->uid();
	    $showuser_url = CreateURL("showuser", $target_user);
1292
	    $deluser_url  = CreateURL("deleteuser", $target_user, $project);
1293 1294 1295 1296

	    echo "<tr>
                      <td>$usr_name</td>\n";

1297 1298
	    echo "<td>$usr_affil</td>\n";

1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	    if (! $projgrp) {
		echo "<td>$usr_email</td>\n";
	    }
	    echo "    <td>
                        <a href='$showuser_url'>$usr_uid</a>
                      </td>\n";

	    if ($trust == TBDB_TRUSTSTRING_NONE) 
		echo "<td><font color=red>$trust</font></td>\n";
	    else
		echo "<td>$trust</td>\n";

	    if ($showdel) {
1312 1313
                if ($this->CanDeleteUser($this_user, $target_user)) {
                    echo "<td align=center>
1314 1315
		          <a href='$deluser_url'>
                             <img alt='Delete User' src=redball.gif></td>\n";
1316 1317 1318 1319
                }
                else {
                    echo "<td></td>\n";
                }
1320 1321 1322 1323
	    }
	    echo "</tr>\n";
	}
	echo "</table>\n";
1324 1325 1326
        echo "<script type='text/javascript' language='javascript'>
	       sorttable.makeSortable(getObjbyName('userlist'));
             </script>\n";
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
    }

    function ShowStats() {
	$gid_idx = $this->gid_idx();

	$query_result =
	    DBQueryFatal("SELECT * from group_stats ".
			 "where gid_idx='$gid_idx'");

	if (! mysql_num_rows($query_result)) {
	    return;
	}
	$row = mysql_fetch_assoc($query_result);

        #
        # Not pretty printed yet.
        #
	echo "<table align=center border=1>\n";
    
	foreach($row as $key => $value) {
	    echo "<tr>
                      <td>$key:</td>
                      <td>$value</td>
                  </tr>\n";
	}
	echo "</table>\n";
    }
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

    #
    # Return list of experiments for a group, or just a count.
    #
    function ExperimentList($listify = 1) {
	$pid = $this->pid();
	$gid = $this->gid();

	$query_result =
	    DBQueryFatal("select idx from experiments ".
			 "where pid='$pid' and gid='$gid'");

	if (! $listify) {
	    return mysql_num_rows($query_result);
	}

	# Else, create a list of the groups.
	$result  = array();

	while ($row = mysql_fetch_array($query_result)) {
	    $idx = $row["idx"];

	    if (! ($experiment = Experiment::Lookup($idx))) {
		TBERROR("Group::ExperimentList: ".
			"Could not load experiment $idx!", 1);
	    }
	    $result[] = $experiment;
	}
	return $result;
    }
1384
}
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

#
# Until all the code is fixed, this is a global function that loads the
# group object and passes the call off.
#
# Determine the trust level for a uid/pid/gid. That is, each uid will have
# a different trust level depending on the project/group in question.
# Return that trust level as one of the numeric values above. 
# 
function TBGrpTrust($uid, $pid, $gid)
{
    #
    # No group, then use the default group.
    #
    if (! $gid) {
	$gid = $pid;
    }

    if (! ($group = Group::LookupByPidGid($pid, $gid))) {
        TBERROR("TBGrpTrust: Could not look up group object for $pid/$eid!",1);
    }
    
    if (! ($user = User::Lookup($uid))) {
        TBERROR("TBGrpTrust: Could not look up user object for $uid!", 1);
    }
    return $group->UserTrust($user);
}
1412
?>