diff --git a/security/LoginForm.php b/security/LoginForm.php index dd768b038..6f06fbc61 100644 --- a/security/LoginForm.php +++ b/security/LoginForm.php @@ -40,5 +40,15 @@ abstract class LoginForm extends Form { return new $this->authenticator_class; } + + /** + * Get the authenticator name. + * @return string The friendly name for use in templates, etc. + */ + public function getAuthenticatorName() { + $authClass = $this->authenticator_class; + return $authClass::get_name(); + } + } diff --git a/security/Security.php b/security/Security.php index 1ab0bfe85..edbc92221 100644 --- a/security/Security.php +++ b/security/Security.php @@ -347,6 +347,9 @@ class Security extends Controller { /** * Show the "login" page * + * For multiple authenticators, Security_MultiAuthenticatorLogin is used. + * See getTemplate and getIncludeTemplate for how to override template logic + * * @return string Returns the "login" page as HTML code. */ public function login() { @@ -360,11 +363,6 @@ class Security extends Controller { if($result instanceof SS_HTTPResponse) return $result; } } - - $customCSS = project() . '/css/tabs.css'; - if(Director::fileExists($customCSS)) { - Requirements::css($customCSS); - } if(class_exists('SiteTree')) { $tmpPage = new Page(); @@ -376,7 +374,8 @@ class Security extends Controller { $controller = Page_Controller::create($tmpPage); $controller->setDataModel($this->model); $controller->init(); - } else { + } + else { $controller = $this; } @@ -385,71 +384,48 @@ class Security extends Controller { $content = ''; $forms = $this->GetLoginForms(); - if(!count($forms)) { + $formCount = count($forms); + + if(!$formCount) { user_error('No login-forms found, please use Authenticator::register_authenticator() to add one', E_USER_ERROR); } - - // only display tabs when more than one authenticator is provided - // to save bandwidth and reduce the amount of custom styling needed - if(count($forms) > 1) { - // Needed because the in the template makes problems - // with the tabstrip library otherwise - $link_base = Director::absoluteURL($this->Link("login")); - - Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js'); - Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-ui/jquery-ui.js'); - - Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js'); - - Requirements::css(THIRDPARTY_DIR . '/jquery-ui-themes/smoothness/jquery-ui.css'); - - Requirements::css(FRAMEWORK_DIR . '/css/Security_login.css'); - - Requirements::javascript(FRAMEWORK_DIR . '/javascript/TabSet.js'); - - $content = '
'; - $content .= '
'; - $content .= '\n" . $content_forms . "\n
\n
\n"; - } else { - $content .= $forms[0]->forTemplate(); + // Handle any for messages from validation, etc. + $message = Session::get('Security.Message.message'); + $messageType = ''; + if(!empty($message)) { + $messageType = Session::get('Security.Message.type'); } - - if(strlen($message = Session::get('Security.Message.message')) > 0) { - $message_type = Session::get('Security.Message.type'); - if($message_type == 'bad') { - $message = "

$message

"; - } else { - $message = "

$message

"; - } - $customisedController = $controller->customise(array( - "Content" => $message, - "Form" => $content, - )); - } else { - $customisedController = $controller->customise(array( - "Form" => $content, - )); - } - + // We've displayed the message in the form output, so reset it for the next run. Session::clear('Security.Message'); - // custom processing + // If many login forms, display all to let user to choose. + if($formCount > 1) { + $viewData = new ArrayData(array( + 'Forms' => new ArrayList($forms), + )); + + $content = $viewData->renderWith( + $this->getIncludeTemplate('MultiAuthenticatorLogin') + ); + } + // Display the form + else { + $content = $forms[0]->forTemplate(); + } + + // Finally, customise the controller to add any form messages and the form. + $customisedController = $controller->customise(array( + "Message" => $message, + "MessageType" => $messageType, + "Form" => $content, + )); + + // Return the customised controller return $customisedController->renderWith( - array('Security_login', 'Security', $this->stat('template_main'), 'BlankPage') + $this->getTemplate('login') ); } @@ -491,7 +467,7 @@ class Security extends Controller { //Controller::$currentController = $controller; return $customisedController->renderWith( - array('Security_lostpassword', 'Security', $this->stat('template_main'), 'BlankPage') + $this->getTemplate('lostpassword') ); } @@ -557,7 +533,7 @@ class Security extends Controller { //Controller::$currentController = $controller; return $customisedController->renderWith( - array('Security_passwordsent', 'Security', $this->stat('template_main'), 'BlankPage') + $this->getTemplate('passwordsent') ); } @@ -661,7 +637,7 @@ class Security extends Controller { } return $customisedController->renderWith( - array('Security_changepassword', 'Security', $this->stat('template_main'), 'BlankPage') + $this->getTemplate('changepassword') ); } @@ -674,6 +650,31 @@ class Security extends Controller { return new ChangePasswordForm($this, 'ChangePasswordForm'); } + /** + * Gets the template for a particular action for use in rendering + * For use in any subclass + * + * @return string|array Returns the template(s) for rendering + */ + public function getTemplate($action) { + return array( + 'Security_' . $action, + 'Security', + $this->stat('template_main'), + 'BlankPage' + ); + } + + /** + * Gets the template for an include used for security. + * For use in any subclass. + * + * @return string|array Returns the template(s) for rendering + */ + public function getIncludeTemplate($name) { + return 'Security_' . $name; + } + /** * Return an existing member with administrator privileges, or create one of necessary. * diff --git a/templates/Includes/Security_MultiAuthenticatorLogin.ss b/templates/Includes/Security_MultiAuthenticatorLogin.ss new file mode 100644 index 000000000..a36a10fbb --- /dev/null +++ b/templates/Includes/Security_MultiAuthenticatorLogin.ss @@ -0,0 +1,13 @@ + +<% loop Forms %> +
+

$AuthenticatorName

+ $forTemplate +
+<% end_loop %>