group_defs.php 28.3 KB
Newer Older
1
2
3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5
6
# All rights reserved.
#
7
8
9
10
11
#
# A cache of groups to avoid lookups. Indexed by gid_idx;
#
$group_cache = array();

12
13
class Group
{
14
    var	$group;
15
    var $project;
16

17
18
19
20
21
22
23
24
25
26
27
28
29
30
    #
    # 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;
	}
31
	$this->group   = mysql_fetch_array($query_result);
32
	$this->project =  null;
33
34
35
36
37
38
39
40
41
    }

    # 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) {
42
43
44
45
46
47
	global $group_cache;

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

50
51
52
53
54
55
	if (! $foo->IsValid())
	    return null;

	# Insert into cache.
	$group_cache["$gid_idx"] = $foo;
	return $foo;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    }

    # 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'];

73
	return Group::Lookup($idx);	
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    }
    
    #
    # 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;
	}
92
	$this->group =& mysql_fetch_array($query_result);
93
94
95
	return 0;
    }

96
97
98
99
100
101
102
103
104
105
106
107
    #
    # 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;
    }
108
109
110
111
112
113
114
115
116
117
118
    function Project() {
	if (! $this->project) {
	    $this->LoadProject();
	}
	return $this->project;
    }
    
    #
    # Return user object for leader.
    #
    function GetLeader() {
119
	$leader_idx = $this->leader_idx();
120

121
122
	if (! ($leader = User::Lookup($leader_idx))) {
	    TBERROR("Could not find user object for $leader_idx", 1);
123
124
125
126
	}
	return $leader;
    }

127
128
129
130
131
132
133
    #
    # Is this group the default project group. Returns boolean.
    #
    function IsProjectGroup() {
	return $this->pid_idx() == $this->gid_idx();
    }

134
    #
135
    # Project permission checks.
136
    #
137
138
139
    #	returns 0 if not allowed.
    #   returns 1 if allowed.
    # 
140
    function AccessCheck($user, $access_type) {
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
	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) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	elseif ($access_type == $TB_PROJECT_MAKEGROUP ||
		$access_type == $TB_PROJECT_DELGROUP) {
	    $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) {
	    $mintrust = $TBDB_TRUST_PROJROOT;
	}
	else {
	    TBERROR("Unexpected access type: $access_type!", 1);
	}

	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    }

    #
    # 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);
289
	$trust_string = $row["trust"];
290
291
292
293

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

295
296
297
298
299
300
301
302
303
    # 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"); }
    function leader()           { return $this->field("leader"); }
304
    function leader_idx()       { return $this->field("leader_idx"); }
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    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;

322
	$starting_gid = $MIN_UNIX_GID;
323
324
	$ending_gid   = 50000;

325
326
327
328
329
330
331
332
333
334
335
        #
        # 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);
	}

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
	# 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();
	}
	
	# 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 ".
356
			"where g.unix_gid>=$starting_gid and ".
357
			"      g.unix_gid<$ending_gid and ".
358
359
360
361
362
363
364
365
366
367
368
369
			"      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() . "'," .
370
			 " leader_idx='" . $leader->uid_idx() . "'," .
371
372
373
374
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
			 " created=now(), ".
			 " description='$description', ".
			 " unix_name='$unix_name', ".
			 " gid_idx=$gid_idx, ".
			 " pid_idx=$pid_idx, ".
			 " unix_gid=$unix_gid")) {
	    return null;
	}

	if (! DBQueryWarn("insert into group_stats (pid, gid, gid_idx) ".
			  "values ('$pid', '$gid', $gid_idx)")) {
	    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.
    #
    function Initialize($uid, $pid) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
407
408
	global $TBOPSPID;
	
409
410
411
412
413
414
	$emulabgroup = Group::LookupByPidGid($TBOPSPID, $TBOPSPID);

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

415
	$user = User::Lookup($uid);
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
448
449
450
451
452
453
454
455
456
457
458

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

	if ($emulabgroup->AddNewMember($user) < 0) {
	    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'");

	DBQueryFatal("update group_membership set date_approved=now(), ".
		     "  trust='" . TBDB_TRUSTSTRING_PROJROOT . "' ".
		     "where uid='$uid' and pid='$pid'");

	return 0;
    }

    #
    # Add *new* member to group; starts out with trust=none.
    #
    function AddNewMember($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();
	    
	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', ".
			 "        'none', now())")) {
	    return -1;
	}
	return 0;
    }

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
    #
    # 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;
    }

477
478
479
480
481
482
483
484
485
486
487
    #
    # Notify leaders of new (and verified) group member.
    #
    function NewMemberNotify($user) {
	global $TBWWW, $TBMAIL_APPROVAL, $TBMAIL_AUDIT, $TBMAIL_WWW;
	
	if (! $this->project) {
	    $this->LoadProject();
	}
	$project	= $this->project;
	$pid            = $project->pid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
488
	$gid            = $this->gid();
489
	$leader		= $project->GetLeader();
490
491
492
	$leader_name	= $leader->name();
	$leader_email	= $leader->email();
	$leader_uid	= $leader->uid();
493
	$allleaders	= $this->LeaderMailList();
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
	$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();
	
	TBMAIL("$leader_name '$leader_uid' <$leader_email>",
	   "$joining_uid $pid Project Join Request",
	   "$usr_name is trying to join your group $gid in project $pid.\n".
	   "\n".
	   "Contact Info:\n".
	   "Name:            $usr_name\n".
	   "Emulab ID:       $joining_uid\n".
	   "Email:           $usr_email\n".
	   "User URL:        $usr_URL\n".
	   "Job Title:       $usr_title\n".
	   "Affiliation:     $usr_affil\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".
	   "\n".
	   "Please return to $TBWWW,\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".
	   "Thanks,\n".
	   "Testbed Operations\n",
	   "From: $TBMAIL_APPROVAL\n".
	   "Cc: $allleaders\n".
	   "Bcc: $TBMAIL_AUDIT\n".
	   "Errors-To: $TBMAIL_WWW");

	return 0;
    }

540
541
542
    #
    # Check if user is a member of this group.
    #
543
544
545
    function IsMember($user, &$approved) {
	global $TBDB_TRUST_USER;
	
546
547
548
549
550
551
552
553
554
	$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'");

555
556
557
558
559
560
561
562
563
564
565
566
	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;
    }

567
568
569
    #
    # Return list of approved members in this group. 
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
570
    function &MemberList($exclude_leader = 1) {
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
	$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 ".
			 "      m.gid_idx='$gid_idx' and ".
			 "      m.trust!='$trust_none'");

	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 B. Stoller's avatar
Leigh B. Stoller committed
593
	    if (! ($user = User::Lookup($uid_idx))) {
594
595
596
597
598
599
		TBERROR("Group::MemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
	    # So caller can get this.
	    $user->SetTempData($trust);
	    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
600
	    $result["$uid_idx"] = $user;
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
	}
	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 ".
			 "      and m.trust!='$trust_none'");

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

	    if (! ($user =& User::Lookup($uid_idx))) {
		TBERROR("Group::NonMemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
	    $result[] =& $user;
	}
	return $result;
    }

638
639
640
641
    #
    # Change the leader for a group.
    #
    function ChangeLeader($leader) {
642
643
644
	$idx     = $this->gid_idx();
	$uid     = $leader->uid();
	$uid_idx = $leader->uid_idx();
645

646
	DBQueryFatal("update groups set leader='$uid',leader_idx='$uid_idx' ".
647
648
649
		     "where gid_idx='$idx'");

	$this->group["leader"] = $uid;
650
	$this->group["leader_idx"] = $uid_idx;
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
	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;
667
	$project = $this->Project();
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
	
        # 
        # 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 =
688
		($project->UserTrust($user) > $TBDB_TRUST_USER ? 1 : 0);
689
690
691
692
693
694
695
696
697
698
699
700
701
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816

	    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;
    }

    #
    # Hmm, this is really a grooup_membership query. Needs different treatment.
    #
    function MemberShipInfo($user, &$trust, &$date_applied, &$date_approved) {
	$uid_idx = $user->uid_idx();
	$gid_idx = $this->gid_idx();

	$query_result =
	    DBQueryFatal("select trust,date_applied,date_approved ".
			 "  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);
	$trust    = $row[0];
	$date_applied  = $row[1];
	$date_approved = $row[2];
	
	return 0;
    }

    #
    # 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 =
	    DBQueryFatal("select distinct usr_name,u.uid,usr_email ".
			 "   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 .= ", ";
	    
817
818
	    $mailstr .= '"' . $row["usr_name"] . " (". $row["uid"] . ")\" <" .
		$row["usr_email"] . ">";
819
	}
820
	return $mailstr;
821
    }
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936

    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();

	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";
	}

	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.
    #
    function ShowMembers($prived = 0) {
	$gid_idx = $this->gid_idx();
	$pid_idx = $this->pid_idx();
937
	$project = $this->Project();
938
939
940
941
942
943
944
945
946

	$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;
	}
	$showdel  = (($prived && $pid_idx == $gid_idx) ? 1 : 0);
947
	$projgrp  = $this->IsProjectGroup();
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980

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

	echo "</center>
              <table align=center border=1 cellpadding=1 cellspacing=2>\n";

	echo "<tr>
                  <th>Name</th>\n";
	if (! $projgrp) {
	    echo "<th>Email</th>\n";
	}
	echo "    <th>UID</th>
                  <th>Privs</th>\n";
	if ($showdel) {
	    echo "<th>Remove</th>\n";
	}
	echo "</tr>\n";

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

	    if (! ($target_user = User::Lookup($uid_idx))) {
		TBERROR("Could not lookup object for user $target_uid", 1);
	    }
	    $usr_name     = $target_user->name();
	    $usr_email    = $target_user->email();
	    $usr_uid      = $target_user->uid();
	    $showuser_url = CreateURL("showuser", $target_user);
981
	    $deluser_url  = CreateURL("deleteuser", $target_user, $project);
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

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

	    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) {
		echo "<td align=center>
		          <a href='$deluser_url'>