From 2d814f383905006130d348899b4c4a01ada03a5b Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Tue, 20 Jul 2021 16:45:17 +0200 Subject: [PATCH] controller refactoring part 10 --- public/mix-manifest.json | 14 ++-- .../Others/CreatePersonaTokenPopup.vue | 2 +- resources/js/views/User/Password.vue | 2 +- routes/user.php | 7 +- .../Controllers/AccessTokenController.php | 64 +++++++++++++++++++ .../Users/Controllers/AccountController.php | 43 ------------- .../Controllers/StorePlansController.php | 2 +- ...bscriptionServiceCredentialsController.php | 2 +- .../Controllers/GetSetupIntentController.php | 2 +- tests/App/Users/PersonalAccessTokenTest.php | 4 +- 10 files changed, 84 insertions(+), 58 deletions(-) create mode 100644 src/App/Users/Controllers/AccessTokenController.php diff --git a/public/mix-manifest.json b/public/mix-manifest.json index f2503a32..c95c86c6 100644 --- a/public/mix-manifest.json +++ b/public/mix-manifest.json @@ -60,11 +60,11 @@ "/chunks/profile.js": "/chunks/profile.js?id=41b536880e846017d2d0", "/chunks/profile~chunks/settings-password.js": "/chunks/profile~chunks/settings-password.js?id=fd25990d1ccec0294602", "/chunks/purchase-code.js": "/chunks/purchase-code.js?id=b35e751cdc1946a4fb60", - "/chunks/settings.js": "/chunks/settings.js?id=b7d5d142d6e4cae354b0", + "/chunks/settings.js": "/chunks/settings.js?id=7070921d97a2510445ac", "/chunks/settings-create-payment-methods.js": "/chunks/settings-create-payment-methods.js?id=0e818baec7f92c7fa542", "/chunks/settings-invoices.js": "/chunks/settings-invoices.js?id=eab2781e9935230c6971", - "/chunks/settings-password.js": "/chunks/settings-password.js?id=b01b17b523e99064d8ca", - "/chunks/settings-payment-methods.js": "/chunks/settings-payment-methods.js?id=1a1c7f6e9c2857ec941f", + "/chunks/settings-password.js": "/chunks/settings-password.js?id=9258c6edb9815296b976", + "/chunks/settings-payment-methods.js": "/chunks/settings-payment-methods.js?id=fc84aad90497e1ae99f9", "/chunks/settings-storage.js": "/chunks/settings-storage.js?id=682f4f690ac252a78a3a", "/chunks/settings-subscription.js": "/chunks/settings-subscription.js?id=4f1793d53633a2c4cc18", "/chunks/settings~chunks/settings-password.js": "/chunks/settings~chunks/settings-password.js?id=f5aaffe3aa073cb2ee19", @@ -78,7 +78,7 @@ "/chunks/stripe-credentials.js": "/chunks/stripe-credentials.js?id=3beb031ba99961e8c51c", "/chunks/subscription-plans.js": "/chunks/subscription-plans.js?id=4ae406cdb5805d9c4a45", "/chunks/subscription-service.js": "/chunks/subscription-service.js?id=a09dc140a3914fb64a1a", - "/chunks/upgrade-billing.js": "/chunks/upgrade-billing.js?id=0de6296ee18d466dca13", + "/chunks/upgrade-billing.js": "/chunks/upgrade-billing.js?id=2ae7d7c13fee27b4edff", "/chunks/upgrade-billing~chunks/upgrade-plan.js": "/chunks/upgrade-billing~chunks/upgrade-plan.js?id=43888143d0a6afe67d7a", "/chunks/upgrade-plan.js": "/chunks/upgrade-plan.js?id=5979a154f760a992e617", "/chunks/user.js": "/chunks/user.js?id=41076be9126702330159", @@ -275,5 +275,9 @@ "/chunks/platform.13a7c4eb981e304dd11c.hot-update.js": "/chunks/platform.13a7c4eb981e304dd11c.hot-update.js", "/chunks/platform.1dc9a86f8f195ad336c8.hot-update.js": "/chunks/platform.1dc9a86f8f195ad336c8.hot-update.js", "/chunks/platform.c72b7ed144ae27e09ac0.hot-update.js": "/chunks/platform.c72b7ed144ae27e09ac0.hot-update.js", - "/chunks/homepage.e80d5d3c53decab802d5.hot-update.js": "/chunks/homepage.e80d5d3c53decab802d5.hot-update.js" + "/chunks/homepage.e80d5d3c53decab802d5.hot-update.js": "/chunks/homepage.e80d5d3c53decab802d5.hot-update.js", + "/chunks/settings-payment-methods.8681aa04995d55912730.hot-update.js": "/chunks/settings-payment-methods.8681aa04995d55912730.hot-update.js", + "/chunks/upgrade-billing.8681aa04995d55912730.hot-update.js": "/chunks/upgrade-billing.8681aa04995d55912730.hot-update.js", + "/chunks/settings.38169fdae69094598f31.hot-update.js": "/chunks/settings.38169fdae69094598f31.hot-update.js", + "/chunks/settings-password.38169fdae69094598f31.hot-update.js": "/chunks/settings-password.38169fdae69094598f31.hot-update.js" } diff --git a/resources/js/components/Others/CreatePersonaTokenPopup.vue b/resources/js/components/Others/CreatePersonaTokenPopup.vue index 27555437..ece5ec08 100644 --- a/resources/js/components/Others/CreatePersonaTokenPopup.vue +++ b/resources/js/components/Others/CreatePersonaTokenPopup.vue @@ -100,7 +100,7 @@ export default { this.isLoading = true axios - .post('/api/user/token/create', { + .post('/api/user/tokens', { name: this.name }) .then(response => { diff --git a/resources/js/views/User/Password.vue b/resources/js/views/User/Password.vue index 2331ca8c..116fed77 100644 --- a/resources/js/views/User/Password.vue +++ b/resources/js/views/User/Password.vue @@ -161,7 +161,7 @@ }) }, deleteToken(id) { - axios.delete(`/api/user/token/revoke/${id}`) + axios.delete(`/api/user/tokens/${id}`) .then(() => { this.tokens = this.tokens.filter(tokenItem => tokenItem.id !== id) diff --git a/routes/user.php b/routes/user.php index f2ac4172..7a67531e 100644 --- a/routes/user.php +++ b/routes/user.php @@ -1,5 +1,6 @@ ['auth:sanctum']], function () { // Account Route::patch('/relationships/settings', [AccountController::class, 'update_user_settings']); - Route::delete('/token/revoke/{token}', [AccountController::class, 'revoke_token']); - Route::post('/token/create', [AccountController::class, 'create_token']); Route::post('/password', [AccountController::class, 'change_password']); Route::get('/subscription', [SubscriptionController::class, 'show']); - Route::get('/tokens', [AccountController::class, 'tokens']); Route::get('/invoices', [AccountController::class, 'invoices']); Route::get('/storage', [AccountController::class, 'storage']); Route::get('/', [AccountController::class, 'user']); + // User Access Token + Route::apiResource('/tokens', AccessTokenController::class); + // Payment cards Route::apiResource('/payment-cards', PaymentMethodsController::class); diff --git a/src/App/Users/Controllers/AccessTokenController.php b/src/App/Users/Controllers/AccessTokenController.php new file mode 100644 index 00000000..7da3e3f8 --- /dev/null +++ b/src/App/Users/Controllers/AccessTokenController.php @@ -0,0 +1,64 @@ +tokens()->get(), 200 + ); + } + + /** + * Create user tokens + */ + public function store(UserCreateAccessTokenRequest $request): Response + { + abort_if(is_demo_account('howdy@hi5ve.digital'), 201, [ + 'name' => 'token', + 'token' => Str::random(40), + 'abilities' => '["*"]', + 'tokenable_id' => Str::uuid(), + 'updated_at' => now(), + 'created_at' => now(), + 'id' => Str::random(40), + ]); + + $token = Auth::user() + ->createToken( + $request->input('name') + ); + + return response($token, 201); + } + + /** + * Delete user token + */ + public function destroy(PersonalAccessToken $token): Response + { + abort_if(is_demo_account('howdy@hi5ve.digital'), 204, 'Deleted!'); + + if (Auth::id() !== $token->tokenable_id) { + return response('Unauthorized', 401); + } + + $token->delete(); + + return response('Deleted!', 204); + } +} \ No newline at end of file diff --git a/src/App/Users/Controllers/AccountController.php b/src/App/Users/Controllers/AccountController.php index 530446e4..f0ee20e9 100644 --- a/src/App/Users/Controllers/AccountController.php +++ b/src/App/Users/Controllers/AccountController.php @@ -129,49 +129,6 @@ class AccountController extends Controller return response('Changed!', 204); } - /** - * Get all user tokens - */ - public function tokens(): Response - { - return response( - Auth::user()->tokens()->get(), - 200 - ); - } - - public function create_token(UserCreateAccessTokenRequest $request): Response - { - // Check if is demo - abort_if(is_demo_account('howdy@hi5ve.digital'), 201, [ - 'name' => 'token', - 'token' => Str::random(40), - 'abilities' => '["*"]', - 'tokenable_id' => Str::uuid(), - 'updated_at' => now(), - 'created_at' => now(), - 'id' => Str::random(40), - ]); - - $token = Auth::user()->createToken($request->input('name')); - - return response($token, 201); - } - - public function revoke_token(PersonalAccessToken $token): Response - { - // Check if is demo - abort_if(is_demo_account('howdy@hi5ve.digital'), 204, 'Deleted!'); - - if (Auth::id() !== $token->tokenable_id) { - return response('Unauthorized', 401); - } - - $token->delete(); - - return response('Deleted!', 204); - } - public function email_verification(string $id, Request $request): RedirectResponse | Response { if (! $request->hasValidSignature()) { diff --git a/src/Domain/SetupWizard/Controllers/StorePlansController.php b/src/Domain/SetupWizard/Controllers/StorePlansController.php index 24c216b6..f9b1d928 100644 --- a/src/Domain/SetupWizard/Controllers/StorePlansController.php +++ b/src/Domain/SetupWizard/Controllers/StorePlansController.php @@ -16,7 +16,7 @@ class StorePlansController extends Controller ) { } - public function store_stripe_plans( + public function __invoke( StoreStripePlansRequest $request ): Response { foreach ($request->input('plans') as $plan) { diff --git a/src/Domain/SetupWizard/Controllers/StoreSubscriptionServiceCredentialsController.php b/src/Domain/SetupWizard/Controllers/StoreSubscriptionServiceCredentialsController.php index 80650767..4defbdfa 100644 --- a/src/Domain/SetupWizard/Controllers/StoreSubscriptionServiceCredentialsController.php +++ b/src/Domain/SetupWizard/Controllers/StoreSubscriptionServiceCredentialsController.php @@ -15,7 +15,7 @@ class StoreSubscriptionServiceCredentialsController extends Controller /** * Store and test stripe credentials */ - public function store_stripe_credentials( + public function __invoke( StoreStripeCredentialsRequest $request ): Response { if (! app()->runningUnitTests()) { diff --git a/src/Domain/Subscriptions/Controllers/GetSetupIntentController.php b/src/Domain/Subscriptions/Controllers/GetSetupIntentController.php index e4fa2429..b20176d3 100644 --- a/src/Domain/Subscriptions/Controllers/GetSetupIntentController.php +++ b/src/Domain/Subscriptions/Controllers/GetSetupIntentController.php @@ -18,7 +18,7 @@ class GetSetupIntentController extends Controller public StripeService $stripe, ) {} - public function setup_intent(): Response + public function __invoke(): Response { return response( $this->stripe->getSetupIntent(Auth::user()), 201 diff --git a/tests/App/Users/PersonalAccessTokenTest.php b/tests/App/Users/PersonalAccessTokenTest.php index cebc7ae3..2f02fb97 100644 --- a/tests/App/Users/PersonalAccessTokenTest.php +++ b/tests/App/Users/PersonalAccessTokenTest.php @@ -18,7 +18,7 @@ class PersonalAccessTokenTest extends TestCase $this ->actingAs($user) - ->postJson('/api/user/token/create', [ + ->postJson('/api/user/tokens', [ 'name' => 'token', ]) ->assertStatus(201); @@ -43,7 +43,7 @@ class PersonalAccessTokenTest extends TestCase $this ->actingAs($user) - ->deleteJson("/api/user/token/revoke/$token_id") + ->deleteJson("/api/user/tokens/$token_id") ->assertStatus(204); $this->assertDatabaseMissing('personal_access_tokens', [