首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >限制symfony管理生成器编辑多选中显示的选项

问限制symfony管理生成器编辑多选中显示的选项
EN

Stack Overflow用户
提问于 2011-04-15 21:20:48
回答 1查看 874关注 0票数 0

我正在使用sfGuardUser模块的管理生成器。generator.yml文件的编辑部分如下所示:

代码语言:javascript
复制
edit:
  title: Editing User "%%username%%"
  display:
    "User":  [first_name, last_name, email_address, username, password, password_again]
    "Permissions and groups": [is_active, groups_list, sites_list]

现在,并不是每个用户都有权访问此表单,只有站点管理员才能允许站点管理员创建和更新自己的用户。在User和Site之间存在多对多关系。每个站点管理员也是用户,因此具有一组关联的站点。

我希望sites_list不显示所有站点,而只显示与站点管理员关联的站点,从而确保站点管理员无法将自己的用户放入与管理员不关联的站点。

在我看来,我需要用其他东西替换sites_list来做这件事,但我不知道在哪里以及如何进行这种改变。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-17 08:54:19

我想要做到这一点的唯一方法是从自动生成的表单中更改sites_lists小部件。例如,在您的示例中,您可以执行以下操作:

代码语言:javascript
复制
<!-- SitesTable -->
public function getByUser($userId){
     //create your query to find all sites from that user
      $userSites = $this->createQuery()->...
                        ->where('user_id = ?', $userId);

      //create the array
      $choices = array();
      foreach ( $userSites as $site ) {
          $choices[$site->getId()] = $site->getName();
      }

      return $choices;
}

<!-- sfGuardUserForm -->
class sfGuardUserForm extends BaseSfGuardUserForm{
    public function configure() {
      //unset the old sites_list
      unset($this['sites_list']);

      //obtain the user id (depends on how it's implemented, i'm not using sfGuard)
      $userId = sfContext::getInstance()->getUser()->getId(); 

      $choices = Doctrine::getTable('Sites')->getByUser($userId);

      //set the new widget filtered
      $this->setWidget('sites_list', new sfWidgetFormChoice(array('choices' => $choices)));
      $this->setValidator('sites_list', new sfValidatorChoice(array('choices' => array_keys($choices))));

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5677421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档