Search code examples
symfony1admin-generator

Restrict objects available in symfony admin generator for all actions


One can change the query that is used for the list action in an admin generator configuration by using the table_method option. For example,

# apps/backend/modules/job/config/generator.yml
config:
  list:
    table_method: retrieveBackendJobList

But I want to restrict all the actions in the admin generator. In particular, I want to restrict all the objects displayed, edited, deleted, etc., to objects that have a particular attribute that may depend on the current day of the week or the time of the day.

I don't want to override the model class because for other apps I want the restriction to be different (or perhaps no restriction at all).

Where (i.e., which file(s)) and how can I make this change?


Solution

  • Probably via the table_method and the generator.yml features to add and remove action buttons you've already taken care of customizing the index (i.e. list) action.

    For the other actions, you might wish to create additional methods in the models for these custom queries. But the place to override the default behavior is in the actions.class.php file for the module.

    So in your example, you'd edit the apps/backend/modules/job/actions/actions.class.php file and write custom code for each action you need to alter.

    So for example you could change the delete behavior like this:

    # apps/backend/modules/job/actions/actions.class.php
    require_once dirname(__FILE__).'/../lib/jobGeneratorConfiguration.class.php';
    require_once dirname(__FILE__).'/../lib/jobGeneratorHelper.class.php';
    
    class jobActions extends autoJobActions
    { 
      /**
       * Override standard delete action.
       * @param sfWebRequest $request A request object
       */
      public function executeDelete(sfWebRequest $request) {
        if ($some_custom_condition) {
          $job = Doctrine_Core::getTable('job')->find($request->getParameter('id'));
          $job->delete();
          $this->getUser()->setFlash('notice', 'Record deleted.');
          return sfView::SUCCESS;
        } else {
          $this->getUser()->setFlash('error', 'You do not have permission to do that.');
          return sfView::ERROR;
        }
      }
    }