2026-01-15 01:13:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
refactor: settings redesign, Spanish translations, language system, strict_types
Settings pages:
- Redesign layout to match Sendkit (max-w-4xl, space-y-12, Separator sections)
- Merge Members page into Workspace settings with Table, invite Dialog, ConfirmDeleteModal
- Add workspace logo upload/delete routes and controller methods
- Translate all hardcoded strings in Workspace.vue modals
Language system:
- Drop languages table, replace language_id FK with locale string column on users
- Create config/languages.php for available languages and default locale
- Add Spanish (es) translations (13 files)
- Simplify HandleInertiaRequests, ProfileController, RegisteredUserController
Code quality:
- Add declare(strict_types=1) to all PHP files
- Fix MastodonPublisher using wrong attribute (filename -> original_filename)
- Fix HasMediaTest for new has_photo/photo_url accessors
- Fix PublishToSocialPlatformTest type error revealed by strict_types
- Remove orphaned Language model from AppServiceProvider morph map
- Update User TypeScript interface (has_photo, photo_url, locale)
- Eager load media relation on workspaces to prevent N+1
- Add 8 new tests for workspace logo upload/delete
- Update workspace settings test to assert members/invitations props
All 710 tests passing.
2026-03-30 03:20:43 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-15 01:13:44 +00:00
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
2026-01-17 17:44:37 +00:00
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
|
|
|
use App\Enums\SocialAccount\Status;
|
2026-01-15 01:13:44 +00:00
|
|
|
use App\Models\Workspace;
|
2026-01-26 14:15:29 +00:00
|
|
|
use App\Services\Social\LinkedInTokenSynchronizer;
|
2026-01-15 01:13:44 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-01-17 02:46:30 +00:00
|
|
|
use Illuminate\View\View;
|
2026-01-15 01:13:44 +00:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class LinkedInController extends SocialController
|
|
|
|
|
{
|
|
|
|
|
protected string $driver = 'linkedin';
|
|
|
|
|
|
|
|
|
|
protected SocialPlatform $platform = SocialPlatform::LinkedIn;
|
|
|
|
|
|
|
|
|
|
protected array $scopes = [
|
|
|
|
|
'openid',
|
|
|
|
|
'profile',
|
|
|
|
|
'email',
|
|
|
|
|
'w_member_social',
|
|
|
|
|
];
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function connect(Request $request): Response|RedirectResponse
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
2026-01-16 15:36:52 +00:00
|
|
|
$this->ensurePlatformEnabled();
|
2026-01-17 02:46:30 +00:00
|
|
|
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.workspaces.create');
|
2026-01-17 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
$this->authorize('manageAccounts', $workspace);
|
2026-01-15 01:13:44 +00:00
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
$existingAccount = $workspace->socialAccounts()
|
|
|
|
|
->where('platform', $this->platform->value)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($existingAccount && ! $existingAccount->isDisconnected()) {
|
2026-01-26 15:01:53 +00:00
|
|
|
session()->flash('flash.banner', __('accounts.flash.already_connected'));
|
|
|
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
|
|
|
|
|
|
return back();
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->redirectToProvider($request, $this->driver, $this->scopes);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function callback(Request $request): View
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
|
|
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
|
|
|
|
|
|
if (! $workspaceId) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Session expired. Please try again.', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Workspace not found.', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$socialUser = Socialite::driver($this->driver)->user();
|
2026-01-17 02:46:30 +00:00
|
|
|
$existingAccount = $workspace->socialAccounts()
|
|
|
|
|
->where('platform', $this->platform->value)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
// If account exists and is connected, don't allow duplicate
|
|
|
|
|
if ($existingAccount && ! $existingAccount->isDisconnected()) {
|
|
|
|
|
return $this->popupCallback(false, 'This platform is already connected.', $this->platform->value);
|
|
|
|
|
}
|
2026-01-15 01:13:44 +00:00
|
|
|
|
|
|
|
|
// Fetch vanityName from LinkedIn API (not available via OpenID)
|
|
|
|
|
$username = $this->fetchVanityName($socialUser->token);
|
|
|
|
|
|
|
|
|
|
Log::info('LinkedIn OAuth User Data', [
|
|
|
|
|
'nickname' => $socialUser->getNickname(),
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'user' => $socialUser->user ?? [],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$avatarPath = uploadFromUrl($socialUser->getAvatar());
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
if ($existingAccount) {
|
|
|
|
|
// Reconnect existing account
|
|
|
|
|
$existingAccount->update([
|
|
|
|
|
'platform_user_id' => $socialUser->getId(),
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'display_name' => $socialUser->getName(),
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $socialUser->token,
|
|
|
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
|
|
|
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
|
|
|
|
|
'scopes' => $socialUser->approvedScopes ?? null,
|
|
|
|
|
]);
|
|
|
|
|
$existingAccount->markAsConnected();
|
|
|
|
|
|
2026-01-26 14:15:29 +00:00
|
|
|
// Sync tokens to LinkedIn Page if it exists
|
|
|
|
|
app(LinkedInTokenSynchronizer::class)->syncTokens($existingAccount);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(true, 'LinkedIn account reconnected!', $this->platform->value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new account
|
2026-01-26 14:15:29 +00:00
|
|
|
$account = $workspace->socialAccounts()->create([
|
2026-01-15 01:13:44 +00:00
|
|
|
'platform' => $this->platform->value,
|
|
|
|
|
'platform_user_id' => $socialUser->getId(),
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'display_name' => $socialUser->getName(),
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $socialUser->token,
|
|
|
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
|
|
|
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
|
|
|
|
|
'scopes' => $socialUser->approvedScopes ?? null,
|
2026-01-17 02:46:30 +00:00
|
|
|
'status' => Status::Connected,
|
2026-01-15 01:13:44 +00:00
|
|
|
]);
|
|
|
|
|
|
2026-01-26 14:15:29 +00:00
|
|
|
// Sync tokens to LinkedIn Page if it exists
|
|
|
|
|
app(LinkedInTokenSynchronizer::class)->syncTokens($account);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(true, 'LinkedIn account connected!', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('LinkedIn OAuth Error', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Error connecting account. Please try again.', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function fetchVanityName(string $accessToken): ?string
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$response = Http::withToken($accessToken)
|
|
|
|
|
->withHeaders(['X-RestLi-Protocol-Version' => '2.0.0'])
|
|
|
|
|
->get('https://api.linkedin.com/v2/me', [
|
|
|
|
|
'projection' => '(id,vanityName,localizedFirstName,localizedLastName)',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Log::info('LinkedIn /me API response', [
|
|
|
|
|
'status' => $response->status(),
|
|
|
|
|
'body' => $response->json(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
|
|
return $response->json('vanityName');
|
|
|
|
|
}
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::warning('Failed to fetch LinkedIn vanityName', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|