user-dashboard.ajax 16.1 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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
# 
# {{{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");

# We set this in CheckPageArgs
$target_user = null;

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

    if (!isset($ajax_args["uid"])) {
	SPITAJAX_ERROR(-1, "Missing target uid");
	return -1;
    }
    $uid = $ajax_args["uid"];
    
    if (!TBvalid_uid($uid)) {
	SPITAJAX_ERROR(-1, "Invalid target uid");
        return -1;
    }
    $target_user = User::Lookup($uid);
    if (!$target_user) {
        sleep(2);
	SPITAJAX_ERROR(-1, "Unknown target uid");
        return -1;
    }
    if ($uid == $this_user->uid())
        return 0;
    
62
    if (!ISADMIN() && !ISFOREIGN_ADMIN() &&
63 64 65 66 67 68 69 70 71 72 73 74 75 76
        !$target_user->AccessCheck($this_user, $TB_USERINFO_READINFO)) {
	SPITAJAX_ERROR(-1, "Not enough permission");
	return -1;
    }
    return 0;
}

function Do_ExperimentList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
77 78 79 80 81
    $blob = array("user_experiments" =>
                     ExperimentList("user", $target_user),
                  "project_experiments" =>
                     ExperimentList("membership", $target_user));
    SPITAJAX_RESPONSE($blob);
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
function Do_ClassicExperimentList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = ClassicExperimentList("user", $target_user);
    SPITAJAX_RESPONSE($results);
}

function Do_ClassicProfileList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = ClassicExperimentList("user", $target_user, "inactive");
    SPITAJAX_RESPONSE($results);
}

106 107 108 109 110 111 112 113 114 115 116
function Do_ProfileList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = ProfileList("user", $target_user);
    SPITAJAX_RESPONSE($results);
}

117 118 119 120 121 122 123 124 125 126 127
function Do_ProjectProfileList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = ProjectProfileList($target_user);
    SPITAJAX_RESPONSE($results);
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
function Do_DatasetList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = DatasetList("user", $target_user);
    SPITAJAX_RESPONSE($results);
}

function Do_ClassicDatasetList()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $results = ClassicDatasetList("user", $target_user);
    SPITAJAX_RESPONSE($results);
}

150 151 152
function Do_ProjectList()
{
    global $this_user, $target_user;
153
    global $TB_PROJECT_READINFO, $newTrustMap;
154 155 156 157 158 159 160

    if (CheckPageArgs()) {
        return;
    }
    $target_idx  = $target_user->uid_idx();
    $target_uuid = $target_user->uuid();
    $results     = array();
161
    $projlist    = $target_user->ProjectMembershipList();
162 163 164 165 166

    #
    # Cull out the nonlocal projects, we do not want to show those
    # since they are just the holding projects.
    #
167 168
    foreach ($projlist as $proj) {
        if (!$proj->IsNonLocal()) {
169 170 171 172
            $blob = array();
            $blob["pid"] = $proj->pid();
            $blob["description"] = $proj->name();
            $leader = $proj->GetLeader();
173 174 175
            $blob["leader"]      = $leader->uid();
            $blob["leader_name"] = $leader->name();
            $blob["leader_idx"]  = $leader->idx();
176
            $membership = $proj->MemberShipInfo($target_user);
177
            $blob["trust"] = $newTrustMap[$membership["trust"]];
178 179 180 181 182

            #
            # User membership in subgroups
            #
            $blob["subgroups"] = array();
183
            
184 185 186 187 188 189 190 191 192 193 194 195
            $groups = $proj->GroupList($target_user);
            if (count($groups)) {
                foreach ($groups as $group) {
                    $glob = array();
                    $leader = $group->GetLeader();

                    $glob["gid"]          = $group->gid();
                    $glob["gid_idx"]      = $group->gid_idx();
                    $glob["description"]  = $group->description();
                    $glob["leader"]       = $leader->uid();
                    $glob["leader_idx"]   = $leader->idx();
                    $glob["leader_name"]  = $leader->name();
196
                    $membership = $group->MemberShipInfo($target_user);
197
                    $glob["trust"] = $newTrustMap[$membership["trust"]];
198 199 200
                    $blob["subgroups"][] = $glob;
                }
            }
201
            $results[$proj->pid()] = $blob;
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
        }
    }

    SPITAJAX_RESPONSE($results);
}

function Do_AccountDetails()
{
    global $this_user, $target_user;

    if (CheckPageArgs()) {
        return;
    }
    $target_idx  = $target_user->uid_idx();
    $target_uuid = $target_user->uuid();
    $results     = array();

    $results["uid"]          = $target_user->uid();
    $results["name"]         = $target_user->name();
    $results["email"]        = $target_user->email();
    $results["affiliation"]  = $target_user->affil();
    $results["country"]      = $target_user->country();
    $results["state"]        = $target_user->state();
    $results["city"]         = $target_user->city();

227 228
    if (ISADMIN()) {
        $results["uid_idx"]       = $target_user->uid_idx();
229
        $results["portal"]        = $target_user->portal();
230 231 232 233 234 235
        $results["created"]       = DateStringGMT($target_user->created());
        $results["status"]        = $target_user->status();
        $results["stud"]          = $target_user->stud() ? true : false;
        $results["admin"]         = $target_user->admin() ? true : false;
        $results["foreign_admin"] = $target_user->foreign_admin() ? true:false;
        $results["web_frozen"]    = $target_user->weblogin_frozen()? true:false;
236 237
        $results["nonlocal_id"]   = ($target_user->nonlocal_id() ?
                                     $target_user->nonlocal_id() : "");
238
        $results["last_weblogin"] = DateStringGMT(LASTWEBLOGIN($target_idx));
239 240
        $results["inactive"]       = ($target_user->status() ==
                                      TBDB_USERSTATUS_INACTIVE ? true : false);
241 242
    }

243 244 245
    SPITAJAX_RESPONSE($results);
}

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 277 278 279 280 281 282 283
#
# Usage for the user, in pnode hours. 
#
function Do_UsageSummary()
{
    global $this_user, $target_user;
    global $ajax_args;

    if (CheckPageArgs()) {
        return;
    }
    list($pcount, $phours)           = Instance::CurrentUsage($target_user);
    list($weekpcount, $weekphours)   = Instance::WeeksUsage($target_user);
    list($monthpcount, $monthphours) = Instance::MonthsUsage($target_user);
    list($rank, $ranktotal)          = Instance::Ranking($target_user, 30);

    if ($phours < 2) {
        $phours = sprintf("%.1f", $phours);
    }
    else {
        $phours = sprintf("%.0f", $phours);
    }
    if ($weekphours < 2) {
        $weekphours = sprintf("%.1f", $weekphours);
    }
    else {
        $weekphours = sprintf("%.0f", $weekphours);
    }
    if ($monthphours < 2) {
        $monthphours = sprintf("%.1f", $monthphours);
    }
    else {
        $monthphours = sprintf("%.0f", $monthphours);
    }
    $blob = array("pnodes"      => $pcount,
                  "phours"      => $phours,
                  "weekpnodes"  => $weekpcount,
                  "weekphours"  => $weekphours,
284
                  "monthpnodes" => $monthpcount,
285 286 287
                  "monthphours" => $monthphours,
                  "rankdays"    => 30,
                  "rank"        => $rank,
288 289
                  "ranktotal"   => $ranktotal,
                  "expcount"    => Instance::CurrentInstanceCount($target_user),
290 291 292 293 294
    );
    
    SPITAJAX_RESPONSE($blob);
}

295 296 297 298 299 300
#
# Toggle flags.
#
function Do_Toggle()
{
    global $this_user, $target_user;
301
    global $ajax_args, $suexec_output;
302 303 304 305 306 307 308 309 310
    global $TBMAIL_OPS, $TBMAIL_AUDIT, $TBMAIL_WWW, $TBADMINGROUP;

    if (CheckPageArgs()) {
        return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Not enough permission");
        return;
    }
311 312
    if ($target_user->status() != TBDB_USERSTATUS_ACTIVE &&
        $target_user->status() != TBDB_USERSTATUS_INACTIVE) {
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 340 341 342 343 344 345 346
	SPITAJAX_ERROR(-1, "User not been activated yet!");
        return;
    }
    $target_idx  = $target_user->uid_idx();
    $target_uid  = $target_user->uid();
    $this_uid    = $this_user->uid();

    if (!isset($ajax_args["toggle"])) {
	SPITAJAX_ERROR(-1, "Missing toggle name");
	return -1;
    }
    $toggle = $ajax_args["toggle"];

    if ($toggle == "admin") {
        $target_user->SetAdminFlag($target_user->admin() ? 0 : 1);
        if ($target_user->admin()) {
            TBMAIL($TBMAIL_OPS,
                   "Admin Flag enabled for '$target_uid'",
                   "$this_uid has enabled the admin flag for $target_uid!\n\n",
                   "From: $TBMAIL_OPS\n".
                   "Bcc: $TBMAIL_AUDIT\n".
                   "Errors-To: $TBMAIL_WWW");
        }
        if (SUEXEC($this_uid, $TBADMINGROUP,
                   "webtbacct mod $target_uid", SUEXEC_ACTION_CONTINUE)) {
            SPITAJAX_ERROR(-1, $suexec_output);
            return -1;
        }
        if (SUEXEC($this_uid, $TBADMINGROUP,
                   "webmodgroups $target_uid", SUEXEC_ACTION_CONTINUE)) {
            SPITAJAX_ERROR(-1, $suexec_output);
            return -1;
        }
    }
347 348 349 350 351 352 353 354 355 356 357 358
    elseif ($toggle == "inactive") {
        $newstatus = ($target_user->IsActive() ?
                      TBDB_USERSTATUS_INACTIVE : TBDB_USERSTATUS_ACTIVE);
        $command   = ($target_user->IsActive() ? "deactivate" : "reactivate");
    
        $target_user->SetStatus($newstatus);
        if (SUEXEC($this_uid, $TBADMINGROUP,
                   "webtbacct $command $target_uid", SUEXEC_ACTION_CONTINUE)) {
            SPITAJAX_ERROR(-1, $suexec_output);
            return -1;
        }
    }
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    elseif ($toggle == "foreign_admin") {
        $target_user->SetForeignAdmin($target_user->foreign_admin() ? 0 : 1);
    }
    elseif ($toggle == "stud") {
        $target_user->SetStudly($target_user->stud() ? 0 : 1);
    }
    elseif ($toggle == "web_frozen") {
        $target_user->SetWebFreeze($target_user->weblogin_frozen() ? 0 : 1);
    }
    else {
	SPITAJAX_ERROR(-1, "Unknown toggle name: $toggle");
        return;
    }
    SPITAJAX_RESPONSE(1);
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
#
# Freeze or Thaw.
#
function Do_FreezeOrThaw()
{
    global $this_user, $target_user;
    global $ajax_args, $suexec_output;
    global $TBADMINGROUP;

    if (CheckPageArgs()) {
        return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Not enough permission");
        return;
    }
    if ($target_user->status() != TBDB_USERSTATUS_ACTIVE &&
        $target_user->status() != TBDB_USERSTATUS_FROZEN) {
	SPITAJAX_ERROR(-1, "User is not active or frozen!");
        return;
    }
    $target_idx  = $target_user->uid_idx();
    $target_uid  = $target_user->uid();
    $this_uid    = $this_user->uid();

    if (!isset($ajax_args["which"])) {
	SPITAJAX_ERROR(-1, "What am I supposed to do?");
	return -1;
    }
    $which = strtolower($ajax_args["which"]);
    if ($which != "freeze" && $which != "thaw") {
	SPITAJAX_ERROR(-1, "Cannot do that: $which");
	return -1;
    }
    # Use -u (update) and -f (force) to make it happen.
    if (SUEXEC($this_uid, $TBADMINGROUP,
               "webtbacct -u -f $which $target_uid", SUEXEC_ACTION_CONTINUE)) {
        SPITAJAX_ERROR(-1, $suexec_output);
        return -1;
    }
    SPITAJAX_RESPONSE(1);
}

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 459 460 461 462 463 464 465 466 467
#
# Send a reset password link, no cookie required.
#
function Do_SendPasswordReset()
{
    global $this_user, $target_user;
    global $ajax_args, $APTBASE, $APTMAIL, $TBMAIL_WWW, $TBMAIL_AUDIT;

    if (CheckPageArgs()) {
        return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(-1, "Not enough permission");
        return;
    }
    if ($target_user->status() != TBDB_USERSTATUS_ACTIVE &&
        $target_user->status() != TBDB_USERSTATUS_FROZEN) {
	SPITAJAX_ERROR(-1, "User is not active or frozen!");
        return;
    }
    $target_idx  = $target_user->uid_idx();
    $target_uid  = $target_user->uid();
    $target_name = $target_user->name();
    $target_email= $target_user->email();

    #
    # Generate a random key and send the user an email message with a URL
    # that will allow them to change their password. 
    #
    $key = GENHASH();
    $target_user->SetChangePassword($key, "UNIX_TIMESTAMP(now())+(60*10)");

    TBMAIL("$target_name <$target_email>",
           "Password reset for '$target_uid'",
           "\n".
           "Here is your password reset authorization URL. Click on this\n".
           "link within the next 10 minutes, and you will be allowed to\n".
           "reset your password.\n".
           "\n".
           "    ${APTBASE}/changepswd.php?user=$target_uid&reset=$key\n".
           "\n".
           "\n".
           "Thanks!\n",
           "From: $APTMAIL\n".
           "Bcc: $TBMAIL_AUDIT\n".
           "Errors-To: $TBMAIL_WWW");

    SPITAJAX_RESPONSE(1);
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
function Do_SendTestMessage()
{
    global $this_user, $target_user;
    global $APTMAIL;

    if (CheckPageArgs()) {
        return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(1, "Only administrators can do this.");
        return;
    }
    $target_idx   = $target_user->uid_idx();
    $target_uuid  = $target_user->uuid();
    $target_email = $target_user->email();
    $target_name  = $target_user->name();
    $target_uid   = $target_user->uid();
    $adminemail   = $target_user->adminEmail();

    TBMAIL("$target_name '$target_uid' <$target_email>",
           "This is a test",
           "\n".
           "Dear $target_name ($target_uid):\n".
           "\n".
           "This is a test message to validate the email address that we\n".
           "have in our database. Please respond to this message\n".
           "as soon as you receive it. If we do not hear back from you, we\n".
           "may be forced to freeze your account.\n".
           "\n".
           "Thank you very much!\n",
           "From: $adminemail\n".
           "Bcc: stoller");
           
    SPITAJAX_RESPONSE(1);
}

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 540 541 542 543 544 545
function Do_NagPI()
{
    global $this_user, $ajax_args;

    if (!isset($ajax_args["pid"])) {
	SPITAJAX_ERROR(-1, "Missing project name");
	return -1;
    }
    if (!TBvalid_pid($ajax_args["pid"])) {
	SPITAJAX_ERROR(-1, "Invalid project name");
	return -1;
    }
    $project = Project::Lookup($ajax_args["pid"]);
    if (!$project) {
	SPITAJAX_ERROR(-1, "No such project");
	return -1;
    }
    $group = $project->DefaultGroup();
    if (!$group) {
	SPITAJAX_ERROR(-1, "Internal error getting group");
	return -1;
    }
    $membership = $group->MemberShipInfo($this_user);
    if ($membership["date_approved"]) {
        SPITAJAX_ERROR(1, "You have already been approved, please refresh ".
                       "the page to see your updated status.");
        return -1;
    }
    if ($membership["date_nagged"]) {
        $nagged = strtotime($membership["date_nagged"]);
        if (time() - $nagged < 3600 * 24) {
            SPITAJAX_ERROR(1, "You recently reminded the project leader, ".
                           "lets give him/her a chance to respond before ".
                           "sending another reminder.");
            return -1;
        }
    }
    # Chalk an error up to a double click, do not bother to do anything.
    $group->NewMemberNotify($this_user, true);
    SPITAJAX_RESPONSE("nagged");
}

Leigh Stoller's avatar
Leigh Stoller committed
546 547 548 549 550 551 552 553 554
function Do_AcceptAUP()
{
    global $this_user, $PORTAL_GENESIS;

    $this_user->AcceptAUP($PORTAL_GENESIS);

    SPITAJAX_RESPONSE("accepted");
}

555 556 557 558
# Local Variables:
# mode:php
# End:
?>