Skip to content

Commit

Permalink
hotfix-21 added functionality 'link/unlink' social networs in the use…
Browse files Browse the repository at this point in the history
…r profile
  • Loading branch information
Aleksandr Yurchenko committed Mar 29, 2022
1 parent e33d6de commit 703bed0
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 27 deletions.
10 changes: 5 additions & 5 deletions config/packages/knpu_oauth2_client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ knpu_oauth2_client:
# whether to check OAuth2 "state": defaults to true
# use_state: true

# will create service: "knpu.oauth2.client.vk_main"
# will create service: "knpu.oauth2.client.vkontakte_main"
# an instance of: KnpU\OAuth2ClientBundle\Client\OAuth2Client
vk_main:
vkontakte_main:
type: generic
provider_class: App\Utils\Oauth2\Vk\Vk
# optional: a class that extends OAuth2Client
Expand All @@ -40,7 +40,7 @@ knpu_oauth2_client:
# now, all the normal options!
client_id: '%env(OAUTH_VK_CLIENT_ID)%'
client_secret: '%env(OAUTH_VK_CLIENT_SECRET)%'
redirect_route: connect_vk_check
redirect_route: connect_vkontakte_check
redirect_params: {}
# whether to check OAuth2 "state": defaults to true
# use_state: true
Expand All @@ -61,14 +61,14 @@ knpu_oauth2_client:
# will create service: "knpu.oauth2.client.github_rus"
# an instance of: KnpU\OAuth2ClientBundle\Client\Provider\GithubClient
# composer require league/oauth2-github
github_rus:
github_ru:
# must be "github" - it activates that type!
type: github
# add and set these environment variables in your .env files
client_id: '%env(OAUTH_GITHUB_RUS_CLIENT_ID)%'
client_secret: '%env(OAUTH_GITHUB_RUS_CLIENT_SECRET)%'
# a route name you'll create
redirect_route: connect_github_rus_check
redirect_route: connect_github_ru_check
redirect_params: { }
# whether to check OAuth2 "state": defaults to true
# use_state: true
6 changes: 3 additions & 3 deletions src/Controller/Front/AuthGithubRusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class AuthGithubRusController extends AbstractController
{
/**
* @Route("/connect/github-rus", name="connect_github_rus_start")
* @Route("/connect/github-ru", name="connect_github_ru_start")
*
* @param ClientRegistry $clientRegistry
*
Expand All @@ -21,12 +21,12 @@ class AuthGithubRusController extends AbstractController
public function connectAction(ClientRegistry $clientRegistry): RedirectResponse
{
return $clientRegistry
->getClient('github_rus')
->getClient('github_ru')
->redirect([], []);
}

/**
* @Route("/connect/github-rus/check", name="connect_github_rus_check")
* @Route("/connect/github-ru/check", name="connect_github_ru_check")
*
* @return void
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Controller/Front/AuthVkontakteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class AuthVkontakteController extends AbstractController
{
/**
* @Route("/connect/vk", name="connect_vk_start")
* @Route("/connect/vkontakte", name="connect_vkontakte_start")
*
* @param ClientRegistry $clientRegistry
*
Expand All @@ -21,12 +21,12 @@ class AuthVkontakteController extends AbstractController
public function connectAction(ClientRegistry $clientRegistry): RedirectResponse
{
return $clientRegistry
->getClient('vk_main')
->getClient('vkontakte_main')
->redirect([], []);
}

/**
* @Route("/connect/vk/check", name="connect_vk_check")
* @Route("/connect/vkontakte/check", name="connect_vkontakte_check")
*
* @return void
*/
Expand Down
48 changes: 48 additions & 0 deletions src/Controller/Front/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use App\Utils\Mailer\Sender\UserRegisteredEmailSender;
use Doctrine\Persistence\ManagerRegistry as Doctrine;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;

class ProfileController extends AbstractController
{
Expand Down Expand Up @@ -67,6 +70,23 @@ public function setEmailSender(UserRegisteredEmailSender $emailSender): ProfileC

return $this;
}

/** @var TranslatorInterface */
private $translator;

/**
* @required
*
* @param TranslatorInterface $translator
*
* @return ProfileController
*/
public function setTranslator(TranslatorInterface $translator): ProfileController
{
$this->translator = $translator;

return $this;
}
// Autowiring <<<

/**
Expand Down Expand Up @@ -141,4 +161,32 @@ public function resendingVerifyEmailLink(Request $request): Response

return $this->redirectToRoute('main_profile_index');
}

/**
* @Route("/profile/unlink_social_network/{socialName}", name="main_profile_unlink_social_network")
*
* @param string $socialName
*
* @return RedirectResponse
*/
public function unlinkSocialNetwork(string $socialName): RedirectResponse
{
/** @var User|null $user */
$user = $this->getUser();

if (!$user) {
throw new NotFoundHttpException('User not found');
}
$em = $this->doctrine->getManager();

$nameMethod = 'set'.ucfirst($socialName).'Id';
$user->$nameMethod(null);

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

$this->addFlash('success', $this->translator->trans('The social network has been successfully unlinked.'));

return $this->redirectToRoute('main_profile_index');
}
}
4 changes: 2 additions & 2 deletions src/Security/Authenticator/Front/GithubRusAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(
public function supports(Request $request): ?bool
{
// continue ONLY if the current ROUTE matches the check ROUTE
return 'connect_github_rus_check' === $request->attributes->get('_route');
return 'connect_github_ru_check' === $request->attributes->get('_route');
}

/**
Expand All @@ -99,7 +99,7 @@ public function supports(Request $request): ?bool
*/
public function authenticate(Request $request): Passport
{
$client = $this->clientRegistry->getClient('github_rus');
$client = $this->clientRegistry->getClient('github_ru');
$accessToken = $this->fetchAccessToken($client);

return new SelfValidatingPassport(
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Authenticator/Front/VkontakteAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(
public function supports(Request $request): ?bool
{
// continue ONLY if the current ROUTE matches the check ROUTE
return 'connect_vk_check' === $request->attributes->get('_route');
return 'connect_vkontakte_check' === $request->attributes->get('_route');
}

/**
Expand All @@ -93,7 +93,7 @@ public function supports(Request $request): ?bool
*/
public function authenticate(Request $request): Passport
{
$client = $this->clientRegistry->getClient('vk_main');
$client = $this->clientRegistry->getClient('vkontakte_main');
$accessToken = $this->fetchAccessToken($client);

return new SelfValidatingPassport(
Expand Down
1 change: 1 addition & 0 deletions src/Security/Authenticator/Front/YandexAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function authenticate(Request $request): Passport

// 3) Maybe you just want to "register" them by creating
// a User object
$request->getSession()->getFlashBag()->add('success', $this->translator->trans('The social network has been successfully linked.'));
$user->setYandexId($yandexUser->getId());
$this->userManager->flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
</a>
</div>
<div class="col-sm-12 text-center">
<a href="{{ path('connect_vk_start') }}" class="btn btn-link btn-dark mb-1">
<a href="{{ path('connect_vkontakte_start') }}" class="btn btn-link btn-dark mb-1">
{{ 'login_social_network.vk'|trans }}
</a>
</div>
{% if app.request.locale == 'ru' %}
<div class="col-sm-12 text-center">
<a href="{{ path('connect_github_rus_start') }}" class="btn btn-link btn-dark mb-">
<a href="{{ path('connect_github_ru_start') }}" class="btn btn-link btn-dark mb-">
{{ 'login_social_network.github'|trans }}
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{% if socialName %}
{% if socialNameId %}
<span class="social-group social-group--unlink">
{{ 'personal_account.social_group.link'|trans }}
</span>
<a class="social-group social-group--link" href="#">
<a class="social-group social-group--link" href="{{ url('main_profile_unlink_social_network', {'socialName': socialName}) }}">
{{ 'personal_account.social_group.unlink'|trans }}
</a>
{% else %}
<a class="social-group social-group--link" href="#">
{{ 'personal_account.social_group.link'|trans }}
</a>
{% if 'github' != socialName %}
<a class="social-group social-group--link" href="{{ path('connect_'~ socialName ~'_start') }}">
{{ 'personal_account.social_group.link'|trans }}
</a>
{% else %}
<a class="social-group social-group--link" href="{{ path('connect_'~ socialName ~ '_' ~ app.request.locale ~'_start') }}">
{{ 'personal_account.social_group.link'|trans }}
</a>
{% endif %}
<span class="social-group social-group--unlink">
{{ 'personal_account.social_group.unlink'|trans }}
</span>
{% endif %}
{% endif %}
20 changes: 16 additions & 4 deletions templates/front/profile/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
</svg>
</td>
<td>
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {'socialName': app.user.googleId} %}
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {
'socialNameId': app.user.googleId,
'socialName': 'google'
} %}
</td>
</tr>
<tr>
Expand All @@ -82,7 +85,10 @@
</svg>
</td>
<td>
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {'socialName': app.user.yandexId} %}
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {
'socialNameId': app.user.yandexId,
'socialName': 'yandex'
} %}
</td>
</tr>
<tr>
Expand All @@ -94,7 +100,10 @@
</svg>
</td>
<td>
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {'socialName': app.user.vkontakteId} %}
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {
'socialNameId': app.user.vkontakteId,
'socialName': 'vkontakte'
} %}
</td>
</tr>
<tr>
Expand All @@ -106,7 +115,10 @@
</svg>
</td>
<td>
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {'socialName': app.user.githubId} %}
{% include 'front/_embed/_utils/_social_network_link_unlink_btn.html.twig' with {
'socialNameId': app.user.githubId,
'socialName': 'github'
} %}
</td>
</tr>
</tbody>
Expand Down
12 changes: 12 additions & 0 deletions translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1180,5 +1180,17 @@
<target>Unlink</target>
</segment>
</unit>
<unit id="cpfVdd4" name="The social network has been successfully unlinked.">
<segment>
<source>The social network has been successfully unlinked.</source>
<target>The social network has been successfully unlinked.</target>
</segment>
</unit>
<unit id="yjTQYPm" name="The social network has been successfully linked.">
<segment>
<source>The social network has been successfully linked.</source>
<target>The social network has been successfully linked.</target>
</segment>
</unit>
</file>
</xliff>
2 changes: 2 additions & 0 deletions translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@ personal_account.social_group.github: Github
personal_account.social_group.integration_service: 'Integration Service'
personal_account.social_group.link: Link
personal_account.social_group.unlink: Unlink
'The social network has been successfully unlinked.': 'The social network has been successfully unlinked.'
'The social network has been successfully linked.': 'The social network has been successfully linked.'
12 changes: 12 additions & 0 deletions translations/messages.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1198,5 +1198,17 @@
<target>Отвязать</target>
</segment>
</unit>
<unit id="cpfVdd4" name="The social network has been successfully unlinked.">
<segment>
<source>The social network has been successfully unlinked.</source>
<target>Социальная сеть успешно отвязана.</target>
</segment>
</unit>
<unit id="yjTQYPm" name="The social network has been successfully linked.">
<segment>
<source>The social network has been successfully linked.</source>
<target>Социальная сеть успешно привязана.</target>
</segment>
</unit>
</file>
</xliff>
2 changes: 2 additions & 0 deletions translations/messages.ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ personal_account.social_group.github: Гитхаб
personal_account.social_group.integration_service: 'Сервис интеграции'
personal_account.social_group.link: Привязать
personal_account.social_group.unlink: Отвязать
'The social network has been successfully unlinked.': 'Социальная сеть успешно отвязана.'
'The social network has been successfully linked.': 'Социальная сеть успешно привязана.'

0 comments on commit 703bed0

Please sign in to comment.