Skip to content

Commit

Permalink
Merge pull request #768 from FeliciLab/feature/steps-to-solve-duplica…
Browse files Browse the repository at this point in the history
…te-cpf

Atividades para resolver o problema de duplicidade de CPF
  • Loading branch information
jeff-doliveira1 authored Jan 13, 2023
2 parents e94f9a9 + 4708da0 commit 5960c0a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace MapasCulturais\AuthProviders;

use MapasCulturais\App;
use MapasCulturais\Entities;
use MapasCulturais\AuthProviders\JWT;

class OpauthKeyCloak extends \MapasCulturais\AuthProvider{
protected $opauth;
Expand Down Expand Up @@ -270,6 +271,23 @@ protected function _createUser($response) {
}

$app->em->persist($user);

/**
* Verifica se CPF está vinculado a outra conta no momento do cadastro
* Remove CPF do agente que tem um email como Username no Keycloak
* Deixa o CPF somente no novo usuário, que tem o CPF como Username no Keycloak
*/
$documento = $response['auth']['raw']['preferred_username'];
$cpf = preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "\$1.\$2.\$3-\$4", $documento);
$agent_metas = $app->repo('AgentMeta')->findBy(['value' => $cpf]);

if ($agent_metas) {
foreach ($agent_metas as $agent_meta) {
$app->em->remove($agent_meta);
$app->em->flush();
}
}

// cria um agente do tipo user profile para o usuário criado acima
$agent = new Entities\Agent($user);
$agent->status = 1;
Expand All @@ -287,9 +305,8 @@ protected function _createUser($response) {

$agent->emailPrivado = $user->email;

$documento = $response['auth']['raw']['preferred_username'];
if (!empty($documento) && strlen($documento) === 11) {
$agent->documento = preg_replace("/(\d{3})(\d{3})(\d{3})(\d{2})/", "\$1.\$2.\$3-\$4", $documento);
$agent->documento = $cpf;
}

$agent->save();
Expand All @@ -310,4 +327,4 @@ protected function _createUser($response) {
function getChangePasswordUrl() {
return $this->baseUrl . 'auth/realms/saude/account/password';
}
}
}
5 changes: 4 additions & 1 deletion src/protected/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
},
{
"type": "git",
"url": "https://github.com/EscolaDeSaudePublica/opauth-keycloak.git"
"url": "https://github.com/FeliciLab/opauth-keycloak.git"
},
{
"packagist": false
}
]
}
108 changes: 103 additions & 5 deletions src/protected/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5960c0a

Please sign in to comment.