diff --git a/GovBr/GovBrStrategy.php b/GovBr/GovBrStrategy.php index db972b8..a01245e 100644 --- a/GovBr/GovBrStrategy.php +++ b/GovBr/GovBrStrategy.php @@ -1,6 +1,7 @@ userinfo($results->id_token); + $userinfo->access_token = $results->access_token; - //@TODO O nome deve ser o primeiro nome - - + $exp_name = explode(" ", $userinfo->name); + $info = [ - 'name' => $userinfo->name, + 'name' => $exp_name[0], 'cpf' => $userinfo->sub, 'email' => $userinfo->email_verified ? $userinfo->email : null, 'phone_number' => $userinfo->phone_number_verified ? $userinfo->phone_number : null, @@ -95,23 +97,11 @@ public function oauth2callback() 'token' => $results->id_token, 'expires' => $userinfo->exp ), - 'raw' => $info, + 'raw' => $userinfo, 'info' => $info ); - - $app->hook("entity(Agent).insert:after", function() use ($userinfo, $token){ - $this->nomeCompleto = $userinfo->name; - - // @TODO definir o avatar - // $curl = new Curl; - // $curl->setHeader('Content-Type', 'application/x-www-form-urlencoded'); - // $curl->setHeader('Authorization', "Basic {$token}"); - - // $curl->post($url, $params); - // $curl->close(); - // $response = $curl->response; - }); - + + $this->callback(); } else { $error = array( @@ -119,7 +109,6 @@ public function oauth2callback() 'message' => 'Failed when attempting to obtain access token', 'raw' => array( 'response' => $response, - 'headers' => $headers ) ); $this->errorCallback($error); @@ -143,4 +132,47 @@ private function userinfo($id_token) $exp = explode(".", $id_token); return json_decode(base64_decode($exp[1])); } + + public static function getFile($owner, $url, $token){ + + $curl = new Curl; + $curl->setHeader('Authorization', "Bearer {$token}"); + $curl->get($url); + $curl->close(); + $response = $curl->response; + + $tmp = tempnam("/tmp", ""); + $handle = fopen($tmp, "wb"); + fwrite($handle,$response); + fclose($handle); + + $class_name = $owner->fileClassName; + + $basename = md5(time()).".jpg"; + + $file = new $class_name([ + "name" => $basename, + "type" => mime_content_type($tmp), + "tmp_name" => $tmp, + "error" => 0, + "size" => filesize($tmp) + ]); + + $file->group = "avatar"; + $file->owner = $owner; + $file->save(true); + } + + public static function newUserProcessor($user, $response) + { + $app = App::i(); + + $userinfo = (object) $response['auth']['raw']; + $app->disableAccessControl(); + $user->profile->nomeCompleto = $userinfo->name; + $user->profile->save(true); + self::getFile($user->profile, $userinfo->picture, $userinfo->access_token); + + $app->enableAccessControl(); + } } diff --git a/Plugin.php b/Plugin.php index d0990b3..cae7200 100644 --- a/Plugin.php +++ b/Plugin.php @@ -20,13 +20,10 @@ public function _init() { // Load JS & CSS $app->hook('<>(auth.<<*>>)', function() use ($app) { - //$app->view->enqueueScript('app', 'multipleLocal', 'js/multipleLocal.js'); - //s$app->view->enqueueStyle('app', 'multipleLocal', 'css/multipleLocal.css'); - $app->view->enqueueStyle('app', 'multipleLocal', 'css/govbr.css'); - $app->view->enqueueScript('app', 'multipleLocal', 'js/app.js'); $app->view->enqueueStyle('app', 'multipleLocal', 'css/app.css'); $app->view->enqueueStyle('app', 'fontawesome', 'https://use.fontawesome.com/releases/v5.8.2/css/all.css'); + $app->view->enqueueStyle('app', 'multipleLocal-govbr', 'css/govbr.css'); }); $app->hook('<>(panel.<<*>>):before', function() use ($app) { diff --git a/Provider.php b/Provider.php index 16d948c..564ce2f 100644 --- a/Provider.php +++ b/Provider.php @@ -53,6 +53,7 @@ function __construct ($config) { 'timeBlockedloginAttemp' => env('AUTH_BLOCK_TIME', 900), // tempo de bloqueio do usuario em segundos 'metadataFieldCPF' => env('AUTH_METADATA_FIELD_DOCUMENT', 'documento'), + 'metadataFieldPhone' => env('AUTH_METADATA_FIELD_PHONE', 'telefone1'), 'urlSupportChat' => env('AUTH_SUPPORT_CHAT', ''), 'urlSupportEmail' => env('AUTH_SUPPORT_EMAIL', ''), @@ -91,15 +92,19 @@ function __construct ($config) { ], 'govbr' => [ 'visible' => env('AUTH_GOV_BR_ID', false), - 'response_type' => null, + 'response_type' => 'code', 'client_id' => null, + 'client_secret' => null, 'scope' => null, 'redirect_uri' => null, 'auth_endpoint' => null, + 'token_endpoint' => null, 'nonce' => null, - 'state' => null, + 'code_verifier' => null, 'code_challenge' => null, - 'code_challenge_method' => null + 'code_challenge_method' => null, + 'userinfo_endpoint' => null, + 'state_salt' => null, ] ] ]; @@ -1221,6 +1226,10 @@ public function getMetadataFieldCpfFromConfig() { return $this->_config['metadataFieldCPF']; } + public function getMetadataFieldPhone() { + return $this->_config['metadataFieldPhone']; + } + public function _getAuthenticatedUser() { if (is_object($this->_authenticatedUser)) { return $this->_authenticatedUser; @@ -1325,6 +1334,7 @@ protected function _createUser($response) { $app->em->persist($user); + // cria um agente do tipo user profile para o usuário criado acima $agent = new Entities\Agent($user); @@ -1332,6 +1342,9 @@ protected function _createUser($response) { $agent->name = $response['auth']['info']['name']; }elseif(isset($response['auth']['info']['first_name']) && isset($response['auth']['info']['last_name'])){ $agent->name = $response['auth']['info']['first_name'] . ' ' . $response['auth']['info']['last_name']; + }if(isset($response['auth']['info']['phone_number'])){ + $metadataFieldPhone = $this->getMetadataFieldPhone(); + $agent->setMetadata($metadataFieldPhone, $response['auth']['info']['phone_number']); }else{ $agent->name = ''; } @@ -1345,15 +1358,23 @@ protected function _createUser($response) { $agent->status = $config['statusCreateAgent']; $agent->emailPrivado = $user->email; + //$app->em->persist($agent); $agent->save(); $app->em->flush(); + $user->profile = $agent; $user->save(true); + if($provider_class = $response['auth']['provider']."Strategy"){ + if(method_exists($provider_class, "newUserProcessor")){ + $provider_class::newUserProcessor($user, $response); + } + } + $app->enableAccessControl(); $redirectUrl = $agent->editUrl; $app->applyHookBoundTo($this, 'auth.createUser:redirectUrl', [&$redirectUrl]); diff --git a/assets/css/govbr.css b/assets/css/govbr.css index ea947e3..20c8099 100644 --- a/assets/css/govbr.css +++ b/assets/css/govbr.css @@ -1,3 +1,11 @@ .br-sign-in{ - background-color: #0000ff3d; + background-color: #eee; } + +.br-sign-in img { + margin-left: 6px; + width: 23% !important; + -webkit-filter: none !important; + filter: none !important; + color:#000; +} \ No newline at end of file diff --git a/assets/img/gov.br_logo.svg b/assets/img/gov.br_logo.svg new file mode 100644 index 0000000..0cf1f4c --- /dev/null +++ b/assets/img/gov.br_logo.svg @@ -0,0 +1,23 @@ + + + + + background + + + + Layer 1 + + + + + + + + + + + + \ No newline at end of file diff --git a/views/auth/multiple-local.php b/views/auth/multiple-local.php index 305306c..c0a4a2a 100644 --- a/views/auth/multiple-local.php +++ b/views/auth/multiple-local.php @@ -115,9 +115,9 @@ function showStrategy($name, $config) { -