AJAX friendly responses for Security class

Final work around issue #1802 - creating templates for complex layout
operations and removing HTML from this controller.
This commit is contained in:
Will Morgan 2013-04-26 12:29:35 +01:00
parent 5a2b765025
commit 72a7f0e672
3 changed files with 89 additions and 65 deletions

View File

@ -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();
}
}

View File

@ -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 <base href=".."> 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 = '<div id="Form_EditForm">';
$content .= '<div class="ss-tabset">';
$content .= '<ul>';
$content_forms = '';
foreach($forms as $form) {
$content .= "<li><a href=\"#{$form->FormName()}_tab\">"
. $form->getAuthenticator()->get_name()
. "</a></li>\n";
$content_forms .= '<div class="tab" id="' . $form->FormName() . '_tab">'
. $form->forTemplate() . "</div>\n";
}
$content .= "</ul>\n" . $content_forms . "\n</div>\n</div>\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 = "<p class=\"message $message_type\">$message</p>";
} else {
$message = "<p>$message</p>";
}
$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.
*

View File

@ -0,0 +1,13 @@
<ul>
<% loop Forms %>
<li>
<a href="#{$FormName}">$AuthenticatorName</a>
</li>
<% end_loop %>
</ul>
<% loop Forms %>
<div class="form-tab" id="{$FormName}">
<h3>$AuthenticatorName</h3>
$forTemplate
</div>
<% end_loop %>