symfony1doctrinesymfony-1.4doctrine-1.2sfdoctrineguard

Symfony sfDoctrineGuardPlugin custom login query


I use symfony sfDoctrineGuardPlugin to manage authentication for both frontend users and backend users. It's fine, except that I don't want frontend users to be able to login to the backend app. I can setup credentials, but credentials are checked after a user gets authenticated. What I want is to have sigin in form to never validate for a user, that is not in a backend group. How can I do this?


Solution

  • I think I found a better solution. sfDoctrineGuard plugin has its own post validator that checks for an optional callable for user retrival.

    //app.yml
    all:
      sf_guard_plugin:
        retrieve_by_username_callable: sfGuardUser::getForBackend
    
    //sfGuardUser.class.php
    
      public static function getForBackend($username)
      {
        $query = Doctrine::getTable('sfGuardUser')->createQuery('u')
          ->leftJoin('u.Groups g')
          ->leftJoin('g.Permissions p')
          ->where('u.username = ? OR u.email_address = ?', array($username, $username))
          ->addWhere('u.is_active = ?', true)
          ->addWhere('p.name = ?', 'backend');
    
        return $query->fetchOne();
      }