From e850ab03c12ccd4d6beddb9d2d98ea9e95faf372 Mon Sep 17 00:00:00 2001 From: huntmori Date: Sun, 14 Jan 2024 20:19:49 +0900 Subject: [PATCH 1/4] token and profile sql MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 토큰 claim에 primary profile uid 추가 - 토큰 로직 수정 - 프로필 select sql 추가 - primary 프로필 없을 시 첫번째 프로필을 primary로 취급 --- .../Common/service/TokenService.php | 8 ++++ .../Common/service/TokenServiceImplement.php | 32 ++++++++++++++++ src/Application/Middleware/JwtHandler.php | 7 +++- .../Common/repository/BaseRepository.php | 19 ++++++++++ .../Profile/Repository/ProfileRepository.php | 2 + .../Repository/ProfileRepositoryImplement.php | 27 ++++++++++++++ .../Profile/controller/ProfileController.php | 7 ++++ src/Domain/Profile/service/ProfileService.php | 2 +- .../service/ProfileServiceImplement.php | 4 +- src/Domain/User/controller/UserController.php | 5 ++- src/Domain/User/service/UserService.php | 3 +- .../User/service/UserServiceImplement.php | 37 ++++++++++++++++--- 12 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/Application/Common/service/TokenService.php create mode 100644 src/Application/Common/service/TokenServiceImplement.php diff --git a/src/Application/Common/service/TokenService.php b/src/Application/Common/service/TokenService.php new file mode 100644 index 0000000..543cf1b --- /dev/null +++ b/src/Application/Common/service/TokenService.php @@ -0,0 +1,8 @@ +passwordEncrypt = $passwordEncrypt; + $this->logger = $logger; + $this->jwtHandler = $jwtHandler; + } + + public function getUserIdFromToken(string $token) { + $decodedToken = $this->passwordEncrypt->decrypt($token); + $decryptedJwt = $this->jwtHandler->decryptToken($decodedToken); + $claims = $this->jwtHandler->decodeJwt($decryptedJwt); + + return $this->jwtHandler->getUserIdFromClaims($claims); + } +} \ No newline at end of file diff --git a/src/Application/Middleware/JwtHandler.php b/src/Application/Middleware/JwtHandler.php index 768544b..d65a077 100644 --- a/src/Application/Middleware/JwtHandler.php +++ b/src/Application/Middleware/JwtHandler.php @@ -18,13 +18,16 @@ public function __construct(SettingsInterface $settings) $this->encryptKey = $settings->get('config')['MEMBER_PASSWORD_ENCRYPT_KEY']; } - public function createToken($userId) :string + public function createToken(array $params) :string { + $userId = $params['userId']; + $profileUid = $params['profileUid']; $claims = [ 'userId' => $userId, + 'profileUid' => $profileUid, 'exp'=>strtotime($this->TOKEN_EXPIRE) ]; - + var_dump($claims); $token = $this->encodeJwt($claims); //echo PHP_EOL.'token : '.$token.PHP_EOL; return $this->encryptToken($token); diff --git a/src/Domain/Common/repository/BaseRepository.php b/src/Domain/Common/repository/BaseRepository.php index 8d0a523..dca0ea5 100644 --- a/src/Domain/Common/repository/BaseRepository.php +++ b/src/Domain/Common/repository/BaseRepository.php @@ -45,4 +45,23 @@ public function selectOne(string $sql, array $paramMap, string $returnType) { $this->disposePdo($pdo); return $result; } + + public function selectList(string $sql, array $paramMap, string $returnType) : array { + $pdo = $this->getPdo(); + $stmt = $pdo->prepare($sql); + + $keys = array_keys($paramMap); + for ($i=0; $ibindValue($key, $paramMap[$key]); + } + + $stmt->execute(); + $array = []; + while($row = $stmt->fetchObject($returnType)) { + $array[] = $row; + } + $this->disposePdo($pdo); + return $array; + } } \ No newline at end of file diff --git a/src/Domain/Profile/Repository/ProfileRepository.php b/src/Domain/Profile/Repository/ProfileRepository.php index 39df67d..a538534 100644 --- a/src/Domain/Profile/Repository/ProfileRepository.php +++ b/src/Domain/Profile/Repository/ProfileRepository.php @@ -13,4 +13,6 @@ public function getUserProfileByProfileIdx(int $profileIdx); public function getUserProfileByUserUid(string $uid); public function getUserProfileByProfileUid(string $uid); + + public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated) : array; } \ No newline at end of file diff --git a/src/Domain/Profile/Repository/ProfileRepositoryImplement.php b/src/Domain/Profile/Repository/ProfileRepositoryImplement.php index df67e31..9215867 100644 --- a/src/Domain/Profile/Repository/ProfileRepositoryImplement.php +++ b/src/Domain/Profile/Repository/ProfileRepositoryImplement.php @@ -85,4 +85,31 @@ public function getUserProfileByProfileUid(string $uid) { return null; } + + public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated): array + { + return $this->selectList( + "SELECT + pro.idx, + pro.uid, + pro.user_uid as userUid, + pro.profile_nickname as profileNickName, + pro.is_primary as isPrimary, + pro.deleted, + pro.activated, + pro.banned, + pro.created_at as createdAt, + pro.updated_at as updatedAt + FROM + profile pro + WHERE 1=1 + AND pro.user_uid = :userUid + AND pro.activated = :activated ", + [ + 'userUid' => $userUid, + 'activated' => $activated + ], + Profile::class + ); + } } \ No newline at end of file diff --git a/src/Domain/Profile/controller/ProfileController.php b/src/Domain/Profile/controller/ProfileController.php index 10b34b6..c3fcd12 100644 --- a/src/Domain/Profile/controller/ProfileController.php +++ b/src/Domain/Profile/controller/ProfileController.php @@ -33,4 +33,11 @@ public function createUserProfile(Request $request, Response $response, array $a $profile = $this->profileService->createUserProfileByRequestDto($requestDto); return $this->respondWithData($response, $profile->toArray(), 200); } + + public function getProfiles(Request $request, Response $response, array $args) : Response + { + $token = $this->profileService->getUserProfilesByRequest($request); + + } + } \ No newline at end of file diff --git a/src/Domain/Profile/service/ProfileService.php b/src/Domain/Profile/service/ProfileService.php index ee78377..ab197e3 100644 --- a/src/Domain/Profile/service/ProfileService.php +++ b/src/Domain/Profile/service/ProfileService.php @@ -7,7 +7,7 @@ interface ProfileService { - public function getUserProfiles(int $userIdx) : array; + public function getUserProfiles(string $uid) : array; public function createUserProfile(int $userIdx, string $userUid, string $nickName); public function checkNickNameDuplicate(string $nickName): bool; diff --git a/src/Domain/Profile/service/ProfileServiceImplement.php b/src/Domain/Profile/service/ProfileServiceImplement.php index caf3e40..9d225c8 100644 --- a/src/Domain/Profile/service/ProfileServiceImplement.php +++ b/src/Domain/Profile/service/ProfileServiceImplement.php @@ -30,9 +30,9 @@ public function __construct( $this->encrypt = $encrypt; } - public function getUserProfiles(int $userIdx): array + public function getUserProfiles(string $uid): array { - return []; + return $this->profileRepository->getUserProfileByUserIdxAndActivate($uid, true); } public function createUserProfile(int $userIdx, string $userUid, string $nickName) : int diff --git a/src/Domain/User/controller/UserController.php b/src/Domain/User/controller/UserController.php index 3883d9b..b8e6b9c 100644 --- a/src/Domain/User/controller/UserController.php +++ b/src/Domain/User/controller/UserController.php @@ -3,6 +3,7 @@ namespace App\Domain\User\controller; use App\Domain\Common\controller\ActionBasedController; +use App\Domain\Profile\service\ProfileService; use App\Domain\User\models\UserCreateRequest; use App\Domain\User\models\UserLoginRequest; use App\Domain\User\service\UserService; @@ -14,6 +15,7 @@ class UserController extends ActionBasedController { private LoggerInterface $logger; private UserService $userService; + private ProfileService $profileService; public function __construct( LoggerInterface $logger, @@ -59,7 +61,8 @@ public function userLogin(Request $request, Response $response, array $args) : R $requestBody = new UserLoginRequest(json_decode($request->getBody())); $userLoginResult = $this->userService->userLogin($requestBody); - echo json_encode($userLoginResult).PHP_EOL; + + return $this->respondWithData($response, $userLoginResult, 200); } } \ No newline at end of file diff --git a/src/Domain/User/service/UserService.php b/src/Domain/User/service/UserService.php index d271cf4..90737c2 100644 --- a/src/Domain/User/service/UserService.php +++ b/src/Domain/User/service/UserService.php @@ -2,6 +2,7 @@ namespace App\Domain\User\service; +use App\Domain\Profile\entities\Profile; use App\Domain\User\entities\User; use App\Domain\User\models\UserCreateRequest; use App\Domain\User\models\UserLoginRequest; @@ -19,7 +20,7 @@ public function userCreateParams( ) : User; public function userLogin(UserLoginRequest $requestBody) : UserLoginResponse; - public function getUserJwtToken(User $user) : string; + public function getUserJwtToken(User $user, Profile $primaryProfile) : string; public function getUserByUserId(string $userId) : ?User; public function getUserByUserIdx(int $userIdx) : ?User; diff --git a/src/Domain/User/service/UserServiceImplement.php b/src/Domain/User/service/UserServiceImplement.php index ab4cbf6..46fefe9 100644 --- a/src/Domain/User/service/UserServiceImplement.php +++ b/src/Domain/User/service/UserServiceImplement.php @@ -4,6 +4,7 @@ use App\Application\Common\MemberPasswordEncrypt; use App\Application\Middleware\JwtHandler; +use App\Domain\Profile\entities\Profile; use App\Domain\Profile\service\ProfileService; use App\Domain\User\entities\User; use App\Domain\User\exceptions\PasswordNotMatchException; @@ -106,10 +107,33 @@ public function userLogin(UserLoginRequest $requestBody): UserLoginResponse throw new PasswordNotMatchException("User not found. may be invalid password or user id", 401); } - $token = $this->getUserJwtToken($user); - //todo : get users profiles - $profiles = $this->profileService->getUserProfiles($user->getIdx()); + $profiles = $this->profileService->getUserProfiles($user->getUid()); + $mainProfileExist = false; + $primaryProfile = null; + for($i=0; $igetIsPrimary()) { + $mainProfileExist = true; + $primaryProfile = $profile; + break; + } + } + + if($mainProfileExist === false) { + for($i=0; $isetIsPrimary(true); + $primaryProfile = $profile; + break; + } + } + + $token = $this->getUserJwtToken($user, $primaryProfile); + + return new UserLoginResponse([ 'profiles'=>$profiles, @@ -118,15 +142,18 @@ public function userLogin(UserLoginRequest $requestBody): UserLoginResponse ]); } - public function getUserJwtToken(User $user) : string + public function getUserJwtToken(User $user, ?Profile $primaryProfile) : string { $userIdx = $user->getIdx(); $redisTokenKey = "JWT_{$userIdx}"; $isSetRedis = $this->redisClient->get($redisTokenKey); $token = null; + $tokenParameter = []; + $tokenParameter['userId'] = $user->getId(); + $tokenParameter['profileUid'] = $primaryProfile?->getUid(); if (is_null($isSetRedis) || true) { - $token = $this->jwtHandler->createToken($user->getId()); + $token = $this->jwtHandler->createToken($tokenParameter); $token = $this->memberPasswordEncrypt->encrypt($token); $this->redisClient->setex($redisTokenKey, 86400, $token); } else { From 5f78b93f344ecea049a41320c05f7f216f2c251d Mon Sep 17 00:00:00 2001 From: huntmori Date: Tue, 16 Jan 2024 13:03:49 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Jwt=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - token Service 추가 - JwtClaim 클래스 추가 --- src/Application/Common/model/JwtClaim.php | 44 +++++++++++++++++++ .../Common/service/TokenService.php | 4 +- .../Common/service/TokenServiceImplement.php | 22 +++++++++- src/Application/Middleware/JwtHandler.php | 19 ++++++-- src/Application/Middleware/JwtMiddleware.php | 5 +-- .../service/ProfileServiceImplement.php | 26 +++++++---- .../User/service/UserServiceImplement.php | 2 - 7 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 src/Application/Common/model/JwtClaim.php diff --git a/src/Application/Common/model/JwtClaim.php b/src/Application/Common/model/JwtClaim.php new file mode 100644 index 0000000..7f336d4 --- /dev/null +++ b/src/Application/Common/model/JwtClaim.php @@ -0,0 +1,44 @@ +userId = $userId; + $this->profileUid = $profileUid; + $this->exp = $exp; + } + + public function initFromArray(array $claim) : JwtClaim + { + $this->init( + $claim['userId'], + $claim['profileUid'], + $claim['exp'] + ); + + return $this; + } + + public function toArray() : array + { + return [ + 'userId'=>$this->userId, + 'profileUid'=>$this->profileUid, + 'exp'=> $this->exp + ]; + } + + public function jsonSerialize(): array + { + return $this->toArray(); + } +} \ No newline at end of file diff --git a/src/Application/Common/service/TokenService.php b/src/Application/Common/service/TokenService.php index 543cf1b..2825972 100644 --- a/src/Application/Common/service/TokenService.php +++ b/src/Application/Common/service/TokenService.php @@ -4,5 +4,7 @@ interface TokenService { - + public function getUserIdFromToken(string $token); + public function getProfileUidFromToken(string $token); + public function getClaimFromToken(string $token); } \ No newline at end of file diff --git a/src/Application/Common/service/TokenServiceImplement.php b/src/Application/Common/service/TokenServiceImplement.php index a814d81..fbadd43 100644 --- a/src/Application/Common/service/TokenServiceImplement.php +++ b/src/Application/Common/service/TokenServiceImplement.php @@ -3,6 +3,7 @@ namespace App\Application\Common\service; use App\Application\Common\MemberPasswordEncrypt; +use App\Application\Common\model\JwtClaim; use App\Application\Middleware\JwtHandler; use Psr\Log\LoggerInterface; @@ -22,11 +23,28 @@ public function __construct( $this->jwtHandler = $jwtHandler; } - public function getUserIdFromToken(string $token) { + public function getUserIdFromToken(string $token) : string + { $decodedToken = $this->passwordEncrypt->decrypt($token); $decryptedJwt = $this->jwtHandler->decryptToken($decodedToken); $claims = $this->jwtHandler->decodeJwt($decryptedJwt); - return $this->jwtHandler->getUserIdFromClaims($claims); + return $claims->userId; + } + + public function getProfileUidFromToken(string $token) : string + { + $decodedToken = $this->passwordEncrypt->decrypt($token); + $decryptedJwt = $this->jwtHandler->decryptToken($decodedToken); + $claims = $this->jwtHandler->decodeJwt($decryptedJwt); + + return $claims->profileUid; + } + + public function getClaimFromToken(string $token) : ?JwtClaim + { + $decodedToken = $this->passwordEncrypt->decrypt($token); + $decryptedJwt = $this->jwtHandler->decryptToken($decodedToken); + return $this->jwtHandler->decodeJwt($decryptedJwt); } } \ No newline at end of file diff --git a/src/Application/Middleware/JwtHandler.php b/src/Application/Middleware/JwtHandler.php index d65a077..c90a26c 100644 --- a/src/Application/Middleware/JwtHandler.php +++ b/src/Application/Middleware/JwtHandler.php @@ -2,6 +2,7 @@ namespace App\Application\Middleware; +use App\Application\Common\model\JwtClaim; use App\Application\Settings\SettingsInterface; use Firebase\JWT\JWT; use Firebase\JWT\Key; @@ -27,6 +28,12 @@ public function createToken(array $params) :string 'profileUid' => $profileUid, 'exp'=>strtotime($this->TOKEN_EXPIRE) ]; + $claims = new JwtClaim(); + $claims->init( + $userId, + $profileUid, + strtotime($this->TOKEN_EXPIRE) + ); var_dump($claims); $token = $this->encodeJwt($claims); //echo PHP_EOL.'token : '.$token.PHP_EOL; @@ -37,18 +44,22 @@ public function getUserIdFromClaims(array $claims) { return $claims['userId']; } - public function encodeJwt($claims) : string + public function encodeJwt(JwtClaim $claims) : string { - return JWT::encode($claims, $this->encryptKey, $this->jwtEncodeAlgorithm); + return JWT::encode( + $claims->toArray(), + $this->encryptKey, + $this->jwtEncodeAlgorithm + ); } - public function decodeJwt($token) : array + public function decodeJwt(string $token) : JwtClaim { $key = new Key($this->encryptKey, $this->jwtEncodeAlgorithm); $headers = new \stdClass(); $decoded = JWT::decode($token, $key, $headers); - return (array)$decoded; + return (new JwtClaim())->initFromArray((array)$decoded); } public function encryptToken(string $token) : string diff --git a/src/Application/Middleware/JwtMiddleware.php b/src/Application/Middleware/JwtMiddleware.php index 9d5dbf4..902a4f9 100644 --- a/src/Application/Middleware/JwtMiddleware.php +++ b/src/Application/Middleware/JwtMiddleware.php @@ -60,12 +60,11 @@ public function validateTokenReturnUserIdx(string $token) : bool $claims = $this->jwtHandler->decodeJwt($tokenDecoded); var_dump($claims); // 유효기간 확인 - $expiredAt = $claims['exp']; + $expiredAt = $claims->exp; $now = strtotime("now"); if ($expiredAt < $now) { return false; } - $userId = $claims['userId']; // 세션 확인 //session_start(); //$tokens = $_SESSION[$userId]; @@ -74,7 +73,7 @@ public function validateTokenReturnUserIdx(string $token) : bool //} // 유저 확인 - return $userId; + return $claims->userId; } public function validateToken(string $token) : bool diff --git a/src/Domain/Profile/service/ProfileServiceImplement.php b/src/Domain/Profile/service/ProfileServiceImplement.php index 9d225c8..3ebc11d 100644 --- a/src/Domain/Profile/service/ProfileServiceImplement.php +++ b/src/Domain/Profile/service/ProfileServiceImplement.php @@ -10,6 +10,7 @@ use App\Domain\User\repository\UserRepository; use App\Domain\User\service\UserService; use Cassandra\Uuid; +use HttpException; class ProfileServiceImplement implements ProfileService { @@ -64,20 +65,24 @@ public function checkNickNameDuplicate(string $nickName): bool */ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile { - // TODO: Implement createUserProfileByToken() method. - var_dump($requestBody); - echo "Before Decoded Token : " . $requestBody->getToken() . PHP_EOL; + /* + var_dump($requestBody); + echo "Before Decoded Token : " . $requestBody->getToken() . PHP_EOL; + */ $decodeToken = $this->encrypt->decrypt($requestBody->getToken()); - echo "Decoded Token : " . $decodeToken . PHP_EOL; + + /* + echo "Decoded Token : " . $decodeToken . PHP_EOL; + */ $decodedJwt = $this->jwtHandler->decryptToken($decodeToken); $claims = $this->jwtHandler->decodeJwt($decodedJwt); - $userId = $this->jwtHandler->getUserIdFromClaims($claims); + $userId = $claims->userId; $user = $this->userRepository->findUserOfUserId($userId); if (is_null($user)) { - throw new \HttpException("user not found", 403); + throw new HttpException("user not found", 403); } $userUid = $user->getUid(); @@ -88,9 +93,12 @@ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody) throw new \HttpException("nickname already used", 503); } - $profileIdx = $this->createUserProfile($userIdx, $userUid, $nickName); - $profile = $this->profileRepository->getUserProfileByProfileIdx($profileIdx); + $profileIdx = $this->createUserProfile( + $userIdx, + $userUid, + $nickName + ); - return $profile; + return $this->profileRepository->getUserProfileByProfileIdx($profileIdx); } } \ No newline at end of file diff --git a/src/Domain/User/service/UserServiceImplement.php b/src/Domain/User/service/UserServiceImplement.php index 46fefe9..dc502e9 100644 --- a/src/Domain/User/service/UserServiceImplement.php +++ b/src/Domain/User/service/UserServiceImplement.php @@ -133,8 +133,6 @@ public function userLogin(UserLoginRequest $requestBody): UserLoginResponse $token = $this->getUserJwtToken($user, $primaryProfile); - - return new UserLoginResponse([ 'profiles'=>$profiles, 'userIdx' => $user->getIdx(), From ac096fa73a39001f88c9ea6723b995184b7f274f Mon Sep 17 00:00:00 2001 From: huntmori Date: Wed, 17 Jan 2024 12:52:55 +0900 Subject: [PATCH 3/4] get one profile get one profile implement --- app/repositories.php | 18 +++----- app/routes.php | 3 ++ src/Application/Common/model/JwtClaim.php | 4 +- .../Common/service/TokenService.php | 4 +- src/Application/Middleware/JwtHandler.php | 13 ++---- .../Profile/Repository/ProfileRepository.php | 1 + .../Repository/ProfileRepositoryImplement.php | 43 ++++++++++++++++++- .../Profile/controller/ProfileController.php | 23 +++++++--- .../Profile/models/ProfileGetByIdRequest.php | 42 ++++++++++++++++++ .../Profile/models/ProfileGetListRequest.php | 18 ++++++++ src/Domain/Profile/service/ProfileService.php | 3 ++ .../service/ProfileServiceImplement.php | 27 ++++++++++-- 12 files changed, 164 insertions(+), 35 deletions(-) create mode 100644 src/Domain/Profile/models/ProfileGetByIdRequest.php create mode 100644 src/Domain/Profile/models/ProfileGetListRequest.php diff --git a/app/repositories.php b/app/repositories.php index 4275a26..3b128e5 100644 --- a/app/repositories.php +++ b/app/repositories.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use App\Application\Common\service\TokenService; +use App\Application\Common\service\TokenServiceImplement; use App\Application\Middleware\JwtHandler; use App\Application\Middleware\JwtMiddleware; use App\Domain\Profile\Repository\ProfileRepository; @@ -20,21 +22,15 @@ // $containerBuilder->addDefinitions([ // UserRepository::class => autowire(InMemoryUserRepository::class), // ]); - $containerBuilder->addDefinitions([ UserService::class => autowire(UserServiceImplement::class), - ]); - $containerBuilder->addDefinitions([ UserRepository::class => autowire(UserRepositoryImplement::class), - ]); - $containerBuilder->addDefinitions([ + ProfileService::class => autowire(ProfileServiceImplement::class), - ]); - $containerBuilder->addDefinitions([ - ProfileRepository::class => autowire(ProfileRepositoryImplement::class) - ]); - $containerBuilder->addDefinitions([ + ProfileRepository::class => autowire(ProfileRepositoryImplement::class), + JwtMiddleware::class => autowire(JwtMiddleware::class), - JwtHandler::class => autowire(JwtHandler::class) + JwtHandler::class => autowire(JwtHandler::class), + TokenService::class => autowire(TokenServiceImplement::class) ]); }; diff --git a/app/routes.php b/app/routes.php index 9d6e40b..6aff005 100644 --- a/app/routes.php +++ b/app/routes.php @@ -46,6 +46,9 @@ "", ProfileController::class . ':createUserProfile' )->add(JwtMiddleware::class); + + $group->get("/{uid}", ProfileController::class.":getProfiles") + ->add(JwtMiddleware::class); }); /* $app->get("/test", function(Request $request, Response $response) use ($app) { diff --git a/src/Application/Common/model/JwtClaim.php b/src/Application/Common/model/JwtClaim.php index 7f336d4..5dd61bc 100644 --- a/src/Application/Common/model/JwtClaim.php +++ b/src/Application/Common/model/JwtClaim.php @@ -10,11 +10,13 @@ class JwtClaim implements JsonSerializable public ?string $profileUid; public ?int $exp; - public function init(string $userId, string $profileUid, int $exp) : void + public function init(string $userId, string $profileUid, int $exp) : JwtClaim { $this->userId = $userId; $this->profileUid = $profileUid; $this->exp = $exp; + + return $this; } public function initFromArray(array $claim) : JwtClaim diff --git a/src/Application/Common/service/TokenService.php b/src/Application/Common/service/TokenService.php index 2825972..1a214e4 100644 --- a/src/Application/Common/service/TokenService.php +++ b/src/Application/Common/service/TokenService.php @@ -2,9 +2,11 @@ namespace App\Application\Common\service; +use App\Application\Common\model\JwtClaim; + interface TokenService { public function getUserIdFromToken(string $token); public function getProfileUidFromToken(string $token); - public function getClaimFromToken(string $token); + public function getClaimFromToken(string $token) : ?JwtClaim; } \ No newline at end of file diff --git a/src/Application/Middleware/JwtHandler.php b/src/Application/Middleware/JwtHandler.php index c90a26c..b3a3605 100644 --- a/src/Application/Middleware/JwtHandler.php +++ b/src/Application/Middleware/JwtHandler.php @@ -23,20 +23,15 @@ public function createToken(array $params) :string { $userId = $params['userId']; $profileUid = $params['profileUid']; - $claims = [ - 'userId' => $userId, - 'profileUid' => $profileUid, - 'exp'=>strtotime($this->TOKEN_EXPIRE) - ]; - $claims = new JwtClaim(); - $claims->init( + + $claims = (new JwtClaim())->init( $userId, $profileUid, strtotime($this->TOKEN_EXPIRE) ); - var_dump($claims); + $token = $this->encodeJwt($claims); - //echo PHP_EOL.'token : '.$token.PHP_EOL; + return $this->encryptToken($token); } diff --git a/src/Domain/Profile/Repository/ProfileRepository.php b/src/Domain/Profile/Repository/ProfileRepository.php index a538534..a8ae362 100644 --- a/src/Domain/Profile/Repository/ProfileRepository.php +++ b/src/Domain/Profile/Repository/ProfileRepository.php @@ -15,4 +15,5 @@ public function getUserProfileByUserUid(string $uid); public function getUserProfileByProfileUid(string $uid); public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated) : array; + public function checkNicknameCount(string $nickname) : int; } \ No newline at end of file diff --git a/src/Domain/Profile/Repository/ProfileRepositoryImplement.php b/src/Domain/Profile/Repository/ProfileRepositoryImplement.php index 9215867..ade9ef3 100644 --- a/src/Domain/Profile/Repository/ProfileRepositoryImplement.php +++ b/src/Domain/Profile/Repository/ProfileRepositoryImplement.php @@ -7,6 +7,7 @@ use App\Domain\Profile\entities\Profile; use App\Domain\Profile\Repository\ProfileRepository; use Psr\Container\ContainerInterface; +use stdClass; class ProfileRepositoryImplement extends BaseRepository implements ProfileRepository { @@ -81,9 +82,29 @@ public function getUserProfileByUserUid(string $uid) return null; } - public function getUserProfileByProfileUid(string $uid) + public function getUserProfileByProfileUid(string $uid) : ?Profile { - return null; + return $this->selectOne( + "SELECT + pro.idx, + pro.uid, + pro.user_uid as userUid, + pro.profile_nickname as profileNickName, + pro.is_primary as isPrimary, + pro.deleted, + pro.activated, + pro.banned, + pro.created_at as createdAt, + pro.updated_at as updatedAt + FROM + profile pro + WHERE 1=1 + AND pro.uid = :profileUid", + [ + 'profileUid'=>$uid + ], + Profile::class + ); } public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated): array @@ -112,4 +133,22 @@ public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activa Profile::class ); } + + public function checkNicknameCount(string $nickname) : int + { + $result = $this->selectList( + " + SELECT 1 + FROM profile pro + WHERE profile_nickname = :nickname + AND deleted = false + ", + [ + 'nickname' => $nickname, + ], + stdClass::class + ); + + return count($result); + } } \ No newline at end of file diff --git a/src/Domain/Profile/controller/ProfileController.php b/src/Domain/Profile/controller/ProfileController.php index c3fcd12..477180d 100644 --- a/src/Domain/Profile/controller/ProfileController.php +++ b/src/Domain/Profile/controller/ProfileController.php @@ -3,7 +3,9 @@ namespace App\Domain\Profile\controller; use App\Domain\Common\controller\ActionBasedController; +use App\Domain\Profile\entities\Profile; use App\Domain\Profile\models\ProfileCreateRequest; +use App\Domain\Profile\models\ProfileGetByIdRequest; use App\Domain\Profile\service\ProfileService; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; @@ -22,22 +24,29 @@ public function __construct( } // TODO : Implement - public function getUserProfile($userIdx) : array + public function getUserProfile($userIdx): array { return []; } - public function createUserProfile(Request $request, Response $response, array $args) : Response + public function createUserProfile(Request $request, Response $response, array $args): Response { $requestDto = new ProfileCreateRequest($request); $profile = $this->profileService->createUserProfileByRequestDto($requestDto); return $this->respondWithData($response, $profile->toArray(), 200); } - public function getProfiles(Request $request, Response $response, array $args) : Response + public function getProfiles(Request $request, Response $response, array $args): Response { - $token = $this->profileService->getUserProfilesByRequest($request); - + $requestDto = new ProfileGetByIdRequest($request, $args); + + /** @var Profile $profile */ + $profile = $this->profileService->getUserProfilesByRequest($requestDto); + var_dump($profile); + return $this->respondWithData( + $response, + $profile->toArray(), + 200 + ); } - -} \ No newline at end of file +} diff --git a/src/Domain/Profile/models/ProfileGetByIdRequest.php b/src/Domain/Profile/models/ProfileGetByIdRequest.php new file mode 100644 index 0000000..a680b0c --- /dev/null +++ b/src/Domain/Profile/models/ProfileGetByIdRequest.php @@ -0,0 +1,42 @@ +token = $this->extractToken($request); + $this->profileUid = $arg['uid']; + } + + public function getProfileUid(): ?string + { + return $this->profileUid; + } + + public function setProfileUid(?string $profileUid): void + { + $this->profileUid = $profileUid; + } + + public function getToken(): ?string + { + return $this->token; + } + + public function setToken(?string $token): void + { + $this->token = $token; + } + + +} \ No newline at end of file diff --git a/src/Domain/Profile/models/ProfileGetListRequest.php b/src/Domain/Profile/models/ProfileGetListRequest.php new file mode 100644 index 0000000..3cab162 --- /dev/null +++ b/src/Domain/Profile/models/ProfileGetListRequest.php @@ -0,0 +1,18 @@ +token = $this->extractToken($request); + } +} \ No newline at end of file diff --git a/src/Domain/Profile/service/ProfileService.php b/src/Domain/Profile/service/ProfileService.php index ab197e3..37e2354 100644 --- a/src/Domain/Profile/service/ProfileService.php +++ b/src/Domain/Profile/service/ProfileService.php @@ -4,6 +4,7 @@ use App\Domain\Profile\entities\Profile; use App\Domain\Profile\models\ProfileCreateRequest; +use App\Domain\Profile\models\ProfileGetByIdRequest; interface ProfileService { @@ -13,4 +14,6 @@ public function createUserProfile(int $userIdx, string $userUid, string $nickNam public function checkNickNameDuplicate(string $nickName): bool; public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile; + + public function getUserProfilesByRequest(ProfileGetByIdRequest $request) : ?Profile; } \ No newline at end of file diff --git a/src/Domain/Profile/service/ProfileServiceImplement.php b/src/Domain/Profile/service/ProfileServiceImplement.php index 3ebc11d..70f979e 100644 --- a/src/Domain/Profile/service/ProfileServiceImplement.php +++ b/src/Domain/Profile/service/ProfileServiceImplement.php @@ -3,14 +3,18 @@ namespace App\Domain\Profile\service; use App\Application\Common\MemberPasswordEncrypt; +use App\Application\Common\model\JwtClaim; +use App\Application\Common\service\TokenService; use App\Application\Middleware\JwtHandler; use App\Domain\Profile\entities\Profile; use App\Domain\Profile\models\ProfileCreateRequest; +use App\Domain\Profile\models\ProfileGetByIdRequest; use App\Domain\Profile\Repository\ProfileRepository; use App\Domain\User\repository\UserRepository; use App\Domain\User\service\UserService; use Cassandra\Uuid; use HttpException; +use Exception; class ProfileServiceImplement implements ProfileService { @@ -18,16 +22,19 @@ class ProfileServiceImplement implements ProfileService private UserRepository $userRepository; private JwtHandler $jwtHandler; private MemberPasswordEncrypt $encrypt; + private TokenService $tokenService; public function __construct( ProfileRepository $profileRepository, JwtHandler $jwtHandler, UserRepository $userRepository, + TokenService $tokenService, MemberPasswordEncrypt $encrypt ) { $this->profileRepository = $profileRepository; $this->jwtHandler = $jwtHandler; $this->userRepository = $userRepository; + $this->tokenService = $tokenService; $this->encrypt = $encrypt; } @@ -56,12 +63,12 @@ public function createUserProfile(int $userIdx, string $userUid, string $nickNam public function checkNickNameDuplicate(string $nickName): bool { - // TODO: Implement checkNickNameDuplicate() method. - return false; + return $this->profileRepository->checkNicknameCount($nickName) > 0; } /** * @throws \HttpException + * @throws Exception */ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody): ?Profile { @@ -82,7 +89,7 @@ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody) $user = $this->userRepository->findUserOfUserId($userId); if (is_null($user)) { - throw new HttpException("user not found", 403); + throw new Exception("user not found", 403); } $userUid = $user->getUid(); @@ -90,7 +97,7 @@ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody) $nickName = $requestBody->getNickname(); if($this->checkNickNameDuplicate($nickName)) { - throw new \HttpException("nickname already used", 503); + throw new Exception("nickname already used", 503); } $profileIdx = $this->createUserProfile( @@ -101,4 +108,16 @@ public function createUserProfileByRequestDto(ProfileCreateRequest $requestBody) return $this->profileRepository->getUserProfileByProfileIdx($profileIdx); } + + public function getUserProfilesByRequest(ProfileGetByIdRequest $request) : ?Profile + { + $token = $request->getToken(); + + /** @var JwtClaim $claims */ + $claims = $this->tokenService->getClaimFromToken($token); + + $profileUid = $claims->profileUid; + var_dump($profileUid); + return $this->profileRepository->getUserProfileByProfileUid($profileUid); + } } \ No newline at end of file From c60c8babd92d6a8779bfe457451f8bf0be382c7e Mon Sep 17 00:00:00 2001 From: huntmori Date: Fri, 19 Jan 2024 05:34:58 +0900 Subject: [PATCH 4/4] implement get user profiles --- app/routes.php | 4 +- .../Profile/controller/ProfileController.php | 40 +++++++++++++++++-- .../Profile/models/ProfileGetListRequest.php | 13 ++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/app/routes.php b/app/routes.php index 6aff005..3428d71 100644 --- a/app/routes.php +++ b/app/routes.php @@ -47,7 +47,9 @@ ProfileController::class . ':createUserProfile' )->add(JwtMiddleware::class); - $group->get("/{uid}", ProfileController::class.":getProfiles") + $group->get("/{uid}", ProfileController::class.":getProfile") + ->add(JwtMiddleware::class); + $group->get("", ProfileController::class.":getProfiles") ->add(JwtMiddleware::class); }); /* diff --git a/src/Domain/Profile/controller/ProfileController.php b/src/Domain/Profile/controller/ProfileController.php index 477180d..9521a27 100644 --- a/src/Domain/Profile/controller/ProfileController.php +++ b/src/Domain/Profile/controller/ProfileController.php @@ -2,11 +2,14 @@ namespace App\Domain\Profile\controller; +use App\Application\Common\service\TokenService; use App\Domain\Common\controller\ActionBasedController; use App\Domain\Profile\entities\Profile; use App\Domain\Profile\models\ProfileCreateRequest; use App\Domain\Profile\models\ProfileGetByIdRequest; +use App\Domain\Profile\models\ProfileGetListRequest; use App\Domain\Profile\service\ProfileService; +use App\Domain\User\service\UserService; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Log\LoggerInterface; @@ -14,12 +17,18 @@ class ProfileController extends ActionBasedController { private ProfileService $profileService; + private UserService $userService; + private TokenService $tokenService; public function __construct( LoggerInterface $logger, - ProfileService $profileService + ProfileService $profileService, + TokenService $tokenService, + UserService $userService ) { $this->profileService = $profileService; + $this->tokenService = $tokenService; + $this->userService = $userService; parent::__construct($logger); } @@ -33,10 +42,14 @@ public function createUserProfile(Request $request, Response $response, array $a { $requestDto = new ProfileCreateRequest($request); $profile = $this->profileService->createUserProfileByRequestDto($requestDto); - return $this->respondWithData($response, $profile->toArray(), 200); + return $this->respondWithData( + $response, + $profile->toArray(), + 200 + ); } - public function getProfiles(Request $request, Response $response, array $args): Response + public function getProfile(Request $request, Response $response, array $args): Response { $requestDto = new ProfileGetByIdRequest($request, $args); @@ -49,4 +62,25 @@ public function getProfiles(Request $request, Response $response, array $args): 200 ); } + + public function getProfiles(Request $request, Response $response, array $args): Response + { + $requestDto = new ProfileGetListRequest($request); + $claim = $this->tokenService->getClaimFromToken($requestDto->getToken()); + $user = $this->userService->getUserByUserId($claim->userId); + $profiles = $this->profileService->getUserProfiles($user->getUid()); + + $result = []; + for($i=0; $itoArray(); + } + + return $this->respondWithData( + $response, + $result, + 200 + ); + } } diff --git a/src/Domain/Profile/models/ProfileGetListRequest.php b/src/Domain/Profile/models/ProfileGetListRequest.php index 3cab162..6fe6f4c 100644 --- a/src/Domain/Profile/models/ProfileGetListRequest.php +++ b/src/Domain/Profile/models/ProfileGetListRequest.php @@ -15,4 +15,17 @@ public function __construct(RequestInterface $request) { $this->token = $this->extractToken($request); } + + public function getToken() : ?string + { + return $this->token; + } + + public function setToken(?string $token) : ?string + { + $this->token = $token; + return $this->token; + } + + } \ No newline at end of file