K
K
kafkiansky2019-06-19 11:54:13
PHP
kafkiansky, 2019-06-19 11:54:13

How to unlink an account?

In general, there is the following: there is a networks table and a users table. During the first authorization (authorization only through social networks), the user is added to both the networks table and the users table. When linking an account, it looks if there is already such a social network in two tables. If there is, an exception is thrown that the account is linked to someone. If not, we add an entry only to the networks table (connection with users many to one). As a result, when logging in from some social network, it first looks at the entry in networks and gets the user in the users table through it. Everything seems to be simple. But what about unbinding, if the presence of mail is required? That is, we go according to the conditions: if the account is left alone, then we simply throw out an exception that cannot be untied. But if a user wants to unlink a social network that is stored in users and networks, and not just networks, but at the same time in networks he has 2 more social networks, how do we untie? You can, for example, take the data of any other social network from the list and replace the entry in users with them, and also refresh the session so that the user is not forced to log in with a new one. However, I think this is wrong. Who can advise what?
Data:
Tab. users
uuid | client_id | email | auth_at | updated_at
Tab. networks
uuid | user_id | client_id | email
Sample handler

if ($command->user->getNetworks()->count() === 1) {
        throw new \DomainException('Нельзя отвязать последнюю соц сеть');
  }

if ($command->user->getClientId() ===
    $this->networkRepository->findOneBy(['clientId' => $command->clientId])
) {
     // Надо понять, что делать в этой итерации
}

 $command->user->detachNetwork(
      $command->network,
      $command->clientId
);

 $this->em->flush();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decadal, 2019-06-19
@mad_maximus

There is the concept of Primary Email - this is the contact information where all requests are sent (to change the password, notifications, etc.). Given your architecture, the email in users is the Primary Email, and in networks the list of secondary emails. Not the most beautiful solution in the end, but you have the following options:
1) pull the associated social networks and select some email from there

$networks = $this->networkRepository->findBy(['clientId' => $command->clientId]);
foreach ($networks as $net) 
{
// здесь логика по которой вы решаете какой именно secondary email делать primary. 
// если решения нет (например нет ни одного акка с email), выбивать ту же ошибку что и при count === 1
}
// решение есть, значит получен некий email

//задаете новый primary, желательно перед этим проверить что нет юзера с таким праймари
$user->setEmail($email);

2) offer the user to independently choose Primary Email in case he tries to untie the account

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question