approve-projects.ajax 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?php
#
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
chdir("..");
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");
include_once("ajax-routines.ajax");

# This is set in CheckPageArgs();
$project = null;

#
# Need to check the permission, since we allow admins to mess with
# other accounts.
#
function CheckPageArgs()
{
    global $this_user;
    global $ajax_args, $project;

    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Not enough permission");
	return -1;
    }
    if (!isset($ajax_args["pid"])) {
        SPITAJAX_ERROR(-1, "Must supply project");
        return -1;
    }
    $project = Project::LookupByPid($ajax_args["pid"]);
    if (!$project) {
        SPITAJAX_ERROR(-1, "No such project");
        return -1;
    }
    return 0;
}

function Do_ProjectList()
{
    global $this_user;
    global $ajax_args;
    $results = array();

    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Not enough permission");
	return;
    }

    #
    # Look in the projects table to see which projects have not been approved.
    #
    $projlist = Project::PendingProjectList();

    foreach ($projlist as $project) {
        $blob = array();
        $leader = $project->GetLeader();
        $portal = $project->portal();
        if (!$portal) {
            $portal = "Classic";
        }
        $blob["pid"]          = $project->pid();
        $blob["pid_idx"]      = $project->pid_idx();
        $blob["created"]      = DateStringGMT($project->created());
        $blob["description"]  = $project->name();
        if ($project->URL() && $project->URL() != "") {
            if (preg_match("/^http[s]?:\/\//", $project->URL())) {
                $blob["url"] = $project->URL();
            }
            else {
                $blob["url"] = "http://" . $project->URL();
            }
        }
        $blob["portal"]       = $portal;
        $blob["why"]          = $project->why();
        $blob["leader"]       = $project->head_uid();
        $blob["leader_idx"]   = $project->head_idx();
        $blob["leader_name"]  = $leader->name();
        $blob["leader_affil"] = $leader->affil();
        $blob["leader_email"] = $leader->email();
100
        $blob["leader_notyet"]= ($leader->status() == TBDB_USERSTATUS_NEWUSER ? 1 : 0);
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 129 130 131 132 133 134 135 136 137 138 139 140 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 265 266 267 268 269 270 271 272 273 274 275 276

        $results[$project->pid()] = $blob;
    }

    SPITAJAX_RESPONSE($results);    
}

#
# Save new project description.
#
function Do_SaveDescription()
{
    global $this_user;
    global $ajax_args, $project;

    if (CheckPageArgs()) {
        return;
    }
    if (!isset($ajax_args["description"])) {
        SPITAJAX_ERROR(-1, "Must supply description");
        return;
    }
    $description = $ajax_args["description"];
    if (!TBvalid_why($description)) {
        SPITAJAX_ERROR(-1, "Not a valid description: " + TBFieldErrorString());
        return;
    }
    $pid_idx  = $project->pid_idx();
    $safe_why = addslashes($description);

    if (!DBQueryWarn("update projects set why='$safe_why' ".
		     "where pid_idx='$pid_idx'")) {
        SPITAJAX_ERROR(-1, "Error saving new description");
        return;
    }
    SPITAJAX_RESPONSE(0);
}

#
# Request more info.
#
function Do_MoreInfo()
{
    global $this_user;
    global $ajax_args, $project;

    if (CheckPageArgs()) {
        return;
    }
    if (!isset($ajax_args["message"])) {
        SPITAJAX_ERROR(-1, "Must supply message");
        return;
    }
    $message = $ajax_args["message"];

    if (! ($leader = $project->GetLeader())) {
        TBERROR("Error getting leader for $pid", 0);
        SPITAJAX_ERROR(-1, "Internal error getting project leader");
        
    }
    $headuid       = $project->head_uid();
    $headuid_email = $leader->email();
    $headname      = $leader->name();
    $pid           = $project->pid();

    SendProjAdminMail
        ($project, "ADMIN", "$headname '$headuid' <$headuid_email>",
         "Request for more info about project '$pid'",
         "This message is to notify you that your project application\n".
         "for $pid has been postponed until we have more information\n".
         "or you take certain actions.  You can just reply to this message\n".
         "to provide that information or report your actions.\n".
         "----------------------------------------------------------------\n".
         "\n".
         $message.
         "\n\n".
         "Thanks!\n");
    
    SPITAJAX_RESPONSE(0);
}

#
# Approve
#
function Do_Approve()
{
    global $this_user;
    global $ajax_args, $project, $TBADMINGROUP, $suexec_output;

    if (CheckPageArgs()) {
        return;
    }
    if (!isset($ajax_args["message"])) {
        SPITAJAX_ERROR(-1, "Must supply message");
        return;
    }
    $message = $ajax_args["message"];

    if (! ($leader = $project->GetLeader())) {
        TBERROR("Error getting leader for $pid", 0);
        SPITAJAX_ERROR(-1, "Internal error getting project leader");
        return;
    }
    $curstatus     = $leader->status();
    $headuid       = $project->head_uid();
    $headuid_email = $leader->email();
    $headname      = $leader->name();
    $pid           = $project->pid();
    # See if we are in an initial Emulab setup.
    $FirstInitState= (TBGetFirstInitState() == "approveproject" ? 1 : 0);
    $optargs       = "";

    # Sanity check the leader status.
    if ($curstatus != TBDB_USERSTATUS_ACTIVE &&
	$curstatus != TBDB_USERSTATUS_UNAPPROVED) {
        SPITAJAX_ERROR(-1, "Invalid $headuid status $curstatus");
        return;
    }
    # Optional message passed along.
    if ($message != "") {
	$filename = tempnam("/tmp", "approveproject");
	$fp = fopen($filename, "w");
        if (!$fp) {
            SPITAJAX_ERROR(-1, "Could not create temporary file for message");
            return;
        }
	fwrite($fp, $message);
	fclose($fp);
	$optargs = " -f " . escapeshellarg($filename);
    }
    $retval = SUEXEC($this_user->uid(), $TBADMINGROUP,
                     "webmkproj $optargs $pid", SUEXEC_ACTION_CONTINUE);
    if (isset($filename)) {
        unlink($filename);
    }
    if ($retval) {
        SPITAJAX_ERROR(-1, "Internal error creating project, ".
                       "see the email sent to your testbed-ops list");
        return;
    }
    #
    # Page throws up a message.
    #
    if ($FirstInitState) {
        #
        # Move to next phase. 
        # 
        TBSetFirstInitState("Ready");
        SPITAJAX_RESPONSE(1);
        return;
    }
    SPITAJAX_RESPONSE(0);
}

#
# Deny
#
function Do_Deny()
{
    global $this_user;
    global $ajax_args, $project, $TBADMINGROUP, $suexec_output;
    $this_uid = $this_user->uid();

    if (CheckPageArgs()) {
        return;
    }
    if (!isset($ajax_args["message"])) {
        SPITAJAX_ERROR(-1, "Must supply message");
        return;
    }
    if (!isset($ajax_args["deleteuser"])) {
        SPITAJAX_ERROR(-1, "Must supply deleteuser");
        return;
    }
    $message = $ajax_args["message"];
    $deleteuser = ($ajax_args["deleteuser"] == 1 ? 1 : 0);
277 278 279 280
    $silent = 0;
    if (isset($ajax_args["silent"]) && $ajax_args["silent"] == 1) {
        $silent = 1;
    }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    if (! ($leader = $project->GetLeader())) {
        TBERROR("Error getting leader for $pid", 0);
        SPITAJAX_ERROR(-1, "Internal error getting project leader");
        return;
    }
    $curstatus     = $leader->status();
    $headuid       = $project->head_uid();
    $headuid_email = $leader->email();
    $headname      = $leader->name();
    $pid           = $project->pid();
    $optargs       = "";

    if ($deleteuser) {
	#
	# Take the user out of the project group first.
	#
	$retval = SUEXEC($this_uid, $TBADMINGROUP,
                         "webmodgroups -r $pid:$pid $headuid",
                         SUEXEC_ACTION_CONTINUE);
        if ($retval) {
            SPITAJAX_ERROR(-1, "Internal error removing user from project");
            return;
        }
	#
	# See if user is in any other projects (even unapproved).
	#
	$project_list = $leader->ProjectMembershipList();

	#
	# If yes, then we cannot safely delete the user account.
	#
	if (! count($project_list)) {
	    #
	    # No other project membership. If the user is unapproved/newuser,
	    # it means he was never approved in any project, and so will
	    # likely not be missed. He will be unapproved if he did his
	    # verification.
	    #
	    if ($curstatus == TBDB_USERSTATUS_NEWUSER ||
                $curstatus == TBDB_USERSTATUS_UNAPPROVED) {
		$retval = SUEXEC($this_uid, $TBADMINGROUP,
                                 "webrmuser -n -p $pid $headuid",
                                 SUEXEC_ACTION_CONTINUE);
                if ($retval) {
                    SPITAJAX_ERROR(-1, "Internal error removing user");
                    return;
                }
	    }
	}
    }
    
    $retval = SUEXEC($this_uid, $TBADMINGROUP,
                     "webrmproj $pid", SUEXEC_ACTION_CONTINUE);

    if ($retval) {
        SPITAJAX_ERROR(-1, "Internal error removing project, ".
                       "see the email sent to your testbed-ops list");
        return;
    }
340 341 342 343 344 345 346 347 348 349 350 351
    if (!$silent) {
        # This works cause we still have the project object in memory.
        SendProjAdminMail
            ($project, "ADMIN", "$headname '$headuid' <$headuid_email>",
             "Project application '$pid' has been denied!",
             "This message is to notify you that your project application\n".
             "for $pid has been denied.\n".
             ($message == "" ? "" :
              "----------------------------------------------------------------\n".
              "\n".
              $message . "\n"));
    }
352 353 354 355 356 357 358
    SPITAJAX_RESPONSE(0);
}

# Local Variables:
# mode:php
# End:
?>