@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).