/** * Create new school * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws InvalidParameterException */ public static function apiCreate(Request $r) { self::authenticateRequest($r); Validators::isStringNonEmpty($r["name"], "name"); Validators::isNumber($r["state_id"], "state_id", false); if (!is_null($r["state_id"])) { try { $r["state"] = StatesDAO::getByPK($r["state_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (is_null($r["state"])) { throw new InvalidParameterException("parameterNotFound", "state"); } } // Create school object $school = new Schools(array("name" => $r["name"], "state_id" => $r["state_id"])); $school_id = 0; try { $existing = SchoolsDAO::findByName($r["name"]); if (count($existing) > 0) { $school_id = $existing[0]->getSchoolId(); } else { // Save in db SchoolsDAO::save($school); $school_id = $school->getSchoolId(); } } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } return array("status" => "ok", "school_id" => $school_id); }
/** * Add contest to a group scoreboard * * @param Request $r */ public static function apiAddContest(Request $r) { self::validateGroupScoreboardAndContest($r); Validators::isInEnum($r["only_ac"], "only_ac", array(0, 1)); Validators::isNumber($r["weight"], "weight"); try { $groupScoreboardContest = new GroupsScoreboardsContests(array("group_scoreboard_id" => $r["scoreboard"]->group_scoreboard_id, "contest_id" => $r["contest"]->contest_id, "only_ac" => $r["only_ac"], "weight" => $r["weight"])); GroupsScoreboardsContestsDAO::save($groupScoreboardContest); self::$log->info("Contest " . $r["contest_alias"] . "added to scoreboard " . $r["scoreboard_alias"]); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array("status" => "ok"); }
/** * Add contest to a group scoreboard * * @param Request $r */ public static function apiAddContest(Request $r) { self::validateGroupScoreboardAndContest($r); Validators::isInEnum($r['only_ac'], 'only_ac', array(0, 1)); Validators::isNumber($r['weight'], 'weight'); try { $groupScoreboardContest = new GroupsScoreboardsContests(array('group_scoreboard_id' => $r['scoreboard']->group_scoreboard_id, 'contest_id' => $r['contest']->contest_id, 'only_ac' => $r['only_ac'], 'weight' => $r['weight'])); GroupsScoreboardsContestsDAO::save($groupScoreboardContest); self::$log->info('Contest ' . $r['contest_alias'] . 'added to scoreboard ' . $r['scoreboard_alias']); } catch (Exception $ex) { throw new InvalidDatabaseOperationException($ex); } return array('status' => 'ok'); }
/** * Entry point to configure omegaup in ec2 mode * * @param Request $r * @return type */ public static function apiScaleOut(Request $r) { self::validateRequest($r); Validators::isNumber($r['count'], 'count'); $response['grader'] = self::setEmbeddedRunners('false'); self::$log->info('Bootstrapping more instances: '); $ec2_cmd_output = array(); $return_var = 0; $cmd = 'ec2-run-instances ami-3e123e7b -k omegaup_backend_test_key -i m1.medium -n ' . $r['count'] . ' --region us-west-1'; self::$log->info('Executing: ' . $cmd); exec($cmd, $ec2_cmd_output, $return_var); if ($return_var !== 0) { // D: self::$log->error($cmd . ' returned: ' . $return_var); throw new InvalidFilesystemOperationException('Error executing ec2-run-instances. Please check log for details'); } $response['ec2-run-instances'] = $ec2_cmd_output; return $response; }
/** * Entry point to configure omegaup in ec2 mode * * @param Request $r * @return type */ public static function apiScaleOut(Request $r) { self::validateRequest($r); Validators::isNumber($r["count"], "count"); $response["grader"] = self::setEmbeddedRunners("false"); self::$log->info("Bootstrapping more instances: "); $ec2_cmd_output = array(); $return_var = 0; $cmd = "ec2-run-instances ami-3e123e7b -k omegaup_backend_test_key -i m1.medium -n " . $r["count"] . " --region us-west-1"; self::$log->info("Executing: " . $cmd); exec($cmd, $ec2_cmd_output, $return_var); if ($return_var !== 0) { // D: self::$log->error($cmd . " returned: " . $return_var); throw new InvalidFilesystemOperationException("Error executing ec2-run-instances. Please check log for details"); } $response["ec2-run-instances"] = $ec2_cmd_output; return $response; }
/** * If no username provided: Gets the top N users who have solved more problems * If username provided: Gets rank for username provided * * @param Request $r * @return string * @throws InvalidDatabaseOperationException */ public static function apiRankByProblemsSolved(Request $r) { Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); $r['user'] = null; if (!is_null($r['username'])) { Validators::isStringNonEmpty($r['username'], 'username'); try { $r['user'] = UsersDAO::FindByUsername($r['username']); if (is_null($r['user'])) { throw new NotFoundException('userNotExist'); } } catch (ApiException $e) { throw $e; } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } } // Defaults for offset and rowcount if (null == $r['offset']) { $r['offset'] = 0; } if (null == $r['rowcount']) { $r['rowcount'] = 100; } return self::getRankByProblemsSolved($r); }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r['offset'])) { $r['offset'] = 0; } if (!isset($r['rowcount'])) { $r['rowcount'] = 100; } if (!Authorization::IsSystemAdmin($r['current_user_id'])) { throw new ForbiddenAccessException('userNotAllowed'); } Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); Validators::isInEnum($r['status'], 'status', array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r['verdict'], 'verdict', array('AC', 'PA', 'WA', 'TLE', 'MLE', 'OLE', 'RTE', 'RFE', 'CE', 'JE', 'NO-AC'), false); // Check filter by problem, is optional if (!is_null($r['problem_alias'])) { Validators::isStringNonEmpty($r['problem_alias'], 'problem'); try { $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r['problem'])) { throw new NotFoundException('problemNotFound'); } } Validators::isInEnum($r['language'], 'language', array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r['username'])) { try { $r['user'] = UserController::resolveUser($r['username']); } catch (NotFoundException $e) { // If not found, simply ignore it $r['username'] = null; $r['user'] = null; } } }
/** * Validator for List API * * @param Request $r * @throws ForbiddenAccessException * @throws InvalidDatabaseOperationException * @throws NotFoundException */ private static function validateList(Request $r) { // Defaults for offset and rowcount if (!isset($r["offset"])) { $r["offset"] = 0; } if (!isset($r["rowcount"])) { $r["rowcount"] = 100; } if (!Authorization::IsSystemAdmin($r["current_user_id"])) { throw new ForbiddenAccessException("userNotAllowed"); } Validators::isNumber($r["offset"], "offset", false); Validators::isNumber($r["rowcount"], "rowcount", false); Validators::isInEnum($r["status"], "status", array('new', 'waiting', 'compiling', 'running', 'ready'), false); Validators::isInEnum($r["verdict"], "verdict", array("AC", "PA", "WA", "TLE", "MLE", "OLE", "RTE", "RFE", "CE", "JE", "NO-AC"), false); // Check filter by problem, is optional if (!is_null($r["problem_alias"])) { Validators::isStringNonEmpty($r["problem_alias"], "problem"); try { $r["problem"] = ProblemsDAO::getByAlias($r["problem_alias"]); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (is_null($r["problem"])) { throw new NotFoundException("problemNotFound"); } } Validators::isInEnum($r["language"], "language", array('c', 'cpp', 'cpp11', 'java', 'py', 'rb', 'pl', 'cs', 'pas', 'kp', 'kj', 'cat', 'hs'), false); // Get user if we have something in username if (!is_null($r["username"])) { try { $r["user"] = UserController::resolveUser($r["username"]); } catch (NotFoundException $e) { // If not found, simply ignore it $r["username"] = null; $r["user"] = null; } } }
/** * Validate list request * * @param Request $r */ private static function validateList(Request $r) { Validators::isNumber($r['offset'], 'offset', false); Validators::isNumber($r['rowcount'], 'rowcount', false); // Defaults for offset and rowcount if (!isset($r['page'])) { if (!isset($r['offset'])) { $r['offset'] = 0; } if (!isset($r['rowcount'])) { $r['rowcount'] = 1000; } } Validators::isStringNonEmpty($r['query'], 'query', false); }
/** * Validate update API request * * @param Request $r * @throws InvalidDatabaseOperationException * @throws ForbiddenAccessException */ private static function validateUpdate(Request $r) { Validators::isNumber($r["clarification_id"], "clarificaion_id"); Validators::isStringNonEmpty($r["answer"], "answer", false); Validators::isInEnum($r["public"], "public", array('0', '1'), false); Validators::isStringNonEmpty($r["message"], "message", false); // Check that clarification exists try { $r['clarification'] = ClarificationsDAO::GetByPK($r["clarification_id"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (!Authorization::CanEditClarification($r["current_user_id"], $r["clarification"])) { throw new ForbiddenAccessException(); } }
/** * Validate list request * * @param Request $r */ private static function validateList(Request $r) { Validators::isNumber($r["offset"], "offset", false); Validators::isNumber($r["rowcount"], "rowcount", false); // Defaults for offset and rowcount if (!isset($r['page'])) { if (!isset($r["offset"])) { $r["offset"] = 0; } if (!isset($r["rowcount"])) { $r["rowcount"] = 1000; } } Validators::isStringNonEmpty($r["query"], "query", false); }