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)); }