Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are looking for is probably a listener attached to the <a href="https://github.com/zendframework/zf2/blob/release-2.1.3/library/Zend/Mvc/MvcEvent.php#L24" rel="noreferrer"><code>Zend\Mvc\MvcEvent::EVENT_DISPATCH</code></a> event of your application. </p> <p>In order, here's what you have to do to block access to any action through an authentication adapter. First of all, define a factory that is responsible for producing your authentication adapter:</p> <pre class="lang-php prettyprint-override"><code>namespace MyApp\ServiceFactory; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\Authentication\Adapter\Http as HttpAdapter; use Zend\Authentication\Adapter\Http\FileResolver; class AuthenticationAdapterFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { $config = $serviceLocator-&gt;get('Config'); $authConfig = $config['my_app']['auth_adapter']; $authAdapter = new HttpAdapter($authConfig['config']); $basicResolver = new FileResolver(); $digestResolver = new FileResolver(); $basicResolver-&gt;setFile($authConfig['basic_passwd_file']); $digestResolver-&gt;setFile($authConfig['digest_passwd_file']); $adapter-&gt;setBasicResolver($basicResolver); $adapter-&gt;setDigestResolver($digestResolver); return $adapter; } } </code></pre> <p>This factory will basically give you a configured auth adapter, and abstract its instantiation logic away.</p> <p>Let's move on and attach a listener to our application's <code>dispatch</code> event so that we can block any request with invalid authentication headers:</p> <pre class="lang-php prettyprint-override"><code>namespace MyApp; use Zend\ModuleManager\Feature\ConfigProviderInterface; use Zend\ModuleManager\Feature\BootstrapListenerInterface; use Zend\EventManager\EventInterface; use Zend\Mvc\MvcEvent; use Zend\Http\Request as HttpRequest; use Zend\Http\Response as HttpResponse; class MyModule implements ConfigProviderInterface, BootstrapListenerInterface { public function getConfig() { // moved out for readability on SO, since config is pretty short anyway return require __DIR__ . '/config/module.config.php'; } public function onBootstrap(EventInterface $event) { /* @var $application \Zend\Mvc\ApplicationInterface */ $application = $event-&gt;getTarget(); $serviceManager = $application-&gt;getServiceManager(); // delaying instantiation of everything to the latest possible moment $application -&gt;getEventManager() -&gt;attach(function (MvcEvent $event) use ($serviceManager) { $request = $event-&gt;getRequest(); $response = $event-&gt;getResponse(); if ( ! ( $request instanceof HttpRequest &amp;&amp; $response instanceof HttpResponse )) { return; // we're not in HTTP context - CLI application? } /* @var $authAdapter \Zend\Authentication\Adapter\Http */ $authAdapter = $serviceManager-&gt;get('MyApp\AuthenticationAdapter'); $authAdapter-&gt;setRequest($request); $authAdapter-&gt;setResponse($response); $result = $adapter-&gt;authenticate(); if ($result-&gt;isValid()) { return; // everything OK } $response-&gt;setBody('Access denied'); $response-&gt;setStatusCode(HttpResponse::STATUS_CODE_401); $event-&gt;setResult($response); // short-circuit to application end return false; // stop event propagation }, MvcEvent::EVENT_DISPATCH); } } </code></pre> <p>And then the module default configuration, which in this case was moved to <code>MyModule/config/module.config.php</code>:</p> <pre class="lang-php prettyprint-override"><code>return array( 'my_app' =&gt; array( 'auth_adapter' =&gt; array( 'config' =&gt; array( 'accept_schemes' =&gt; 'basic digest', 'realm' =&gt; 'MyApp Site', 'digest_domains' =&gt; '/my_app /my_site', 'nonce_timeout' =&gt; 3600, ), 'basic_passwd_file' =&gt; __DIR__ . '/dummy/basic.txt', 'digest_passwd_file' =&gt; __DIR__ . '/dummy/digest.txt', ), ), 'service_manager' =&gt; array( 'factories' =&gt; array( 'MyApp\AuthenticationAdapter' =&gt; 'MyApp\ServiceFactory\AuthenticationAdapterFactory', ), ), ); </code></pre> <p>This is the essence of how you can get it done.</p> <p>Obviously, you need to place something like an <code>my_app.auth.local.php</code> file in your <code>config/autoload/</code> directory, with the settings specific to your current environment (please note that this file should NOT be committed to your SCM):</p> <pre class="lang-php prettyprint-override"><code>&lt;?php return array( 'my_app' =&gt; array( 'auth_adapter' =&gt; array( 'basic_passwd_file' =&gt; __DIR__ . '/real/basic_passwd.txt', 'digest_passwd_file' =&gt; __DIR__ . '/real/digest_passwd.txt', ), ), ); </code></pre> <p>Eventually, if you also want to have better testable code, you may want to move the listener defined as a closure to an own class implementing the <a href="https://github.com/zendframework/zf2/blob/release-2.1.3/library/Zend/EventManager/ListenerAggregateInterface.php" rel="noreferrer"><code>Zend\EventManager\ListenerAggregateInterface</code></a>.</p> <p>You can achieve the same results by using <a href="https://github.com/ZF-Commons/ZfcUser" rel="noreferrer"><code>ZfcUser</code></a> backed by a <code>Zend\Authentication\Adapter\Http</code>, combined with <a href="https://github.com/bjyoungblood/BjyAuthorize/" rel="noreferrer"><code>BjyAuthorize</code></a>, which handles the listener logic on unauthorized actions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload