Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Breaking down AuthComponent::startup() into multiple methods for easi…

…er management and extension.
  • Loading branch information...
commit 49157d83ae008c3127113a2e31b3a7d3c824205f 1 parent 13029cc
@ADmad ADmad authored
Showing with 50 additions and 24 deletions.
  1. +50 −24 lib/Cake/Controller/Component/AuthComponent.php
View
74 lib/Cake/Controller/Component/AuthComponent.php
@@ -294,12 +294,36 @@ public function startup(Controller $controller) {
if (!$this->_setDefaults()) {
return false;
}
- $request = $controller->request;
- $url = '';
+ if ($this->_isAllowed($controller)) {
+ return true;
+ }
+
+ if (!$this->_getUser()) {
+ return $this->_unauthenticated($controller);
+ }
+
+ if (empty($this->authorize) || $this->isAuthorized($this->user())) {
+ return true;
+ }
+
+ return $this->_unauthorized($controller);
+ }
+
+/**
+ * Checks whether current action is accessible without authentication.
+ * If current action is login action referrer url is saved in session which is
+ * later accessible using AuthComponent::redirectUrl().
+ *
+ * @param Controller $controller A reference to the instantiating controller object
+ * @return boolean True if action is accessible without authentication else false
+ */
+ protected function _isAllowed(Controller $controller) {
+ $action = strtolower($controller->request->params['action']);
- if (isset($request->url)) {
- $url = $request->url;
+ $url = '';
+ if (isset($controller->request->url)) {
+ $url = $controller->request->url;
}
$url = Router::normalize($url);
$loginAction = Router::normalize($this->loginAction);
@@ -309,35 +333,37 @@ public function startup(Controller $controller) {
}
if ($loginAction == $url) {
- if (empty($request->data)) {
+ if (empty($controller->request->data)) {
if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
$this->Session->write('Auth.redirect', $controller->referer(null, true));
}
}
return true;
}
+ return false;
+ }
- if (!$this->_getUser()) {
- if (!$request->is('ajax')) {
- $this->flash($this->authError);
- $this->Session->write('Auth.redirect', $request->here());
- $controller->redirect($loginAction);
- return false;
- }
- if (!empty($this->ajaxLogin)) {
- $controller->viewPath = 'Elements';
- echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
- $this->_stop();
- return false;
- }
- $controller->redirect(null, 403);
+/**
+ * Handle unauthenticated access attempt.
+ *
+ * @param Controller $controller A reference to the controller object
+ * @return boolean Returns false
+ */
+ protected function _unauthenticated(Controller $controller) {
+ if (!$controller->request->is('ajax')) {
+ $this->flash($this->authError);
+ $this->Session->write('Auth.redirect', $controller->request->here());
+ $controller->redirect($this->loginAction);
+ return false;
}
-
- if (empty($this->authorize) || $this->isAuthorized($this->user())) {
- return true;
+ if (!empty($this->ajaxLogin)) {
+ $controller->viewPath = 'Elements';
+ echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
+ $this->_stop();
+ return false;
}
-
- return $this->_unauthorized($controller);
+ $controller->redirect(null, 403);
+ return false;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.