group_defs.php 34.7 KB
Newer Older
1
2
<?php
#
3
# Copyright (c) 2006-2015 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
357
358
359
360
	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) {
361
362
363
364
365
366
367
            if ($access_type == $TB_PROJECT_DELGROUP) {
                $project = $this->Project();
                $leader  = $this->GetLeader();
		if ($user->SameUser($leader)) {
                    return 1;
                }
            }
368
369
370
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
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) {
	    $mintrust = $TBDB_TRUST_PROJROOT;
	}
	else {
	    TBERROR("Unexpected access type: $access_type!", 1);
	}

	return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust);
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    }

    #
    # 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);
472
	$trust_string = $row["trust"];
473
474
475
476

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

478
479
480
481
482
483
484
485
    # 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"); }
486
    function uuid()             { return $this->field("gid_uuid"); }
487
    function leader()           { return $this->field("leader"); }
488
    function leader_idx()       { return $this->field("leader_idx"); }
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    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;

506
	$starting_gid = $MIN_UNIX_GID;
507
508
	$ending_gid   = 50000;

509
510
511
512
513
514
515
516
517
518
519
        #
        # 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);
	}

520
521
522
523
524
525
526
527
528
529
530
531
	# 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();
	}
532
	$uuid = NewUUID();
533
534
535
536
537
538
539
540
	
	# 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 ".
541
			"where g.unix_gid>=$starting_gid and ".
542
			"      g.unix_gid<$ending_gid and ".
543
544
545
546
547
548
549
550
551
552
553
554
			"      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() . "'," .
555
			 " leader_idx='" . $leader->uid_idx() . "'," .
556
557
558
			 " created=now(), ".
			 " description='$description', ".
			 " unix_name='$unix_name', ".
559
			 " gid_uuid='$uuid', ".
560
561
562
563
564
565
			 " gid_idx=$gid_idx, ".
			 " pid_idx=$pid_idx, ".
			 " unix_gid=$unix_gid")) {
	    return null;
	}

566
567
568
569
	if (! DBQueryWarn("insert into group_stats ".
			  "  (pid,gid,gid_idx,pid_idx,gid_uuid) ".
			  "values ('$pid', '$gid', $gid_idx, ".
			  "        $pid_idx, '$uuid')")) {
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
	    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.
    #
594
    function Initialize($uid) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
595
596
	global $TBOPSPID;
	
597
598
599
600
601
602
	$emulabgroup = Group::LookupByPidGid($TBOPSPID, $TBOPSPID);

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

603
	$user = User::Lookup($uid);
604
605
606
607
608

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

609
610
	if (!$emulabgroup->IsMember($user, $ignore) &&
	    $emulabgroup->AddNewMember($user) < 0) {
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
638
639
640
641
642
	    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.
    #
    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;
    }

643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    #
    # 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;
    }

661
662
663
664
    #
    # Notify leaders of new (and verified) group member.
    #
    function NewMemberNotify($user) {
665
	global $TBWWW, $APTBASE,
666
	       $TBMAIL_APPROVAL, $TBMAIL_AUDIT, $TBMAIL_WWW, $TBMAIL_NOREPLY;
667
668
669
670
671
672
	
	if (! $this->project) {
	    $this->LoadProject();
	}
	$project	= $this->project;
	$pid            = $project->pid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
673
	$gid            = $this->gid();
674
	$leader		= $project->GetLeader();
675
676
677
	$leader_name	= $leader->name();
	$leader_email	= $leader->email();
	$leader_uid	= $leader->uid();
678
	$allleaders	= $this->LeaderMailList();
679
680
681
682
683
684
685
686
687
688
689
690
691
	$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();
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
	$url            = $project->wwwBase();
	
	if ($project->isAPT() || $project->isCloud()) {
	    $url .= "/approveuser.php?uid=$joining_uid&pid=$pid";
	    $message =
		"You can approve or reject this user:\n\n".
		"Approve:  ${url}&action=approve\n".
		"or\n".
		"Deny:     ${url}&action=deny\n";
	    $from = $project->ApprovalEmailAddress();
	}
	else {
	    $message =
		"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";
	    $from = "$usr_name '$joining_uid' <$usr_email>";
	}
	TBMAIL("$leader_name '$leader_uid' <$leader_email>",
711
712
713
714
715
716
	   "$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".
717
718
	   "Email:           $usr_email\n" .
	   "Affiliation:     $usr_affil\n".
719
720
721
	   "User URL:        $usr_URL\n".
	   "Job Title:       $usr_title\n".
	   "Address 1:       $usr_addr\n".
722
	   "Address 2:       $usr_addr2\n" .
723
724
725
726
727
728
	   "City:            $usr_city\n".
	   "State:           $usr_state\n".
	   "ZIP/Postal Code: $usr_zip\n".
	   "Country:         $usr_country\n".
	   "Phone:           $usr_phone\n".
	   "\n".
729
730
	   $message .
	   "\n".
731
	   "Thanks!\n",
732
           "From: $from\n".
733
734
735
	   "Cc: $allleaders\n".
	   "Bcc: $TBMAIL_AUDIT\n".
	   "Errors-To: $TBMAIL_WWW");
736
737
738
739

	return 0;
    }

740
741
742
    #
    # Check if user is a member of this group.
    #
743
744
745
    function IsMember($user, &$approved) {
	global $TBDB_TRUST_USER;
	
746
747
748
749
750
751
752
753
754
	$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'");

755
756
757
758
759
760
761
762
763
764
765
766
	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;
    }

767
768
769
    #
    # Return list of approved members in this group. 
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
770
    function &MemberList($exclude_leader = 1) {
771
772
773
774
775
776
777
778
779
780
781
782
783
	$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 ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
784
785
			 "      m.trust!='$trust_none' ".
			 "order by m.uid");
786
787
788
789
790
791
792
793

	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
794
	    if (! ($user = User::Lookup($uid_idx))) {
795
796
797
798
799
800
		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
801
	    $result["$uid_idx"] = $user;
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
	}
	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 B. Stoller's avatar
Leigh B. Stoller committed
825
826
			 "      and m.trust!='$trust_none' ".
			 "order by m.uid");
827
828
829
830

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

831
	    if (! ($user = User::Lookup($uid_idx))) {
832
833
834
		TBERROR("Group::NonMemberList: ".
			"Could not load user $uid_idx!", 1);
	    }
835
	    $result[] = $user;
836
837
838
839
	}
	return $result;
    }

840
841
842
843
    #
    # Change the leader for a group.
    #
    function ChangeLeader($leader) {
844
845
846
	$idx     = $this->gid_idx();
	$uid     = $leader->uid();
	$uid_idx = $leader->uid_idx();
847

848
	DBQueryFatal("update groups set leader='$uid',leader_idx='$uid_idx' ".
849
850
851
		     "where gid_idx='$idx'");

	$this->group["leader"] = $uid;
852
	$this->group["leader_idx"] = $uid_idx;
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
	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;
869
	$project = $this->Project();
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
	
        # 
        # 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 =
890
		($project->UserTrust($user) > $TBDB_TRUST_USER ? 1 : 0);
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
937
938
939
940
941
942
943
944
945
946
947
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
981
982
983
984
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

	    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 .= ", ";
	    
1019
1020
	    $mailstr .= '"' . $row["usr_name"] . " (". $row["uid"] . ")\" <" .
		$row["usr_email"] . ">";
1021
	}
1022
	return $mailstr;
1023
    }
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043

    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();
1044
	$uuid        = $this->uuid();
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115

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

1116
1117
1118
1119
1120
1121
1122
	if (ISADMIN()) {
	    echo "<tr>
                    <td>UUID: </td>
                    <td class=left>$uuid</td>
                  </tr>\n";
	}

1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
	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();
1147
	$project = $this->Project();
1148
1149
1150
1151
1152
1153
1154
1155
1156

	$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);
1157
	$projgrp  = $this->IsProjectGroup();
1158
1159
1160
1161
1162
1163
1164
1165

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

	echo "</center>
1166
1167
              <table align=center border=1 cellpadding=1
                     id='userlist' cellspacing=2>\n";
1168

1169
	echo "<thead class='sort'>";
1170
1171
	echo "<tr>
                  <th>Name</th>\n";
1172
	echo "<th>Affiliation</th>\n";
1173
1174
1175
1176
1177
1178
1179
1180
	if (! $projgrp) {
	    echo "<th>Email</th>\n";
	}
	echo "    <th>UID</th>
                  <th>Privs</th>\n";
	if ($showdel) {
	    echo "<th>Remove</th>\n";
	}
1181
	echo "</tr></thead>\n";
1182
1183
1184
1185
1186
1187

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

	    if (! ($target_user = User::Lookup($uid_idx))) {
1188
		TBERROR("Could not lookup object for user $uid_idx", 1);
1189
1190
	    }
	    $usr_name     = $target_user->name();
1191
	    $usr_affil    = $target_user->affil_abbrev();
1192
1193
1194
	    $usr_email    = $target_user->email();
	    $usr_uid      = $target_user->uid();
	    $showuser_url = CreateURL("showuser", $target_user);
1195
	    $deluser_url  = CreateURL("deleteuser", $target_user, $project);
1196
1197
1198
1199

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

1200
1201
	    echo "<td>$usr_affil</td>\n";

1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
	    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'>
                             <img alt='Delete User' src=redball.gif></td>\n";
	    }
	    echo "</tr>\n";
	}
	echo "</table>\n";
1222
1223
1224
        echo "<script type='text/javascript' language='javascript'>
	       sorttable.makeSortable(getObjbyName('userlist'));
             </script>\n";
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
    }

    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";
    }
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281

    #
    # 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;
    }
1282
}
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309

#
# 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);
}
1310
?>