mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:54:04 +00:00
Add more tenant key generators (#36)
* Add RandomHexGenerator, create new namespace * phpstan ignore * add base64 generator * add note about base64 being case sensitive * docblock updates * replace old UUIDGenerator with a class that throws an exception * replace base64 generator with a random string generator * Fix namespace * Fix code style (php-cs-fixer) * add test for the deprecated uuid generator * update comments --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
bf1ba69fe3
commit
dc430666ba
7 changed files with 126 additions and 9 deletions
|
|
@ -4,16 +4,15 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
// todo@move move to separate namespace
|
||||
|
||||
// todo@deprecation remove after 2024-04-12
|
||||
class UUIDGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static function generate(Model $model): string
|
||||
{
|
||||
return Uuid::uuid4()->toString();
|
||||
throw new Exception('Tenancy update note: UUIDGenerator has been renamed to Stancl\Tenancy\UniqueIdentifierGenerators\UUIDGenerator. Please update your config/tenancy.php');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
src/UniqueIdentifierGenerators/RandomHexGenerator.php
Normal file
25
src/UniqueIdentifierGenerators/RandomHexGenerator.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
/**
|
||||
* Generates a cryptographically secure random hex string for the tenant key.
|
||||
*
|
||||
* To customize the byte length, change the static `bytes` property.
|
||||
* The produced string length is 2 * byte length.
|
||||
* The number of unique combinations is 2 ^ (8 * byte length).
|
||||
*/
|
||||
class RandomHexGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static int $bytes = 6;
|
||||
|
||||
public static function generate(Model $model): string
|
||||
{
|
||||
return bin2hex(random_bytes(static::$bytes));
|
||||
}
|
||||
}
|
||||
25
src/UniqueIdentifierGenerators/RandomStringGenerator.php
Normal file
25
src/UniqueIdentifierGenerators/RandomStringGenerator.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
/**
|
||||
* Generates a cryptographically secure random string for the tenant key.
|
||||
*
|
||||
* To customize the string length, change the static `$length` property.
|
||||
* The number of unique combinations is 36 ^ string length.
|
||||
*/
|
||||
class RandomStringGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static int $length = 8;
|
||||
|
||||
public static function generate(Model $model): string
|
||||
{
|
||||
return Str::random(static::$length);
|
||||
}
|
||||
}
|
||||
20
src/UniqueIdentifierGenerators/UUIDGenerator.php
Normal file
20
src/UniqueIdentifierGenerators/UUIDGenerator.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\UniqueIdentifierGenerators;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
/**
|
||||
* Generates a UUID for the tenant key.
|
||||
*/
|
||||
class UUIDGenerator implements UniqueIdentifierGenerator
|
||||
{
|
||||
public static function generate(Model $model): string
|
||||
{
|
||||
return Uuid::uuid4()->toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue