Skip to content

Commit

Permalink
Add Members: Validate Password (pre-save) filter
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-e committed Nov 1, 2017
1 parent 68ef6ff commit 972263d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
14 changes: 14 additions & 0 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,15 @@ public function appendFilter($context) {
);
}

if(FieldManager::isFieldUsed(self::getFieldType('authentication'))) {
// Add Member: Validate Password filter
$context['options'][] = array(
'member-validate-password',
in_array('member-validate-password', $selected),
__('Members: Validate Password')
);
}

if(FieldManager::isFieldUsed(self::getFieldType('authentication'))) {
// Add Member: Update Password filter
$context['options'][] = array(
Expand Down Expand Up @@ -1256,6 +1265,11 @@ private function __processEventFilters(array &$context) {
$this->getMemberDriver()->filter_LockActivation($context);
}

// Process validating a Member's Password
if (in_array('member-validate-password', $context['event']->eParamFILTERS)) {
$this->getMemberDriver()->filter_ValidatePassword($context);
}

// Process updating a Member's Password
if (in_array('member-update-password', $context['event']->eParamFILTERS)) {
$this->getMemberDriver()->filter_UpdatePassword($context);
Expand Down
4 changes: 4 additions & 0 deletions lib/class.members.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ public function filter_LockActivation(array &$context) {
return true;
}

public function filter_ValidatePassword(array &$context) {
return true;
}

public function filter_UpdatePassword(array &$context) {
return true;
}
Expand Down
65 changes: 65 additions & 0 deletions lib/member.symphony.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,71 @@ public function filter_LockActivation(array &$context) {
}
}

/**
* Validate Password
* If there is an authentication field, we can check if a posted password is
* valid for the currently logged-in Member. This filter is supposed to be
* called as a pre-save filter, so it can terminate a section event (thus
* prevent saving) if no valid password is provided.
*
* @param array $context
*/
public function filter_ValidatePassword(array &$context) {
$member = $this->Member;
if (!$member instanceof Entry) {
$context['messages'][] = array(
'member-validate-password',
false,
__('Member not found.')
);

return;
}

if (is_null($this->section->getFieldHandle('authentication'))) {
$context['messages'][] = array(
'member-validate-password',
false,
__('No Authentication field found.')
);

return;
}

$member_id = null;
if (isset($_POST['fields'][$this->section->getFieldHandle('authentication')]['validate'])) {
$password = Symphony::Database()->cleanValue(
$_POST['fields'][$this->section->getFieldHandle('authentication')]['validate']
);

if ($password) {
// Handle which is the Identity field, either the Member: Username or Member: Email field
$identity = is_null($this->section->getFieldHandle('identity')) ? 'email' : 'identity';

$member_id = $this->findMemberIDFromCredentials(
array(
$this->section->getFieldHandle($identity) => $member->getData($this->section->getField($identity)->get('id'), true)->value,
$this->section->getFieldHandle('authentication') => $password
),
false
);
}
}

if ($member_id) {
$context['messages'][] = array(
'member-validate-password',
true
);
} else {
$context['messages'][] = array(
'member-validate-password',
false,
__('No valid password has been provided.')
);
}
}

/**
* Part 1 - Update Password
* If there is an Authentication field, we need to inject the 'optional'
Expand Down

0 comments on commit 972263d

Please sign in to comment.