Cleaning up the AuthComponent

Simplify if statements, return early and less variable use
This commit is contained in:
dogmatic69 2012-09-14 01:50:24 +01:00
parent 51e0715001
commit 2c70319d27

View file

@ -300,23 +300,24 @@ class AuthComponent extends Component {
} }
} }
return true; return true;
} else { }
if (!$this->_getUser()) { if (!$this->_getUser()) {
if (!$request->is('ajax')) { if (!$request->is('ajax')) {
$this->flash($this->authError); $this->flash($this->authError);
$this->Session->write('Auth.redirect', $request->here()); $this->Session->write('Auth.redirect', $request->here());
$controller->redirect($loginAction); $controller->redirect($loginAction);
return false; return false;
} elseif (!empty($this->ajaxLogin)) { }
if (!empty($this->ajaxLogin)) {
$controller->viewPath = 'Elements'; $controller->viewPath = 'Elements';
echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout); echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
$this->_stop(); $this->_stop();
return false; return false;
} else { }
$controller->redirect(null, 403); $controller->redirect(null, 403);
} }
}
}
if (empty($this->authorize) || $this->isAuthorized($this->user())) { if (empty($this->authorize) || $this->isAuthorized($this->user())) {
return true; return true;
} }
@ -360,7 +361,8 @@ class AuthComponent extends Component {
public function isAuthorized($user = null, $request = null) { public function isAuthorized($user = null, $request = null) {
if (empty($user) && !$this->user()) { if (empty($user) && !$this->user()) {
return false; return false;
} elseif (empty($user)) { }
if (empty($user)) {
$user = $this->user(); $user = $this->user();
} }
if (empty($request)) { if (empty($request)) {
@ -428,13 +430,13 @@ class AuthComponent extends Component {
$args = func_get_args(); $args = func_get_args();
if (empty($args) || $action === null) { if (empty($args) || $action === null) {
$this->allowedActions = $this->_methods; $this->allowedActions = $this->_methods;
} else { return;
}
if (isset($args[0]) && is_array($args[0])) { if (isset($args[0]) && is_array($args[0])) {
$args = $args[0]; $args = $args[0];
} }
$this->allowedActions = array_merge($this->allowedActions, $args); $this->allowedActions = array_merge($this->allowedActions, $args);
} }
}
/** /**
* Removes items from the list of allowed/no authentication required actions. * Removes items from the list of allowed/no authentication required actions.
@ -454,7 +456,8 @@ class AuthComponent extends Component {
$args = func_get_args(); $args = func_get_args();
if (empty($args) || $action === null) { if (empty($args) || $action === null) {
$this->allowedActions = array(); $this->allowedActions = array();
} else { return;
}
if (isset($args[0]) && is_array($args[0])) { if (isset($args[0]) && is_array($args[0])) {
$args = $args[0]; $args = $args[0];
} }
@ -466,7 +469,6 @@ class AuthComponent extends Component {
} }
$this->allowedActions = array_values($this->allowedActions); $this->allowedActions = array_values($this->allowedActions);
} }
}
/** /**
* Maps action names to CRUD operations. Used for controller-based authentication. Make sure * Maps action names to CRUD operations. Used for controller-based authentication. Make sure