Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/Loader/WordpressLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Sword\SwordBundle\Loader;

use Sword\SwordBundle\Event\WooCommerceRegistrationEvent;
use Sword\SwordBundle\Exception\WordpressLoginSuccessfulException;
use Sword\SwordBundle\Exception\WordpressLougoutSuccessfulException;
use Sword\SwordBundle\Security\UserAuthenticator;
use Sword\SwordBundle\Store\WordpressWidgetStore;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
Expand Down Expand Up @@ -55,7 +53,8 @@ public function onWooCommerceRegistration(WooCommerceRegistrationEvent $event):
public function createWordpressResponse(string $urlPathName): Response
{
$request = $this->requestStack->getCurrentRequest();

$session = $this->requestStack->getSession();

ob_start();
$obLevel = ob_get_level();

Expand All @@ -65,30 +64,41 @@ public function createWordpressResponse(string $urlPathName): Response
foreach (WordpressGlobals::GLOBALS as $global) {
global $$global;
}

$entryPoint = $this->wordpressDirectory . '/index.php';

if (\in_array(basename($urlPathName), ['wp-login.php', 'wp-signup.php', 'wp-comments-post.php'], true)) {
$_SERVER['PHP_SELF'] = '/' . basename($urlPathName);
$entryPoint = $this->wordpressDirectory . '/' . basename($urlPathName);
} elseif (str_starts_with($urlPathName, 'wp-admin/')) {
if ($urlPathName === 'wp-admin/') {
$urlPathName = 'wp-admin/index.php';
}

$_SERVER['PHP_SELF'] = '/' . $urlPathName;
$entryPoint = $this->wordpressDirectory . '/' . $urlPathName;
} else {
$_SERVER['PHP_SELF'] = '/' . $urlPathName;
}

$_SERVER['SCRIPT_FILENAME'] = $entryPoint;

try {
require_once $entryPoint;
} catch (WordpressLoginSuccessfulException $exception) {
return $this->getAuthResponse($exception->username, $exception->password, $exception->rememberMe);
} catch (WordpressLougoutSuccessfulException) {

require_once $entryPoint;

if ($session->has('loginSuccessData')) {
$loginData = $session->get('loginSuccessData');
$session->remove('loginSuccessData');
return $this->getAuthResponse(
$loginData['username'],
$loginData['password'],
$loginData['rememberMe']
);
}

if ($session->has('logoutSuccessResponse')) {
$response = $session->get('logoutSuccessResponse');
$session->remove('logoutSuccessResponse');

return new RedirectResponse($this->userAuthenticator->getLoginUrl($request));
}

Expand Down Expand Up @@ -120,7 +130,7 @@ public function loadWordpress(): void

require_once $this->wordpressDirectory . '/wp-load.php';
}

private function getAuthResponse(string $username, string $password, bool $rememberMe): RedirectResponse
{
$session = $this->requestStack->getSession();
Expand Down
43 changes: 22 additions & 21 deletions src/Security/UserAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Sword\SwordBundle\Controller\Routes;
use Sword\SwordBundle\Event\WooCommerceRegistrationEvent;
use Sword\SwordBundle\Exception\WordpressLoginSuccessfulException;
use Sword\SwordBundle\Exception\WordpressLougoutSuccessfulException;
use Sword\SwordBundle\Service\WordpressService;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -107,10 +105,11 @@ public function onWordpressLogout(): void
}

$this->tokenStorage->setToken();
$this->requestStack->getSession()
->invalidate();

throw new WordpressLougoutSuccessfulException($response);

$session = $this->requestStack->getSession();

$session->invalidate();
$session->set('logoutSuccessResponse', $response);
}

public function onWooCommerceRegister(int $customerId, array $newCustomerData, bool $passwordGenerated): void
Expand All @@ -129,19 +128,19 @@ public function supports(Request $request): bool
}

return $this->wordpressUsername && $this->wordpressPassword && \in_array(
$request->getPathInfo(),
[$this->getLoginUrl($request), '/wp-login.php'],
true,
);
$request->getPathInfo(),
[$this->getLoginUrl($request), '/wp-login.php'],
true,
);
}

public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
{
$url = '/wp-login.php?' . http_build_query([
'redirect_to' => $request->getUri(),
'reauth' => 0,
]);

'redirect_to' => $request->getUri(),
'reauth' => 0,
]);
return new RedirectResponse($url, 302);
}

Expand Down Expand Up @@ -210,13 +209,15 @@ private function getRedirectPath(Request $request): ?string

return null;
}

private function onWordpressLoginSuccess(): never
private function onWordpressLoginSuccess(): void
{
throw new WordpressLoginSuccessfulException(
$this->wordpressUsername,
$this->wordpressPassword,
$this->wordpressRememberMe,
);
$session = $this->requestStack->getSession();

$session->set('loginSuccessData', [
'username' => $this->wordpressUsername,
'password' => $this->wordpressPassword,
'rememberMe' => $this->wordpressRememberMe,
]);
}
}