83 lines
2.6 KiB
PHP
83 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyAuth;
|
|
|
|
use App\Entities\UserEntity;
|
|
use App\Libraries\MySocket\GoogleSocket;
|
|
use App\Models\UserSNSModel;
|
|
use App\Models\UserModel;
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
|
use Google\Service\Oauth2;
|
|
|
|
class GoogleAuth extends MyAuth
|
|
{
|
|
private ?GoogleSocket $_mySocket = null;
|
|
private string $_site = "GOOGLE";
|
|
private ?UserSNSModel $_model = null;
|
|
private string $access_code = "";
|
|
public function __construct(string $access_code)
|
|
{
|
|
parent::__construct();
|
|
$this->access_code = $access_code;
|
|
}
|
|
|
|
public function getMySocket(): GoogleSocket
|
|
{
|
|
if ($this->_mySocket === null) {
|
|
$this->_mySocket = new GoogleSocket();
|
|
$this->_mySocket->setToken($this->access_code);
|
|
}
|
|
return $this->_mySocket;
|
|
}
|
|
|
|
final protected function getModel(): UserSNSModel
|
|
{
|
|
if ($this->_model === null) {
|
|
$this->_model = model(UserSNSModel::class);
|
|
}
|
|
return $this->_model;
|
|
}
|
|
|
|
public function checkUser(): UserEntity
|
|
{
|
|
try {
|
|
// Google 서비스 설정
|
|
$userInfo = $this->getMySocket()->getUserInfo();
|
|
//기존 등록된 사용자가 있는지 검사
|
|
$this->getModel()->where(UserSNSModel::SITE, $this->_site);
|
|
$entity = $this->getModel()->getEntityByID($userInfo['id']);
|
|
if ($entity === null) {
|
|
//없다면 새로 등록
|
|
$formDatas = [
|
|
'site' => $this->_site,
|
|
'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("{$this->_site}}의{$userInfo['email']}:{$userInfo['name']}님은 " . $entity->status . "입니다");
|
|
}
|
|
//local db 사용와의 연결 확인
|
|
$userModel = model(UserModel::class);
|
|
$user_entity = $userModel->getEntityByID($entity->getID());
|
|
if ($user_entity === null) {
|
|
throw new PageNotFoundException("{$this->_site}의{$userInfo['email']}:{$userInfo['name']}님은 아직 사용자 연결이 이루어지지 않았습니다. ");
|
|
}
|
|
return $user_entity;
|
|
} catch (\Google_Service_Exception $e) {
|
|
log_message('error', '구글 서비스 예외: ' . $e->getMessage());
|
|
throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요.");
|
|
} catch (\Exception $e) {
|
|
log_message('error', $e->getMessage());
|
|
throw new PageNotFoundException("관리자에게 문의하시기 바랍니다.<BR>{$e->getMessage()}");
|
|
}
|
|
}
|
|
}
|