diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 96d37c0..8de601a 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -100,21 +100,16 @@ class UserController extends FrontController public function login(): RedirectResponse|string { $this->init('login'); - //Transaction Start - $this->getModel()->transStart(); try { $this->create_validate($this->action, $this->fields); $this->formDatas = $this->getFormDatas(); $auth = new LocalAuth(); $auth->login($auth->checkUser($this->formDatas)); $this->message = "로그인 성공"; - $this->getModel()->transCommit(); log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 return redirect()->to($this->popPreviousUrl())->with('message', $this->message); } catch (\Exception $e) { - //Transaction Rollback - $this->getModel()->transRollback(); log_message("error", $e->getMessage()); return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); } @@ -122,8 +117,6 @@ class UserController extends FrontController public function google_login(): RedirectResponse|string { $this->init('login'); - //Transaction Start - $this->getModel()->transStart(); try { $access_code = $this->request->getVar('code'); if (!$access_code) { @@ -132,13 +125,10 @@ class UserController extends FrontController $auth = new GoogleAuth(); $auth->login($auth->checkUser($access_code)); $this->message = "로그인 성공"; - $this->getModel()->transCommit(); log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 return redirect()->to($this->popPreviousUrl())->with('message', $this->message); } catch (\Exception $e) { - //Transaction Rollback - $this->getModel()->transRollback(); log_message("error", $e->getMessage()); return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); } diff --git a/app/Language/en/UserSNS.php b/app/Language/en/UserSNS.php index d391750..7862de3 100644 --- a/app/Language/en/UserSNS.php +++ b/app/Language/en/UserSNS.php @@ -14,6 +14,6 @@ return [ ], "STATUS" => [ "use" => "사용", - "unuse" => "사용않함", + "unuse" => "승인대기", ], ]; diff --git a/app/Libraries/MyAuth/GoogleAuth.php b/app/Libraries/MyAuth/GoogleAuth.php index a83b453..2a649a4 100644 --- a/app/Libraries/MyAuth/GoogleAuth.php +++ b/app/Libraries/MyAuth/GoogleAuth.php @@ -3,7 +3,7 @@ namespace App\Libraries\MyAuth; use App\Entities\UserEntity; -use App\Libraries\MySocket\GoogleSocket\CURL; +// use App\Libraries\MySocket\GoogleSocket\CURL; use App\Libraries\MySocket\GoogleSocket\API as GoogleSocket; use App\Models\UserModel; use CodeIgniter\Exceptions\PageNotFoundException; @@ -23,7 +23,6 @@ class GoogleAuth extends MyAuth $this->_mySocket = new GoogleSocket(); } $this->_mySocket->setToken($this->_access_code); - $this->_mySocket->setUserSNSEntity(); return $this->_mySocket; } @@ -54,7 +53,7 @@ class GoogleAuth extends MyAuth throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요."); } catch (\Exception $e) { log_message('error', $e->getMessage()); - throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.
{$e->getMessage()}"); + throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.\n{$e->getMessage()}"); } } } diff --git a/app/Libraries/MySocket/GoogleSocket/API.php b/app/Libraries/MySocket/GoogleSocket/API.php index 90ae210..f563c45 100644 --- a/app/Libraries/MySocket/GoogleSocket/API.php +++ b/app/Libraries/MySocket/GoogleSocket/API.php @@ -4,13 +4,15 @@ namespace App\Libraries\MySocket\GoogleSocket; use App\Entities\UserSNSEntity; use CodeIgniter\Exceptions\ConfigException; -use CodeIgniter\Exceptions\PageNotFoundException; use Google\Client; use Google\Service\Oauth2; class API extends GoogleSocket { - public function __construct() {} + public function __construct() + { + parent::__construct(); + } public function getClient(): Client { @@ -70,7 +72,7 @@ class API extends GoogleSocket if (isset($tokenInfo['error'])) { throw new ConfigException($tokenInfo['error']); } - log_message("debug", var_export($tokenInfo, true)); + // log_message("debug", var_export($tokenInfo, true)); $this->_access_token = $tokenInfo[$this->_token_name]; // Google Service에 접근하기 위해 Access Token 설정 $this->getClient()->setAccessToken([ @@ -90,26 +92,9 @@ class API extends GoogleSocket $this->getClient()->setAccessToken($this->getToken()); $oauth = new Oauth2($this->getClient()); $userInfo = $oauth->userinfo->get(); - log_message("debug", var_export($userInfo, true)); + $detail = var_export($userInfo, true); + // log_message("debug", $detail); // 사용자정보 설정하기 - $this->getModel()->where($this->getModel()::SITE, $this->getSite()); - $entity = $this->getModel()->getEntityByID($userInfo['id']); - if ($entity === null) { - //없다면 새로 등록 - $formDatas = [ - 'site' => $this->getSite(), - 'id' => $userInfo->id, - 'name' => $userInfo->name, - 'email' => $userInfo->email, - 'detail' => var_export($userInfo, true), - 'status' => 'standby', - ]; - $entity = $this->getModel()->create($formDatas); - } - //상태가 use(승인완료)가 아니라면 - if ($entity->status !== DEFAULTS['STATUS']) { - throw new PageNotFoundException("{$entity->getSite()}의{$entity->getEmail()}:{$entity->getTitle()}님은 {$entity->status}입니다 "); - } - return $entity; + return $this->setUserSNSEntity($userInfo->id, $userInfo->name, $userInfo->email, $detail); } } diff --git a/app/Libraries/MySocket/GoogleSocket/CURL.php b/app/Libraries/MySocket/GoogleSocket/CURL.php index 6383d7e..7f6c91b 100644 --- a/app/Libraries/MySocket/GoogleSocket/CURL.php +++ b/app/Libraries/MySocket/GoogleSocket/CURL.php @@ -4,8 +4,6 @@ namespace App\Libraries\MySocket\GoogleSocket; use App\Entities\UserSNSEntity; use App\Libraries\MySocket\MySocket as Client; -use App\Models\UserSNSModel; -use CodeIgniter\Exceptions\PageNotFoundException; class CURL extends GoogleSocket { @@ -141,7 +139,7 @@ class CURL extends GoogleSocket "headers" => [ "Authorization" => "Bearer {$this->getToken()}", "Accept" => "application/json", - 'User-Agent' => $this->getUserAgent() + 'User-Agent' => $this->getUserAgent() ], ]; $response = $this->get("https://www.googleapis.com/oauth2/v3/userinfo", $options); @@ -156,7 +154,8 @@ class CURL extends GoogleSocket throw new \Exception($message); } $userInfo = json_decode($response->getBody(), true); - log_message("debug", var_export($userInfo, true)); + $detail = var_export($userInfo, true); + // log_message("debug", $detail); if (isset($userInfo['error']) || !isset($userInfo['email']) || empty($userInfo['email'])) { $message = sprintf( "Google: User 정보가 없습니다.\n--userInfo--\n%s\n", @@ -167,24 +166,6 @@ class CURL extends GoogleSocket throw new \Exception($message); } // 사용자정보 설정하기 - $this->getModel()->where(UserSNSModel::SITE, $this->getSite()); - $entity = $this->getModel()->getEntityByID($userInfo['id']); - if ($entity === null) { - //없다면 새로 등록 - $formDatas = [ - 'site' => $this->getSite(), - 'id' => $userInfo['id'], - 'name' => $userInfo['name'], - 'email' => $userInfo['email'], - 'detail' => var_export($userInfo, true), - 'status' => 'standby', - ]; - $entity = $this->getModel()->create($formDatas); - } - //상태가 use(승인완료)가 아니라면 - if ($entity->status !== DEFAULTS['STATUS']) { - throw new PageNotFoundException("{$entity->getSite()}의{$entity->getEmail()}:{$entity->getTitle()}님은 {$entity->status}입니다 "); - } - return $entity; + return $this->setUserSNSEntity($userInfo["id"], $userInfo["name"], $userInfo["email"], $detail); } } diff --git a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php index 1911a22..7aa1887 100644 --- a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php +++ b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php @@ -2,10 +2,11 @@ namespace App\Libraries\MySocket\GoogleSocket; -use App\Entities\UserSNSEntity; -use App\Libraries\MySocket\MySocket; -use App\Models\UserSNSModel; use Config\Services; +use CodeIgniter\Exceptions\PageNotFoundException; +use App\Models\UserSNSModel; +use App\Libraries\MySocket\MySocket; +use App\Entities\UserSNSEntity; abstract class GoogleSocket extends MySocket { @@ -38,4 +39,36 @@ abstract class GoogleSocket extends MySocket } return $this->_model; } + final protected function setUserSNSEntity(string $id, string $name, string $email, string $detail): UserSNSEntity + { + $this->getModel()->where(UserSNSModel::SITE, $this->getSite()); + $entity = $this->getModel()->getEntityByID($id); + if ($entity === null) { + //Transaction Start + $this->getModel()->transStart(); + try { + //없다면 새로 등록 + $formDatas = [ + 'site' => $this->getSite(), + 'id' => $id, + 'name' => $name, + 'email' => $email, + 'detail' => $detail, + 'status' => 'unuse', + ]; + $entity = $this->getModel()->create($formDatas); + $this->getModel()->transCommit(); + } catch (\Exception $e) { + //Transaction Rollback + $this->getModel()->transRollback(); + log_message("error", $e->getMessage()); + throw new \Exception(__FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); + } + } + //상태가 use(승인완료)가 아니라면 + if ($entity->status !== DEFAULTS['STATUS']) { + throw new PageNotFoundException("{$entity->getSite()}의{$entity->getEmail()}:{$entity->getTitle()}님은 {$entity->status}입니다 "); + } + return $entity; + } } diff --git a/app/Models/UserSNSModel.php b/app/Models/UserSNSModel.php index d7d1b54..5a2d83c 100644 --- a/app/Models/UserSNSModel.php +++ b/app/Models/UserSNSModel.php @@ -11,9 +11,9 @@ class UserSNSModel extends CommonModel const PK = "uid"; const TITLE = "name"; const SITE = "site"; - protected $table = self::TABLE; - protected $primaryKey = self::PK; - protected $returnType = UserSNSEntity::class; + protected $table = self::TABLE; + protected $primaryKey = self::PK; + protected $returnType = UserSNSEntity::class; protected $allowedFields = [ "site", "id", @@ -34,7 +34,7 @@ class UserSNSModel extends CommonModel { switch ($field) { case "id": - $rule = "required|trim|min_length[4]|max_length[20]|is_unique[{$this->table}.{$field}]"; + $rule = "required|trim|min_length[4]|is_unique[{$this->table}.{$field}]"; break; case $this->getTitleField(): $rule = "required|trim|string";