Members works perfectly with the LuceneSearch Bundle. In fact, there are already two build-in events, which will append some data to the crawler:
lucene_search.task.parser.asset_restriction
: This Event will add the asset restriction to the parserlucene_search.frontend.restriction_context
: This Event will add some restriction info to the frontend search.
But those events are useless unless you let the LuceneSearch Bundle know about it. So, there is still work to do: To allow LuceneSearch to crawl all the restricted routes, you need to implement a guardian. Don't worry, it's very easy!
Simple add the MembersBundle\Security\LuceneSearchAuthenticator
service to your app/config/config.yml
.
This authenticator comes with the Members Bundle, so there is nothing else to do. From now on, your system will watch every request for a lucene search crawl event.
Feel free to add your custom authenticator if needed.
security:
firewalls:
members_fe:
guard:
authenticators:
- MembersBundle\Security\LuceneSearchAuthenticator
You're almost there. As a last step you need to pass the credentials to each crawler request, so the Members Guard can identify your crawler.
- Create a Members User (for example lucene_search_crawler). Select all groups and save it.
- Create a EventListener in
AppBundle\EventListener
(for example LuceneSearchCrawlerHeader)
<?php
namespace AppBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use LuceneSearchBundle\Event\CrawlerRequestHeaderEvent;
class LuceneSearchCrawlerHeader implements EventSubscriberInterface
{
protected $userName;
protected $password;
public function __construct($userName = NULL, $password = NULL)
{
$this->userName = $userName;
$this->password = $password;
}
public static function getSubscribedEvents()
{
return [
'lucene_search.task.crawler.request_header' => 'addAuthHeader'
];
}
public function addAuthHeader(CrawlerRequestHeaderEvent $event)
{
$event->addHeader([
'name' => 'x-lucene-search-authorization',
'value' => 'Basic ' . base64_encode($this->userName . ':' . $this->password),
'identifier' => 'lucene-search-auth'
]);
}
}
- Register a EventListener in
app/config/config.yml
:
# credentials of the new lucene search user
parameters:
lucene_search_user_name: 'lucene_search_crawler'
lucene_search_password: 'crawler@universe.org'
services:
_defaults:
autowire: true
public: false
AppBundle\EventListener\LuceneSearchCrawlerHeader:
arguments:
$userName: '%lucene_search_user_name%'
$password: '%lucene_search_password%'
tags:
- { name: kernel.event_subscriber }
- Done! Re-Run your crawler. All your restricted pages should be available in the index now. Don't worry, they are save since the Members Bundle automatically protects the user frontend search (check top of this page).