login($user); $request->session()->put(self::MARKER, true); // Prevent session fixation. $request->session()->regenerate(); } /** * Removes the user's live integration sessions only (not the normal * dashboard session). * * @return int number of sessions terminated */ public function terminateForUser(int $userId): int { $ids = []; DB::table('sessions') ->where('user_id', $userId) ->select('id', 'payload') ->orderBy('id') ->each(function ($session) use (&$ids) { if ($this->payloadIsIntegrationSession($session->payload)) { $ids[] = $session->id; } }); if (empty($ids)) { return 0; } return DB::table('sessions')->whereIn('id', $ids)->delete(); } /** * Checks the stored session payload for the integration marker. */ public function payloadIsIntegrationSession(?string $payload): bool { if (empty($payload)) { return false; } $decoded = @unserialize(@base64_decode($payload, true), ['allowed_classes' => false]); if (!is_array($decoded)) { $json = json_decode((string) $payload, true); $decoded = is_array($json) ? $json : null; } return is_array($decoded) && !empty($decoded[self::MARKER]); } }