cfmgrv4 init...2

This commit is contained in:
최준흠 2024-10-10 21:04:24 +09:00
parent 7aae21010a
commit f4fa080107
7 changed files with 55 additions and 67 deletions

View File

@ -100,21 +100,16 @@ class UserController extends FrontController
public function login(): RedirectResponse|string public function login(): RedirectResponse|string
{ {
$this->init('login'); $this->init('login');
//Transaction Start
$this->getModel()->transStart();
try { try {
$this->create_validate($this->action, $this->fields); $this->create_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas(); $this->formDatas = $this->getFormDatas();
$auth = new LocalAuth(); $auth = new LocalAuth();
$auth->login($auth->checkUser($this->formDatas)); $auth->login($auth->checkUser($this->formDatas));
$this->message = "로그인 성공"; $this->message = "로그인 성공";
$this->getModel()->transCommit();
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('message', $this->message); return redirect()->to($this->popPreviousUrl())->with('message', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback
$this->getModel()->transRollback();
log_message("error", $e->getMessage()); log_message("error", $e->getMessage());
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $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 public function google_login(): RedirectResponse|string
{ {
$this->init('login'); $this->init('login');
//Transaction Start
$this->getModel()->transStart();
try { try {
$access_code = $this->request->getVar('code'); $access_code = $this->request->getVar('code');
if (!$access_code) { if (!$access_code) {
@ -132,13 +125,10 @@ class UserController extends FrontController
$auth = new GoogleAuth(); $auth = new GoogleAuth();
$auth->login($auth->checkUser($access_code)); $auth->login($auth->checkUser($access_code));
$this->message = "로그인 성공"; $this->message = "로그인 성공";
$this->getModel()->transCommit();
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('message', $this->message); return redirect()->to($this->popPreviousUrl())->with('message', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback
$this->getModel()->transRollback();
log_message("error", $e->getMessage()); log_message("error", $e->getMessage());
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
} }

View File

@ -14,6 +14,6 @@ return [
], ],
"STATUS" => [ "STATUS" => [
"use" => "사용", "use" => "사용",
"unuse" => "사용않함", "unuse" => "승인대기",
], ],
]; ];

View File

@ -3,7 +3,7 @@
namespace App\Libraries\MyAuth; namespace App\Libraries\MyAuth;
use App\Entities\UserEntity; 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\Libraries\MySocket\GoogleSocket\API as GoogleSocket;
use App\Models\UserModel; use App\Models\UserModel;
use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\Exceptions\PageNotFoundException;
@ -23,7 +23,6 @@ class GoogleAuth extends MyAuth
$this->_mySocket = new GoogleSocket(); $this->_mySocket = new GoogleSocket();
} }
$this->_mySocket->setToken($this->_access_code); $this->_mySocket->setToken($this->_access_code);
$this->_mySocket->setUserSNSEntity();
return $this->_mySocket; return $this->_mySocket;
} }
@ -54,7 +53,7 @@ class GoogleAuth extends MyAuth
throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요."); throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요.");
} catch (\Exception $e) { } catch (\Exception $e) {
log_message('error', $e->getMessage()); log_message('error', $e->getMessage());
throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.<BR>{$e->getMessage()}"); throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.\n{$e->getMessage()}");
} }
} }
} }

View File

@ -4,13 +4,15 @@ namespace App\Libraries\MySocket\GoogleSocket;
use App\Entities\UserSNSEntity; use App\Entities\UserSNSEntity;
use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use Google\Client; use Google\Client;
use Google\Service\Oauth2; use Google\Service\Oauth2;
class API extends GoogleSocket class API extends GoogleSocket
{ {
public function __construct() {} public function __construct()
{
parent::__construct();
}
public function getClient(): Client public function getClient(): Client
{ {
@ -70,7 +72,7 @@ class API extends GoogleSocket
if (isset($tokenInfo['error'])) { if (isset($tokenInfo['error'])) {
throw new ConfigException($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]; $this->_access_token = $tokenInfo[$this->_token_name];
// Google Service에 접근하기 위해 Access Token 설정 // Google Service에 접근하기 위해 Access Token 설정
$this->getClient()->setAccessToken([ $this->getClient()->setAccessToken([
@ -90,26 +92,9 @@ class API extends GoogleSocket
$this->getClient()->setAccessToken($this->getToken()); $this->getClient()->setAccessToken($this->getToken());
$oauth = new Oauth2($this->getClient()); $oauth = new Oauth2($this->getClient());
$userInfo = $oauth->userinfo->get(); $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()); return $this->setUserSNSEntity($userInfo->id, $userInfo->name, $userInfo->email, $detail);
$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;
} }
} }

View File

@ -4,8 +4,6 @@ namespace App\Libraries\MySocket\GoogleSocket;
use App\Entities\UserSNSEntity; use App\Entities\UserSNSEntity;
use App\Libraries\MySocket\MySocket as Client; use App\Libraries\MySocket\MySocket as Client;
use App\Models\UserSNSModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class CURL extends GoogleSocket class CURL extends GoogleSocket
{ {
@ -141,7 +139,7 @@ class CURL extends GoogleSocket
"headers" => [ "headers" => [
"Authorization" => "Bearer {$this->getToken()}", "Authorization" => "Bearer {$this->getToken()}",
"Accept" => "application/json", "Accept" => "application/json",
'User-Agent' => $this->getUserAgent() 'User-Agent' => $this->getUserAgent()
], ],
]; ];
$response = $this->get("https://www.googleapis.com/oauth2/v3/userinfo", $options); $response = $this->get("https://www.googleapis.com/oauth2/v3/userinfo", $options);
@ -156,7 +154,8 @@ class CURL extends GoogleSocket
throw new \Exception($message); throw new \Exception($message);
} }
$userInfo = json_decode($response->getBody(), true); $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'])) { if (isset($userInfo['error']) || !isset($userInfo['email']) || empty($userInfo['email'])) {
$message = sprintf( $message = sprintf(
"Google: User 정보가 없습니다.\n--userInfo--\n%s\n", "Google: User 정보가 없습니다.\n--userInfo--\n%s\n",
@ -167,24 +166,6 @@ class CURL extends GoogleSocket
throw new \Exception($message); throw new \Exception($message);
} }
// 사용자정보 설정하기 // 사용자정보 설정하기
$this->getModel()->where(UserSNSModel::SITE, $this->getSite()); return $this->setUserSNSEntity($userInfo["id"], $userInfo["name"], $userInfo["email"], $detail);
$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;
} }
} }

View File

@ -2,10 +2,11 @@
namespace App\Libraries\MySocket\GoogleSocket; namespace App\Libraries\MySocket\GoogleSocket;
use App\Entities\UserSNSEntity;
use App\Libraries\MySocket\MySocket;
use App\Models\UserSNSModel;
use Config\Services; 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 abstract class GoogleSocket extends MySocket
{ {
@ -38,4 +39,36 @@ abstract class GoogleSocket extends MySocket
} }
return $this->_model; 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;
}
} }

View File

@ -11,9 +11,9 @@ class UserSNSModel extends CommonModel
const PK = "uid"; const PK = "uid";
const TITLE = "name"; const TITLE = "name";
const SITE = "site"; const SITE = "site";
protected $table = self::TABLE; protected $table = self::TABLE;
protected $primaryKey = self::PK; protected $primaryKey = self::PK;
protected $returnType = UserSNSEntity::class; protected $returnType = UserSNSEntity::class;
protected $allowedFields = [ protected $allowedFields = [
"site", "site",
"id", "id",
@ -34,7 +34,7 @@ class UserSNSModel extends CommonModel
{ {
switch ($field) { switch ($field) {
case "id": 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; break;
case $this->getTitleField(): case $this->getTitleField():
$rule = "required|trim|string"; $rule = "required|trim|string";