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);
 }
 /**
  * @param \NetteAddons\Model\Addon
  * @return AddonForm
  */
 public function setAddon(Addon $addon)
 {
     $this->addon = $addon;
     if (!is_null($addon->repositoryHosting)) {
         if (is_null($addon->id)) {
             $this->removeComponent($this['repository']);
         }
         if ($addon->defaultLicense) {
             $this->removeComponent($this['defaultLicense']);
         }
     }
     if ($addon->composerFullName) {
         $this->removeComponent($this['composerFullName']);
     }
     $license = $addon->defaultLicense;
     if (is_string($license)) {
         if ($license === 'NOLICENSE') {
             $license = array();
         } else {
             $license = array_map('trim', explode(',', $license));
         }
     }
     if ($key = array_search('NOLICENSE', $license)) {
         unset($license[$key]);
     }
     $tagItems = $this['tags']->getItems();
     $tags = array();
     foreach ($addon->getTagsIds() as $id) {
         if (!isset($tagItems[$id])) {
             $tag = $this->tags->find($id);
             if ($tag) {
                 $tagItems[$tag->id] = $tag->name;
             }
         }
         if (isset($tagItems[$id])) {
             $tags[] = $id;
         }
     }
     $this['tags']->setItems($tagItems)->setDefaultValue($tags);
     $this->setDefaults(array('name' => $addon->name, 'shortDescription' => $addon->shortDescription, 'description' => $addon->description, 'descriptionFormat' => $addon->descriptionFormat, 'defaultLicense' => $license, 'repository' => $addon->repository, 'demo' => $addon->demo));
     return $this;
 }
 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"));
 }