1
votes

I'm using Symfony 1.4 backend to Create / Update users with sfGuardUser Plugin, which contains users from with different permissions.

I'm using another table, with a 1-1 relation with sfGuardUser. In this table I store information relative to a specific type of user differentiated by his permissions.

Symfony has done its job and has generated a select Field with all the users stored in sfGuardUser.

I'd like to filter this list in order to show only sfGuardUsers with a specific permission.

I found a lot of articles an ressources on how to create specific filters in the backend but none on how solve my problem.

2
Does this specific permission has to be dynamic ?j0k
The permission is not dynamic. The related table comes with specific information for a specific permission (actually a type of user with dedicated application and modules). @Michal 's answer seems to be the good one, i'll check it tomorrow. Thanks !Yann Kastelnik

2 Answers

1
votes

@j0k's question is vital for the issue. It's fairly easy to achieve what you want if you'll always need the same permission. The solution gets a bit tricky if you want the permission to be dynamic.

In the former case you'll have to edit the Form class which is used by actions in backend (new and edit) for the table related to sgGuardUser. You'l have to edit the choice widget responsible for retrieving the list of users: (assuming that the relation column is user_id and that you're using Doctrine):

$this->widgetSchema['user_id']->setOption('table_method' => 'retrieveForPermissionX');

Then in the sfGuardUserTable.class.php you will have to add the function which will prepare the proper query:

public function retrieveForPermissionX()
{
    return $this->createQuery('u')
        ->innerJoin('u.sfGuardUserPermission up')
        ->innerJoin('u.Permissions p')
        ->where('p.name = "PermissionX"')
        ->orderBy('u.first_name ASC');
} 

As I said it gets more tricky if you need to use different permissions. The basics is the same - you have to select proper users for the widget inside the form, but you'll have to somehow tell the form which permission to use.

I think you would have to override the auto-generated new and edit actions. Pass the needed permission as a paramater in URL (or use specific URL for given permissions you need) and inside the action pass the permissions as an option to your Form:

$this->form = new UserInfoForm(null, array('permissions' => $permissions));

Then you can use this information to invoke proper table method on the choice widget (or generate a query object - alternatively to the table method you can also pass a query object which will be used to retrieve the objects for the widget).

1
votes

Exactly what i was looking for. I've made a few modifications on the query and code as detailled above :

In lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUserTable.class.php

public function retrieveForPermissionX()
{
     return $this->createQuery('u')
          ->leftJoin('u.sfGuardUserPermission up')
          ->innerJoin('u.Permissions p')
          ->where('p.name = "PermissionX"')
          ->orderBy('u.first_name ASC');             
}

In apps/your_app_name/modules/your_module_name/actions/actions.class.php

public function executeNew(sfWebRequest $request)
{
    $this->table_client = new tableClient();
    $this->form = new TableClientForm($this->table_client);

    $widgetSchema = $this->form->getWidgetSchema();
    $widgetSchema['sf_guard_user_id']->setOption('table_method', 'retrieveForPermissionX');
}