Skip to content

Commit 9be199e

Browse files
committed
OXDEV-10116 Extract removeExternalAuthFlag to service layer
1 parent d57dd50 commit 9be199e

7 files changed

Lines changed: 100 additions & 7 deletions

File tree

src/Authentication/OAuth2/Infrastructure/Repository/UserRepository.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public function getUserByEmail(string $username): UserDTOInterface
3737
return $this->userDTOFactory->createFromModel($userModel);
3838
}
3939

40+
public function removeExternalAuthFlag(string $userId): void
41+
{
42+
$userModel = $this->userFactory->create();
43+
44+
if (!$userModel->load($userId)) {
45+
throw new UserNotFoundException();
46+
}
47+
48+
$userModel->assign(['OESMEXTERNALAUTH' => 0]);
49+
$userModel->save();
50+
}
51+
4052
public function createUser(OAuth2UserDTOInterface $userDTO): UserDTOInterface
4153
{
4254
$userModel = $this->userFactory->create();

src/Authentication/OAuth2/Infrastructure/Repository/UserRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ interface UserRepositoryInterface
1818
*/
1919
public function getUserByEmail(string $username): UserDTOInterface;
2020

21+
/**
22+
* @throws UserNotFoundException
23+
*/
24+
public function removeExternalAuthFlag(string $userId): void;
25+
2126
public function createUser(OAuth2UserDTOInterface $userDTO): UserDTOInterface;
2227
}

src/Authentication/OAuth2/Service/UserService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,12 @@ public function login(OAuth2UserDTOInterface $auth2UserDTO): void
4242

4343
$this->session->set('usr', $userDTO->getId());
4444
}
45+
46+
public function removeExternalAuthFlag(): void
47+
{
48+
$userId = $this->session->get('usr');
49+
if ($userId) {
50+
$this->userRepository->removeExternalAuthFlag((string)$userId);
51+
}
52+
}
4553
}

src/Authentication/OAuth2/Service/UserServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ interface UserServiceInterface
1818
* @throws UserBlockedException If the user is blocked.
1919
*/
2020
public function login(OAuth2UserDTOInterface $auth2UserDTO): void;
21+
22+
public function removeExternalAuthFlag(): void;
2123
}

src/Shared/Controller/ForgotPasswordController.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace OxidEsales\SecurityModule\Shared\Controller;
1111

12-
use OxidEsales\Eshop\Application\Model\User;
1312
use OxidEsales\Eshop\Core\Exception\StandardException;
1413
use OxidEsales\Eshop\Core\Registry;
14+
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\UserServiceInterface;
1515
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
1616
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsServiceInterface;
1717

@@ -47,12 +47,8 @@ public function updatePassword()
4747
$result = parent::updatePassword();
4848

4949
if ($result === 'forgotpwd?success=1') {
50-
$userId = Registry::getSession()->getVariable('usr');
51-
$user = oxNew(User::class);
52-
if ($userId && $user->load($userId) && $user->getFieldData('oesmexternalauth')) {
53-
$user->assign(['OESMEXTERNALAUTH' => 0]);
54-
$user->save();
55-
}
50+
$this->getService(UserServiceInterface::class)
51+
->removeExternalAuthFlag();
5652
}
5753

5854
return $result;

tests/Unit/Authentication/OAuth2/Infrastructure/Repository/UserRepositoryTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@ public function testGetUserByEmailThrowsExceptionIfUserCannotBeLoaded(): void
9090
$repository->getUserByEmail($username);
9191
}
9292

93+
public function testRemoveExternalAuthFlag(): void
94+
{
95+
$userId = uniqid();
96+
97+
$userModelMock = $this->createMock(UserModel::class);
98+
$userModelMock->method('load')->with($userId)->willReturn(true);
99+
$userModelMock->expects($this->once())->method('assign')->with(['OESMEXTERNALAUTH' => 0]);
100+
$userModelMock->expects($this->once())->method('save');
101+
102+
$userFactoryStub = $this->createStub(UserFactoryInterface::class);
103+
$userFactoryStub->method('create')->willReturn($userModelMock);
104+
105+
$repository = $this->getSut(userFactory: $userFactoryStub);
106+
107+
$repository->removeExternalAuthFlag($userId);
108+
}
109+
110+
public function testRemoveExternalAuthFlagThrowsExceptionIfUserNotFound(): void
111+
{
112+
$userId = uniqid();
113+
114+
$userModelStub = $this->createStub(UserModel::class);
115+
$userModelStub->method('load')->willReturn(false);
116+
117+
$userFactoryStub = $this->createStub(UserFactoryInterface::class);
118+
$userFactoryStub->method('create')->willReturn($userModelStub);
119+
120+
$repository = $this->getSut(userFactory: $userFactoryStub);
121+
122+
$this->expectException(UserNotFoundException::class);
123+
124+
$repository->removeExternalAuthFlag($userId);
125+
}
126+
93127
public function testCreateUser(): void
94128
{
95129
$firstName = uniqid();

tests/Unit/Authentication/OAuth2/Service/UserServiceTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,42 @@ public function testCannotLoginWithoutEmail(): void
115115
$sut->login($oAuth2UserStub);
116116
}
117117

118+
public function testRemoveExternalAuthFlag(): void
119+
{
120+
$userId = uniqid();
121+
122+
$sessionStub = $this->createStub(SessionInterface::class);
123+
$sessionStub->method('get')->with('usr')->willReturn($userId);
124+
125+
$userRepositoryMock = $this->createMock(UserRepositoryInterface::class);
126+
$userRepositoryMock->expects($this->once())
127+
->method('removeExternalAuthFlag')
128+
->with($userId);
129+
130+
$sut = $this->getSut(
131+
userRepository: $userRepositoryMock,
132+
session: $sessionStub
133+
);
134+
135+
$sut->removeExternalAuthFlag();
136+
}
137+
138+
public function testRemoveExternalAuthFlagSkipsWhenNoUserInSession(): void
139+
{
140+
$sessionStub = $this->createStub(SessionInterface::class);
141+
$sessionStub->method('get')->with('usr')->willReturn(null);
142+
143+
$userRepositoryMock = $this->createMock(UserRepositoryInterface::class);
144+
$userRepositoryMock->expects($this->never())->method('removeExternalAuthFlag');
145+
146+
$sut = $this->getSut(
147+
userRepository: $userRepositoryMock,
148+
session: $sessionStub
149+
);
150+
151+
$sut->removeExternalAuthFlag();
152+
}
153+
118154
private function getSut(
119155
UserRepositoryInterface $userRepository = null,
120156
SessionInterface $session = null,

0 commit comments

Comments
 (0)