mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
init
This commit is contained in:
30
app/Actions/StorageProvider/AddStorageProvider.php
Normal file
30
app/Actions/StorageProvider/AddStorageProvider.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\StorageProvider;
|
||||
|
||||
use App\Models\StorageProvider;
|
||||
use App\Models\User;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class AddStorageProvider
|
||||
{
|
||||
use ValidateProvider;
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function add(User $user, array $input): mixed
|
||||
{
|
||||
$this->validate($user, $input);
|
||||
|
||||
$storageProvider = new StorageProvider([
|
||||
'user_id' => $user->id,
|
||||
'provider' => $input['provider'],
|
||||
'label' => $input['label'],
|
||||
'connected' => false,
|
||||
]);
|
||||
$storageProvider->save();
|
||||
|
||||
return $storageProvider->provider()->connect();
|
||||
}
|
||||
}
|
34
app/Actions/StorageProvider/HandleProviderCallback.php
Executable file
34
app/Actions/StorageProvider/HandleProviderCallback.php
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\StorageProvider;
|
||||
|
||||
use App\Models\StorageProvider;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Laravel\Socialite\Facades\Socialite;
|
||||
use Laravel\Socialite\Two\User;
|
||||
use Throwable;
|
||||
|
||||
class HandleProviderCallback
|
||||
{
|
||||
public function callback(Request $request, string $provider): string|RedirectResponse
|
||||
{
|
||||
try {
|
||||
$providerId = $request->session()->get('storage_provider_id');
|
||||
/** @var StorageProvider $storageProvider */
|
||||
$storageProvider = StorageProvider::query()->findOrFail($providerId);
|
||||
/** @var User $oauthUser */
|
||||
$oauthUser = Socialite::driver($provider)->user();
|
||||
$storageProvider->token = $oauthUser->token;
|
||||
$storageProvider->refresh_token = $oauthUser->refreshToken;
|
||||
$storageProvider->token_expires_at = now()->addSeconds($oauthUser->expiresIn);
|
||||
$storageProvider->connected = true;
|
||||
$storageProvider->save();
|
||||
/** @TODO toast success message */
|
||||
} catch (Throwable) {
|
||||
/** @TODO toast failed message */
|
||||
}
|
||||
|
||||
return redirect()->route('storage-providers');
|
||||
}
|
||||
}
|
28
app/Actions/StorageProvider/ValidateProvider.php
Executable file
28
app/Actions/StorageProvider/ValidateProvider.php
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\StorageProvider;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
trait ValidateProvider
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(User $user, array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'label' => [
|
||||
'required',
|
||||
Rule::unique('storage_providers', 'label')->where('user_id', $user->id),
|
||||
],
|
||||
'provider' => [
|
||||
'required',
|
||||
Rule::in(config('core.storage_providers')),
|
||||
],
|
||||
])->validateWithBag('addStorageProvider');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user