开发者

Restrict choices shown in symfony admin generator edit multi-select

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_agai开发者_StackOverflow中文版n]
    "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.


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜