Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MembersLogin delegate, for external log in #301

Merged
merged 5 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fields/field.memberemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function createTable(){
public function fetchMemberIDBy($needle, $member_id = null) {
$email = null;
if (is_array($needle) && !empty($needle['email'])) {
$emaill = $needle['email'];
$email = $needle['email'];
} else {
$email = $needle;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/class.members.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public function addMemberDetailsToPageParams(array $context = null) {
$context['params']['member-id'] = $this->getMemberID();
$context['params']['member-section-id'] = $this->getMemberSectionID();

if (!$this->getMember()) {
return;
}

if(!is_null($this->section->getFieldHandle('role'))) {
$role_data = $this->getMember()->getData($this->section->getField('role')->get('id'));
$role = RoleManager::fetch($role_data['role_id']);
Expand Down
93 changes: 62 additions & 31 deletions lib/member.symphony.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,73 @@ public function setIdentityField(array $credentials, $simplified = true) {
* @return integer
*/
public function findMemberIDFromCredentials(array $credentials, $isHashed = false) {
if((
(!isset($credentials['username']) || is_null($credentials['username']))
&& (!isset($credentials['email']) || is_null($credentials['email']))
)) {
if (empty($credentials['username']) && empty($credentials['email'])) {
return null;
}

$identity = $this->setIdentityField($credentials);

if(!$identity instanceof Field) return null;
if (!($identity instanceof Field)) {
return null;
}

// Member from Identity
$member_id = $identity->fetchMemberIDBy($credentials);

// Validate against Password
$auth = $this->section->getField('authentication');
if(!is_null($auth)) {
if ($auth instanceof Field) {
$member_id = $auth->fetchMemberIDBy($credentials, $member_id, $isHashed);
} else {
// No authentication field defined, let extensions try
$isLoggedIn = false;
/**
* Fired when no authentication field is present.
* Never fired if the Members section contains an authentication field.
* Allow extensions to define their own auth mechanism for password less login.
*
* @delegate MembersLogin
* @since members 1.9.0
* @param string $context
* '/frontend/'
* @param boolean is-logged-in
* If the current login is valid or not.
* Extensions are expected to update this value.
* @param SymphonyMember $driver
* The SymphonyMember driver
* @param array $credentials
* The credentials value
* @param int $member_id
* The member_id found for this credentials
* Extensions are expected to update this value.
* @param array $errors
* The error array
*/
Symphony::ExtensionManager()->notifyMembers('MembersLogin', '/frontend/', array(
'is-logged-in' => &$isLoggedIn,
'driver' => $this,
'credentials' => $credentials,
'member_id' => &$member_id,
'errors' => &extension_Members::$_errors,
));
if (!$isLoggedIn) {
$member_id = null;
}
}

// No Member found, can't even begin to check Activation
// Return null
if(is_null($member_id)) return null;
if (!$member_id) {
return null;
}

// Check that if there's activiation, that this Member is activated.
if(!is_null($this->section->getFieldHandle('activation'))) {
// Check that if there's activation, that this Member is activated.
if (!is_null($this->section->getFieldHandle('activation'))) {
$entry = EntryManager::fetch($member_id, NULL, NULL, NULL, NULL, NULL, false, true, array($this->section->getFieldHandle('activation')));

$isActivated = $entry[0]->getData($this->section->getField('activation')->get('id'), true)->activated == "yes";

// If we are denying login for non activated members, lets do so now
if($this->section->getField('activation')->get('deny_login') == 'yes' && !$isActivated) {
if ($this->section->getField('activation')->get('deny_login') == 'yes' && !$isActivated) {
extension_Members::$_errors[$this->section->getFieldHandle('activation')] = array(
'message' => __('Member is not activated.'),
'type' => 'invalid',
Expand All @@ -107,7 +142,7 @@ public function findMemberIDFromCredentials(array $credentials, $isHashed = fals

// If the member isn't activated and a Role field doesn't exist
// just return false.
if(!$isActivated && !FieldManager::isFieldUsed(extension_Members::getFieldType('role'))) {
if (!$isActivated && !FieldManager::isFieldUsed(extension_Members::getFieldType('role'))) {
extension_Members::$_errors[$this->section->getFieldHandle('activation')] = array(
'message' => __('Member is not activated.'),
'type' => 'invalid',
Expand Down Expand Up @@ -165,31 +200,30 @@ public function login(array $credentials, $isHashed = false) {
$data = extension_Members::$_errors = array();

// Map POST data to simple terms
if(isset($credentials[$this->section->getFieldHandle('identity')])) {
if (isset($credentials[$this->section->getFieldHandle('identity')])) {
$username = $credentials[$this->section->getFieldHandle('identity')];
}

if(isset($credentials[$this->section->getFieldHandle('email')])) {
if (isset($credentials[$this->section->getFieldHandle('email')])) {
$email = $credentials[$this->section->getFieldHandle('email')];
}

// Allow login via username OR email. This normalises the $data array from the custom
// field names to simple names for ease of use.
if(isset($username)) {
if (!empty($username)) {
$data['username'] = Symphony::Database()->cleanValue($username);
}
else if(isset($email) && !is_null($this->section->getFieldHandle('email'))) {
} elseif (isset($email) && !is_null($this->section->getFieldHandle('email'))) {
$data['email'] = Symphony::Database()->cleanValue($email);
}

// Map POST data for password to `$password`
if(isset($credentials[$this->section->getFieldHandle('authentication')])) {
if (isset($credentials[$this->section->getFieldHandle('authentication')])) {
$password = $credentials[$this->section->getFieldHandle('authentication')];
$data['password'] = (!empty($password)) ? $password : '';
}

// Check to ensure that we actually have some data to try and log a user in with.
if(empty($data['password']) && isset($credentials[$this->section->getFieldHandle('authentication')])) {
if (empty($data['password']) && isset($credentials[$this->section->getFieldHandle('authentication')])) {
extension_Members::$_errors[$this->section->getFieldHandle('authentication')] = array(
'label' => $this->section->getField('authentication')->get('label'),
'type' => 'missing',
Expand All @@ -198,7 +232,7 @@ public function login(array $credentials, $isHashed = false) {
);
}

if(isset($data['username']) && empty($data['username'])) {
if (isset($data['username']) && empty($data['username'])) {
extension_Members::$_errors[$this->section->getFieldHandle('identity')] = array(
'label' => $this->section->getField('identity')->get('label'),
'type' => 'missing',
Expand All @@ -207,42 +241,39 @@ public function login(array $credentials, $isHashed = false) {
);
}

if(isset($data['email']) && empty($data['email'])) {
if (isset($data['email']) && empty($data['email'])) {
extension_Members::$_errors[$this->section->getFieldHandle('email')] = array(
'label' => $this->section->getField('email')->get('label'),
'type' => 'missing',
'message-id' => EventMessages::FIELD_MISSING,
'message' => __('%s is a required field.', array($this->section->getField('email')->get('label'))),
);
}
else if(!fieldMemberEmail::applyValidationRule($email)) {
} elseif (!fieldMemberEmail::applyValidationRule($data['email'])) {
extension_Members::$_errors[$this->section->getFieldHandle('email')] = array(
'message' => __('\'%s\' contains invalid characters.', array($this->section->getField('email')->get('label'))),
'message-id' => EventMessages::FIELD_INVALID,
'type' => 'invalid',
'label' => $this->section->getField('email')->get('label')
);
return null;
}

// If there is errors already, no point continuing, return false
if(!empty(extension_Members::$_errors)) {
if (!empty(extension_Members::$_errors)) {
return false;
}

if($id = $this->findMemberIDFromCredentials($data, $isHashed)) {
try{
if ($id = $this->findMemberIDFromCredentials($data, $isHashed)) {
try {
self::$member_id = $id;
$this->initialiseCookie();
$this->initialiseMemberObject();

$this->cookie->set('id', $id);
$this->cookie->set('members-section-id', $this->getMember()->get('section_id'));

if(isset($username)) {
if (!empty($username)) {
$this->cookie->set('username', $data['username']);
}
else {
} else {
$this->cookie->set('email', $data['email']);
}

Expand All @@ -252,7 +283,7 @@ public function login(array $credentials, $isHashed = false) {

self::$isLoggedIn = true;

} catch(Exception $ex){
} catch (Exception $ex){
// Or do something else?
throw new Exception($ex);
}
Expand Down