2026-01-15 01:13:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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 Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
|
|
|
|
|
class LinkedInPageController extends SocialController
|
|
|
|
|
{
|
|
|
|
|
protected string $driver = 'linkedin-openid';
|
|
|
|
|
|
|
|
|
|
protected SocialPlatform $platform = SocialPlatform::LinkedInPage;
|
|
|
|
|
|
|
|
|
|
protected array $scopes = [
|
|
|
|
|
'openid',
|
|
|
|
|
'profile',
|
|
|
|
|
'email',
|
|
|
|
|
'w_organization_social',
|
|
|
|
|
'r_organization_social',
|
|
|
|
|
'rw_organization_admin',
|
|
|
|
|
'w_member_social',
|
|
|
|
|
];
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function connect(Request $request): SymfonyResponse|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) {
|
|
|
|
|
return redirect()->route('workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
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-15 17:24:39 +00:00
|
|
|
return back()->with('error', 'This platform is already connected.');
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
session([
|
|
|
|
|
'social_connect_workspace' => $workspace->id,
|
|
|
|
|
'linkedin_page_reconnect_id' => $existingAccount?->id,
|
|
|
|
|
'social_connect_onboarding' => $request->boolean('onboarding'),
|
|
|
|
|
]);
|
2026-01-15 01:13:44 +00:00
|
|
|
|
|
|
|
|
return Inertia::location(
|
|
|
|
|
Socialite::driver($this->driver)
|
|
|
|
|
->scopes($this->scopes)
|
|
|
|
|
->with([
|
|
|
|
|
'redirect_uri' => config('services.linkedin-openid.redirect_page'),
|
|
|
|
|
])
|
|
|
|
|
->redirect()
|
|
|
|
|
->getTargetUrl()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function callback(Request $request): View|RedirectResponse
|
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)
|
|
|
|
|
->scopes($this->scopes)
|
|
|
|
|
->with([
|
|
|
|
|
'redirect_uri' => config('services.linkedin-openid.redirect_page'),
|
|
|
|
|
])
|
|
|
|
|
->user();
|
|
|
|
|
|
|
|
|
|
// Fetch organizations the user is admin of
|
|
|
|
|
$organizations = $this->fetchOrganizations($socialUser->token);
|
|
|
|
|
|
|
|
|
|
if (empty($organizations)) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'You are not an administrator of any LinkedIn page.', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store data in session and redirect to selection page
|
|
|
|
|
session([
|
|
|
|
|
'linkedin_page_pending' => [
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'user_id' => $socialUser->getId(),
|
|
|
|
|
'name' => $socialUser->getName(),
|
|
|
|
|
'avatar' => $socialUser->getAvatar(),
|
|
|
|
|
'token' => $socialUser->token,
|
|
|
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
|
|
|
'expires_in' => $socialUser->expiresIn,
|
|
|
|
|
'organizations' => $organizations,
|
2026-01-17 02:46:30 +00:00
|
|
|
'reconnect_id' => session('linkedin_page_reconnect_id'),
|
2026-01-15 01:13:44 +00:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return redirect()->route('social.linkedin-page.select-page');
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('LinkedIn Page 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function selectPage(Request $request): Response|RedirectResponse
|
|
|
|
|
{
|
|
|
|
|
$pendingData = session('linkedin_page_pending');
|
|
|
|
|
|
|
|
|
|
if (! $pendingData) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return redirect()->route('dashboard')
|
2026-01-15 17:24:39 +00:00
|
|
|
->with('error', 'Session expired. Please try again.');
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$workspace = Workspace::find($pendingData['workspace_id']);
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return redirect()->route('dashboard')
|
2026-01-15 17:24:39 +00:00
|
|
|
->with('error', 'Workspace not found.');
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Inertia::render('accounts/LinkedInPageSelect', [
|
|
|
|
|
'workspace' => $workspace,
|
|
|
|
|
'organizations' => $pendingData['organizations'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function select(Request $request): View
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'organization_id' => 'required',
|
|
|
|
|
'organization_name' => 'required|string',
|
|
|
|
|
'organization_vanity_name' => 'nullable|string',
|
|
|
|
|
'organization_logo' => 'nullable|string',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$pendingData = session('linkedin_page_pending');
|
|
|
|
|
|
|
|
|
|
if (! $pendingData) {
|
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($pendingData['workspace_id']);
|
|
|
|
|
|
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 {
|
|
|
|
|
$avatarPath = uploadFromUrl($request->organization_logo);
|
2026-01-17 02:46:30 +00:00
|
|
|
$reconnectId = $pendingData['reconnect_id'] ?? null;
|
|
|
|
|
|
|
|
|
|
if ($reconnectId) {
|
|
|
|
|
// Reconnect existing account
|
|
|
|
|
$existingAccount = $workspace->socialAccounts()->find($reconnectId);
|
|
|
|
|
|
|
|
|
|
if ($existingAccount) {
|
|
|
|
|
$existingAccount->update([
|
|
|
|
|
'platform_user_id' => $request->organization_id,
|
|
|
|
|
'username' => $request->organization_vanity_name,
|
|
|
|
|
'display_name' => $request->organization_name,
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $pendingData['token'],
|
|
|
|
|
'refresh_token' => $pendingData['refresh_token'],
|
|
|
|
|
'token_expires_at' => $pendingData['expires_in'] ? now()->addSeconds($pendingData['expires_in']) : null,
|
|
|
|
|
'meta' => [
|
|
|
|
|
'organization_id' => $request->organization_id,
|
|
|
|
|
'admin_user_id' => $pendingData['user_id'],
|
|
|
|
|
'admin_name' => $pendingData['name'],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
$existingAccount->markAsConnected();
|
|
|
|
|
|
|
|
|
|
session()->forget(['linkedin_page_pending', 'linkedin_page_reconnect_id']);
|
|
|
|
|
|
|
|
|
|
return $this->popupCallback(true, 'LinkedIn Page reconnected!', $this->platform->value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-15 01:13:44 +00:00
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
// Create new account
|
2026-01-15 01:13:44 +00:00
|
|
|
$workspace->socialAccounts()->create([
|
|
|
|
|
'platform' => $this->platform->value,
|
|
|
|
|
'platform_user_id' => $request->organization_id,
|
|
|
|
|
'username' => $request->organization_vanity_name,
|
|
|
|
|
'display_name' => $request->organization_name,
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $pendingData['token'],
|
|
|
|
|
'refresh_token' => $pendingData['refresh_token'],
|
|
|
|
|
'token_expires_at' => $pendingData['expires_in'] ? now()->addSeconds($pendingData['expires_in']) : null,
|
2026-01-17 02:46:30 +00:00
|
|
|
'status' => Status::Connected,
|
2026-01-15 01:13:44 +00:00
|
|
|
'meta' => [
|
|
|
|
|
'organization_id' => $request->organization_id,
|
|
|
|
|
'admin_user_id' => $pendingData['user_id'],
|
|
|
|
|
'admin_name' => $pendingData['name'],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
session()->forget(['linkedin_page_pending', 'linkedin_page_reconnect_id']);
|
2026-01-15 01:13:44 +00:00
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(true, 'LinkedIn Page connected!', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('LinkedIn Page selection error', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Error connecting page. Please try again.', $this->platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function fetchOrganizations(string $accessToken): array
|
|
|
|
|
{
|
|
|
|
|
$response = Http::withToken($accessToken)
|
|
|
|
|
->get('https://api.linkedin.com/v2/organizationAcls', [
|
|
|
|
|
'q' => 'roleAssignee',
|
|
|
|
|
'role' => 'ADMINISTRATOR',
|
|
|
|
|
'projection' => '(elements*(organization~(id,localizedName,vanityName,logoV2(original~:playableStreams))))',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($response->failed()) {
|
|
|
|
|
Log::error('LinkedIn Organizations fetch error', [
|
|
|
|
|
'error' => $response->body(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $response->json();
|
|
|
|
|
$organizations = [];
|
|
|
|
|
|
|
|
|
|
foreach ($data['elements'] ?? [] as $element) {
|
|
|
|
|
$org = $element['organization~'] ?? null;
|
|
|
|
|
if ($org) {
|
|
|
|
|
$logoUrl = null;
|
|
|
|
|
if (isset($org['logoV2']['original~']['elements'][0]['identifiers'][0]['identifier'])) {
|
|
|
|
|
$logoUrl = $org['logoV2']['original~']['elements'][0]['identifiers'][0]['identifier'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$organizations[] = [
|
|
|
|
|
'id' => $org['id'],
|
|
|
|
|
'name' => $org['localizedName'] ?? 'Unknown',
|
|
|
|
|
'vanity_name' => $org['vanityName'] ?? null,
|
|
|
|
|
'logo' => $logoUrl,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $organizations;
|
|
|
|
|
}
|
|
|
|
|
}
|