diff --git a/src/Auth/Db.php b/src/Auth/Db.php index b3b288e..a6d694e 100644 --- a/src/Auth/Db.php +++ b/src/Auth/Db.php @@ -138,18 +138,29 @@ class Db extends Backend { */ public static function add_user($info) { $values = [ - App::get('auth.db.username_field') => $info['username'], - App::get('auth.db.password_field') => password_hash( - $info['password'], - constant('PASSWORD_'.strtoupper(App::get('auth.db.password_hash_algo'))) + self :: $username_field => $info['username'] ?? null, + self :: $password_field => ( + ($info['password'] ?? null)? + password_hash( + $info['password'], + constant('PASSWORD_'.strtoupper(App::get('auth.db.password_hash_algo'))) + ): + null ), ]; foreach($info as $field => $value) { if (!$value) { - Log :: error("add_user: field %s is missing", $field); + Log :: error("add_user: field %s is missing (or null)", $field); return false; } } + + // Check username uniqueness + if (self :: get_user($info['username'])) { + Log :: error("add_user: a user with username %s already exist"); + return false; + } + foreach(App :: get('auth.db.exposed_fields') as $field) if (isset($info[$field]) && $info[$field]) $values[$field] = $info[$field];