Skip to content

Commit

Permalink
Allow maintainers to delete packages if they have less than 50 instal…
Browse files Browse the repository at this point in the history
…ls, fixes #166
  • Loading branch information
Seldaek committed Aug 29, 2012
1 parent c7ada8a commit 67540d4
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/Packagist/WebBundle/Controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function viewPackageAction(Request $req, $name)
if ($maintainerForm = $this->createAddMaintainerForm($package)) {
$data['form'] = $maintainerForm->createView();
}
if ($deleteForm = $this->createDeletePackageForm()) {
if ($deleteForm = $this->createDeletePackageForm($package)) {
$data['deleteForm'] = $deleteForm->createView();
}

Expand Down Expand Up @@ -438,7 +438,7 @@ public function deletePackageAction(Request $req, $name)
throw new NotFoundHttpException('The requested package, '.$name.', was not found.');
}

$form = $this->createDeletePackageForm();
$form = $this->createDeletePackageForm($package);
$form->bind($req->request->get('form'));
if ($form->isValid()) {
$versionRepo = $doctrine->getRepository('PackagistWebBundle:Version');
Expand Down Expand Up @@ -603,11 +603,34 @@ private function createAddMaintainerForm($package)
}
}

private function createDeletePackageForm()
private function createDeletePackageForm(Package $package)
{
if ($this->get('security.context')->isGranted('ROLE_DELETE_PACKAGES')) {
return $this->createFormBuilder(array())->getForm();
if (!$user = $this->getUser()) {
return;
}

// super admins bypass additional checks
if (!$this->get('security.context')->isGranted('ROLE_DELETE_PACKAGES')) {
// non maintainers can not delete
if (!$package->getMaintainers()->contains($user)) {
return;
}

try {
/** @var $redis \Snc\RedisBundle\Client\Phpredis\Client */
$redis = $this->get('snc_redis.default');
$downloads = $redis->get('dl:'.$package->getId());
} catch (\Exception $e) {
return;
}

// more than 50 downloads = established package, do not allow deletion by maintainers
if ($downloads > 50) {
return;
}
}

return $this->createFormBuilder(array())->getForm();
}

private function createSearchForm()
Expand Down

0 comments on commit 67540d4

Please sign in to comment.