在我的Symfony 2应用程序中,我有3个不同的用户角色,可以访问后端管理部分:
role_hierarchy:
ROLE_STAFF: ROLE_USER
ROLE_MODERATOR: ROLE_STAFF
ROLE_ADMIN: ROLE_MODERATOR
Run Code Online (Sandbox Code Playgroud)
对于像这样的路线http://example.org/admin/post/,我希望我的应用程序根据用户角色显示不同的信息,这意味着3个控制器绑定到唯一的路径.
处理这个问题的最佳方法是什么?
我在考虑一些解决方案,但似乎没有一个对我好:
一个控制器,在每个动作中我只测试用户角色:
<?php
/**
* @Route("/admin/post")
*/
class PostController extends Controller
{
/**
* Lists all post entities.
*
* @Route("/", name="post_index")
* @Template()
* @Secure(roles="ROLE_STAFF")
*/
public function indexAction()
{
$user = $this->get('security.context')->getToken()->getUser();
if ($this->get('security.context')->isGranted('ROLE_STAFF')) {
// Do ROLE_STAFF related stuff
} else if ($this->get('security.context')->isGranted('ROLE_MODERATOR')) {
// Do ROLE_MODERATOR related stuff
} else if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
// Do ROLE_ADMIN related stuff
} …Run Code Online (Sandbox Code Playgroud)我之前从未使用过Javascript,但现在我需要在我的Symfony-Project(Symfony 1.4)中使用它,并且在添加代码以使其工作的地方存在一些问题:
我在模块"aktionLimit"中有一个模板(indexSuccess),其中我显示了类"Aktion"的Object变量的值:
<p>Value1: <?php echo $aktion->getValue1() ?></p>
<p>Value2: <?php echo $aktion->getValue2() ?></p>
<p>Value3: <?php echo $aktion->getValue3() ?></p>
<?php include_partial('form', array('form' => $form)) ?>
Run Code Online (Sandbox Code Playgroud)
这是executeIndex:
$this->aktion = AktionlimitPeer::getByName($this->getUser()->getAttribute('aktion'));
$aktionParam = new Aktion();
$aktionParam->setName($this->aktion->getName());
$this->form = new AktionlimitForm(array(), array("aktion" => $aktionParam));
Run Code Online (Sandbox Code Playgroud)
AktionLimitForm看起来像这样:
$aktionForm = new AktionForm($this->getOption('aktion'), array("limit" => true));
$this->embedForm('Aktion', $aktionForm);
$this->widgetSchema['neuerWert']= new sfWidgetFormInputText();
Run Code Online (Sandbox Code Playgroud)
嵌入的AktionForm只有一个元素(一个下拉框,其中包含"Aktion"对象的唯一名称):
$this->widgetSchema['name'] = new sfWidgetFormChoice(array('choices' => AktionPeer::getTypes()),array('onchange' => '???'));
Run Code Online (Sandbox Code Playgroud)
每当用户在下拉框中选择另一个"aktion"时,我想在executeIndex中加载当前对象"aktion"(由名称引用)并在indexSuccess中显示其值(值1-3).(提交时会导致另一个动作.)
我需要一个在onChange-Event中调用executeIndex的函数.我可能要在web/js-folder中声明该函数,这是对的吗?但是如何从表单中调用函数?我如何在JavaScript函数中调用一个动作?
我希望你理解我写的东西 - 今天我用英语解释这个问题有些困难:)有人可以帮忙吗?