diff --git a/README.md b/README.md index 2cd6a9d..745b439 100644 --- a/README.md +++ b/README.md @@ -36,12 +36,13 @@ Works for Laravel versions above 5.5 including Laravel 9. ## Most Recent Update -**v2.6.1** +**v2.6.2** -- Added support for PHP 8.1 -- Set minimum PHP version to 7.4 +- Fixed potential SQL injection issue when using REGEXP function. +- Fixed issue where REGEXP function was not returning correct number of similar usernames only when using a separator. +- Changed default config option `prefer_regexp` from `true` to `false` -*Updated January 2, 2022* +*Updated Feb 21, 2022* @@ -650,6 +651,12 @@ MIT ## Change Log +**v2.6.2** + +- Fixed potential SQL injection issue when using REGEXP function. +- Fixed issue where REGEXP function was not returning correct number of similar usernames only when using a separator. +- Changed default config option `prefer_regexp` from `true` to `false` + **v2.6.1** - Added support for PHP 8.1 diff --git a/src/FindSimilarUsernames.php b/src/FindSimilarUsernames.php index 96c452f..82aaa33 100644 --- a/src/FindSimilarUsernames.php +++ b/src/FindSimilarUsernames.php @@ -72,9 +72,7 @@ private function searchUsingLike(string $username) */ private function searchUsingRegexp(string $username) { - $column = $this->getColumn(); - - return static::whereRaw("$column REGEXP '{$username}([0-9]*)?$'")->get(); + return static::where($this->getColumn(), 'REGEXP', $username.'('.$this->getSeparator().')?([0-9]*)?$')->get(); } /** @@ -86,4 +84,23 @@ private function getColumn(): string { return $this->usernameColumn ?? config('username_generator.column', 'username'); } + + /** + * Get the username separator. + * + * Check if the model has a custom separator in its class before checking config. + * + * @return string + */ + private function getSeparator(): string + { + if (method_exists($this, 'generatorConfig')) { + $generator = new Generator(); + $this->generatorConfig($generator); + + return $generator->getConfig('separator', ''); + } + + return config('username_generator.separator', ''); + } } diff --git a/src/config/username_generator.php b/src/config/username_generator.php index f029086..16f7d50 100644 --- a/src/config/username_generator.php +++ b/src/config/username_generator.php @@ -1,6 +1,5 @@ User::class, + 'model' => \App\Models\User::class, /* * Database field to check and store username @@ -102,9 +101,11 @@ 'generate_entered_username' => true, /* - * Prefer using REGEXP + * Prefer using database REGEXP function? + * + * LIKE function will be used as a backup on failure. */ - 'prefer_regexp' => true, + 'prefer_regexp' => false, /* * Field Map