public function onKernelResponse(FilterResponseEvent $event) { if (!$this->enabled) { return; } $isNoindex = $this->xRobotsTag->isNoindex(); $isNofollow = $this->xRobotsTag->isNofollow(); if (!$this->xRobotsTag->isExplicitlySet()) { $requiredUserRoles = $this->getRequiredUserRolesForRequest($event->getRequest()); foreach ($requiredUserRoles as $requiredUserRole) { if ($this->xRobotsTag->isNoindexForUserRole($requiredUserRole) || $this->xRobotsTag->isNofollowForUserRole($requiredUserRole)) { $isNoindex = $this->xRobotsTag->isNoindexForUserRole($requiredUserRole); $isNofollow = $this->xRobotsTag->isNofollowForUserRole($requiredUserRole); break; } } } $tags = []; if ($isNoindex) { $tags[] = 'noindex'; } if ($isNofollow) { $tags[] = 'nofollow'; } if (count($tags)) { $event->getResponse()->headers->set('X-Robots-Tag', implode(',', $tags)); } }
public function testUserRoles() { $roleUser = '******'; $roleAdmin = 'ROLE_ADMIN'; $xRobotsTag = new XRobotsTag(); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertFalse($xRobotsTag->isNofollowForUserRole($roleUser)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => true, 'nofollow' => true]]]); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleUser)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => false, 'nofollow' => true]]]); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleUser)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => true, 'nofollow' => false]]]); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertFalse($xRobotsTag->isNofollowForUserRole($roleUser)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => false, 'nofollow' => false]]]); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertFalse($xRobotsTag->isNofollowForUserRole($roleUser)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => false, 'nofollow' => true], $roleAdmin => ['noindex' => true, 'nofollow' => false]]]); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleAdmin)); $this->assertFalse($xRobotsTag->isNofollowForUserRole($roleAdmin)); $xRobotsTag = new XRobotsTag(['user_roles' => ['*' => ['noindex' => false, 'nofollow' => true]]]); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleUser)); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleAdmin)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleAdmin)); $xRobotsTag = new XRobotsTag(['user_roles' => ['*' => ['noindex' => true, 'nofollow' => true]]]); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleAdmin)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleAdmin)); $xRobotsTag = new XRobotsTag(['user_roles' => [$roleUser => ['noindex' => false, 'nofollow' => false], '*' => ['noindex' => true, 'nofollow' => true]]]); $this->assertFalse($xRobotsTag->isNoindexForUserRole($roleUser)); $this->assertFalse($xRobotsTag->isNofollowForUserRole($roleUser)); $this->assertTrue($xRobotsTag->isNoindexForUserRole($roleAdmin)); $this->assertTrue($xRobotsTag->isNofollowForUserRole($roleAdmin)); }
public function testExplicitValuesOverrideUserRoleRules() { $accessMapMock = $this->getAccessMapMock(); $accessMapMock->method('getPatterns')->willReturn([['ROLE_ADMIN']]); $headersSpy = $this->getHeadersSpy(); $headersSpy->expects($this->once())->method('set')->with($this->identicalTo('X-Robots-Tag'), $this->identicalTo('noindex')); $responseEventMock = $this->getResponseEventMock($headersSpy); $xRobotsTag = new XRobotsTag(['user_roles' => ['ROLE_ADMIN' => ['noindex' => true, 'nofollow' => true]]]); $xRobotsTag->setNoindex(true); $responseListener = new ResponseListener($xRobotsTag, $accessMapMock, true); $responseListener->onKernelResponse($responseEventMock); $headersSpy = $this->getHeadersSpy(); $headersSpy->expects($this->never())->method('set'); $responseEventMock = $this->getResponseEventMock($headersSpy); $xRobotsTag = new XRobotsTag(['user_roles' => ['ROLE_ADMIN' => ['noindex' => true, 'nofollow' => true]]]); $xRobotsTag->setNoindex(false); $responseListener = new ResponseListener($xRobotsTag, $accessMapMock, true); $responseListener->onKernelResponse($responseEventMock); }