Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Added social-media user-provider #100

Draft
wants to merge 7 commits into
base: 2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/SuluCommunityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('validator.xml');

$bundles = $container->getParameter('kernel.bundles');
if (array_key_exists('HttplugBundle', $bundles) && array_key_exists('HWIOAuthBundle', $bundles)) {
$loader->load('social-media-login.xml');
}

if ($lastLoginEnabled) {
$lastLoginRefreshInterval = $config[Configuration::LAST_LOGIN][Configuration::REFRESH_INTERVAL];

Expand Down
71 changes: 71 additions & 0 deletions Entity/UserAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Entity;

use Sulu\Component\Security\Authentication\UserInterface;

class UserAccessToken
{
/**
* @var UserInterface
*/
private $user;

/**
* @var string
*/
private $service;

/**
* @var string
*/
private $identifier;

/**
* @var string
*/
private $accessToken = '';

public function __construct(UserInterface $user, string $service, string $identifier)
{
$this->user = $user;
$this->service = $service;
$this->identifier = $identifier;
}

public function getUser(): UserInterface
{
return $this->user;
}

public function getService(): string
{
return $this->service;
}

public function getIdentifier(): string
{
return $this->identifier;
}

public function getAccessToken(): string
{
return $this->accessToken;
}

public function setAccessToken(string $accessToken): self
{
$this->accessToken = $accessToken;

return $this;
}
}
32 changes: 32 additions & 0 deletions Entity/UserAccessTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CommunityBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Sulu\Component\Security\Authentication\UserInterface;

class UserAccessTokenRepository extends EntityRepository
{
public function create(UserInterface $user, string $service, string $identifier): UserAccessToken
{
$class = $this->getClassName();
$accessToken = new $class($user, $service, $identifier);
$this->getEntityManager()->persist($accessToken);

return $accessToken;
}

public function findByIdentifier(string $service, string $identifier): ?UserAccessToken
{
return $this->findOneBy(['service' => $service, 'identifier' => $identifier]);
}
}
30 changes: 28 additions & 2 deletions Manager/CommunityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sulu\Bundle\SecurityBundle\Entity\UserRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
Expand Down Expand Up @@ -83,28 +84,44 @@ class CommunityManager implements CommunityManagerInterface
*/
protected $mailFactory;

/**
* @var RequestStack
*/
protected $requestStack;

/**
* @var string
*/
protected $defaultLocale;

/**
* @param array $config
* @param string $webspaceKey
* @param EventDispatcherInterface $eventDispatcher
* @param TokenStorageInterface $tokenStorage
* @param UserManagerInterface $userManager
* @param MailFactoryInterface $mailFactory
* @param RequestStack $requestStack
* @param string $defaultLocale
*/
public function __construct(
array $config,
$webspaceKey,
EventDispatcherInterface $eventDispatcher,
TokenStorageInterface $tokenStorage,
UserManagerInterface $userManager,
MailFactoryInterface $mailFactory
MailFactoryInterface $mailFactory,
RequestStack $requestStack,
string $defaultLocale
) {
$this->config = $config;
$this->webspaceKey = $webspaceKey;
$this->eventDispatcher = $eventDispatcher;
$this->tokenStorage = $tokenStorage;
$this->userManager = $userManager;
$this->mailFactory = $mailFactory;
$this->requestStack = $requestStack;
$this->defaultLocale = $defaultLocale;
}

/**
Expand All @@ -122,7 +139,7 @@ public function register(User $user)
{
// User need locale
if (null === $user->getLocale()) {
$user->setLocale('en');
$user->setLocale($this->getLocale());
}

// Enable User by config
Expand Down Expand Up @@ -315,4 +332,13 @@ public function getConfigTypeProperty($type, $property)

return $this->config[$type][$property];
}

private function getLocale(): string
{
if ($currentRequest = $this->requestStack->getCurrentRequest()) {
return $currentRequest->getLocale();
}

return $this->defaultLocale;
}
}
30 changes: 30 additions & 0 deletions Resources/config/doctrine/UserAccessToken.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Sulu\Bundle\CommunityBundle\Entity\UserAccessToken" table="com_user_access_tokens"
repository-class="Sulu\Bundle\CommunityBundle\Entity\UserAccessTokenRepository">
<indexes>
<index columns="identifier"/>
</indexes>

<unique-constraints>
<unique-constraint columns="service,identifier"/>
</unique-constraints>

<id name="user" type="integer" association-key="true">
<generator strategy="NONE"/>
</id>

<id name="service" type="string" length="32">
<generator strategy="NONE"/>
</id>

<field name="identifier" type="string" nullable="false" length="128"/>
<field name="accessToken" type="string" nullable="false"/>

<many-to-one field="user" target-entity="Sulu\Component\Security\Authentication\UserInterface">
<join-column name="userId" referenced-column-name="id" on-delete="CASCADE"/>
</many-to-one>
</entity>
</doctrine-mapping>
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<argument type="service" id="security.token_storage"/>
<argument type="service" id="sulu_community.user_manager"/>
<argument type="service" id="sulu_community.mail_factory"/>
<argument type="service" id="request_stack"/>
<argument>%kernel.default_locale%</argument>
</service>

<!-- User Manager -->
Expand Down
13 changes: 13 additions & 0 deletions Resources/config/social-media-login.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sulu_community.social_media.oauth_provider" class="Sulu\Bundle\CommunityBundle\SocialMedia\SocialMediaUserProvider">
<argument type="service" id="sulu_community.community_manager.registry"/>
<argument type="service" id="request_stack"/>
<argument type="service" id="doctrine"/>
</service>
</services>
</container>
Loading