Skip to content

Commit

Permalink
Merge pull request #54 from taylornetwork/dev
Browse files Browse the repository at this point in the history
2.6.2 - Fix #52
  • Loading branch information
itssamtaylor committed Feb 21, 2022
2 parents 543e3b6 + 111a43a commit 23d08b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*



Expand Down Expand Up @@ -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
Expand Down
23 changes: 20 additions & 3 deletions src/FindSimilarUsernames.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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', '');
}
}
9 changes: 5 additions & 4 deletions src/config/username_generator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

use App\Models\User;
use TaylorNetwork\UsernameGenerator\Drivers\EmailDriver;
use TaylorNetwork\UsernameGenerator\Drivers\NameDriver;

Expand Down Expand Up @@ -54,7 +53,7 @@
*
* This is only used if unique is true
*/
'model' => User::class,
'model' => \App\Models\User::class,

/*
* Database field to check and store username
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23d08b2

Please sign in to comment.