0

原谅我的英语。

我想不是从动作端而是从其他功能重定向。

控制器

public function editAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getEntityManager();
    $article = $em->getRepository('MySampleBundle:Article')->find($id);

    // TODO:
    // Since other actions have the same processing,
    // I would like to do check work in other function. 
    // And when inaccurate,
    // I want to make it move from that function to other page. 
    $this->is_edit_granted($article);

    $form = $this->createForm(new ArticleType(), $article);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            // ...
        }
    }

    return $this->render('MySampleBundle:Article:edit.html.twig', array(
        'form' => $form->createView(),
        'article' => $article,
    ));
}

public function is_edit_granted($article)
{
    // TODO:
    // I check not at the action side but at this place,
    // and want to make it move from this function to other page. 

    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        return $this->redirect( // doesn't work here
            $this->generateUrl('home', array(
                "id" => $article->getId()
            ))
        );
    }
}

我也尝试过类似的代码:

use Symfony\Component\HttpFoundation\RedirectResponse;

class SampleController extends Controller
{
    // ...

    public function editAction(Request $request, $id)
    {
        // ...

        $this->is_edit_granted($article);

        // ...
    }

    public function is_edit_granted($article)
    {
        if (!$article) {
            throw $this->createNotFoundException('No article found.');
        } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
            return new RedirectResponse(
                $this->generateUrl('home', array(
                    "id" => $article->getId()
                ))
            );
        }
    }
}

但它不起作用。

它在 Symfony 2.1.2 环境中执行。我怎样才能做到这一点?或者,有没有更好的方法?

4

2 回答 2

3

执行以下操作:

public function editAction(Request $request, $id)
{
    // ...

    $response = $this->is_edit_granted($article);
    if ($response) return $response;

    // ...
}

public function is_review_granted($article)
{
    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        return new RedirectResponse(
            $this->generateUrl('home', array(
                "id" => $article->getId()
            ))
        );
    }
    return null;
}
于 2012-10-17T09:27:32.227 回答
1

is_review_granted在不返回RedirectResponse表单的情况下无法从 重定向editAction。所以卡洛斯格拉纳多斯的答案是正确的。

另一种选择是在 is_review_granted 方法中抛出 AccessDeniedException :

public function is_review_granted($article)
{
    if (!$article) {
        throw $this->createNotFoundException('No article found.');
    } else if ( $article->getAuthor() != $this->getUser()->getId() ) {
        throw new Symfony\Component\Security\Core\Exception\AccessDeniedException('no acces');
    }
}

您还可以查看一些更深入的解决方案,例如ACLSecurityVoters

于 2012-10-17T10:57:25.383 回答