protected function startup()
 {
     parent::startup();
     if (!($row = $this->addons->find($this->id, $this->auth->isAllowed('addon', 'delete')))) {
         $this->error('Addon not found!');
     }
     $this->addon = Addon::fromActiveRow($row, $this->addonVotes);
     $this->addonVersions->rsort($this->addon->versions);
 }
 protected function startup()
 {
     parent::startup();
     if ($this->token && $this->addonId) {
         $this->error('Parameters token and addonId must not be present at the same time.', 409);
     }
     if ($this->token) {
         $this->addon = $this->manager->restoreAddon($this->token);
     } elseif ($this->addonId) {
         $deleted = $this->auth->isAllowed('addon', 'delete');
         $row = $this->addons->find($this->addonId, $deleted);
         if (!$row) {
             $this->error('Addon not found.');
         }
         $this->addon = Addon::fromActiveRow($row);
     }
     if ($this->addon && !$this->auth->isAllowed($this->addon, 'manage')) {
         $this->error('You are not allowed to manage this addon.', 403);
     }
 }
 /**
  * Post receive hook, updates addon info
  */
 public function actionPostReceive()
 {
     $post = $this->getRequest()->getPost();
     if (!isset($post['payload'], $post['username'], $post['apiToken'])) {
         $this->error('Invalid request.');
     }
     $response = $this->getHttpResponse();
     try {
         $payload = Json::decode($post['payload']);
         if (!isset($payload->repository->url)) {
             $response->setCode(IResponse::S400_BAD_REQUEST);
             $this->sendJson(array('status' => 'error', 'message' => 'Missing or invalid payload'));
         }
     } catch (\Nette\Utils\JsonException $e) {
         $this->error('Invalid request.');
     }
     $username = $post['username'];
     $token = $post['apiToken'];
     $user = $this->users->findOneByName($username);
     if (!$user || $user->apiToken !== $token) {
         $response->setCode(IResponse::S403_FORBIDDEN);
         $this->sendJson(array('status' => 'error', 'message' => 'Invalid credentials'));
     }
     if (!GitHubImporter::isValid($payload->repository->url)) {
         $response->setCode(IResponse::S400_BAD_REQUEST);
         $this->sendJson(array('status' => 'error', 'message' => 'Could not parse payload repository URL'));
     }
     $repositoryUrl = GitHubImporter::normalizeUrl($payload->repository->url);
     $row = $this->addons->findOneBy(array('repository' => $repositoryUrl));
     if (!$row) {
         $this->error('Addon not found.');
     }
     $addon = Addon::fromActiveRow($row);
     $userIdentity = $this->users->createIdentity($user);
     $importer = $this->importerManager->createFromUrl($addon->repository);
     $this->manager->updateVersions($addon, $importer, $userIdentity);
     $this->sendJson(array('status' => "success"));
 }