-
-
Save cupertinobr/e619ec895430f67692c7f53e4f1f6ad1 to your computer and use it in GitHub Desktop.
INTEGRAÇÃO LOGIN SOCIAL DO GOOGLE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# AGRADECIMENTO: WELLIGTON PIVATTO | |
# INSTALE O PACOTE COMPOSER NO PROJETO: google/apiclient | |
# CONTEÚDO ARQUIVO google.ini (app/config/google.ini) | |
/* | |
[google_oauth2] | |
client_id = "303046463171-cisqtpsaqbvd6xpg547rfbhi7ea1un69.apps.googleusercontent.com" | |
client_secret = "YHMKPX-wqMQF1Dc06GI5Prd2IYVivJ3df6V" | |
redirect_uri = "https://SEU_DOMINIO/NOME_PROJETO/index.php?class=LoginForm" | |
*/ | |
# 1 - CLASSE (app/control/admin): GoogleClient.php | |
use Google\Client as GoogleClientComposer; | |
use Google\Service\Oauth2 as ServiceOauth2; | |
use GuzzleHttp\Client as GuzzleClient; | |
class GoogleClient | |
{ | |
public $client; | |
private $data; | |
public function __construct() | |
{ | |
$this->client = new GoogleClientComposer; | |
$this->init(); | |
} | |
public function init() | |
{ | |
$config = parse_ini_file('app/config/google.ini', true); | |
if ($config === false) | |
{ | |
throw new Exception('Arquivo de configuração google.ini não encontrado.'); | |
} | |
$guzzleClient = new GuzzleClient(['curl' => [CURLOPT_SSL_VERIFYPEER => false]]); | |
$this->client->setHttpClient($guzzleClient); | |
$this->client->setClientId($config['google_oauth2']['client_id']); | |
$this->client->setClientSecret($config['google_oauth2']['client_secret']); | |
$this->client->setRedirectUri($config['google_oauth2']['redirect_uri']); | |
$this->client->addScope('email'); | |
$this->client->addScope('profile'); | |
} | |
public function authorized() | |
{ | |
if (isset($_GET['code'])) | |
{ | |
$token = $this->client->fetchAccessTokenWithAuthCode($_GET['code']); | |
$this->client->setAccessToken($token['access_token']); | |
$googleService = new ServiceOauth2($this->client); | |
$this->data = $googleService->userinfo->get(); | |
return true; | |
} | |
return false; | |
} | |
public function getData() | |
{ | |
return $this->data; | |
} | |
public function generateAuthLink() | |
{ | |
return $this->client->createAuthUrl(); | |
} | |
} | |
# 2 - CLASSE: LoginForm.php | |
class LoginForm extends TPage | |
{ | |
protected $form; | |
function __construct($param) | |
{ | |
parent::__construct(); | |
$ini = AdiantiApplicationConfig::get(); | |
if (isset($_GET['code'])) {self::onLoginGoogle();} | |
$this->style = 'clear:both'; | |
$this->form = new BootstrapFormBuilder('form_login'); | |
$this->form->setFormTitle( 'BEM VINDO(a)!' ); | |
$login = new TEntry('login'); | |
$password = new TPassword('password'); | |
$previous_class = new THidden('previous_class'); | |
$previous_method = new THidden('previous_method'); | |
$previous_parameters = new THidden('previous_parameters'); | |
if (!empty($param['previous_class']) && $param['previous_class'] !== 'LoginForm') | |
{ | |
$previous_class->setValue($param['previous_class']); | |
if (!empty($param['previous_method'])) | |
{ | |
$previous_method->setValue($param['previous_method']); | |
} | |
$previous_parameters->setValue(serialize($param)); | |
} | |
$login->setSize('100%', 40); | |
$password->setSize('100%', 40); | |
$login->style = 'height:35px; font-size:14px;float:left;border-bottom-left-radius: 0;border-top-left-radius: 0;'; | |
$password->style = 'height:35px;font-size:14px;float:left;border-bottom-left-radius: 0;border-top-left-radius: 0;'; | |
$login->placeholder = _t('User'); | |
$password->placeholder = _t('Password'); | |
$login->autofocus = 'autofocus'; | |
$user = '<span class="login-avatar"><span class="fa fa-user"></span></span>'; | |
$locker = '<span class="login-avatar"><span class="fa fa-lock"></span></span>'; | |
$unit = '<span class="login-avatar"><span class="fa fa-university"></span></span>'; | |
$lang = '<span class="login-avatar"><span class="fa fa-globe"></span></span>'; | |
$row = $this->form->addFields( [$user, $login] ); | |
$row->layout = ['col-sm-12 display-flex']; | |
$row = $this->form->addFields( [$locker, $password] ); | |
$row->layout = ['col-sm-12 display-flex']; | |
$this->form->addFields( [$previous_class, $previous_method, $previous_parameters] ); | |
if (!empty($ini['general']['multiunit']) and $ini['general']['multiunit'] == '1') | |
{ | |
$unit_id = new TCombo('unit_id'); | |
$unit_id->setSize('100%'); | |
$unit_id->style = 'height:35px;font-size:14px;float:left;border-bottom-left-radius: 0;border-top-left-radius: 0;'; | |
$row = $this->form->addFields( [$unit, $unit_id] ); | |
$row->layout = ['col-sm-12 display-flex']; | |
$login->setExitAction(new TAction( [$this, 'onExitUser'] ) ); | |
} | |
if (!empty($ini['general']['multi_lang']) and $ini['general']['multi_lang'] == '1') | |
{ | |
$lang_id = new TCombo('lang_id'); | |
$lang_id->setSize('100%'); | |
$lang_id->style = 'height:35px;font-size:14px;float:left;border-bottom-left-radius: 0;border-top-left-radius: 0;'; | |
$lang_id->addItems( $ini['general']['lang_options'] ); | |
$lang_id->setValue( $ini['general']['language'] ); | |
$lang_id->setDefaultOption(FALSE); | |
$row = $this->form->addFields( [$lang, $lang_id] ); | |
$row->layout = ['col-sm-12 display-flex']; | |
} | |
$btn = $this->form->addAction('ENTRAR', new TAction(array($this, 'onLogin')), ''); | |
$btn->class = 'btn btn-default'; | |
$btn->style = 'height: 40px;width: 90%;display: block;margin: auto;font-size:17px;'; | |
# BOTÃO LOGIN GOOGLE | |
$btn_google = $this->form->addAction('ENTRAR COM GOOGLE', new TAction(array($this, 'onLoginRedirectGoogle')), ''); | |
$btn_google->class = 'btn btn-default'; | |
$btn_google->style = 'height: 40px;width: 90%;display: block;margin: 10px auto 0;font-size:17px;'; | |
$btn_google->setImage('app/output/icon_google.png'); | |
$wrapper = new TElement('div'); | |
$wrapper->style = 'margin:auto; margin-top:100px;max-width:460px;'; | |
$wrapper->id = 'login-wrapper'; | |
$h3 = new TElement('h1'); | |
$h3->style = 'text-align:center;'; | |
$h3->add('Login Social (Google)'); | |
$divLogo = new TElement('div'); | |
$divLogo->class = 'login-medium-logo'; | |
$wrapper->add($divLogo); | |
$wrapper->add($h3); | |
$wrapper->add($this->form); | |
parent::add($wrapper); | |
} | |
public static function onLoginGoogle() | |
{ | |
try | |
{ | |
$googleClient = new GoogleClient; | |
$googleClient->init(); | |
if ($googleClient->authorized()) | |
{ | |
$data = $googleClient->getData(); | |
} | |
$data = (object) $data; | |
(new TRequiredValidator)->validate(_t('Email'), $data->email); | |
TSession::regenerate(); | |
TScript::create("__adianti_clear_tabs()"); | |
TTransaction::open('permission'); | |
$user = SystemUsers::where('email', '=', $data->email)->first(); | |
if ($user) | |
{ | |
$user->get_unit(); | |
$user->get_frontpage(); | |
ApplicationAuthenticationService::setUnit($data->unit_id ?? null); | |
ApplicationAuthenticationService::setLang($data->lang_id ?? null); | |
SystemAccessLogService::registerLogin(); | |
SystemAccessNotificationLogService::registerLogin(); | |
$frontpage = $user->frontpage; | |
if (!empty($param['previous_class']) && $param['previous_class'] !== 'LoginForm') | |
{ | |
AdiantiCoreApplication::gotoPage($param['previous_class'], $param['previous_method'], unserialize($param['previous_parameters'])); // reload | |
} else if ($frontpage instanceof SystemProgram and $frontpage->controller) | |
{ | |
AdiantiCoreApplication::gotoPage($frontpage->controller); | |
TSession::setValue('frontpage', $frontpage->controller); | |
} else { | |
AdiantiCoreApplication::gotoPage('EmptyPage'); | |
TSession::setValue('frontpage', 'EmptyPage'); | |
} | |
ApplicationAuthenticationService::loadSessionVars($user); | |
TTransaction::close(); | |
} else { | |
throw new Exception('E-MAIL NÃO TEM PERMISSÃO DE ACESSO NO GOOGLE'); | |
} | |
} catch (Exception $e) { | |
TSession::freeSession(); | |
new TMessage('error', $e->getMessage()); | |
sleep(2); | |
TTransaction::rollback(); | |
} | |
} | |
public function onLoginRedirectGoogle() | |
{ | |
try | |
{ | |
$googleClient = new GoogleClient; | |
$googleClient->init(); | |
$urlAuthenticateGoogle = $googleClient->generateAuthLink(); | |
TScript::create('window.location.href = "' . $urlAuthenticateGoogle . '";'); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error',$e->getMessage()); | |
TTransaction::rollback(); | |
} | |
} | |
public static function onExitUser($param) | |
{ | |
try | |
{ | |
TTransaction::open('permission'); | |
$user = SystemUsers::newFromLogin( $param['login'] ); | |
if ($user instanceof SystemUsers) | |
{ | |
$units = $user->getSystemUserUnits(); | |
$options = []; | |
if ($units) | |
{ | |
foreach ($units as $unit) | |
{ | |
$options[$unit->id] = $unit->name; | |
} | |
} | |
TCombo::reload('form_login', 'unit_id', $options); | |
} | |
TTransaction::close(); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error',$e->getMessage()); | |
TTransaction::rollback(); | |
} | |
} | |
public static function onLogin($param) | |
{ | |
$ini = AdiantiApplicationConfig::get(); | |
try | |
{ | |
$data = (object) $param; | |
(new TRequiredValidator)->validate( _t('Login'), $data->login); | |
(new TRequiredValidator)->validate( _t('Password'), $data->password); | |
if (!empty($ini['general']['multiunit']) and $ini['general']['multiunit'] == '1') | |
{ | |
(new TRequiredValidator)->validate( _t('Unit'), $data->unit_id); | |
} | |
if (!empty($ini['general']['require_terms']) && $ini['general']['require_terms'] == '1' && !empty($param['usage_term_policy']) AND empty($data->accept)) | |
{ | |
throw new Exception(_t('You need read and agree to the terms of use and privacy policy')); | |
} | |
TSession::regenerate(); | |
TScript::create("__adianti_clear_tabs()"); | |
$user = ApplicationAuthenticationService::authenticate( $data->login, $data->password ); | |
$term_policy = SystemPreference::findInTransaction('permission', 'term_policy'); | |
if (!empty($ini['general']['require_terms']) && $ini['general']['require_terms'] == '1' && $user->accepted_term_policy !== 'Y' && !empty($term_policy) && empty($data->accept)) | |
{ | |
TSession::freeSession(); | |
$param['usage_term_policy'] = 'Y'; | |
$action = new TAction(['LoginForm', 'onLogin'], $param); | |
$form = new BootstrapFormBuilder('term_policy'); | |
$content = new TElement('div'); | |
$content->style = "max-height: 45vh; overflow: auto; margin-bottom: 10px;"; | |
$content->add($term_policy->preference); | |
$check = new TCheckGroup('accept'); | |
$check->addItems(['Y' => _t('I have read and agree to the terms of use and privacy policy')]); | |
$form->addContent([$content]); | |
$form->addFields([$check]); | |
$form->addAction( _t('Accept'), $action, 'fas:check'); | |
return new TInputDialog(_t('Terms of use and privacy policy'), $form); | |
} | |
if (!empty($ini['general']['require_terms']) && $ini['general']['require_terms'] == '1' && $user->accepted_term_policy !== 'Y' && !empty($term_policy) && !empty($data->accept)) | |
{ | |
TTransaction::open('permission'); | |
$user->accepted_term_policy = 'Y'; | |
$user->accepted_term_policy_at = date('Y-m-d H:i:s'); | |
$user->store(); | |
TTransaction::close(); | |
} | |
if ($user) | |
{ | |
ApplicationAuthenticationService::setUnit( $data->unit_id ?? null ); | |
ApplicationAuthenticationService::setLang( $data->lang_id ?? null ); | |
SystemAccessLogService::registerLogin(); | |
SystemAccessNotificationLogService::registerLogin(); | |
$frontpage = $user->frontpage; | |
if (!empty($param['previous_class']) && $param['previous_class'] !== 'LoginForm') | |
{ | |
AdiantiCoreApplication::gotoPage($param['previous_class'], $param['previous_method'], unserialize($param['previous_parameters'])); // reload | |
} | |
else if ($frontpage instanceof SystemProgram and $frontpage->controller) | |
{ | |
AdiantiCoreApplication::gotoPage($frontpage->controller); // reload | |
TSession::setValue('frontpage', $frontpage->controller); | |
} | |
else | |
{ | |
AdiantiCoreApplication::gotoPage('EmptyPage'); // reload | |
TSession::setValue('frontpage', 'EmptyPage'); | |
} | |
} | |
} | |
catch (Exception $e) | |
{ | |
TSession::freeSession(); | |
new TMessage('error',$e->getMessage()); | |
sleep(2); | |
TTransaction::rollback(); | |
} | |
} | |
public static function reloadPermissions() | |
{ | |
try | |
{ | |
TTransaction::open('permission'); | |
$user = SystemUsers::newFromLogin( TSession::getValue('login') ); | |
if ($user) | |
{ | |
ApplicationAuthenticationService::loadSessionVars($user, false); | |
$frontpage = $user->frontpage; | |
if ($frontpage instanceof SystemProgram AND $frontpage->controller) | |
{ | |
TApplication::gotoPage($frontpage->controller); // reload | |
} | |
else | |
{ | |
TApplication::gotoPage('EmptyPage'); // reload | |
} | |
} | |
TTransaction::close(); | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
public function onLoad($param) | |
{ | |
} | |
public static function onLogout() | |
{ | |
SystemAccessLogService::registerLogout(); | |
TSession::freeSession(); | |
AdiantiCoreApplication::gotoPage('LoginForm', ''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment