浏览代码

allow minimal BC only

euromark 12 年之前
父节点
当前提交
b70980798d
共有 3 个文件被更改,包括 38 次插入18 次删除
  1. 19 9
      Controller/QloginController.php
  2. 9 2
      Model/Qlogin.php
  3. 10 7
      Test/Case/Model/QloginTest.php

+ 19 - 9
Controller/QloginController.php

@@ -41,8 +41,9 @@ class QloginController extends ToolsAppController {
 			return $this->Common->autoRedirect($default);
 		}
 		//die(returns($entry));
-		$uid = $entry['CodeKey']['user_id'];
-		$url = $entry['CodeKey']['url'];
+		$alias = Configure::read('Qlogin.generator') ?: 'Token';
+		$uid = $entry[$alias]['user_id'];
+		$url = $entry[$alias]['url'];
 
 		if (!$this->Session->read('Auth.User.id')) {
 			if ($this->Common->manualLogin($uid)) {
@@ -64,6 +65,7 @@ class QloginController extends ToolsAppController {
 	 * - user_id
 	 * - url (base64encoded)
 	 *
+	 * @return void
 	 */
 	public function admin_index() {
 		if ($this->Common->isPosted()) {
@@ -87,21 +89,29 @@ class QloginController extends ToolsAppController {
 		$this->User = ClassRegistry::init(CLASS_USER);
 		$users = $this->User->find('list');
 
-		$this->CodeKey = ClassRegistry::init('Tools.CodeKey');
-		$qlogins = $this->CodeKey->find('count', array('conditions' => array('type' => 'qlogin')));
+		$this->Token = ClassRegistry::init('Tools.Token');
+		$qlogins = $this->Token->find('count', array('conditions' => array('type' => 'qlogin')));
 
 		$this->set(compact('users', 'qlogins'));
 	}
 
+	/**
+	 * QloginController::admin_listing()
+	 *
+	 * @return void
+	 */
 	public function admin_listing() {
 	}
 
+	/**
+	 * QloginController::admin_reset()
+	 *
+	 * @return void
+	 */
 	public function admin_reset() {
-		if (!$this->Common->isPosted()) {
-			throw new MethodNotAllowedException();
-		}
-		$this->CodeKey = ClassRegistry::init('Tools.CodeKey');
-		$this->CodeKey->deleteAll(array('type' => 'qlogin'));
+		$this->request->onlyAllow('post', 'delete');
+		$this->Token = ClassRegistry::init('Tools.Token');
+		$this->Token->deleteAll(array('type' => 'qlogin'));
 		$this->Common->flashMessage(__('Success'), 'success');
 		return $this->Common->autoRedirect(array('action' => 'index'));
 	}

+ 9 - 2
Model/Qlogin.php

@@ -7,7 +7,7 @@ App::uses('CakeSession', 'Model/Datasource');
 /**
  * Manage Quick Logins
  *
- * TODO: make it work with Token by default.
+ * TODO: Remove CodeKey BC
  *
  * @author Mark Scherer
  * @cakephp 2.x
@@ -17,7 +17,7 @@ class Qlogin extends ToolsAppModel {
 
 	public $useTable = false;
 
-	public $generator = 'CodeKey'; // TODO: switch to Token ASAP, then remove this
+	public $generator = 'Token'; // TODO: switch to Token ASAP, then remove this
 
 	public $validate = array(
 		'url' => array(
@@ -41,6 +41,13 @@ class Qlogin extends ToolsAppModel {
 		),
 	);
 
+	public function __construct($id = false, $table = null, $ds = null) {
+		if ($generator = Configure::read('Qlogin.generator')) {
+			$this->generator = $generator;
+		}
+		parent::__construct($id, $table, $ds);
+	}
+
 	/**
 	 * Qlogin::_useKey()
 	 *

+ 10 - 7
Test/Case/Model/QloginTest.php

@@ -21,21 +21,28 @@ class QloginTest extends MyCakeTestCase {
 	}
 
 	public function testGenerateDeprecated() {
+		$this->Qlogin->generator = 'CodeKey';
+
+		$this->CodeKey = ClassRegistry::init('Tools.CodeKey');
+		$count = $this->CodeKey->find('count');
+
 		$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));
+
+		$count2 = $this->CodeKey->find('count');
+		$this->assertTrue($count2 > $count);
 	}
 
 	public function testUseDeprecated() {
+		$this->Qlogin->generator = 'CodeKey';
+
 		$key = $this->Qlogin->generate(array('controller' => 'test', 'action' => 'foo', 'bar'), 1);
 		$res = $this->Qlogin->translate($key);
 		$this->assertTrue(is_array($res) && !empty($res));
@@ -49,8 +56,6 @@ class QloginTest extends MyCakeTestCase {
 	}
 
 	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));
@@ -66,8 +71,6 @@ 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));