Restrict choices shown in symfony admin generator edit multi-select

孤街醉人 提交于 2019-12-12 06:04:44

问题


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.


回答1:


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

}


来源:https://stackoverflow.com/questions/5677421/restrict-choices-shown-in-symfony-admin-generator-edit-multi-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!