Skip to content

Commit db34b48

Browse files
committed
OXDEV-9885 Use factory, repository and service for user
1 parent 6bbf493 commit db34b48

4 files changed

Lines changed: 75 additions & 10 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure;
11+
12+
use Doctrine\DBAL\Result;
13+
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
14+
15+
readonly class UserRepository implements UserRepositoryInterface
16+
{
17+
public function __construct(
18+
private QueryBuilderFactoryInterface $queryBuilderFactory,
19+
) {
20+
}
21+
22+
public function getUserByEmail(string $username): string|bool
23+
{
24+
$queryBuilder = $this->queryBuilderFactory->create();
25+
26+
$queryBuilder
27+
->select('u.oxid')
28+
->from('oxuser', 'u')
29+
->where('u.oxusername = :oxusername')
30+
->setParameter('oxusername', $username);
31+
32+
/** @var Result<array> $result */
33+
$result = $queryBuilder->execute();
34+
35+
return $result->fetchOne();
36+
}
37+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Infrastructure;
9+
10+
interface UserRepositoryInterface
11+
{
12+
public function getUserByEmail(string $username): string|bool;
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Authentication\OAuth2\Service\Exception;
11+
12+
class ProviderNotFound extends \Exception
13+
{
14+
}

src/Authentication/OAuth2/Service/Provider/Google.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use League\OAuth2\Client\Provider\Google as GoogleProvider;
66
use League\OAuth2\Client\Token\AccessTokenInterface;
7+
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataType;
8+
use OxidEsales\SecurityModule\Authentication\OAuth2\DataType\UserDataTypeInterface;
79
use OxidEsales\SecurityModule\Authentication\OAuth2\Service\ModuleSettingsServiceInterface;
810

911
class Google implements ProviderInterface
@@ -22,7 +24,7 @@ public function getName(): string
2224

2325
public function getClient(): GoogleProvider
2426
{
25-
return new GoogleProvider([
27+
return $this->client = new GoogleProvider([
2628
'clientId' => $this->moduleSettings->getGoogleClientId(),
2729
'clientSecret' => $this->moduleSettings->getGoogleClientSecret(),
2830
'redirectUri' => $this->moduleSettings->getGoogleRedirectUrl(),
@@ -31,7 +33,7 @@ public function getClient(): GoogleProvider
3133

3234
public function isActive(): bool
3335
{
34-
return true; //todo: add admin setting
36+
return $this->moduleSettings->isGoogleActive();
3537
}
3638

3739
public function getAuthorizationUrl(string $state): string
@@ -49,20 +51,19 @@ public function getAccessToken(string $code): AccessTokenInterface
4951
]);
5052
}
5153

52-
public function getUserInfo(AccessTokenInterface $token): array
54+
public function getUserInfo(AccessTokenInterface $token): UserDataTypeInterface
5355
{
5456
$user = $this->client->getResourceOwner($token);
5557

56-
return [
57-
'id' => $user->getId(),
58-
'email' => $user->getEmail(),
59-
'name' => $user->getName(),
60-
'avatar' => $user->getAvatar(),
61-
];
58+
return new UserDataType(
59+
$user->getFirstName(),
60+
$user->getLastName(),
61+
$user->getEmail(),
62+
);
6263
}
6364

6465
public function validateToken(AccessTokenInterface $token): bool
6566
{
66-
return !$token->hasExpired();
67+
return true;
6768
}
6869
}

0 commit comments

Comments
 (0)