Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupalnetforum_authNetforum;
- use DrupalCoreLoggerLoggerChannelFactoryInterface;
- use DrupaluserUserInterface;
- use DrupalCoreEntityEntityTypeManagerInterface;
- use DrupalCoreLanguageLanguageManagerInterface;
- use DrupalComponentUtilityUnicode;
- use DrupaluserEntityUser;
- /**
- * Class NetforumAccountManager.
- *
- * @package Drupalnetforum_authNetforum
- */
- class NetforumAccountManager implements NetforumAccountManagerInterface {
- /**
- * The entity type manager.
- *
- * @var DrupalCoreLanguageLanguageManagerInterface
- */
- protected $entityTypeManager;
- /**
- * The language manager.
- *
- * @var DrupalCoreLanguageLanguageManagerInterface
- */
- protected $languageManager;
- /**
- * The logger.
- *
- * @var DrupalCoreLoggerLoggerChannelFactoryInterface
- */
- protected $logger;
- /**
- * The Netforum client.
- *
- * @var Drupalnetforum_authNetforumNetforumClientInterface
- */
- protected $client;
- /**
- * Class constructor.
- */
- public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, LoggerChannelFactoryInterface $logger_channel_factory, NetforumClientInterface $client) {
- $this->entityTypeManager = $entity_type_manager;
- $this->languageManager = $language_manager;
- $this->logger = $logger_channel_factory;
- $this->client = $client;
- }
- /**
- * {@inheritdoc}
- */
- public function createUser(stdClass $netforum_account) : int {
- $user = $this->entityTypeManager->getStorage('user')->create();
- $langcode = $this->languageManager->getCurrentLanguage()->getId();
- $email_address = $netforum_account->result->Customer->cst_eml_address_dn;
- $customer_key = $netforum_account->result->Customer->cst_key;
- $customer_number = $netforum_account->result->Customer->cst_recno;
- $parts = explode('@', $email_address);
- $drupal_username = $parts[0] . $customer_number;
- $password = user_password(15);
- $user->enforceIsNew();
- $user->setUsername($drupal_username);
- $user->setPassword($password);
- $user->setEmail($email_address);
- $user->set("init", $email_address);
- $user->set("langcode", $langcode);
- $user->set("preferred_langcode", $langcode);
- $user->set("preferred_admin_langcode", $langcode);
- $user->set("preferred_admin_langcode", $langcode);
- $user->set("field_netforum_customer_key", $customer_key);
- $user->set("field_netforum_customer_recno", $customer_number);
- $user->activate();
- $user->save();
- // Set the roles.
- $account = User::load($user->id());
- $this->updateUserRoles($account, $netforum_account);
- return $user->id();
- }
- /**
- * {@inheritdoc}
- */
- public function updateUser(UserInterface $account, stdClass $netforum_account) {
- // @todo: This will be filled in later if they do any profile related stuff.
- // Update the roles.
- $this->updateUserRoles($account, $netforum_account);
- }
- /**
- * Wipe and rebuild a users roles to be in-sync with Netforum memberships.
- */
- public function updateUserRoles(UserInterface $account, $netforum_account) {
- $netforum_role = $netforum_account->result->Individual->ind_int_code;
- $membership_info = $this->getActiveMembershipsForCustomer($netforum_account);
- $member_types = $this->getActiveMemberTypesFromCustomerMembership($membership_info);
- // Remove previous roles, for security reasons.
- $previous_roles = $account->getRoles(TRUE);
- foreach ($previous_roles as $role) {
- // Don't remove Administrator if it was previously set.
- if ($role !== 'administrator') {
- $account->removeRole($role);
- }
- }
- $roles = [];
- // Staff is an actual user role, not a membership type
- if ($netforum_role == 'Staff') {
- $roles[] = 'content_editor';
- }
- foreach ($member_types as $role_name) {
- $roles[] = $this->mapToDrupalRole($role_name);
- }
- // At the least, we should assign a boilerplate role.
- if (empty($roles)) {
- $account->addRole('netforum_member');
- }
- foreach ($roles as $role) {
- $account->addRole($role);
- }
- $account->save();
- }
- /**
- * Maps netforum membership types to local Drupal roles.
- */
- public function mapToDrupalRole(string $membership_type) : string {
- switch (Unicode::strtoupper($membership_type)) {
- case 'CARRIER DIVISION':
- $role = 'carrier_division';
- break;
- case 'RAIL DIVISION':
- $role = 'rail_division';
- break;
- case 'SUPPLIER DIVISION':
- $role = 'supplier_division';
- break;
- case 'ASSOCIATE':
- $role = 'associate';
- break;
- default:
- $role = 'netforum_member';
- break;
- }
- return $role;
- }
- /**
- * Returns all membership information for a given netforum user.
- *
- * I really dislike this call because it returns just a string of XML instead
- * of the responses we are used to receiving. It is a custom SQL call done by
- * using ExecuteMethod, a unique API method in Netforum.
- */
- public function getActiveMembershipsForCustomer(stdClass $netforum_account) {
- $customer_key = $netforum_account->result->Customer->cst_key;
- $arguments = [
- 'serviceName' => 'GetActiveMembershipsForCustomer',
- 'methodName' => 'GetActiveMemberships',
- 'parameters' => [
- 'Parameter' => [
- 'Name' => 'cst_key',
- 'Value' => $customer_key,
- ],
- ],
- ];
- $response = $this->client->request('ExecuteMethod', [$arguments]);
- $parts = explode('-', $customer_key);
- $last_segment = end($parts);
- if ($response->error) {
- $message = $response->result->Message . ' Customer key ending in: @key.';
- $this->logger->get('netforum_auth')->error($message, ['@key' => $last_segment]);
- }
- elseif (!$response->result) {
- $message = 'Customer key ending in: @key has no membership information.';
- $this->logger->get('netforum_auth')->info($message, ['@key' => $last_segment]);
- }
- else {
- // This entire result is returned as an xml string, convert it.
- $xml = simplexml_load_string($response->result->any);
- $json = json_encode($xml);
- $response->result = json_decode($json, TRUE);
- }
- return $response;
- }
- /**
- * Filters out active roles from the membership information.
- */
- public function getActiveMemberTypesFromCustomerMembership($membership_information) : array {
- $roles = [];
- if ($membership_information->result) {
- foreach ($membership_information->result['Membership'] as $membership) {
- if ($membership['MbrStatus'] == 'Active') {
- $roles[] = $membership['MbrType'];
- }
- }
- }
- return $roles;
- }
- }
- <?php
- namespace DrupalTestsnetforum_authKernel;
- use Drupalnetforum_authNetforumNetforumAccountManager;
- use DrupalTeststokenKernelKernelTestBase;
- use orgbovigovfsvfsStream;
- use orgbovigovfsvisitorvfsStreamStructureVisitor;
- /**
- * Class UserCreateTest
- *
- * @package DrupalTestsnetforum_authKernel
- * @group netforum_auth
- */
- class UserCreateTest extends KernelTestBase {
- /**
- * Modules to enable.
- *
- * @var array
- */
- public static $modules = ['system', 'user', 'netforum_auth'];
- protected function setUp() {
- parent::setUp();
- // create roles
- }
- public function testUserIsCreated() {
- $netforum_wsdl_response = new stdClass();
- $netforum_wsdl_response->result = new stdClass();
- $netforum_wsdl_response->result->Customer = new stdClass();
- $netforum_wsdl_response->result->Customer->cst_eml_address_dn = 'zztesting@example.comzz';
- $netforum_wsdl_response->result->Customer->cst_key = '123-456-789';
- $netforum_wsdl_response->result->Customer->cst_recno = '123456';
- $result = new stdClass();
- $result->response = [
- 'Membership' => [
- 0 => [
- 'MbrStatus' => 'Active',
- 'MbrType' => 'Associate'
- ]
- ]
- ];
- $prophecy = $this->prophesize(NetforumAccountManager::CLASS);
- $prophecy->getActiveMembershipsForCustomer($netforum_wsdl_response)->willReturn($result);
- $netforum_account_manager = $prophecy->reveal();
- $uid = $netforum_account_manager->createUser($netforum_wsdl_response);
- // check $uid is a User
- // check role(s)
- }
- }
- Method call:
- - createUser(stdClass:00000000584be36000000000303910bf Object (
- 'result' => stdClass:00000000584be19c00000000303910bf Object (
- 'Customer' => stdClass:00000000584be05a00000000303910bf Object (
- 'cst_eml_address_dn' => 'zztesting@example.comzz'
- 'cst_key' => '123-456-789'
- 'cst_recno' => '123456'
- )
- )
- ))
- on DoubleDrupaliana_netforum_authNetforumNetforumAccountManagerP1 was not expected, expected calls were:
- - getActiveMembershipsForCustomer(exact(stdClass:00000000584be36000000000303910bf Object (
- 'result' => stdClass:00000000584be19c00000000303910bf Object (
- 'Customer' => stdClass:00000000584be05a00000000303910bf Object (
- 'cst_eml_address_dn' => 'zztesting@example.comzz'
- 'cst_key' => '123-456-789'
- 'cst_recno' => '123456'
- )
- )
- )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement