Skip to content
Permalink
Browse files

Make validation more resilient against inconsistent DB states

  • Loading branch information...
flack committed May 24, 2019
1 parent 9eadf53 commit 6ba6999706308aeae2ae8fa9e8a1b5cd7d186d47
@@ -23,20 +23,20 @@ class midgard_admin_user_validator
*/
public function is_username_available(array $fields)
{
$result = [];
if (!empty($fields["username"])) {
$user = midcom::get()->auth->get_user_by_name($fields["username"]);
if ( $user
$mc = new midgard_collector('midgard_user', 'login', $fields["username"]);
$mc->set_key_property('person');
$mc->add_constraint('authtype', '=', midcom::get()->config->get('auth_type'));
$mc->execute();
$keys = $mc->list_keys();
if ( count($keys) > 0
&& ( !isset($fields['person'])
|| $user->guid != $fields['person'])) {
$result["username"] = sprintf(midcom::get()->i18n->get_string("username %s is already in use", "midgard.admin.user"), $fields['username']);
|| key($keys) != $fields['person'])) {
return [
"username" => sprintf(midcom::get()->i18n->get_string("username %s is already in use", "midgard.admin.user"), $fields['username'])
];
}
}
if (!empty($result)) {
return $result;
}
return true;
}
}
@@ -1,7 +1,7 @@
'lostpassword_by_username' => [
'description' => 'lost password by username',
'validation' => [
[
[
'callback' => [new org_openpsa_user_validator, 'username_exists'],
],
],
@@ -18,7 +18,7 @@
'lostpassword_by_email' => [
'description' => 'lost password by email',
'validation' => [
[
[
'callback' => [new org_openpsa_user_validator, 'email_exists'],
],
],
@@ -36,7 +36,7 @@
'lostpassword_by_email_username' => [
'description' => 'lost password by email and username',
'validation' => [
[
[
'callback' => [new org_openpsa_user_validator, 'email_and_username_exist'],
],
],
@@ -23,7 +23,7 @@ public function validate_edit_form(array $fields)
{
$result = $this->is_username_available($fields);
if(isset($fields['new_password'])){
if (isset($fields['new_password'])) {
$result_password = $this->password_check($fields);
if (is_array($result_password)) {
$result = (is_array($result)) ? array_merge($result, $result_password) : $result_password;
@@ -47,7 +47,7 @@ public function validate_create_form(array $fields)
$result = $this->is_username_available($fields);
$accounthelper = new org_openpsa_user_accounthelper();
if($fields['password']['switch'] && !$accounthelper->check_password_strength($fields['password']['password'])){
if ($fields['password']['switch'] && !$accounthelper->check_password_strength($fields['password']['password'])){
$result = ['password' => midcom::get()->i18n->get_string('password weak', 'org.openpsa.user')];
}
@@ -86,14 +86,8 @@ public function verify_existing_password(array $fields)
*/
public function username_exists(array $fields)
{
$result = [];
$user = midcom::get()->auth->get_user_by_name($fields["username"]);
if (!$user) {
$result["username"] = midcom::get()->i18n->get_string("unknown username", "org.openpsa.user");
}
if (!empty($result)) {
return $result;
if ($this->is_username_available(['username' => $fields['username']]) === true) {
return ["username" => midcom::get()->i18n->get_string("unknown username", "org.openpsa.user")];
}
return true;
}

0 comments on commit 6ba6999

Please sign in to comment.
You can’t perform that action at this time.