Browse Source

Switch from using properties to config() for CookieComponent.

ADmad 12 years ago
parent
commit
38a39c94cc

+ 83 - 127
src/Controller/Component/CookieComponent.php

@@ -38,89 +38,34 @@ use Cake\Utility\Security;
 class CookieComponent extends Component {
 
 /**
- * The name of the cookie.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->name = 'CookieName';
- *
- * @var string
- */
-	public $name = 'CakeCookie';
-
-/**
- * The time a cookie will remain valid.
- *
- * Can be either integer Unix timestamp or a date string.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->time = '5 Days';
- *
- * @var mixed
+ * Default config
+ *
+ * - `name` - The name of the cookie.
+ * - `time` - The time a cookie will remain valid. Can be either integer
+ *   unix timestamp or a date string.
+ * - `path` - The path on the server in which the cookie will be available on.
+ *   If path is set to '/foo/', the cookie will only be available within the
+ *   /foo/ directory and all sub-directories such as /foo/bar/ of domain.
+ *   The default value is the entire domain.
+ * - `domain` - The domain that the cookie is available. To make the cookie
+ *   available on all subdomains of example.com set domain to '.example.com'.
+ * - `secure` - Indicates that the cookie should only be transmitted over a
+ *   secure HTTPS connection. When set to true, the cookie will only be set if
+ *   a secure connection exists.
+ * - `key` - Encryption key.
+ * - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only
+ *   are not accessible in JavaScript. Default false
+ * @var array
  */
-	public $time = null;
-
-/**
- * Cookie path.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->path = '/';
- *
- * The path on the server in which the cookie will be available on.
- * If public $cookiePath is set to '/foo/', the cookie will only be available
- * within the /foo/ directory and all sub-directories such as /foo/bar/ of domain.
- * The default value is the entire domain.
- *
- * @var string
- */
-	public $path = '/';
-
-/**
- * Domain path.
- *
- * The domain that the cookie is available.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->domain = '.example.com';
- *
- * To make the cookie available on all subdomains of example.com.
- * Set $this->Cookie->domain = '.example.com'; in your controller beforeFilter
- *
- * @var string
- */
-	public $domain = '';
-
-/**
- * Secure HTTPS only cookie.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->secure = true;
- *
- * Indicates that the cookie should only be transmitted over a secure HTTPS connection.
- * When set to true, the cookie will only be set if a secure connection exists.
- *
- * @var boolean
- */
-	public $secure = false;
-
-/**
- * Encryption key.
- *
- * Overridden with the controller beforeFilter();
- * $this->Cookie->key = 'SomeRandomString';
- *
- * @var string
- */
-	public $key = null;
-
-/**
- * HTTP only cookie
- *
- * Set to true to make HTTP only cookies. Cookies that are HTTP only
- * are not accessible in JavaScript.
- *
- * @var boolean
- */
-	public $httpOnly = false;
+	protected $_defaultConfig = [
+		'name' => 'CakeCookie',
+		'time' => null,
+		'path' => '/',
+		'domain' => '',
+		'secure' => false,
+		'key' => null,
+		'httpOnly' => false
+	];
 
 /**
  * Values stored in the cookie.
@@ -178,10 +123,14 @@ class CookieComponent extends Component {
  * @param array $config Array of config.
  */
 	public function __construct(ComponentRegistry $collection, $config = array()) {
-		$this->key = Configure::read('Security.salt');
 		parent::__construct($collection, $config);
-		if (isset($this->time)) {
-			$this->_expire($this->time);
+
+		if ($this->config('key')) {
+			$this->config('key', Configure::read('Security.salt'));
+		}
+
+		if ($this->config('time')) {
+			$this->_expire($this->config('time'));
 		}
 
 		$controller = $collection->getController();
@@ -205,9 +154,9 @@ class CookieComponent extends Component {
  * @return void
  */
 	public function startup(Event $event) {
-		$this->_expire($this->time);
+		$this->_expire($this->config('time'));
 
-		$this->_values[$this->name] = array();
+		$this->_values[$this->config('name')] = array();
 	}
 
 /**
@@ -231,7 +180,8 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::write
  */
 	public function write($key, $value = null, $encrypt = true, $expires = null) {
-		if (empty($this->_values[$this->name])) {
+		$cookieName = $this->config('name');
+		if (empty($this->_values[$cookieName])) {
 			$this->read();
 		}
 
@@ -253,20 +203,20 @@ class CookieComponent extends Component {
 			$firstName = $names[0];
 			$isMultiValue = (is_array($value) || count($names) > 1);
 
-			if (!isset($this->_values[$this->name][$firstName]) && $isMultiValue) {
-				$this->_values[$this->name][$firstName] = array();
+			if (!isset($this->_values[$cookieName][$firstName]) && $isMultiValue) {
+				$this->_values[$cookieName][$firstName] = array();
 			}
 
 			if (count($names) > 1) {
-				$this->_values[$this->name][$firstName] = Hash::insert(
-					$this->_values[$this->name][$firstName],
+				$this->_values[$cookieName][$firstName] = Hash::insert(
+					$this->_values[$cookieName][$firstName],
 					$names[1],
 					$value
 				);
 			} else {
-				$this->_values[$this->name][$firstName] = $value;
+				$this->_values[$cookieName][$firstName] = $value;
 			}
-			$this->_write('[' . $firstName . ']', $this->_values[$this->name][$firstName]);
+			$this->_write('[' . $firstName . ']', $this->_values[$cookieName][$firstName]);
 		}
 		$this->_encrypted = true;
 	}
@@ -282,29 +232,30 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
  */
 	public function read($key = null) {
-		$values = $this->_request->cookie($this->name);
-		if (empty($this->_values[$this->name]) && $values) {
-			$this->_values[$this->name] = $this->_decrypt($values);
+		$name = $this->config('name');
+		$values = $this->_request->cookie($name);
+		if (empty($this->_values[$name]) && $values) {
+			$this->_values[$name] = $this->_decrypt($values);
 		}
-		if (empty($this->_values[$this->name])) {
-			$this->_values[$this->name] = array();
+		if (empty($this->_values[$name])) {
+			$this->_values[$name] = array();
 		}
 		if ($key === null) {
-			return $this->_values[$this->name];
+			return $this->_values[$name];
 		}
 
 		if (strpos($key, '.') !== false) {
 			$names = explode('.', $key, 2);
 			$key = $names[0];
 		}
-		if (!isset($this->_values[$this->name][$key])) {
+		if (!isset($this->_values[$name][$key])) {
 			return null;
 		}
 
 		if (!empty($names[1])) {
-			return Hash::get($this->_values[$this->name][$key], $names[1]);
+			return Hash::get($this->_values[$name][$key], $names[1]);
 		}
-		return $this->_values[$this->name][$key];
+		return $this->_values[$name][$key];
 	}
 
 /**
@@ -340,22 +291,23 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete
  */
 	public function delete($key) {
-		if (empty($this->_values[$this->name])) {
+		$name = $this->config('name');
+		if (empty($this->_values[$name])) {
 			$this->read();
 		}
 		if (strpos($key, '.') === false) {
-			if (isset($this->_values[$this->name][$key]) && is_array($this->_values[$this->name][$key])) {
-				foreach ($this->_values[$this->name][$key] as $idx => $val) {
+			if (isset($this->_values[$name][$key]) && is_array($this->_values[$name][$key])) {
+				foreach ($this->_values[$name][$key] as $idx => $val) {
 					$this->_delete("[$key][$idx]");
 				}
 			}
 			$this->_delete("[$key]");
-			unset($this->_values[$this->name][$key]);
+			unset($this->_values[$name][$key]);
 			return;
 		}
 		$names = explode('.', $key, 2);
-		if (isset($this->_values[$this->name][$names[0]])) {
-			$this->_values[$this->name][$names[0]] = Hash::remove($this->_values[$this->name][$names[0]], $names[1]);
+		if (isset($this->_values[$name][$names[0]])) {
+			$this->_values[$name][$names[0]] = Hash::remove($this->_values[$name][$names[0]], $names[1]);
 		}
 		$this->_delete('[' . implode('][', $names) . ']');
 	}
@@ -370,18 +322,19 @@ class CookieComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::destroy
  */
 	public function destroy() {
-		if (empty($this->_values[$this->name])) {
+		$cookieName = $this->config('name');
+		if (empty($this->_values[$cookieName])) {
 			$this->read();
 		}
 
-		foreach ($this->_values[$this->name] as $name => $value) {
+		foreach ($this->_values[$cookieName] as $name => $value) {
 			if (is_array($value)) {
 				foreach ($value as $key => $val) {
-					unset($this->_values[$this->name][$name][$key]);
+					unset($this->_values[$cookieName][$name][$key]);
 					$this->_delete("[$name][$key]");
 				}
 			}
-			unset($this->_values[$this->name][$name]);
+			unset($this->_values[$cookieName][$name]);
 			$this->_delete("[$name]");
 		}
 	}
@@ -444,14 +397,15 @@ class CookieComponent extends Component {
  * @return void
  */
 	protected function _write($name, $value) {
+		$config = $this->config();
 		$this->_response->cookie(array(
-			'name' => $this->name . $name,
+			'name' => $config['name'] . $name,
 			'value' => $this->_encrypt($value),
 			'expire' => $this->_expires,
-			'path' => $this->path,
-			'domain' => $this->domain,
-			'secure' => $this->secure,
-			'httpOnly' => $this->httpOnly
+			'path' => $config['path'],
+			'domain' => $config['domain'],
+			'secure' => $config['secure'],
+			'httpOnly' => $config['httpOnly']
 		));
 
 		if (!empty($this->_reset)) {
@@ -467,14 +421,15 @@ class CookieComponent extends Component {
  * @return void
  */
 	protected function _delete($name) {
+		$config = $this->config();
 		$this->_response->cookie(array(
-			'name' => $this->name . $name,
+			'name' => $config['name'] . $name,
 			'value' => '',
 			'expire' => time() - 42000,
-			'path' => $this->path,
-			'domain' => $this->domain,
-			'secure' => $this->secure,
-			'httpOnly' => $this->httpOnly
+			'path' => $config['path'],
+			'domain' => $config['domain'],
+			'secure' => $config['secure'],
+			'httpOnly' => $config['httpOnly']
 		));
 	}
 
@@ -493,10 +448,10 @@ class CookieComponent extends Component {
 		}
 		$prefix = "Q2FrZQ==.";
 		if ($this->_type === 'rijndael') {
-			$cipher = Security::rijndael($value, $this->key, 'encrypt');
+			$cipher = Security::rijndael($value, $this->config('key'), 'encrypt');
 		}
 		if ($this->_type === 'aes') {
-			$cipher = Security::encrypt($value, $this->key);
+			$cipher = Security::encrypt($value, $this->config('key'));
 		}
 		return $prefix . base64_encode($cipher);
 	}
@@ -537,10 +492,10 @@ class CookieComponent extends Component {
 		}
 		$value = base64_decode(substr($value, strlen($prefix)));
 		if ($this->_type === 'rijndael') {
-			$plain = Security::rijndael($value, $this->key, 'decrypt');
+			$plain = Security::rijndael($value, $this->config('key'), 'decrypt');
 		}
 		if ($this->_type === 'aes') {
-			$plain = Security::decrypt($value, $this->key);
+			$plain = Security::decrypt($value, $this->config('key'));
 		}
 		return $this->_explode($plain);
 	}
@@ -578,4 +533,5 @@ class CookieComponent extends Component {
 		}
 		return $array;
 	}
+
 }

+ 37 - 31
tests/TestCase/Controller/Component/CookieComponentTest.php

@@ -48,12 +48,14 @@ class CookieComponentTest extends TestCase {
 		$this->Cookie = $controller->Cookie;
 		$this->request = $controller->request;
 
-		$this->Cookie->name = 'CakeTestCookie';
-		$this->Cookie->time = 10;
-		$this->Cookie->path = '/';
-		$this->Cookie->domain = '';
-		$this->Cookie->secure = false;
-		$this->Cookie->key = 'somerandomhaskeysomerandomhaskey';
+		$this->Cookie->config([
+			'name' => 'CakeTestCookie',
+			'time' => 10,
+			'path' => '/',
+			'domain' => '',
+			'secure' => false,
+			'key' => 'somerandomhaskeysomerandomhaskey'
+		]);
 
 		$event = new Event('Controller.startup', $this->Controller);
 		$this->Cookie->startup($event);
@@ -97,8 +99,8 @@ class CookieComponentTest extends TestCase {
 			'path' => '/'
 		);
 		$Cookie = new CookieComponent(new ComponentRegistry(), $settings);
-		$this->assertEquals($Cookie->time, $settings['time']);
-		$this->assertEquals($Cookie->path, $settings['path']);
+		$this->assertEquals($Cookie->config('time'), $settings['time']);
+		$this->assertEquals($Cookie->config('path'), $settings['path']);
 	}
 
 /**
@@ -107,7 +109,7 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testCookieName() {
-		$this->assertEquals('CakeTestCookie', $this->Cookie->name);
+		$this->assertEquals('CakeTestCookie', $this->Cookie->config('name'));
 	}
 
 /**
@@ -158,7 +160,7 @@ class CookieComponentTest extends TestCase {
 		);
 		$this->assertEquals('value', $this->Cookie->read('key'));
 
-		$this->Cookie->name = 'OtherTestCookie';
+		$this->Cookie->config('name', 'OtherTestCookie');
 		$this->assertEquals('other value', $this->Cookie->read('key'));
 	}
 
@@ -217,11 +219,11 @@ class CookieComponentTest extends TestCase {
 		$this->Cookie->write('key1', 'value1', false);
 		$this->Cookie->write('key2', 'value2', false);
 
-		$name = $this->Cookie->name . '[key1]';
+		$name = $this->Cookie->config('name') . '[key1]';
 		$result = $this->Controller->response->cookie($name);
 		$this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
 
-		$name = $this->Cookie->name . '[key2]';
+		$name = $this->Cookie->config('name') . '[key2]';
 		$result = $this->Controller->response->cookie($name);
 		$this->assertWithinMargin(time() + 10, $result['expire'], 2, 'Expiry time is wrong');
 	}
@@ -237,13 +239,13 @@ class CookieComponentTest extends TestCase {
 		$future->modify('+90 years');
 
 		$expected = array(
-			'name' => $this->Cookie->name . '[Testing]',
+			'name' => $this->Cookie->config('name') . '[Testing]',
 			'value' => 'value',
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => false);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
 
 		$this->assertEquals($future->format('U'), $result['expire'], '', 3);
 		unset($result['expire']);
@@ -257,18 +259,20 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteHttpOnly() {
-		$this->Cookie->httpOnly = true;
-		$this->Cookie->secure = false;
+		$this->Cookie->config([
+			'httpOnly' => true,
+			'secure' => false
+		]);
 		$this->Cookie->write('Testing', 'value', false);
 		$expected = array(
-			'name' => $this->Cookie->name . '[Testing]',
+			'name' => $this->Cookie->config('name') . '[Testing]',
 			'value' => 'value',
 			'expire' => time() + 10,
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => true);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
 		$this->assertEquals($expected, $result);
 	}
 
@@ -278,18 +282,20 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testDeleteHttpOnly() {
-		$this->Cookie->httpOnly = true;
-		$this->Cookie->secure = false;
+		$this->Cookie->config([
+			'httpOnly' => true,
+			'secure' => false
+		]);
 		$this->Cookie->delete('Testing', false);
 		$expected = array(
-			'name' => $this->Cookie->name . '[Testing]',
+			'name' => $this->Cookie->config('name') . '[Testing]',
 			'value' => '',
 			'expire' => time() - 42000,
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => true);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
 		$this->assertEquals($expected, $result);
 	}
 
@@ -316,16 +322,16 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteArrayValues() {
-		$this->Cookie->secure = false;
+		$this->Cookie->config('secure', false);
 		$this->Cookie->write('Testing', array(1, 2, 3), false);
 		$expected = array(
-			'name' => $this->Cookie->name . '[Testing]',
+			'name' => $this->Cookie->config('name') . '[Testing]',
 			'value' => '[1,2,3]',
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => false);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]');
 
 		$this->assertWithinMargin($result['expire'], time() + 10, 1);
 		unset($result['expire']);
@@ -338,18 +344,18 @@ class CookieComponentTest extends TestCase {
  * @return void
  */
 	public function testWriteMixedArray() {
-		$this->Cookie->encrypt = false;
+		$this->Cookie->config('encrypt', false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$expected = array(
-			'name' => $this->Cookie->name . '[User]',
+			'name' => $this->Cookie->config('name') . '[User]',
 			'value' => '{"name":"mark","email":"mark@example.com"}',
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => false
 		);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]');
 		unset($result['expire']);
 
 		$this->assertEquals($expected, $result);
@@ -357,14 +363,14 @@ class CookieComponentTest extends TestCase {
 		$this->Cookie->write('User.email', 'mark@example.com', false);
 		$this->Cookie->write('User', array('name' => 'mark'), false);
 		$expected = array(
-			'name' => $this->Cookie->name . '[User]',
+			'name' => $this->Cookie->config('name') . '[User]',
 			'value' => '{"name":"mark"}',
 			'path' => '/',
 			'domain' => '',
 			'secure' => false,
 			'httpOnly' => false
 		);
-		$result = $this->Controller->response->cookie($this->Cookie->name . '[User]');
+		$result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]');
 		unset($result['expire']);
 
 		$this->assertEquals($expected, $result);
@@ -738,7 +744,7 @@ class CookieComponentTest extends TestCase {
 		if (is_array($value)) {
 			$value = $this->_implode($value);
 		}
-		return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->key));
+		return "Q2FrZQ==." . base64_encode(Security::encrypt($value, $this->Cookie->config('key')));
 	}
 
 }