ソースを参照

geocoder tests

euromark 13 年 前
コミット
ef9bdb6b0c

+ 36 - 26
Controller/Component/AuthExtComponent.php

@@ -24,24 +24,28 @@ App::uses('AuthComponent', 'Controller/Component');
  * - auto-raises login counter and sets last_login date
  * - preps the session data according to completeAuth() method (adds parent data etc)
  * - dynamic login scope validation
- * 
+ *
  * @author Mark Scherer
- * @cakephp 2.0
+ * @cakephp 2.x
  * @license MIT
  * 2011-12-18 ms
  */
 class AuthExtComponent extends AuthComponent {
 
 	public $intermediateModel = 'RoleUser';
+
 	public $roleModel = 'Role';
 
 	public $fieldKey = 'role_id';
-	
+
 	public $loginAction = array('controller' => 'account', 'action' => 'login', 'admin' => false, 'plugin' => false);
+
 	public $loginRedirect = array('controller' => 'overview', 'action' => 'home', 'admin' => false, 'plugin' => false);
+
 	public $autoRedirect = false;
+
 	public $loginError = null;
-	
+
 	public $settings = array(
 		'multi' => null, # null=auto - yes/no multiple roles (HABTM table between users and roles)
 		'parentModelAlias' => USER_ROLE_KEY,
@@ -53,10 +57,10 @@ class AuthExtComponent extends AuthComponent {
 
 
 	public function __construct(ComponentCollection $Collection, $settings = array()) {
-		$settings = array_merge($this->settings, (array) $settings, (array) Configure::read('Auth'));
+		$settings = array_merge($this->settings, (array)$settings, (array)Configure::read('Auth'));
 		$this->Controller = $Collection->getController();
 		parent::__construct($Collection, $settings);
-		
+
 		# auto-select multi if necessary
 		if ($this->settings['multi'] === null) {
 			$Model = $this->getModel();
@@ -88,13 +92,13 @@ class AuthExtComponent extends AuthComponent {
 
 		parent::initialize($Controller);
 	}
-	
+
 	/**
-	 * 2.1 fix for allowing * as wildcard
+	 * 2.1 fix for allowing * as wildcard (tmp solution)
 	 * 2012-01-10 ms
 	 */
 	public function allow($action = null) {
-		if (((array) $action) === array('*')) {
+		if (((array)$action) === array('*')) {
 			parent::allow();
 			return;
 		}
@@ -110,7 +114,7 @@ class AuthExtComponent extends AuthComponent {
 		$this->_setDefaults();
 
 		if (empty($user)) {
-			$user = $this->identify($this->request, $this->response);
+			$user = $this->identify($this->Controller->request, $this->Controller->response);
 		}
 		if (empty($user)) {
 			$this->loginError = __('invalidLoginCredentials');
@@ -162,7 +166,7 @@ class AuthExtComponent extends AuthComponent {
 			$this->loginError = __('Email not active yet');
 			return false;
 		}
-		
+
 		if ($user) {
 			# update login counter
 			if (isset($user['logins'])) {
@@ -171,7 +175,7 @@ class AuthExtComponent extends AuthComponent {
 					$Model->loginUpdate($user);
 				}
 			}
-			
+
 			$this->Session->renew();
 			$this->Session->write(self::$sessionKey, $user);
 			$this->Session->write(self::$sessionKey, $this->completeAuth($user));
@@ -192,7 +196,7 @@ class AuthExtComponent extends AuthComponent {
 			}
 			$user = array_shift($user);
 		}
-				
+
 		if (isset($Model->hasMany[$this->intermediateModel]['className'])) {
 			$with = $Model->hasMany[$this->intermediateModel]['className'];
 		} elseif (isset($Model->belongsTo[$this->roleModel]['className'])) {
@@ -212,7 +216,7 @@ class AuthExtComponent extends AuthComponent {
 				$this->{$withModel} = ClassRegistry::init($with);
 			}
 			# only for multi
-			
+
 			if ($this->settings['multi'] || !isset($completeAuth[$this->settings['userModel']]['role_id'])) {
 				$parentModelAlias = $this->settings['parentModelAlias'];
 				$completeAuth[$this->settings['userModel']][$parentModelAlias] = array(); # default: no roles!
@@ -220,12 +224,12 @@ class AuthExtComponent extends AuthComponent {
 				if (!empty($roles)) {
 					//$primaryRole = $this->user($this->fieldKey);
 					// retrieve associated role that are not the primary one
-		
+
 					# MAYBE USEFUL FOR GUEST!!!
 					//$roles = set::extract('/'.$with.'['.$this->fieldKey.'!='.$primaryRole.']/'.$this->fieldKey, $roles);
-		
+
 					// add the suplemental roles id under the Auth session key
-		
+
 					$completeAuth[$this->settings['userModel']][$parentModelAlias] = $roles; // or USER_ROLE_KEY
 					//pr($completeAuth);
 				}
@@ -233,7 +237,7 @@ class AuthExtComponent extends AuthComponent {
 				//$completeAuth[$this->settings['userModel']][$parentModelAlias][] = $completeAuth[$this->settings['userModel']]['role_id'];
 			}
 		}
-		
+
 		# deprecated!
 		if (isset($Model->hasOne['UserInfo'])) {
 			$with = $Model->hasOne['UserInfo']['className'];
@@ -281,7 +285,7 @@ class AuthExtComponent extends AuthComponent {
 	 * @param Controller $controller A reference to the instantiating controller object
 	 * @return boolean
 	 */
-	public function startup($controller) {
+	public function startup(Controller $controller) {
 		if ($controller->name == 'CakeError') {
 			return true;
 		}
@@ -333,7 +337,7 @@ class AuthExtComponent extends AuthComponent {
 			if (!$this->_getUser()) {
 				if (!$request->is('ajax')) {
 					$this->flash($this->authError);
-					$this->Session->write('Auth.redirect', Router::reverse($request));
+					$this->Session->write('Auth.redirect', $request->here());
 					$controller->redirect($loginAction);
 					return false;
 				} elseif (!empty($this->ajaxLogin)) {
@@ -351,32 +355,38 @@ class AuthExtComponent extends AuthComponent {
 		}
 
 		$this->flash($this->authError);
-		# redirect fix
-		$controller->redirect($controller->referer($this->loginRedirect), null, true);
+		$default = '/';
+		if (!empty($this->loginRedirect)) {
+			$default = $this->loginRedirect;
+		}
+		$controller->redirect($controller->referer($default), null, true);
+		return false;
 	}
-	
+
 	/**
 	 * Quickfix
 	 * TODO: improve - maybe use Authenticate
+	 * @deprecated
 	 * @return bool $success
 	 */
 	public function verifyUser($id, $pwd) {
+		//trigger_error('deprecated - use Authenticate class');
 		$options = array(
 			'conditions' => array('id'=>$id, 'password'=>$this->password($pwd)),
 		);
 		return $this->getModel()->find('first', $options);
-		
+
 		$this->constructAuthenticate();
 		$this->request->data['User']['password'] = $pwd;
 		return $this->identify($this->request, $this->response);
 	}
-	
+
 	/**
 	 * returns the current User model
 	 * @return object $User
 	 */
 	public function getModel() {
-		return ClassRegistry::init(CLASS_USER); 
+		return ClassRegistry::init(CLASS_USER);
 	}
 
 }

+ 14 - 6
Model/Behavior/GeocoderBehavior.php

@@ -4,6 +4,7 @@ App::uses('GeocodeLib', 'Tools.Lib');
 /**
  * A geocoding behavior for CakePHP to easily geocode addresses.
  * Uses the GeocodeLib for actual geocoding.
+ * Also provides some useful geocoding tools like validation and distance conditions
  *
  * @author Mark Scherer
  * @cakephp 2.x
@@ -209,7 +210,7 @@ class GeocoderBehavior extends ModelBehavior {
 		$Model->virtualFields['distance'] = $this->distance($Model, $lat, $lng, $modelName);
 	}
 
-	public function distanceConditions(Model $Model, $distance = null, $modelName = null) {
+	public function distanceConditions(Model $Model, $distance = null, $fieldName = null, $modelName = null) {
 		if ($modelName === null) {
 			$modelName = $Model->alias;
 		}
@@ -217,8 +218,9 @@ class GeocoderBehavior extends ModelBehavior {
 			$modelName . '.lat <> 0',
 			$modelName . '.lng <> 0',
 		);
+		$fieldName = !empty($fieldName) ? $fieldName : 'distance';
 		if ($distance !== null) {
-			$conditions[] = '1=1 HAVING distance < ' . intval($distance);
+			$conditions[] = '1=1 HAVING '.$modelName.'.'.$fieldName.' < ' . intval($distance);
 		}
 		return $conditions;
 	}
@@ -232,14 +234,20 @@ class GeocoderBehavior extends ModelBehavior {
 			'AS '.(!empty($fieldName) ? $fieldName : 'distance');
 	}
 
-	public function distance(Model $Model, $lat, $lng, $modelName = null) {
+	public function distance(Model $Model, $lat, $lng, $fieldLat = null, $fieldLng = null, $modelName = null) {
+		if ($fieldLat === null) {
+			$fieldLat = $this->settings[$Model->alias]['lat'];
+		}
+		if ($fieldLng === null) {
+			$fieldLng = $this->settings[$Model->alias]['lng'];
+		}
 		if ($modelName === null) {
 			$modelName = $Model->alias;
 		}
-		return '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - '.$modelName.'.lat)) * ' .
+		return '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - '.$modelName.'.'.$fieldLat.')) * ' .
 			'COS( PI()/2 - RADIANS(90 - '. $lat .')) * ' .
-			'COS( RADIANS('.$modelName.'.lng) - RADIANS('. $lng .')) + ' .
-			'SIN( PI()/2 - RADIANS(90 - '.$modelName.'.lat)) * ' .
+			'COS( RADIANS('.$modelName.'.'.$fieldLat.') - RADIANS('. $lng .')) + ' .
+			'SIN( PI()/2 - RADIANS(90 - '.$modelName.'.'.$fieldLng.')) * ' .
 			'SIN( PI()/2 - RADIANS(90 - '. $lat . ')))';
 	}
 

+ 12 - 2
Test/Case/Model/Behavior/GeocoderBehaviorTest.php

@@ -1,8 +1,6 @@
 <?php
-
 App::uses('GeocoderBehavior', 'Tools.Model/Behavior');
 App::uses('Set', 'Utility');
-//App::uses('Model', 'Model');
 App::uses('AppModel', 'Model');
 
 class GeocoderBehaviorTest extends CakeTestCase {
@@ -173,6 +171,18 @@ class GeocoderBehaviorTest extends CakeTestCase {
 		$this->assertTrue(!empty($res['Comment']['lat']) && !empty($res['Comment']['lng']));
 	}
 
+	public function testDistance() {
+		$res = $this->Comment->distance(12, 14);
+		$expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.lat)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.lat) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.lng)) * SIN( PI()/2 - RADIANS(90 - 12)))';
+		$this->assertEquals($expected, $res);
+
+		$this->Comment->Behaviors->detach('Geocoder');
+		$this->Comment->Behaviors->attach('Geocoder', array('lat'=>'x', 'lng'=>'y'));
+		$res = $this->Comment->distance(12, 14);
+		$expected = '6371.04 * ACOS( COS( PI()/2 - RADIANS(90 - Comment.x)) * COS( PI()/2 - RADIANS(90 - 12)) * COS( RADIANS(Comment.x) - RADIANS(14)) + SIN( PI()/2 - RADIANS(90 - Comment.y)) * SIN( PI()/2 - RADIANS(90 - 12)))';
+		$this->assertEquals($expected, $res);
+	}
+
 }