浏览代码

Made editing of repository URL working.

 * Added updatePackage() method, which updates the package from the
   repository.
 * Added authorization to the controller action.
Christoph Hochstrasser 13 年之前
父节点
当前提交
2ab8400346

+ 46 - 8
src/Packagist/WebBundle/Controller/PackageController.php

@@ -2,12 +2,21 @@
 
 
 namespace Packagist\WebBundle\Controller;
 namespace Packagist\WebBundle\Controller;
 
 
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 
 
+use Packagist\WebBundle\Entity\Package;
+use Packagist\WebBundle\Package\Updater;
+
+use Composer\IO\NullIO;
+use Composer\Factory;
+use Composer\Repository\VcsRepository;
+
 class PackageController extends Controller
 class PackageController extends Controller
 {
 {
     /**
     /**
@@ -20,12 +29,15 @@ class PackageController extends Controller
      */
      */
     public function editAction(Request $req, $name)
     public function editAction(Request $req, $name)
     {
     {
-        $package = $this->getDoctrine()
-            ->getRepository('PackagistWebBundle:Package')
-            ->findOneByName($name);
+        $packages = $this->getDoctrine()->getRepository('PackagistWebBundle:Package');
+        $package = $packages->findOneByName($name);
 
 
         if (!$package) {
         if (!$package) {
-            throw new NotFoundHttpException("The requested package, $name, could not be found.");
+            throw $this->createNotFoundException("The requested package, $name, could not be found.");
+        }
+
+        if (!$package->getMaintainers()->contains($this->getUser()) && !$this->get('security.context')->isGranted('ROLE_UPDATE_PACKAGES')) {
+            throw new AccessDeniedException;
         }
         }
 
 
         $form = $this->createFormBuilder($package)
         $form = $this->createFormBuilder($package)
@@ -33,14 +45,40 @@ class PackageController extends Controller
             ->getForm();
             ->getForm();
 
 
         if ('POST' === $req->getMethod()) {
         if ('POST' === $req->getMethod()) {
+            $package->setEntityRepository($packages);
+
             $form->bindRequest($req);
             $form->bindRequest($req);
 
 
-            if ($form->isValid()) {
-                // Save
+            if ($this->get("validator")->validateProperty($package, "repository")) {
+                $em = $this->getDoctrine()->getEntityManager();
+                $em->persist($package);
+                $em->flush();
+
+                $this->updatePackage($package);
+
+                $this->get("session")->setFlash("notice", "Changes saved.");
+
+                return $this->redirect(
+                    $this->generateUrl("view_package", array("name" => $package->getName()))
+                );
             }
             }
         }
         }
 
 
-        return array("package" => $package, "form" => $form->createView());
+        return array(
+            "package" => $package, "form" => $form->createView()
+        );
+    }
+
+    private function updatePackage(Package $package)
+    {
+        $doctrine = $this->getDoctrine();
+
+        set_time_limit(3600);
+        $updater = $this->get('packagist.package_updater');
+
+        $config = Factory::createConfig();
+        $repository = new VcsRepository(array('url' => $package->getRepository()), new NullIO, $config);
+        $updater->update($package, $repository, Updater::UPDATE_TAGS);
     }
     }
 }
 }
 
 

+ 0 - 1
src/Packagist/WebBundle/Resources/views/Package/edit.html.twig

@@ -7,7 +7,6 @@
 {% block content %}
 {% block content %}
     <div class="box clearfix">
     <div class="box clearfix">
         <h1>
         <h1>
-            <a href="{{ path('view_package', {'name': package.name}) }}">&larr; Back</a>
             Edit {{ package.name }}
             Edit {{ package.name }}
         </h1>
         </h1>