mlanthaler: Switched to an authenticator and a form class to be able to add other authentication methods. (merged from branches/gsoc)

mlanthaler: The missing authenticator base class...  (merged from branches/gsocmlanthaler: Switched to an authenticator and a form class to be able to add other authentication methods.  (merged from branches/gsoc)
mlanthaler: The missing authenticator base class...  (merged from branches/gsoc))


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41729 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2007-09-14 03:12:21 +00:00
parent f7646412f4
commit a377a67e54
6 changed files with 329 additions and 174 deletions

View File

@ -458,6 +458,35 @@ JS;
return $this->PopupBaseLink() . "&methodName={$_REQUEST['methodName']}&ctf[childID]={$item->ID}&ctf[start]={$start}";
}
/**
* Method handles pagination in asset popup.
*
* @return Object DataObjectSet
*/
function pagination() {
$this->pageSize = 10;
$currentItem = $this->PopupCurrentItem();
$result = new DataObjectSet();
if($currentItem < 6) {
$offset = 1;
} elseif($this->totalCount - $currentItem <= 4) {
$offset = $currentItem - (10 - ($this->totalCount - $currentItem));
$offset = $offset <= 0 ? 1 : $offset;
} else {
$offset = $currentItem - 5;
}
for($i = $offset;$i <= $offset + $this->pageSize && $i <= $this->totalCount;$i++) {
$start = $i - 1;
$item = $this->unpagedSourceItems->getOffset($i-1);
$links['link'] = $this->PopupBaseLink() . "&methodName={$_REQUEST['methodName']}&ctf[childID]={$item->ID}&ctf[start]={$start}";
$links['number'] = $i;
$links['active'] = $i == $currentItem ? false : true;
$result->push(new ArrayData($links));
}
return $result;
}
/**

View File

@ -0,0 +1,33 @@
<?php
/**
* Abstract base class for an authentication method
*
* This class is used as a base class for the different authentication
* methods like {@link MemberAuthenticator} or {@link OpenIDAuthenticator}.
*
* @author Markus Lanthaler <markus@silverstripe.com>
*/
abstract class Authenticator extends Object
{
/**
* Method to authenticate an user
*
* @param array $RAW_data Raw data to authenticate the user
* @return bool|Member Returns FALSE if authentication fails, otherwise
* the member object
*/
public abstract function authenticate(array $RAW_data);
/**
* Method that creates the login form for this authentication method
*
* @return Form Returns the login form to use with this authentication
* method
*/
public abstract function getLoginForm();
}
?>

View File

@ -0,0 +1,43 @@
<?php
/**
* Authenticator for the default "member" method
*
* @author Markus Lanthaler <markus@silverstripe.com>
*/
class MemberAuthenticator extends Authenticator {
/**
* Method to authenticate an user
*
* @param array $RAW_data Raw data to authenticate the user
* @return bool|Member Returns FALSE if authentication fails, otherwise
* the member object
*/
public function authenticate(array $RAW_data) {
$SQL_user = Convert::raw2sql($RAW_data['Email']);
$SQL_password = Convert::raw2sql($RAW_data['Password']);
$member = DataObject::get_one(
"Member", "Email = '$SQL_user' And Password = '$SQL_password'");
if($member) {
Session::clear("BackURL");
}
return $member;
}
/**
* Method that creates the login form for this authentication method
*
* @return Form Returns the login form to use with this authentication
* method
*/
public function getLoginForm() {
return Object::create("MemberLoginForm", $this, "LoginForm");
}
}
?>

View File

@ -1,14 +1,29 @@
<?php
/**
* Standard log-in form.
* Log-in form for the "member" authentication method
*/
class LoginForm extends Form {
class MemberLoginForm extends Form {
/**
* Constructor
*
* @param $controller
* @param $name
* @param $fields
* @param $actions
* @param $checkCurrentUser
*/
function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true) {
$customCSS = project() . '/css/member_login.css';
if(Director::fileExists($customCSS)) {
Requirements::css($customCSS);
}
if(isset($_REQUEST['BackURL'])) {
$backURL = $_REQUEST['BackURL'];
} else {
$backURL = Session::get('BackURL');
Session::clear("BackURL");
//Session::clear("BackURL"); don't clear the back URL here! Should be used until the right password is entered!
}
if($checkCurrentUser && Member::currentUserID()) {
@ -17,7 +32,8 @@ class LoginForm extends Form {
} else {
if(!$fields) {
$fields = new FieldSet(
new TextField("Email", "Email address", Session::get('SessionForms.LoginForm.Email')),
new HiddenField("AuthenticationMethod", null, "Member"),
new TextField("Email", "Email address", Session::get('SessionForms.MemberLoginForm.Email')),
new EncryptField("Password", "Password"),
new CheckboxField("Remember", "Remember me next time?",true)
);
@ -37,17 +53,30 @@ class LoginForm extends Form {
parent::__construct($controller, $name, $fields, $actions);
}
/**
* Get message from session
*/
protected function getMessageFromSession() {
parent::getMessageFromSession();
if(($member = Member::currentUser()) && !Session::get('LoginForm.force_message')) {
if(($member = Member::currentUser()) && !Session::get('MemberLoginForm.force_message')) {
$this->message = "You're logged in as $member->FirstName.";
}
Session::set('LoginForm.force_message', false);
Session::set('MemberLoginForm.force_message', false);
}
/**
* Login form handler method
*
* This method is called when the user clicks on "Log in"
*
* @param array $data Submitted data
*/
public function dologin($data) {
if($this->performLogin($data)){
if(isset($_REQUEST['BackURL']) && $backURL = $_REQUEST['BackURL']) {
if($backURL = $_REQUEST['BackURL']) {
Session::clear("BackURL");
Director::redirect($backURL);
}else
@ -62,17 +91,25 @@ class LoginForm extends Form {
}
}
/**
* Log out
*
* @todo Figure out for what this method is used!
*/
public function logout(){
$s = new Security();
return $s->logout();
}
/* check the membership
* if one of them or both don't match, set the fields which are unmatched with red star *
/**
* Check the membership
*
* If one of them or both don't match, set the fields which are unmatched with red star *
*/
public function performLogin($data){
if($member = Security::authenticate($data['Email'], $data['Password'])) {
if($member = MemberAuthenticator::authenticate($data)) {
$firstname = Convert::raw2xml($member->FirstName);
$this->sessionMessage("Welcome Back, {$firstname}", "good");
$member->LogIn();
@ -89,7 +126,13 @@ class LoginForm extends Form {
}
/**
* Forgot password form handler method
*
* This method is called when the user clicks on "Log in"
*
* @param array $data Submitted data
*/
function forgotPassword($data) {
$SQL_data = Convert::raw2sql($data);
if($data['Email'] && $member = DataObject::get_one("Member", "Member.Email = '$SQL_data[Email]'")) {

View File

@ -74,9 +74,7 @@ class Security extends Controller {
}
function LoginForm() {
$customCSS = project() . '/css/login.css';
if(Director::fileExists($customCSS)) Requirements::css($customCSS);
return Object::create("LoginForm", $this, "LoginForm");
return MemberAuthenticator::GetLoginForm();
}
function Link($action = null) {
return "Security/$action";
@ -105,7 +103,7 @@ class Security extends Controller {
$controller = new Page_Controller($tmpPage);
$controller->init();
//Controller::$currentController = $controller;
Controller::$currentController = $controller;
if(SSViewer::hasTemplate("Security_login")) {
return $controller->renderWith(array("Security_login", "Page"));
@ -164,7 +162,7 @@ class Security extends Controller {
function LostPasswordForm() {
return new LoginForm($this, "LostPasswordForm", new FieldSet(
return new MemberLoginForm($this, "LostPasswordForm", new FieldSet(
new EmailField("Email", "Email address")
), new FieldSet(
new FormAction("forgotPassword", "Send me my password")

View File

@ -11,26 +11,35 @@
<% if IsAddMode %>
<% else %>
<% if ShowPagination %>
<table class="PageControls">
<tr>
<td class="Left">
<% if PopupFirstLink %><a href="$PopupFirstLink" title="View first $NameSingular"><img src="cms/images/pagination/record-first.png" alt="View first $NameSingular" /></a>
<% else %><img src="cms/images/pagination/record-first-g.png" alt="View first $NameSingular" /><% end_if %>
<% if PopupPrevLink %><a href="$PopupPrevLink" title="View previous $NameSingular"><img src="cms/images/pagination/record-prev.png" alt="View previous $NameSingular" /></a>
<% else %><img src="cms/images/pagination/record-prev-g.png" alt="View previous $NameSingular" /><% end_if %>
</td>
<td class="Count">
Displaying $PopupCurrentItem of $TotalCount
</td>
<td class="Right">
<% if PopupNextLink %><a href="$PopupNextLink" title="View next $NameSingular"><img src="cms/images/pagination/record-next.png" alt="View next $NameSingular" /></a>
<% else %><img src="cms/images/pagination/record-next-g.png" alt="View next $NameSingular" /><% end_if %>
<% if PopupLastLink %><a href="$PopupLastLink" title="View last $NameSingular"><img src="cms/images/pagination/record-last.png" alt="View last $NameSingular" /></a>
<% else %><img src="cms/images/pagination/record-last-g.png" alt="View last $NameSingular" /><% end_if %>
</td>
</tr>
</table>
<div id="Pagination">
<% if PopupPrevLink %>
<div id="Pagination_Previous">
<a href="$PopupPrevLink"><img src="cms/images/pagination/previousArrow.png" /></a>
<a href="$PopupPrevLink"><div>Previous</div></a>
</div>
<% end_if %>
<% if TotalCount == 1 %>
<% else %>
<% control pagination %>
<% if active %>
<a href="$link">$number</a>
<% else %>
<span>$number</span>
<% end_if %>
<% end_control %>
<% end_if %>
<% if PopupNextLink %>
<div id="Pagination_Next">
<a href="$PopupNextLink"><img src="cms/images/pagination/nextArrow.png" /></a>
<a href="$PopupNextLink"><div>Next</div></a>
</div>
<% end_if %>
<% end_if %>
<% end_if %>
<script type="text/javascript">
divQ = $('Pagination').getElementsByTagName('div').length;
aQ = $('Pagination').getElementsByTagName('a').length - divQ + 1;
$('Pagination').style.width = aQ * 15 + 130 + "px";
</script>
</body>
</html>