Commit 32a29f30 authored by RoSk0's avatar RoSk0

Ported ability to load contact information for the current user.

parent 00c3b4d8
......@@ -468,16 +468,6 @@ function crm_core_user_sync_form_crm_core_ui_admin_config_form_alter(&$form, &$f
);
}
/**
* Implements hook_init().
*/
function crm_core_user_sync_init() {
if (variable_get('crm_core_contact_load', FALSE)) {
global $user;
$user->crm_core['contact'] = crm_core_user_sync_get_contact_from_uid($user->uid);
}
}
/**
* Implements hook_theme
*/
......
......@@ -2,6 +2,13 @@ services:
crm_core_user_sync.relation:
class: Drupal\crm_core_user_sync\CrmCoreUserSyncRelation
arguments: ['@entity_type.manager', '@crm_core_user_sync.relation_rules']
crm_core_user_sync.relation_rules:
class: Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules
arguments: ['@config.factory', 'crm_core_user_sync.settings']
crm_core_user_sync.request.event_subscriber:
class: Drupal\crm_core_user_sync\EventSubscriber\RequestSubscriber
arguments: ['@current_user', '@config.factory', '@crm_core_user_sync.relation', '@entity_type.manager']
tags:
- { name: event_subscriber }
......@@ -9,9 +9,11 @@ use Drupal\crm_core_user_sync\Entity\Relation;
use Drupal\user\UserInterface;
/**
* CrmCoreUserSyncRelation service.
* Relation service.
*
* @package Drupal\crm_core_user_sync
*/
class CrmCoreUserSyncRelation {
class CrmCoreUserSyncRelation implements CrmCoreUserSyncRelationInterface {
/**
* Relation storage.
......@@ -41,10 +43,7 @@ class CrmCoreUserSyncRelation {
}
/**
* Retrieves the individual contact id for specified user.
*
* @return int|null
* Individual id, if relation exists.
* {@inheritdoc}
*/
public function getUserIndividualId($user_id) {
$individual_id = NULL;
......@@ -65,10 +64,7 @@ class CrmCoreUserSyncRelation {
}
/**
* Retrieves the user id for specified individual contact.
*
* @return int|null
* User id, if relation exists.
* {@inheritdoc}
*/
public function getIndividualUserId($individual_id) {
$user_id = NULL;
......@@ -89,10 +85,7 @@ class CrmCoreUserSyncRelation {
}
/**
* Retrieves the relation for specified user.
*
* @return int|null
* Relation ID, if exists.
* {@inheritdoc}
*/
public function getUserRelationId($user_id) {
$rids = $this->storage->getQuery()
......@@ -108,10 +101,7 @@ class CrmCoreUserSyncRelation {
}
/**
* Retrieves the user id for specified individual contact.
*
* @return int|null
* Relation ID, if exists.
* {@inheritdoc}
*/
public function getIndividualRelationId($individual_id) {
$rids = $this->storage->getQuery()
......@@ -127,19 +117,7 @@ class CrmCoreUserSyncRelation {
}
/**
* Synchronizes user and contact.
*
* @param \Drupal\user\UserInterface $account
* Account to be synchronized. Programmatically created accounts can
* override default behavior by setting
* $account->crm_core_no_auto_sync = TRUE.
* @param \Drupal\crm_core_contact\IndividualInterface $contact
* Contact to be associated with $account.
*
* @return \Drupal\crm_core_contact\ContactInterface
* A contact object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* {@inheritdoc}
*/
public function relate(UserInterface $account, IndividualInterface $contact = NULL) {
// No contact and $account->crm_core_no_auto_sync => no sync.
......
<?php
namespace Drupal\crm_core_user_sync;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\user\UserInterface;
/**
* CrmCoreUserSyncRelation service.
*/
interface CrmCoreUserSyncRelationInterface {
/**
* Retrieves the individual contact id for specified user.
*
* @return int|null
* Individual id, if relation exists.
*/
public function getUserIndividualId($user_id);
/**
* Retrieves the user id for specified individual contact.
*
* @return int|null
* User id, if relation exists.
*/
public function getIndividualUserId($individual_id);
/**
* Retrieves the relation for specified user.
*
* @return int|null
* Relation ID, if exists.
*/
public function getUserRelationId($user_id);
/**
* Retrieves the user id for specified individual contact.
*
* @return int|null
* Relation ID, if exists.
*/
public function getIndividualRelationId($individual_id);
/**
* Synchronizes user and contact.
*
* @param \Drupal\user\UserInterface $account
* Account to be synchronized. Programmatically created accounts can
* override default behavior by setting
* $account->crm_core_no_auto_sync = TRUE.
* @param \Drupal\crm_core_contact\IndividualInterface $contact
* Contact to be associated with $account.
*
* @return \Drupal\crm_core_contact\ContactInterface
* A contact object.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function relate(UserInterface $account, IndividualInterface $contact = NULL);
}
<?php
namespace Drupal\crm_core_user_sync\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* CRM Core User Synchronization event subscriber.
*/
class RequestSubscriber implements EventSubscriberInterface {
/**
* Current logged in user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Relation service.
*
* @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface
*/
protected $relationService;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs event subscriber.
*
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* Current logged in user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface $relation_service
* The relation service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(AccountProxyInterface $current_user, ConfigFactoryInterface $config_factory, CrmCoreUserSyncRelationInterface $relation_service, EntityTypeManagerInterface $entityTypeManager) {
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
$this->relationService = $relation_service;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Kernel request event handler.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* Response event.
*/
public function onKernelRequest(GetResponseEvent $event) {
if ($this->currentUser->isAuthenticated()) {
$config = $this->configFactory->get('crm_core_user_sync.settings');
if ($config->get('contact_load')) {
$individual_id = $this->relationService->getUserIndividualId($this->currentUser->id());
if ($individual_id) {
$individual = $this->entityTypeManager->getStorage('crm_core_individual')->load($individual_id);
$account = $this->currentUser->getAccount();
$account->crm_core['contact'] = $individual;
$this->currentUser->setAccount($account);
}
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}
......@@ -3,7 +3,6 @@
namespace Drupal\Tests\crm_core_user_sync\Kernel;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\Entity\IndividualType;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\user\Entity\User;
......@@ -27,7 +26,7 @@ class CrmCoreUserSyncRelationTest extends EntityKernelTestBase {
/**
* Relation service.
*
* @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation
* @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface
*/
protected $relationService;
......
......@@ -4,11 +4,9 @@ namespace Drupal\Tests\crm_core_user_sync\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
/**
......
<?php
namespace Drupal\Tests\crm_core_user_sync\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface;
use Drupal\crm_core_user_sync\EventSubscriber\RequestSubscriber;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Test description.
*
* @group crm_core_user_sync
*/
class RequestSubscriberTest extends UnitTestCase {
/**
* Tests nothing happens for Anonymous.
*/
public function testRequestSubscriberAnonymous() {
$current_user = $this->createMock(AccountProxyInterface::class);
$current_user->expects($this->once())
->method('isAuthenticated')
->willReturn(FALSE);
$configFactory = $this->createMock(ConfigFactoryInterface::class);
$configFactory
->expects($this->never())
->method('get');
$relationService = $this->createMock(CrmCoreUserSyncRelationInterface::class);
$entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$entityTypeManager
->expects($this->never())
->method('getStorage');
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = Request::create('/', 'GET');
$subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
$event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber->onKernelRequest($event);
// Nothing to assert as we should exit immediately. Current user expectation
// will fail the test if something...
}
/**
* Tests nothing loaded for user without related contact.
*/
public function testRequestSubscriberAuthenticatedWithoutContact() {
$current_user = $this->createMock(AccountProxyInterface::class);
$current_user->expects($this->once())
->method('isAuthenticated')
->willReturn(TRUE);
$current_user
->expects($this->once())
->method('id')
->willReturn('101');
$config = $this->getMockBuilder(ImmutableConfig::class)
->disableOriginalConstructor()
->getMock();
$config
->expects($this->once())
->method('get')
->with('contact_load')
->willReturn(TRUE);
$config_name = 'crm_core_user_sync.settings';
$configFactory = $this->createMock(ConfigFactoryInterface::class);
$configFactory
->expects($this->once())
->method('get')
->with($config_name)
->willReturn($config);
$relationService = $this->createMock(CrmCoreUserSyncRelationInterface::class);
$relationService
->expects($this->once())
->method('getUserIndividualId')
->willReturn(FALSE);
$entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$entityTypeManager
->expects($this->never())
->method('getStorage');
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = Request::create('/', 'GET');
$subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
$event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber->onKernelRequest($event);
// Nothing to assert. Entity type manager expectations will the test.
}
/**
* Tests contact loaded for the user with related contact.
*/
public function testRequestSubscriberAuthenticatedWithContact() {
$current_user = $this->createMock(AccountProxyInterface::class);
$current_user->expects($this->once())
->method('isAuthenticated')
->willReturn(TRUE);
$current_user
->expects($this->once())
->method('id')
->willReturn('101');
$account = $this->createMock(AccountInterface::class);
$current_user
->expects($this->once())
->method('getAccount')
->willReturn($account);
$current_user
->expects($this->at(0))
->method('setAccount')
->willReturnReference($account);
$config = $this->getMockBuilder(ImmutableConfig::class)
->disableOriginalConstructor()
->getMock();
$config
->expects($this->once())
->method('get')
->with('contact_load')
->willReturn(TRUE);
$config_name = 'crm_core_user_sync.settings';
$configFactory = $this->createMock(ConfigFactoryInterface::class);
$configFactory
->expects($this->once())
->method('get')
->with($config_name)
->willReturn($config);
$individualId = 101;
$individual = $this->createMock(IndividualInterface::class);
$relationService = $this->createMock(CrmCoreUserSyncRelationInterface::class);
$relationService
->expects($this->once())
->method('getUserIndividualId')
->willReturn($individualId);
$entityStorage = $this->createMock(EntityStorageInterface::class);
$entityStorage
->expects($this->once())
->method('load')
->with($individualId)
->willReturn($individual);
$entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
$entityTypeManager
->expects($this->once())
->method('getStorage')
->with('crm_core_individual')
->willReturn($entityStorage);
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = Request::create('/', 'GET');
$subscriber = new RequestSubscriber($current_user, $configFactory, $relationService, $entityTypeManager);
$event = new GetResponseEvent($kernel->reveal(), $request, HttpKernelInterface::MASTER_REQUEST);
$subscriber->onKernelRequest($event);
$this->assertEquals($individual, $account->crm_core['contact'], 'Related contact was loaded');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment