Skip to content

Commit

Permalink
Added valid_password and valid_password_with methods in ValidationRules
Browse files Browse the repository at this point in the history
  • Loading branch information
najdanovicivan committed May 8, 2020
1 parent 7a83429 commit ceccea0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Authentication/Passwords/ValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,72 @@ public function strong_password(string $value, string &$error1 = null, array $da
return $result;
}

/**
* A validation helper method to check if the passed
* current user's password is valid
*
* @param string $password
*
* @return bool
*/
public function valid_password(string $password)
{
helper('auth');
$user = user();

if (empty($user)) {
return false;
}

$result = password_verify(base64_encode(
hash('sha384', $password, true)
), $user->password_hash);

return $result;
}

/**
* A validation helper method to check if the passed
* current user's password is valid only when any of
* the other required fields are present in the data.
*
* Example (password is checked when the newPassword field is present):
*
* required_with[newPassword]
*
* @param string $password
* @param string $fields
* @param array $data
*
* @return bool
*/
public function valid_password_with(string $password, string $fields, array $data)
{
$fields = explode(',', $fields);

foreach ($fields as $field)
{
if (array_key_exists($field, $data))
{
$requiredFields[] = $field;
}
}

// Remove any keys with empty values since, that means they
// weren't truly there, as far as this is concerned.
$requiredFields = array_filter($requiredFields, function ($item) use ($data) {
return ! empty($data[$item]);
});

if(empty($requiredFields))
{
return true;
}

return $this->valid_password($password);
}


/**
* Builds a new user instance from the global request.
*
Expand Down
6 changes: 6 additions & 0 deletions src/Language/en/Validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'valid_password' => 'The {field} is not valid.',
'valid_password_with' => 'Valid {field} is needed in order to set {param}'
];

0 comments on commit ceccea0

Please sign in to comment.