public static function CanViewRun($user_id, Runs $run)
 {
     if (is_null($run) || !is_a($run, 'Runs')) {
         return false;
     }
     return $run->getUserId() === $user_id || Authorization::CanEditRun($user_id, $run);
 }
 /**
  * Re-sends a problem to Grader.
  *
  * @param Request $r
  * @throws InvalidDatabaseOperationException
  */
 public static function apiRejudge(Request $r)
 {
     // Init
     self::initializeGrader();
     // Get the user who is calling this API
     self::authenticateRequest($r);
     self::validateDetailsRequest($r);
     if (!Authorization::CanEditRun($r['current_user_id'], $r['run'])) {
         throw new ForbiddenAccessException('userNotAllowed');
     }
     self::$log->info('Run being rejudged!!');
     // Try to delete existing directory, if exists.
     try {
         $grade_dir = RunController::getGradePath($r['run']);
         FileHandler::DeleteDirRecursive($grade_dir);
     } catch (Exception $e) {
         // Soft error :P
         self::$log->warn($e);
     }
     try {
         self::$grader->Grade([$r['run']->guid], true, $r['debug'] || false);
     } catch (Exception $e) {
         self::$log->error('Call to Grader::grade() failed:');
         self::$log->error($e);
     }
     $response = array();
     $response['status'] = 'ok';
     self::invalidateCacheOnRejudge($r['run']);
     // Expire ranks
     UserController::deleteProblemsSolvedRankCacheList();
     return $response;
 }