diff --git a/src/app/AuthAttempt.php b/src/app/AuthAttempt.php --- a/src/app/AuthAttempt.php +++ b/src/app/AuthAttempt.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Iatstuti\Database\Support\NullableFields; +use App\Traits\UuidStrKeyTrait; use Carbon\Carbon; /** @@ -14,6 +15,7 @@ class AuthAttempt extends Model { use NullableFields; + use UuidStrKeyTrait; // No specific reason public const REASON_NONE = ''; @@ -43,9 +45,6 @@ 'last_seen' => 'datetime' ]; - public $incrementing = false; - protected $keyType = 'string'; - /** * Prepare a date for array / JSON serialization. * diff --git a/src/app/Discount.php b/src/app/Discount.php --- a/src/app/Discount.php +++ b/src/app/Discount.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidStrKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -17,9 +18,7 @@ class Discount extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidStrKeyTrait; protected $casts = [ 'discount' => 'integer', diff --git a/src/app/Domain.php b/src/app/Domain.php --- a/src/app/Domain.php +++ b/src/app/Domain.php @@ -3,6 +3,7 @@ namespace App; use App\Wallet; +use App\Traits\UuidIntKeyTrait; use App\Traits\DomainConfigTrait; use App\Traits\SettingsTrait; use Illuminate\Database\Eloquent\Model; @@ -18,6 +19,7 @@ */ class Domain extends Model { + use UuidIntKeyTrait; use DomainConfigTrait; use SettingsTrait; use SoftDeletes; @@ -48,10 +50,6 @@ public const HASH_TEXT = 2; public const HASH_CNAME = 3; - public $incrementing = false; - - protected $keyType = 'bigint'; - protected $fillable = [ 'namespace', 'status', diff --git a/src/app/Entitlement.php b/src/app/Entitlement.php --- a/src/app/Entitlement.php +++ b/src/app/Entitlement.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use App\Traits\UuidStrKeyTrait; /** * The eloquent definition of an Entitlement. @@ -27,20 +28,7 @@ class Entitlement extends Model { use SoftDeletes; - - /** - * This table does not use auto-increment. - * - * @var boolean - */ - public $incrementing = false; - - /** - * The key type is actually a string. - * - * @var string - */ - protected $keyType = 'string'; + use UuidStrKeyTrait; /** * The fillable columns for this Entitlement diff --git a/src/app/Group.php b/src/app/Group.php --- a/src/app/Group.php +++ b/src/app/Group.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidIntKeyTrait; use App\Wallet; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; @@ -17,6 +18,7 @@ */ class Group extends Model { + use UuidIntKeyTrait; use SoftDeletes; // we've simply never heard of this domain @@ -30,10 +32,6 @@ // domain has been created in LDAP public const STATUS_LDAP_READY = 1 << 4; - public $incrementing = false; - - protected $keyType = 'bigint'; - protected $fillable = [ 'email', 'status', diff --git a/src/app/Observers/AuthAttemptObserver.php b/src/app/Observers/AuthAttemptObserver.php deleted file mode 100644 --- a/src/app/Observers/AuthAttemptObserver.php +++ /dev/null @@ -1,31 +0,0 @@ -{$authAttempt->getKeyName()} = $allegedly_unique; - break; - } - } - } -} diff --git a/src/app/Observers/DiscountObserver.php b/src/app/Observers/DiscountObserver.php --- a/src/app/Observers/DiscountObserver.php +++ b/src/app/Observers/DiscountObserver.php @@ -18,14 +18,6 @@ */ public function creating(Discount $discount): void { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Discount::find($allegedly_unique)) { - $discount->{$discount->getKeyName()} = $allegedly_unique; - break; - } - } - $discount->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/DomainObserver.php b/src/app/Observers/DomainObserver.php --- a/src/app/Observers/DomainObserver.php +++ b/src/app/Observers/DomainObserver.php @@ -16,14 +16,6 @@ */ public function creating(Domain $domain): void { - while (true) { - $allegedly_unique = \App\Utils::uuidInt(); - if (!Domain::withTrashed()->find($allegedly_unique)) { - $domain->{$domain->getKeyName()} = $allegedly_unique; - break; - } - } - $domain->namespace = \strtolower($domain->namespace); $domain->status |= Domain::STATUS_NEW; diff --git a/src/app/Observers/EntitlementObserver.php b/src/app/Observers/EntitlementObserver.php --- a/src/app/Observers/EntitlementObserver.php +++ b/src/app/Observers/EntitlementObserver.php @@ -45,14 +45,6 @@ return false; } - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Entitlement::withTrashed()->find($allegedly_unique)) { - $entitlement->{$entitlement->getKeyName()} = $allegedly_unique; - break; - } - } - return true; } diff --git a/src/app/Observers/GroupObserver.php b/src/app/Observers/GroupObserver.php --- a/src/app/Observers/GroupObserver.php +++ b/src/app/Observers/GroupObserver.php @@ -16,14 +16,6 @@ */ public function creating(Group $group): void { - while (true) { - $allegedly_unique = \App\Utils::uuidInt(); - if (!Group::withTrashed()->find($allegedly_unique)) { - $group->{$group->getKeyName()} = $allegedly_unique; - break; - } - } - $group->status |= Group::STATUS_NEW | Group::STATUS_ACTIVE; $group->tenant_id = \config('app.tenant_id'); diff --git a/src/app/Observers/PackageObserver.php b/src/app/Observers/PackageObserver.php --- a/src/app/Observers/PackageObserver.php +++ b/src/app/Observers/PackageObserver.php @@ -12,22 +12,12 @@ /** * Handle the "creating" event on an Package. * - * Ensures that the entry uses a custom ID (uuid). - * * @param Package $package The Package being created. * * @return void */ public function creating(Package $package) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Package::find($allegedly_unique)) { - $package->{$package->getKeyName()} = $allegedly_unique; - break; - } - } - $package->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/PlanObserver.php b/src/app/Observers/PlanObserver.php --- a/src/app/Observers/PlanObserver.php +++ b/src/app/Observers/PlanObserver.php @@ -20,14 +20,6 @@ */ public function creating(Plan $plan) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Plan::find($allegedly_unique)) { - $plan->{$plan->getKeyName()} = $allegedly_unique; - break; - } - } - $plan->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/SignupInvitationObserver.php b/src/app/Observers/SignupInvitationObserver.php --- a/src/app/Observers/SignupInvitationObserver.php +++ b/src/app/Observers/SignupInvitationObserver.php @@ -18,14 +18,6 @@ */ public function creating(SI $invitation) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!SI::find($allegedly_unique)) { - $invitation->{$invitation->getKeyName()} = $allegedly_unique; - break; - } - } - $invitation->status = SI::STATUS_NEW; $invitation->tenant_id = \config('app.tenant_id'); diff --git a/src/app/Observers/SkuObserver.php b/src/app/Observers/SkuObserver.php --- a/src/app/Observers/SkuObserver.php +++ b/src/app/Observers/SkuObserver.php @@ -15,14 +15,6 @@ */ public function creating(Sku $sku) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Sku::find($allegedly_unique)) { - $sku->{$sku->getKeyName()} = $allegedly_unique; - break; - } - } - $sku->tenant_id = \config('app.tenant_id'); } } diff --git a/src/app/Observers/TransactionObserver.php b/src/app/Observers/TransactionObserver.php --- a/src/app/Observers/TransactionObserver.php +++ b/src/app/Observers/TransactionObserver.php @@ -15,14 +15,6 @@ */ public function creating(Transaction $transaction): void { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Transaction::find($allegedly_unique)) { - $transaction->{$transaction->getKeyName()} = $allegedly_unique; - break; - } - } - if (!isset($transaction->user_email)) { $transaction->user_email = \App\Utils::userEmailOrNull(); } diff --git a/src/app/Observers/UserObserver.php b/src/app/Observers/UserObserver.php --- a/src/app/Observers/UserObserver.php +++ b/src/app/Observers/UserObserver.php @@ -23,16 +23,6 @@ */ public function creating(User $user) { - if (!$user->id) { - while (true) { - $allegedly_unique = \App\Utils::uuidInt(); - if (!User::withTrashed()->find($allegedly_unique)) { - $user->{$user->getKeyName()} = $allegedly_unique; - break; - } - } - } - $user->email = \strtolower($user->email); // only users that are not imported get the benefit of the doubt. diff --git a/src/app/Observers/WalletObserver.php b/src/app/Observers/WalletObserver.php --- a/src/app/Observers/WalletObserver.php +++ b/src/app/Observers/WalletObserver.php @@ -18,14 +18,6 @@ */ public function creating(Wallet $wallet) { - while (true) { - $allegedly_unique = \App\Utils::uuidStr(); - if (!Wallet::find($allegedly_unique)) { - $wallet->{$wallet->getKeyName()} = $allegedly_unique; - break; - } - } - $wallet->currency = \config('app.currency'); } diff --git a/src/app/Package.php b/src/app/Package.php --- a/src/app/Package.php +++ b/src/app/Package.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidStrKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -32,9 +33,7 @@ class Package extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidStrKeyTrait; public $timestamps = false; diff --git a/src/app/Plan.php b/src/app/Plan.php --- a/src/app/Plan.php +++ b/src/app/Plan.php @@ -2,6 +2,7 @@ namespace App; +use App\Traits\UuidStrKeyTrait; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; @@ -27,9 +28,8 @@ class Plan extends Model { use HasTranslations; + use UuidStrKeyTrait; - public $incrementing = false; - protected $keyType = 'string'; public $timestamps = false; protected $fillable = [ diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php --- a/src/app/Providers/AppServiceProvider.php +++ b/src/app/Providers/AppServiceProvider.php @@ -44,7 +44,6 @@ */ public function boot() { - \App\AuthAttempt::observe(\App\Observers\AuthAttemptObserver::class); \App\Discount::observe(\App\Observers\DiscountObserver::class); \App\Domain::observe(\App\Observers\DomainObserver::class); \App\Entitlement::observe(\App\Observers\EntitlementObserver::class); diff --git a/src/app/SignupInvitation.php b/src/app/SignupInvitation.php --- a/src/app/SignupInvitation.php +++ b/src/app/SignupInvitation.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use App\Traits\UuidStrKeyTrait; /** * The eloquent definition of a signup invitation. @@ -16,6 +17,8 @@ */ class SignupInvitation extends Model { + use UuidStrKeyTrait; + // just created public const STATUS_NEW = 1 << 0; // it's been sent successfully @@ -27,20 +30,6 @@ /** - * Indicates if the IDs are auto-incrementing. - * - * @var bool - */ - public $incrementing = false; - - /** - * The "type" of the auto-incrementing ID. - * - * @var string - */ - protected $keyType = 'string'; - - /** * The attributes that are mass assignable. * * @var array diff --git a/src/app/Sku.php b/src/app/Sku.php --- a/src/app/Sku.php +++ b/src/app/Sku.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; +use App\Traits\UuidStrKeyTrait; /** * The eloquent definition of a Stock Keeping Unit (SKU). @@ -23,9 +24,7 @@ class Sku extends Model { use HasTranslations; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidStrKeyTrait; protected $casts = [ 'units_free' => 'integer' diff --git a/src/app/Traits/UuidIntKeyTrait.php b/src/app/Traits/UuidIntKeyTrait.php new file mode 100644 --- /dev/null +++ b/src/app/Traits/UuidIntKeyTrait.php @@ -0,0 +1,51 @@ +{$model->getKeyName()})) { + $allegedly_unique = \App\Utils::uuidInt(); + + // Verify if unique + if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { + while ($model->withTrashed()->find($allegedly_unique)) { + $allegedly_unique = \App\Utils::uuidInt(); + } + } else { + while ($model->find($allegedly_unique)) { + $allegedly_unique = \App\Utils::uuidInt(); + } + } + + $model->{$model->getKeyName()} = $allegedly_unique; + } + }); + } + + /** + * Get if the key is incrementing. + * + * @return bool + */ + public function getIncrementing() + { + return false; + } + + /** + * Get the key type. + * + * @return string + */ + public function getKeyType() + { + return 'bigint'; + } +} diff --git a/src/app/Traits/UuidStrKeyTrait.php b/src/app/Traits/UuidStrKeyTrait.php new file mode 100644 --- /dev/null +++ b/src/app/Traits/UuidStrKeyTrait.php @@ -0,0 +1,51 @@ +{$model->getKeyName()})) { + $allegedly_unique = \App\Utils::uuidStr(); + + // Verify if unique + if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { + while ($model->withTrashed()->find($allegedly_unique)) { + $allegedly_unique = \App\Utils::uuidStr(); + } + } else { + while ($model->find($allegedly_unique)) { + $allegedly_unique = \App\Utils::uuidStr(); + } + } + + $model->{$model->getKeyName()} = $allegedly_unique; + } + }); + } + + /** + * Get if the key is incrementing. + * + * @return bool + */ + public function getIncrementing() + { + return false; + } + + /** + * Get the key type. + * + * @return string + */ + public function getKeyType() + { + return 'string'; + } +} diff --git a/src/app/Transaction.php b/src/app/Transaction.php --- a/src/app/Transaction.php +++ b/src/app/Transaction.php @@ -4,6 +4,7 @@ use App\Entitlement; use App\Wallet; +use App\Traits\UuidStrKeyTrait; use Illuminate\Database\Eloquent\Model; /** @@ -20,6 +21,8 @@ */ class Transaction extends Model { + use UuidStrKeyTrait; + public const ENTITLEMENT_BILLED = 'billed'; public const ENTITLEMENT_CREATED = 'created'; public const ENTITLEMENT_DELETED = 'deleted'; @@ -56,12 +59,6 @@ 'amount' => 'integer', ]; - /** @var boolean This model uses an automatically incrementing integer primary key? */ - public $incrementing = false; - - /** @var string The type of the primary key */ - protected $keyType = 'string'; - /** * Returns the entitlement to which the transaction is assigned (if any) diff --git a/src/app/User.php b/src/app/User.php --- a/src/app/User.php +++ b/src/app/User.php @@ -5,6 +5,7 @@ use App\Entitlement; use App\UserAlias; use App\Sku; +use App\Traits\UuidIntKeyTrait; use App\Traits\UserConfigTrait; use App\Traits\UserAliasesTrait; use App\Traits\SettingsTrait; @@ -27,6 +28,7 @@ */ class User extends Authenticatable { + use UuidIntKeyTrait; use NullableFields; use UserConfigTrait; use UserAliasesTrait; @@ -47,11 +49,6 @@ // user mailbox has been created in IMAP public const STATUS_IMAP_READY = 1 << 5; - - // change the default primary key type - public $incrementing = false; - protected $keyType = 'bigint'; - /** * The attributes that are mass assignable. * diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -4,6 +4,7 @@ use App\User; use App\Traits\SettingsTrait; +use App\Traits\UuidStrKeyTrait; use Carbon\Carbon; use Iatstuti\Database\Support\NullableFields; use Illuminate\Database\Eloquent\Model; @@ -25,9 +26,7 @@ { use NullableFields; use SettingsTrait; - - public $incrementing = false; - protected $keyType = 'string'; + use UuidStrKeyTrait; public $timestamps = false;