Skip to content

Commit

Permalink
Merge pull request #119 from juulhobert/master
Browse files Browse the repository at this point in the history
Configurable cookie settings
  • Loading branch information
suncat2000 committed Sep 14, 2017
2 parents fd5e979 + 91692a3 commit b768902
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 5 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function getConfigTreeBuilder()
->end()
->end()
->scalarNode('cookie_key')->defaultValue(DeviceView::COOKIE_KEY_DEFAULT)->cannotBeEmpty()->end()
->scalarNode('cookie_path')->defaultValue(DeviceView::COOKIE_PATH_DEFAULT)->cannotBeEmpty()->end()
->scalarNode('cookie_domain')->defaultValue(DeviceView::COOKIE_DOMAIN_DEFAULT)->cannotBeEmpty()->end()
->booleanNode('cookie_secure')->defaultValue(DeviceView::COOKIE_SECURE_DEFAULT)->end()
->booleanNode('cookie_httponly')->defaultValue(DeviceView::COOKIE_HTTP_ONLY_DEFAULT)->end()
->scalarNode('cookie_expire_datetime_modifier')->defaultValue(DeviceView::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT)->cannotBeEmpty()->end()
->scalarNode('switch_param')->defaultValue(DeviceView::SWITCH_PARAM_DEFAULT)->cannotBeEmpty()->end()
->scalarNode('mobile_detector_class')->defaultValue('SunCat\MobileDetectBundle\DeviceDetector\MobileDetector')->cannotBeEmpty()->end()
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/MobileDetectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('mobile_detect.switch_device_view.save_referer_path', $config['switch_device_view']['save_referer_path']);

$container->setParameter('mobile_detect.cookie_key', $config['cookie_key']);
$container->setParameter('mobile_detect.cookie_path', $config['cookie_path']);
$container->setParameter('mobile_detect.cookie_domain', $config['cookie_domain']);
$container->setParameter('mobile_detect.cookie_secure', $config['cookie_secure']);
$container->setParameter('mobile_detect.cookie_httponly', $config['cookie_httponly']);
$container->setParameter('mobile_detect.cookie_expire_datetime_modifier', $config['cookie_expire_datetime_modifier']);
$container->setParameter('mobile_detect.switch_param', $config['switch_param']);

Expand Down
122 changes: 117 additions & 5 deletions Helper/DeviceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ class DeviceView
const VIEW_FULL = 'full';
const VIEW_NOT_MOBILE = 'not_mobile';

const COOKIE_KEY_DEFAULT = 'device_view';
const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT = '1 month';
const SWITCH_PARAM_DEFAULT = 'device_view';
const COOKIE_KEY_DEFAULT = 'device_view';
const COOKIE_PATH_DEFAULT = '/';
const COOKIE_DOMAIN_DEFAULT = '';
const COOKIE_SECURE_DEFAULT = false;
const COOKIE_HTTP_ONLY_DEFAULT = true;
const COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT = '1 month';
const SWITCH_PARAM_DEFAULT = 'device_view';

/**
* @var Request
Expand All @@ -52,6 +56,26 @@ class DeviceView
*/
protected $cookieKey = self::COOKIE_KEY_DEFAULT;

/**
* @var string
*/
protected $cookiePath = self::COOKIE_PATH_DEFAULT;

/**
* @var string
*/
protected $cookieDomain = self::COOKIE_DOMAIN_DEFAULT;

/**
* @var bool
*/
protected $cookieSecure = self::COOKIE_SECURE_DEFAULT;

/**
* @var bool
*/
protected $cookieHttpOnly = self::COOKIE_HTTP_ONLY_DEFAULT;

/**
* @var string
*/
Expand Down Expand Up @@ -312,7 +336,87 @@ public function getCookieKey()
}

/**
* Setter of SwitchParam
* Getter of CookiePath.
*
* @return string
*/
public function getCookiePath()
{
return $this->cookiePath;
}

/**
* Setter of CookiePath.
*
* @param string $cookiePath
*/
public function setCookiePath($cookiePath)
{
$this->cookiePath = $cookiePath;
}

/**
* Getter of CookieDomain.
*
* @return string
*/
public function getCookieDomain()
{
return $this->cookieDomain;
}

/**
* Setter of CookieDomain.
*
* @param string $cookieDomain
*/
public function setCookieDomain($cookieDomain)
{
$this->cookieDomain = $cookieDomain;
}

/**
* Is the cookie secure.
*
* @return bool
*/
public function isCookieSecure()
{
return $this->cookieSecure;
}

/**
* Setter of CookieSecure.
*
* @param bool $cookieSecure
*/
public function setCookieSecure($cookieSecure)
{
$this->cookieSecure = $cookieSecure;
}

/**
* Is the cookie http only.
*
* @return bool
*/
public function isCookieHttpOnly()
{
return $this->cookieHttpOnly;
}

/**
* Setter of CookieHttpOnly.
*
* @param bool $cookieHttpOnly
*/
public function setCookieHttpOnly($cookieHttpOnly)
{
$this->cookieHttpOnly = $cookieHttpOnly;
}

/**
* Setter of SwitchParam.
*
* @param string $switchParam
*/
Expand Down Expand Up @@ -362,7 +466,15 @@ protected function createCookie($value)
$expire = new \Datetime(self::COOKIE_EXPIRE_DATETIME_MODIFIER_DEFAULT);
}

return new Cookie($this->getCookieKey(), $value, $expire);
return new Cookie(
$this->getCookieKey(),
$value,
$expire,
$this->getCookiePath(),
$this->getCookieDomain(),
$this->isCookieSecure(),
$this->isCookieHttpOnly()
);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/mobile_detect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
<call method="setCookieKey">
<argument>%mobile_detect.cookie_key%</argument>
</call>
<call method="setCookiePath">
<argument>%mobile_detect.cookie_path%</argument>
</call>
<call method="setCookieDomain">
<argument>%mobile_detect.cookie_domain%</argument>
</call>
<call method="setCookieSecure">
<argument>%mobile_detect.cookie_secure%</argument>
</call>
<call method="setCookieHttpOnly">
<argument>%mobile_detect.cookie_httponly%</argument>
</call>
<call method="setSwitchParam">
<argument>%mobile_detect.switch_param%</argument>
</call>
Expand Down
26 changes: 26 additions & 0 deletions Tests/Helper/DeviceViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_MockObject_MockBuilder;
use SunCat\MobileDetectBundle\Helper\DeviceView;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;

Expand Down Expand Up @@ -405,6 +406,31 @@ public function getRedirectResponseWithCookieViewMobile()
}
}

/**
* @test
*/
public function getRedirectResponseAndCheckCookieSettings()
{
$this->request->query = new ParameterBag();
$deviceView = new DeviceView($this->requestStack);
$deviceView->setCookiePath('/test');
$deviceView->setCookieDomain('example.com');
$deviceView->setCookieSecure(true);
$deviceView->setCookieHttpOnly(false);

$response = $deviceView->getRedirectResponse(DeviceView::VIEW_MOBILE, 'http://mobilesite.com', 302);
$this->assertInstanceOf('SunCat\MobileDetectBundle\Helper\RedirectResponseWithCookie', $response);
$this->assertEquals(302, $response->getStatusCode());

/** @var Cookie[] $cookies */
$cookies = $response->headers->getCookies();
$this->assertEquals(1, count($cookies));
$this->assertEquals('/test', $cookies[0]->getPath());
$this->assertEquals('example.com', $cookies[0]->getDomain());
$this->assertTrue($cookies[0]->isSecure());
$this->assertFalse($cookies[0]->isHttpOnly());
}

/**
* @test
*/
Expand Down

0 comments on commit b768902

Please sign in to comment.