editgroup.php3 8.42 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
<?php
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
include("defs.php3");
include("showstuff.php3");

#
# Standard Testbed Header
#
PAGEHEADER("Edit Group Membership");

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

21 22
ignore_user_abort(1);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
23 24 25 26 27 28 29 30 31 32 33 34 35
#
# First off, sanity check page args.
#
if (!isset($pid) ||
    strcmp($pid, "") == 0) {
    USERERROR("Must provide a Project ID!", 1);
}
if (!isset($gid) ||
    strcmp($gid, "") == 0) {
    USERERROR("Must privide a Group ID!", 1);
}

#
36
# The default group membership cannot be changed, but the trust levels can.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
#
38
$defaultgroup = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39
if (strcmp($gid, $pid) == 0) {
40
    $defaultgroup = 1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42 43 44 45
}

#
# Verify permission. 
#
46
if (! TBProjAccessCheck($uid, $pid, $gid, $TB_PROJECT_EDITGROUP)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47 48 49 50 51 52
    USERERROR("You do not have permission to edit group $gid in ".
	      "project $pid!", 1);
}

#
# Grab the current user list for the group. The group leader cannot be
53 54 55
# removed! Neither can the project leader. Do not include members that
# have not been approved to main group either! This will force them to
# go through the approval page first.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56 57 58 59 60 61
#
$curmembers_result =
    DBQueryFatal("select distinct m.uid from group_membership as m ".
		 "left join groups as g on g.pid=m.pid and g.gid=m.gid ".
		 "left join projects as p on p.pid=m.pid ".
		 "where m.pid='$pid' and m.gid='$gid' and ".
62 63
		 "      m.uid!=g.leader and m.uid!=p.head_uid ".
		 "      and m.trust!='none'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
64 65 66

#
# Grab the user list from the project. These are the people who can be
67 68 69
# 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.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70 71
# 
$nonmembers_result =
72
    DBQueryFatal("select m.uid from group_membership as m ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
73 74
		 "left join group_membership as a on ".
		 "     a.uid=m.uid and a.pid=m.pid and a.gid='$gid' ".
75 76
		 "where m.pid='$pid' and m.gid=m.pid and a.uid is NULL ".
		 "      and m.trust!='none'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
function TBCheckTrustConsistency($user, $pid, $gid, $newtrust)
{
    #
    # If changing default group trust level, then compare levels.
    # A user may not have greater permission in the default group than
    # in a subgroup.
    #
    if (strcmp($pid, $gid)) {
	$projtrust = TBProjTrust($user, $pid);
	if ($projtrust > TBTrustConvert($newtrust)) {
	    USERERROR("User $user may not have a higher trust level in ".
		      "the default group of $pid, than in subgroup $gid!", 1);
	}
    }
    else
	$projtrust = TBTrustConvert($newtrust);

    #
    # Get all the subgroups not equal to the subgroup being changed.
    # 
    $query_result =
	DBQueryFatal("select trust,gid from group_membership ".
		     "where uid='$user' and pid='$pid' and trust!='none' ".
		     " and gid!=pid and gid!='$gid'");

    while ($row = mysql_fetch_array($query_result)) {
	$grptrust = $row[0];
	$ogid     = $row[1];

	if ($projtrust > TBTrustConvert($grptrust)) {
	    USERERROR("User $user may not have a higher trust level in ".
		      "the default group of $pid, than in subgroup $ogid!", 1);
	}

	if (strcmp($pid, $gid)) {
            #
            # Check to make sure new trust is same as all other subgroup trust.
            # 
	    if (strcmp($newtrust, $grptrust)) {
		USERERROR("User $user may not have different trust levels in ".
			  "different subgroups of $pid!", 1);
	    }
	}
    }
    return 1;
}

#
# First pass does checks. Second pass does the real thing. 
#

Leigh B. Stoller's avatar
Leigh B. Stoller committed
129 130 131
#
# Go through the list of current members. For each one, check to see if
# the checkbox for that person was checked. If not, delete the person
132 133
# from the group membership. Otherwise, look to see if the trust level
# has been changed.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134 135 136
# 
if (mysql_num_rows($curmembers_result)) {
    while ($row = mysql_fetch_array($curmembers_result)) {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	$user = $row[0];
	$foo  = "change_$user";

	#
	# Is member to be deleted?
	# 
	if (!$defaultgroup && !isset($$foo)) {
	    # Yes.
	    continue;
	}

        #
        # There should be a corresponding trust variable in the POST vars.
        # Note that we construct the variable name and indirect to it.
        #
        $foo      = "$user\$\$trust";
	$newtrust = $$foo;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
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
	if (!$newtrust || strcmp($newtrust, "") == 0) {
	    TBERROR("Error finding trust for $user in editgroup.php3", 1);
	}

	if (strcmp($newtrust, "user") &&
	    strcmp($newtrust, "local_root") &&
	    strcmp($newtrust, "group_root")) {
	    TBERROR("Invalid trust $newtrust for $user in editgroup.php3.", 1);
	}

	TBCheckTrustConsistency($user, $pid, $gid, $newtrust);
    }
}

#
# Go through the list of non members. For each one, check to see if
# the checkbox for that person was checked. If so, add the person
# to the group membership, with the trust level specified.
# 
if (!$defaultgroup && mysql_num_rows($nonmembers_result)) {
    while ($row = mysql_fetch_array($nonmembers_result)) {
	$user = $row[0];
	$foo  = "add_$user";
	
	if (isset($$foo)) {
            #
            # There should be a corresponding trust variable in the POST vars.
            # Note that we construct the variable name and indirect to it.
            #
            $bar      = "$user\$\$trust";
	    $newtrust = $$bar;
	
	    if (!$newtrust || strcmp($newtrust, "") == 0) {
		TBERROR("Error finding trust for $user in editgroup.php3",
			1);
	    }

	    if (strcmp($newtrust, "user") &&
		strcmp($newtrust, "local_root") &&
		strcmp($newtrust, "group_root")) {
		TBERROR("Invalid trust $newtrust for $user in editgroup.php3.",
			1);
	    }
	
	    TBCheckTrustConsistency($user, $pid, $gid, $newtrust);
	}
    }
}

#
205 206
# Now do the second pass, which makes the changes. Record the user IDs
# that are changed so that we can pass that to setgroups below.
207
#
208
$modusers = "";
209 210 211 212 213 214 215 216 217 218 219 220 221

#
# Go through the list of current members. For each one, check to see if
# the checkbox for that person was checked. If not, delete the person
# from the group membership. Otherwise, look to see if the trust level
# has been changed.
#
if (mysql_num_rows($curmembers_result)) {
    mysql_data_seek($curmembers_result, 0);
    
    while ($row = mysql_fetch_array($curmembers_result)) {
	$user = $row[0];
	$foo  = "change_$user";
222

223
	if (!$defaultgroup && !isset($$foo)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
224
	    DBQueryFatal("delete from group_membership ".
225 226
			 "where pid='$pid' and gid='$gid' and uid='$user'");

227
	    $modusers = "$modusers $user";
228
	    continue;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229
	}
230 231 232 233 234 235 236 237 238
        #
        # There should be a corresponding trust variable in the POST vars.
        # Note that we construct the variable name and indirect to it.
        #
        $foo      = "$user\$\$trust";
	$newtrust = $$foo;
	
	DBQueryFatal("update group_membership set trust='$newtrust' ".
		     "where pid='$pid' and gid='$gid' and uid='$user'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
239 240 241 242 243 244
    }
}

#
# Go through the list of non members. For each one, check to see if
# the checkbox for that person was checked. If so, add the person
245
# to the group membership, with the trust level specified.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246
# 
247 248 249
if (!$defaultgroup && mysql_num_rows($nonmembers_result)) {
    mysql_data_seek($nonmembers_result, 0);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
250
    while ($row = mysql_fetch_array($nonmembers_result)) {
251 252
	$user = $row[0];
	$foo  = "add_$user";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
253 254
	
	if (isset($$foo)) {
255 256 257 258 259 260 261
            #
            # There should be a corresponding trust variable in the POST vars.
            # Note that we construct the variable name and indirect to it.
            #
            $bar      = "$user\$\$trust";
	    $newtrust = $$bar;
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262
	    DBQueryFatal("insert into group_membership ".
263 264 265
			 "(uid, pid, gid, trust, ".
			 " date_applied,date_approved) ".
			 "values ('$user','$pid','$gid', '$newtrust', ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
266
			 "        now(), now())");
267 268
	    
	    $modusers = "$modusers $user";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269 270 271 272 273 274 275 276 277
	}
    }
}

#
# Grab the unix GID for running scripts.
#
TBGroupUnixInfo($pid, $pid, $unix_gid, $unix_name);

278 279 280 281 282 283 284
echo "<br>
      Group '$gid' in project '$pid' is being updated!<br><br>
      This will take a minute or two. <b>Please</b> do not click the Stop
      button during this time. If you do not receive notification within
      a reasonable amount of time, please contact $TBMAILADDR.\n";
flush();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
285 286 287 288 289
#
# Run the script. This will do the account stuff for all the people
# in the group. This is the same script that gets run when the group
# is first created.
#
290
SUEXEC($uid, $unix_gid, "websetgroups -p $pid $modusers", 1);
291 292 293 294

echo "<br><br>
      <b>Done!</b>
      <br>\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
295 296

#
297
# Show it again!
Leigh B. Stoller's avatar
Leigh B. Stoller committed
298 299 300
#
SHOWGROUP($pid, $gid);

301 302
SHOWGROUPMEMBERS($pid, $gid);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
303
#
304
# Back to ...
Leigh B. Stoller's avatar
Leigh B. Stoller committed
305
# 
306 307
echo "<br>
       <A href='showgroup.php3?pid=$pid&gid=$gid'>Back to Group page</a>\n";
308

Leigh B. Stoller's avatar
Leigh B. Stoller committed
309 310 311 312 313
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>