Beispiel #1
0
/**
 * Creates or resets the account for the given user.
 * @param string $netid The user's University of Rochester NetID.
 * @param string $password The user's NetID password.
 * @param mixed $lcc The user's LCC.
 * @return array An associative array with indices "status" and "message".
 * When "status" is STATUS OK, "user_existed", "has_lab_access", and "lcc" keys
 * will also be returned.
 */
function create_account($netid, $password, $lcc)
{
    global $twig;
    // We'll be using Twig to render email templates
    // Continue running this script even if the client disconnects
    ignore_user_abort(true);
    try {
        $user_existed = CifUser::user_exists($netid);
        $cif_user = CifUser::create($netid, $password, $lcc);
        if ($user_existed) {
            $has_lab_access = $cif_user->is_a_member_of(LAB_ACCESS_GROUP);
            // Send a reset email if the user reset their account
            $message = $twig->render('email/account-reset.html', array('has_lab_access' => $has_lab_access, 'old_lcc' => $lcc, 'new_lcc' => $cif_user->lcc));
            mail($cif_user->email, 'Your CIF account was reset', $message, EMAIL_HEADER);
        } else {
            // Give new users lab access
            // TODO Should still support an access list to pre-set access for users who do not yet exist;
            // TODO (cont) such a list must be synced between dev and prod
            $cif_user->join_group(LAB_ACCESS_GROUP);
            $has_lab_access = $cif_user->is_a_member_of(LAB_ACCESS_GROUP);
            // Send an account creation email to board
            $message = $twig->render('email/account-creation-notification.html', array('full_name' => $cif_user->full_name, 'email' => $cif_user->email, 'year' => $cif_user->year));
            mail(BOARD_EMAIL, "CIF account created for {$cif_user->full_name}", $message, EMAIL_HEADER);
            // Send a welcome email to the new user
            $message = $twig->render('email/welcome.html', array('first_name' => $cif_user->first_name, 'has_lab_access' => $has_lab_access, 'lcc' => $lcc));
            mail($cif_user->email, 'Your CIF account was created!', $message, EMAIL_HEADER);
        }
        // Write the log to disk
        file_put_contents(LOG_DIR . $netid, $cif_user->get_log(), FILE_APPEND);
        return array('status' => STATUS_OK, 'message' => "Your account was successfully " . ($user_existed ? 'reset' : 'created') . "!", 'info' => array('user_existed' => $user_existed, 'has_lab_access' => $has_lab_access, 'lcc' => $cif_user->lcc));
    } catch (PasswordException $pe) {
        return array('status' => STATUS_AUTH_ERROR, 'message' => "The NetID or password you entered seem to be incorrect.");
    } catch (Exception $e) {
        // Alert the tech directors if a log can be written
        if (isset($cif_user)) {
            // Write the log to disk
            file_put_contents(LOG_DIR . $netid, $cif_user->get_log(), FILE_APPEND);
            $subject = 'CIF Account ' . ($user_existed ? 'Reset' : 'Creation') . " Failed ({$netid})";
            $message = "This is a notice to check " . LOG_DIR . "{$netid} for the reason of failure.";
            mail(TECHDIRECTOR_EMAIL, $subject, $message);
        }
        return array('status' => STATUS_ERROR, 'message' => $e->getMessage());
    }
}