限制 symfony admin 生成器中显示的选择编辑多选

发布于 2024-11-02 02:40:30 字数 618 浏览 5 评论 0原文

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

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]

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

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

在我看来,我需要用其他东西替换 sites_list 才能做到这一点,但我不知道在哪里以及如何进行此更改。

I am using the admin generator for the sfGuardUser module. The edit portion of the generator.yml file looks like this:

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]

Now, not every user will have access to this form, only site administrators allowing site administrators to create and update their own users. There is a many-to-many relation between User and Site. Each site administrator is also a user and as such has a set of associated sites.

I would like sites_list to not show ALL sites, but rather, only the sites the site administrator is associated with thereby ensuring that a site administrator cannot put one of her own users into a site the administrator is not associated with.

It seems to me I need to replace sites_list with something else to do this, but I do not know where and how to make this change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2024-11-09 02:40:30

我认为实现这一点的唯一方法是从自动生成的表单中更改sites_lists小部件。例如,就您而言,您可以执行以下操作:

<!-- 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))));

}

The only way that I think to to this is by changing the sites_lists widget from the Autogenerated Form. In your case, for example, you could do something like:

<!-- 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))));

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文