-
Couldn't load subscription status.
- Fork 92
Tokens support #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Tokens support #277
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1a87ea
Tokens support
jacksleight 0936715
Rename import
jacksleight bb69f88
Switch to file to make it opt in
ryanmitchell dbe1627
Update test suite
ryanmitchell 9d32896
Merge branch 'statamic-5' into feature/tokens
ryanmitchell 620c7a2
Missed removing this file
ryanmitchell fc3d771
Update to use TokenRepository contract
jacksleight 8fb3b8b
Merge branch 'feature/tokens' of https://github.com/jacksleight/eloqu…
jacksleight 21b3c2b
Merge branch 'statamic-5' into feature/tokens
jasonvarga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
database/migrations/2024_03_07_100000_create_tokens_table.php
This file contains hidden or 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,25 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
| use Statamic\Eloquent\Database\BaseMigration as Migration; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up() | ||
| { | ||
| Schema::create($this->prefix('tokens'), function (Blueprint $table) { | ||
| $table->id(); | ||
| $table->string('token')->unique(); | ||
| $table->string('handler'); | ||
| $table->jsonb('data'); | ||
| $table->timestamp('expire_at'); | ||
| $table->timestamps(); | ||
| }); | ||
| } | ||
|
|
||
| public function down() | ||
| { | ||
| Schema::dropIfExists($this->prefix('tokens')); | ||
| } | ||
| }; |
This file contains hidden or 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 hidden or 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,46 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Eloquent\Tokens; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Statamic\Contracts\Tokens\Token as Contract; | ||
| use Statamic\Tokens\Token as AbstractToken; | ||
|
|
||
| class Token extends AbstractToken | ||
| { | ||
| protected $model; | ||
|
|
||
| public static function fromModel(Model $model) | ||
| { | ||
| return (new static($model->token, $model->handler, $model->data)) | ||
| ->expireAt($model->expire_at) | ||
| ->model($model); | ||
| } | ||
|
|
||
| public function toModel() | ||
| { | ||
| return self::makeModelFromContract($this); | ||
| } | ||
|
|
||
| public static function makeModelFromContract(Contract $source) | ||
| { | ||
| $class = app('statamic.eloquent.tokens.model'); | ||
|
|
||
| return $class::firstOrNew(['token' => $source->token()])->fill([ | ||
| 'handler' => $source->handler(), | ||
| 'data' => $source->data(), | ||
| 'expire_at' => $source->expiry(), | ||
| ]); | ||
| } | ||
|
|
||
| public function model($model = null) | ||
| { | ||
| if (func_num_args() === 0) { | ||
| return $this->model; | ||
| } | ||
|
|
||
| $this->model = $model; | ||
|
|
||
| return $this; | ||
| } | ||
| } |
This file contains hidden or 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,17 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Eloquent\Tokens; | ||
|
|
||
| use Statamic\Eloquent\Database\BaseModel; | ||
|
|
||
| class TokenModel extends BaseModel | ||
| { | ||
| protected $guarded = []; | ||
|
|
||
| protected $table = 'tokens'; | ||
|
|
||
| protected $casts = [ | ||
| 'data' => 'json', | ||
| 'expire_at' => 'datetime', | ||
| ]; | ||
| } |
This file contains hidden or 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,42 @@ | ||
| <?php | ||
|
|
||
| namespace Statamic\Eloquent\Tokens; | ||
|
|
||
| use Statamic\Contracts\Tokens\Token as TokenContract; | ||
| use Statamic\Tokens\TokenRepository as Repository; | ||
|
|
||
| class TokenRepository extends Repository | ||
| { | ||
| public function find(string $token): ?Token | ||
| { | ||
| $model = app('statamic.eloquent.tokens.model')::whereToken($token)->first(); | ||
|
|
||
| if (! $model) { | ||
| return null; | ||
| } | ||
|
|
||
| return Token::fromModel($model); | ||
| } | ||
|
|
||
| public function save(TokenContract $token): bool | ||
| { | ||
| return $token->toModel()->save(); | ||
| } | ||
|
|
||
| public function delete(TokenContract $token): bool | ||
| { | ||
| return $token->toModel()->delete(); | ||
| } | ||
|
|
||
| public function collectGarbage(): void | ||
| { | ||
| app('statamic.eloquent.tokens.model')::where('expire_at', '<', now())->delete(); | ||
| } | ||
|
|
||
| public static function bindings(): array | ||
| { | ||
| return [ | ||
| TokenContract::class => Token::class, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or 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,50 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Repositories; | ||
|
|
||
| use Carbon\Carbon; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Statamic\Contracts\Tokens\Token; | ||
| use Statamic\Eloquent\Tokens\TokenRepository; | ||
| use Tests\TestCase; | ||
|
|
||
| class TokenRepositoryTest extends TestCase | ||
| { | ||
| use RefreshDatabase; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->repo = new TokenRepository(); | ||
|
|
||
| $this->repo->make('abc', 'ExampleHandler', ['foo' => 'bar'])->save(); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_gets_a_token() | ||
| { | ||
| tap($this->repo->find('abc'), function ($token) { | ||
| $this->assertInstanceOf(Token::class, $token); | ||
| $this->assertEquals('abc', $token->token()); | ||
| $this->assertEquals('ExampleHandler', $token->handler()); | ||
| $this->assertEquals(['foo' => 'bar'], $token->data()->all()); | ||
| $this->assertInstanceOf(Carbon::class, $token->expiry()); | ||
| }); | ||
|
|
||
| $this->assertNull($this->repo->find('unknown')); | ||
| } | ||
|
|
||
| /** @test */ | ||
| public function it_saves_a_token_to_the_database() | ||
| { | ||
| $token = $this->repo->make('new', 'ExampleHandler', ['foo' => 'bar']); | ||
|
|
||
| $this->assertNull($this->repo->find('new')); | ||
|
|
||
| $this->repo->save($token); | ||
|
|
||
| $this->assertNotNull($item = $this->repo->find('new')); | ||
| $this->assertEquals(['foo' => 'bar'], $item->data()->all()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.