/**
  *
  * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
  * @return \Zend\Authentication\AuthenticationService
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $chain = new Chain();
     $chain->add($serviceLocator->get('doctrine.authenticationstorage.orm_default'), 2);
     $conn = $serviceLocator->get('doctrine.connection.orm_default');
     $chain->add(new DbalStorage($conn), 1);
     return new AuthenticationService($chain, $serviceLocator->get('doctrine.authenticationadapter.orm_default'));
 }
Пример #2
0
 /**
  * Ensure that chain will yield non-empty if one of its underlying storage
  * engines is non-empty.
  *
  * Make sure that storage engines with higher priority then the first non-empty
  * storage engine get populated with that same content.
  */
 public function testSuccessfullReadWillPopulateStoragesWithHigherPriority()
 {
     $emptyStorageA = $this->storageFactory();
     $emptyStorageB = $this->storageFactory();
     $storageC = $this->storageFactory(self::ID);
     $emptyStorageD = $this->storageFactory();
     $chain = new Chain();
     $chain->add($emptyStorageA);
     $chain->add($emptyStorageB);
     $chain->add($storageC);
     $chain->add($emptyStorageD);
     // Chain is non empty
     $this->assertFalse($chain->isEmpty());
     $this->assertEquals(self::ID, $chain->read());
     // Storage A and B are filled
     $this->assertFalse($emptyStorageA->isEmpty());
     $this->assertEquals(self::ID, $emptyStorageA->read());
     $this->assertFalse($emptyStorageA->isEmpty());
     $this->assertEquals(self::ID, $emptyStorageB->read());
     // Storage C and D remain identical
     $this->assertFalse($storageC->isEmpty());
     $this->assertEquals(self::ID, $storageC->read());
     $this->assertTrue($emptyStorageD->isEmpty());
 }
 /**
  * Registers services on the given app.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  *
  * @param Application $app
  */
 public function register(Application $app)
 {
     $app['zend.authentication.firewalls'] = isset($app['zend.authentication.firewalls']) ? $app['zend.authentication.firewalls'] : [];
     $app['zend.authentication.storage_factory.session'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             return new Session(isset($options['namespace']) ? $options['namespace'] : null, isset($options['member']) ? $options['member'] : null);
         });
     });
     $app['zend.authentication.storage_factory.non_persistent'] = $app->protect(function () use($app) {
         return $app->share(function () {
             return new NonPersistent();
         });
     });
     $app['zend.authentication.storage_factory.chain'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             $options = array_map(function ($option) use($app) {
                 /* @var $factory \Closure */
                 $factory = is_string($option) ? $app->raw($option) : $app['zend.authentication.storage_factory.' . $option['type']]($option['options']);
                 return $factory($app);
             }, $options);
             $chain = new Chain();
             foreach ($options as $option) {
                 $chain->add($option);
             }
             return $chain;
         });
     });
     $app['zend.authentication.adapter_factory.db'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             return new Database($app['db'], $options);
         });
     });
     $app['zend.authentication.adapter_factory.digest'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             return new Digest($options);
         });
     });
     $app['zend.authentication.adapter_factory.http_basic'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             $filename = $options['filename'];
             unset($options['filename']);
             return new Http\HttpBasic(new FileResolver($filename), $this['request_stack']->getCurrentRequest(), $options);
         });
     });
     $app['zend.authentication.adapter_factory.http_digest'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             $filename = $options['filename'];
             unset($options['filename']);
             return new Http\HttpDigest(new FileResolver($filename), $this['request_stack']->getCurrentRequest(), $options);
         });
     });
     $app['zend.authentication.adapter_factory.callback'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($options) {
             return new Callback($options['callback']);
         });
     });
     $app['zend.authentication.adapter_factory.chain'] = $app->protect(function (array $options) use($app) {
         return $app->share(function () use($app, $options) {
             $options = array_map(function ($option) use($app) {
                 /* @var $factory \Closure */
                 $factory = is_string($option) ? $app->raw($option) : $app['zend.authentication.adapter_factory.' . $option['type']]($option['options']);
                 return $factory($app);
             }, $options);
             return new \SilexCMF\ZendAuthentication\Adapter\Chain($options, isset($app['logger']) ? $app['logger'] : null);
         });
     });
     $app['zend.authentication.listener_factory.form'] = $app->protect(function ($name, array $options) use($app) {
         return $app->share(function () use($app, $name, $options) {
             return new FormListener($app['zend.authentication.service.' . $name], $options['pattern'], $options['whitelist'], new Container('zend.authentication.session.' . $name), $options['form']);
         });
     });
     $app['zend.authentication.listener_factory.http'] = $app->protect(function ($name, array $options) use($app) {
         return $app->share(function () use($app, $name, $options) {
             return new HttpListener($app['zend.authentication.service.' . $name], $options['pattern'], $options['whitelist']);
         });
     });
     $app['zend.authentication.listener_factory.logout'] = $app->protect(function ($name, array $options) use($app) {
         return $app->share(function () use($app, $name, $options) {
             $options = $this->logoutListenerOptions->resolve($options);
             return new LogoutListener($app['zend.authentication.service.' . $name], $options['logout_url'], $options['target_url']);
         });
     });
     $app['zend.authentication.service_factory'] = $app->protect(function ($name, array $options) use($app) {
         $options = $this->authenticationServiceOptions->resolve($options);
         $storageId = 'zend.authentication.storage.' . $name;
         if (!isset($app[$storageId])) {
             $app[$storageId] = is_string($options['storage']) ? $app->raw($options['storage']) : $app['zend.authentication.storage_factory.' . $options['storage']['type']]($options['storage']['options']);
         }
         $adapterId = 'zend.authentication.adapter.' . $name;
         if (!isset($app[$adapterId])) {
             $app[$adapterId] = is_string($options['adapter']) ? $app->raw($options['adapter']) : $app['zend.authentication.adapter_factory.' . $options['storage']['type']]($options['storage']['options']);
         }
         if (isset($options['form'])) {
             $app['zend.authentication.listener.form.' . $name] = $app['zend.authentication.listener_factory.form']($options);
         }
         if (isset($options['http'])) {
             $app['zend.authentication.listener.http.' . $name] = $app['zend.authentication.listener_factory.http']();
         }
         if (isset($options['logout'])) {
             $app['zend.authentication.listener.logout.' . $name] = $app['zend.authentication.listener_factory.logout']($options['logout']);
         }
         return $app->share(function () use($app, $name, $options) {
             return new AuthenticationService($app['zend.authentication.storage.' . $name], $app['zend.authentication.adapter.' . $name]);
         });
     });
 }