Browse Source

Make Qlogin work with Token

euromark 12 years ago
parent
commit
359a471098
3 changed files with 74 additions and 20 deletions
  1. 39 15
      Model/Qlogin.php
  2. 2 2
      Model/Token.php
  3. 33 3
      Test/Case/Model/QloginTest.php

+ 39 - 15
Model/Qlogin.php

@@ -7,6 +7,8 @@ App::uses('CakeSession', 'Model/Datasource');
 /**
  * Manage Quick Logins
  *
+ * TODO: make it work with Token by default.
+ *
  * @author Mark Scherer
  * @cakephp 2.x
  * @license MIT
@@ -15,6 +17,8 @@ class Qlogin extends ToolsAppModel {
 
 	public $useTable = false;
 
+	public $generator = 'CodeKey'; // TODO: switch to Token ASAP, then remove this
+
 	public $validate = array(
 		'url' => array(
 			'notEmpty' => array(
@@ -34,36 +38,49 @@ class Qlogin extends ToolsAppModel {
 				'message' => 'valErrMandatoryField',
 				'last' => true
 			),
-			/*
-			'validateUnique' => array(
-				'rule' => array('validateUnique', array('url')),
-				'message' => 'key already exists',
-			),
-			*/
 		),
 	);
 
+	/**
+	 * Qlogin::_useKey()
+	 *
+	 * @param mixed $key
+	 * @return boolean Success
+	 */
 	protected function _useKey($key) {
-		if (!isset($this->CodeKey)) {
-			$this->CodeKey = ClassRegistry::init('Tools.CodeKey');
+		if (!isset($this->{$this->generator})) {
+			$this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
 		}
-		return $this->CodeKey->useKey('qlogin', $key);
+		return $this->{$this->generator}->useKey('qlogin', $key);
 	}
 
+	/**
+	 * Qlogin::_newKey()
+	 *
+	 * @param mixed $uid
+	 * @param mixed $content
+	 * @return string $key
+	 */
 	protected function _newKey($uid, $content) {
-		if (!isset($this->CodeKey)) {
-			$this->CodeKey = ClassRegistry::init('Tools.CodeKey');
+		if (!isset($this->{$this->generator})) {
+			$this->{$this->generator} = ClassRegistry::init('Tools.' . $this->generator);
 		}
-		return $this->CodeKey->newKey('qlogin', null, $uid, $content);
+		return $this->{$this->generator}->newKey('qlogin', null, $uid, $content);
 	}
 
+	/**
+	 * Qlogin::translate()
+	 *
+	 * @param mixed $key
+	 * @return array
+	 */
 	public function translate($key) {
 		$res = $this->_useKey($key);
 		if (!$res) {
 			return false;
 		}
-		$res['CodeKey']['content'] = unserialize($res['CodeKey']['content']);
-		$res['CodeKey']['url'] = Router::url($res['CodeKey']['content'], true);
+		$res[$this->generator]['content'] = unserialize($res[$this->generator]['content']);
+		$res[$this->generator]['url'] = Router::url($res[$this->generator]['content'], true);
 		return $res;
 	}
 
@@ -79,6 +96,12 @@ class Qlogin extends ToolsAppModel {
 		return $this->_newKey($uid, $content);
 	}
 
+	/**
+	 * Qlogin::urlByKey()
+	 *
+	 * @param string $key
+	 * @return string URL (absolute)
+	 */
 	public static function urlByKey($key) {
 		return Router::url(array('admin' => false, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'go', $key), true);
 	}
@@ -88,7 +111,8 @@ class Qlogin extends ToolsAppModel {
 	 * uses generate() internally to get the key
 	 *
 	 * @param mixed $url
-	 * @return string url (absolute)
+	 * @param midex $uid (optional)
+	 * @return string URL (absolute)
 	 */
 	public function url($url, $uid = null) {
 		if ($uid === null) {

+ 2 - 2
Model/Token.php

@@ -15,9 +15,9 @@ class Token extends ToolsAppModel {
 
 	public $order = array('Token.created' => 'DESC');
 
-	protected $defaultLength = 22;
+	public $defaultLength = 22;
 
-	protected $validity = MONTH;
+	public $validity = MONTH;
 
 	public $validate = array(
 		'type' => array(

+ 33 - 3
Test/Case/Model/QloginTest.php

@@ -8,7 +8,7 @@ class QloginTest extends MyCakeTestCase {
 
 	public $Qlogin = null;
 
-	public $fixtures = array('plugin.tools.code_key');
+	public $fixtures = array('plugin.tools.code_key', 'plugin.tools.token');
 
 	public function setUp() {
 		parent::setUp();
@@ -20,7 +20,37 @@ class QloginTest extends MyCakeTestCase {
 		$this->assertInstanceOf('Qlogin', $this->Qlogin);
 	}
 
+	public function testGenerateDeprecated() {
+		$url = Router::url(array('admin' => false, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'go'), true) . '/';
+		//debug($url);
+		$this->assertTrue(!empty($url));
+
+		$res = $this->Qlogin->url(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
+		//debug($res);
+		$this->assertTrue(is_string($res) && !empty($res));
+		$this->assertTrue(strpos($res, $url) === 0);
+
+		$res = $this->Qlogin->url('/test/foo/bar', 2);
+		//debug($res);
+		$this->assertTrue(is_string($res) && !empty($res));
+	}
+
+	public function testUseDeprecated() {
+		$key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
+		$res = $this->Qlogin->translate($key);
+		$this->assertTrue(is_array($res) && !empty($res));
+
+		$key = $this->Qlogin->generate('/test/foo/bar', 2);
+		$res = $this->Qlogin->translate($key);
+		$this->assertTrue(is_array($res) && !empty($res));
+
+		$res = $this->Qlogin->translate('foobar');
+		$this->assertFalse($res);
+	}
+
 	public function testGenerate() {
+		$this->Qlogin->generator = 'Token';
+
 		$url = Router::url(array('admin' => false, 'plugin' => 'tools', 'controller' => 'qlogin', 'action' => 'go'), true) . '/';
 		//debug($url);
 		$this->assertTrue(!empty($url));
@@ -36,6 +66,8 @@ class QloginTest extends MyCakeTestCase {
 	}
 
 	public function testUse() {
+		$this->Qlogin->generator = 'Token';
+
 		$key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
 		$res = $this->Qlogin->translate($key);
 		$this->assertTrue(is_array($res) && !empty($res));
@@ -48,6 +80,4 @@ class QloginTest extends MyCakeTestCase {
 		$this->assertFalse($res);
 	}
 
-	//TODO
-
 }