-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
363 additions
and
84 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
database/migrations/2024_05_14_091321_create_auth_codes_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class() extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('root_auth_codes', static function (Blueprint $table): void { | ||
$table->id(); | ||
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete(); | ||
$table->integer('code')->unsigned(); | ||
$table->timestamp('expires_at'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('root_auth_codes'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Interfaces\Models; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
interface AuthCode | ||
{ | ||
/** | ||
* Get the user for the model. | ||
*/ | ||
public function user(): BelongsTo; | ||
|
||
/** | ||
* Determine whether the code is active. | ||
*/ | ||
public function active(): bool; | ||
|
||
/** | ||
* Determine whether the code is expired. | ||
*/ | ||
public function expired(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Cone\Root\Models; | ||
|
||
use Cone\Root\Interfaces\Models\AuthCode as Contract; | ||
use Cone\Root\Traits\InteractsWithProxy; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Facades\Date; | ||
|
||
class AuthCode extends Model implements Contract | ||
{ | ||
use HasFactory; | ||
use InteractsWithProxy; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $casts = [ | ||
'code' => 'int', | ||
'expires_at' => 'datetime', | ||
]; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = []; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'root_auth_codes'; | ||
|
||
/** | ||
* Get the proxied interface. | ||
*/ | ||
public static function getProxiedInterface(): string | ||
{ | ||
return Contract::class; | ||
} | ||
|
||
/** | ||
* Get the user for the model. | ||
*/ | ||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
/** | ||
* Determine whether the code is active. | ||
*/ | ||
public function active(): bool | ||
{ | ||
return $this->expires_at->gt(Date::now()); | ||
} | ||
|
||
/** | ||
* Determine whether the code is expired. | ||
*/ | ||
public function expired(): bool | ||
{ | ||
return ! $this->active(); | ||
} | ||
|
||
/** | ||
* Scope the query only to include the active codes. | ||
*/ | ||
public function scopeActive(Builder $query): Builder | ||
{ | ||
return $query->where($query->qualifyColumn('expires_at'), '>', Date::now()); | ||
} | ||
|
||
/** | ||
* Scope the query only to include the expired codes. | ||
*/ | ||
public function scopeExpired(Builder $query): Builder | ||
{ | ||
return $query->where($query->qualifyColumn('expires_at'), '<=', Date::now()); | ||
} | ||
} |
Oops, something went wrong.