public function execute(CommandContext $context) { if (!UserStatus::isAdmin() || !Current_User::allow('hms', 'search')) { PHPWS_Core::initModClass('hms', 'exception/PermissionException.php'); throw new PermissionException('You do not have permission to lookup student names!'); } $student = null; $error = new JsonError(403); $username = $context->get('username'); $banner_id = (int) $context->get('banner_id'); try { if ($banner_id) { $student = StudentFactory::getStudentByBannerID($banner_id, Term::getSelectedTerm()); } elseif (!empty($username)) { $student = StudentFactory::getStudentByUsername($username, Term::getSelectedTerm()); } else { $error->setMessage('Did not receive Banner ID or user name.'); $context->setContent(json_encode($error)); } $student->gender_string = HMS_Util::formatGender($student->gender); $context->setContent(json_encode($student)); } catch (\StudentNotFoundException $e) { $error->setMessage($e->getMessage()); $context->setContent(json_encode($error)); } }
public function process() { // Set headers to allow Cross-origin scripting $rh = getallheaders(); header('Allow: GET,HEAD,POST,PUT,DELETE,OPTIONS'); if (array_key_exists('Origin', $rh)) { header('Access-Control-Allow-Origin:' . $rh['Origin']); } if (array_key_exists('Access-Control-Request-Headers', $rh)) { header('Access-Control-Allow-Headers:' . $rh['Access-Control-Request-Headers']); } header('Access-Control-Allow-Credentials: true'); try { parent::process(); } catch (PermissionException $e) { $error = new JsonError('401 Unauthorized'); $error->setMessage('You are not authorized to perform this action. You may need to sign back in.'); $error->renderStatus(); $content = $error->encode(); } catch (Exception $e) { $error = new JsonError('500 Internal Server Error'); $error->setMessage($e->getMessage()); $error->renderStatus(); $content = $error->encode(); // Log the exception error_log('Caught API Exception: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . ' Trace: ' . $e->getTraceAsString()); $message = $this->formatException($e); $this->emailError($message); } $callback = $this->context->get('callback'); $content = $this->context->getContent(); // Wrap a jsonp request in it's function callback $response = !is_null($callback) ? "{$callback}({$content})" : $content; header('Content-Type: application/json; charset=utf-8'); echo $response; // This sets NQ (notifications), which aren't valid for AJAX, so we aren't doing it. Just exit instead. //HMS::quit(); exit; }