Skip to content
This repository
Browse code

Cleaning up the AuthComponent

Simplify if statements, return early and less variable use
  • Loading branch information...
commit 2c70319d2721e09cfd8ad3aae34d0aa26f95a136 1 parent 51e0715
Carl Sutton authored September 14, 2012
64  lib/Cake/Controller/Component/AuthComponent.php
@@ -300,23 +300,24 @@ public function startup(Controller $controller) {
300 300
 				}
301 301
 			}
302 302
 			return true;
303  
-		} else {
304  
-			if (!$this->_getUser()) {
305  
-				if (!$request->is('ajax')) {
306  
-					$this->flash($this->authError);
307  
-					$this->Session->write('Auth.redirect', $request->here());
308  
-					$controller->redirect($loginAction);
309  
-					return false;
310  
-				} elseif (!empty($this->ajaxLogin)) {
311  
-					$controller->viewPath = 'Elements';
312  
-					echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
313  
-					$this->_stop();
314  
-					return false;
315  
-				} else {
316  
-					$controller->redirect(null, 403);
317  
-				}
  303
+		}
  304
+
  305
+		if (!$this->_getUser()) {
  306
+			if (!$request->is('ajax')) {
  307
+				$this->flash($this->authError);
  308
+				$this->Session->write('Auth.redirect', $request->here());
  309
+				$controller->redirect($loginAction);
  310
+				return false;
  311
+			}
  312
+			if (!empty($this->ajaxLogin)) {
  313
+				$controller->viewPath = 'Elements';
  314
+				echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
  315
+				$this->_stop();
  316
+				return false;
318 317
 			}
  318
+			$controller->redirect(null, 403);
319 319
 		}
  320
+
320 321
 		if (empty($this->authorize) || $this->isAuthorized($this->user())) {
321 322
 			return true;
322 323
 		}
@@ -360,7 +361,8 @@ protected function _setDefaults() {
360 361
 	public function isAuthorized($user = null, $request = null) {
361 362
 		if (empty($user) && !$this->user()) {
362 363
 			return false;
363  
-		} elseif (empty($user)) {
  364
+		}
  365
+		if (empty($user)) {
364 366
 			$user = $this->user();
365 367
 		}
366 368
 		if (empty($request)) {
@@ -428,12 +430,12 @@ public function allow($action = null) {
428 430
 		$args = func_get_args();
429 431
 		if (empty($args) || $action === null) {
430 432
 			$this->allowedActions = $this->_methods;
431  
-		} else {
432  
-			if (isset($args[0]) && is_array($args[0])) {
433  
-				$args = $args[0];
434  
-			}
435  
-			$this->allowedActions = array_merge($this->allowedActions, $args);
  433
+			return;
  434
+		}
  435
+		if (isset($args[0]) && is_array($args[0])) {
  436
+			$args = $args[0];
436 437
 		}
  438
+		$this->allowedActions = array_merge($this->allowedActions, $args);
437 439
 	}
438 440
 
439 441
 /**
@@ -454,18 +456,18 @@ public function deny($action = null) {
454 456
 		$args = func_get_args();
455 457
 		if (empty($args) || $action === null) {
456 458
 			$this->allowedActions = array();
457  
-		} else {
458  
-			if (isset($args[0]) && is_array($args[0])) {
459  
-				$args = $args[0];
460  
-			}
461  
-			foreach ($args as $arg) {
462  
-				$i = array_search($arg, $this->allowedActions);
463  
-				if (is_int($i)) {
464  
-					unset($this->allowedActions[$i]);
465  
-				}
  459
+			return;
  460
+		}
  461
+		if (isset($args[0]) && is_array($args[0])) {
  462
+			$args = $args[0];
  463
+		}
  464
+		foreach ($args as $arg) {
  465
+			$i = array_search($arg, $this->allowedActions);
  466
+			if (is_int($i)) {
  467
+				unset($this->allowedActions[$i]);
466 468
 			}
467  
-			$this->allowedActions = array_values($this->allowedActions);
468 469
 		}
  470
+		$this->allowedActions = array_values($this->allowedActions);
469 471
 	}
470 472
 
471 473
 /**

0 notes on commit 2c70319

Please sign in to comment.
Something went wrong with that request. Please try again.