Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupalnetforum_authNetforum;
  4.  
  5. use DrupalCoreLoggerLoggerChannelFactoryInterface;
  6. use DrupaluserUserInterface;
  7. use DrupalCoreEntityEntityTypeManagerInterface;
  8. use DrupalCoreLanguageLanguageManagerInterface;
  9. use DrupalComponentUtilityUnicode;
  10. use DrupaluserEntityUser;
  11.  
  12. /**
  13. * Class NetforumAccountManager.
  14. *
  15. * @package Drupalnetforum_authNetforum
  16. */
  17. class NetforumAccountManager implements NetforumAccountManagerInterface {
  18.  
  19. /**
  20. * The entity type manager.
  21. *
  22. * @var DrupalCoreLanguageLanguageManagerInterface
  23. */
  24. protected $entityTypeManager;
  25.  
  26. /**
  27. * The language manager.
  28. *
  29. * @var DrupalCoreLanguageLanguageManagerInterface
  30. */
  31. protected $languageManager;
  32.  
  33. /**
  34. * The logger.
  35. *
  36. * @var DrupalCoreLoggerLoggerChannelFactoryInterface
  37. */
  38. protected $logger;
  39.  
  40. /**
  41. * The Netforum client.
  42. *
  43. * @var Drupalnetforum_authNetforumNetforumClientInterface
  44. */
  45. protected $client;
  46.  
  47. /**
  48. * Class constructor.
  49. */
  50. public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, LoggerChannelFactoryInterface $logger_channel_factory, NetforumClientInterface $client) {
  51. $this->entityTypeManager = $entity_type_manager;
  52. $this->languageManager = $language_manager;
  53. $this->logger = $logger_channel_factory;
  54. $this->client = $client;
  55. }
  56.  
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function createUser(stdClass $netforum_account) : int {
  61. $user = $this->entityTypeManager->getStorage('user')->create();
  62. $langcode = $this->languageManager->getCurrentLanguage()->getId();
  63. $email_address = $netforum_account->result->Customer->cst_eml_address_dn;
  64. $customer_key = $netforum_account->result->Customer->cst_key;
  65. $customer_number = $netforum_account->result->Customer->cst_recno;
  66.  
  67. $parts = explode('@', $email_address);
  68. $drupal_username = $parts[0] . $customer_number;
  69. $password = user_password(15);
  70.  
  71. $user->enforceIsNew();
  72. $user->setUsername($drupal_username);
  73. $user->setPassword($password);
  74. $user->setEmail($email_address);
  75. $user->set("init", $email_address);
  76. $user->set("langcode", $langcode);
  77. $user->set("preferred_langcode", $langcode);
  78. $user->set("preferred_admin_langcode", $langcode);
  79. $user->set("preferred_admin_langcode", $langcode);
  80. $user->set("field_netforum_customer_key", $customer_key);
  81. $user->set("field_netforum_customer_recno", $customer_number);
  82. $user->activate();
  83. $user->save();
  84.  
  85. // Set the roles.
  86. $account = User::load($user->id());
  87. $this->updateUserRoles($account, $netforum_account);
  88.  
  89. return $user->id();
  90. }
  91.  
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function updateUser(UserInterface $account, stdClass $netforum_account) {
  96. // @todo: This will be filled in later if they do any profile related stuff.
  97.  
  98. // Update the roles.
  99. $this->updateUserRoles($account, $netforum_account);
  100. }
  101.  
  102. /**
  103. * Wipe and rebuild a users roles to be in-sync with Netforum memberships.
  104. */
  105. public function updateUserRoles(UserInterface $account, $netforum_account) {
  106. $netforum_role = $netforum_account->result->Individual->ind_int_code;
  107. $membership_info = $this->getActiveMembershipsForCustomer($netforum_account);
  108. $member_types = $this->getActiveMemberTypesFromCustomerMembership($membership_info);
  109.  
  110. // Remove previous roles, for security reasons.
  111. $previous_roles = $account->getRoles(TRUE);
  112.  
  113. foreach ($previous_roles as $role) {
  114. // Don't remove Administrator if it was previously set.
  115. if ($role !== 'administrator') {
  116. $account->removeRole($role);
  117. }
  118. }
  119.  
  120. $roles = [];
  121.  
  122. // Staff is an actual user role, not a membership type
  123. if ($netforum_role == 'Staff') {
  124. $roles[] = 'content_editor';
  125. }
  126.  
  127. foreach ($member_types as $role_name) {
  128. $roles[] = $this->mapToDrupalRole($role_name);
  129. }
  130.  
  131. // At the least, we should assign a boilerplate role.
  132. if (empty($roles)) {
  133. $account->addRole('netforum_member');
  134. }
  135.  
  136. foreach ($roles as $role) {
  137. $account->addRole($role);
  138. }
  139.  
  140. $account->save();
  141. }
  142.  
  143. /**
  144. * Maps netforum membership types to local Drupal roles.
  145. */
  146. public function mapToDrupalRole(string $membership_type) : string {
  147. switch (Unicode::strtoupper($membership_type)) {
  148. case 'CARRIER DIVISION':
  149. $role = 'carrier_division';
  150. break;
  151.  
  152. case 'RAIL DIVISION':
  153. $role = 'rail_division';
  154. break;
  155.  
  156. case 'SUPPLIER DIVISION':
  157. $role = 'supplier_division';
  158. break;
  159.  
  160. case 'ASSOCIATE':
  161. $role = 'associate';
  162. break;
  163.  
  164. default:
  165. $role = 'netforum_member';
  166. break;
  167. }
  168.  
  169. return $role;
  170. }
  171.  
  172. /**
  173. * Returns all membership information for a given netforum user.
  174. *
  175. * I really dislike this call because it returns just a string of XML instead
  176. * of the responses we are used to receiving. It is a custom SQL call done by
  177. * using ExecuteMethod, a unique API method in Netforum.
  178. */
  179. public function getActiveMembershipsForCustomer(stdClass $netforum_account) {
  180. $customer_key = $netforum_account->result->Customer->cst_key;
  181.  
  182. $arguments = [
  183. 'serviceName' => 'GetActiveMembershipsForCustomer',
  184. 'methodName' => 'GetActiveMemberships',
  185. 'parameters' => [
  186. 'Parameter' => [
  187. 'Name' => 'cst_key',
  188. 'Value' => $customer_key,
  189. ],
  190. ],
  191. ];
  192.  
  193. $response = $this->client->request('ExecuteMethod', [$arguments]);
  194.  
  195. $parts = explode('-', $customer_key);
  196. $last_segment = end($parts);
  197.  
  198. if ($response->error) {
  199. $message = $response->result->Message . ' Customer key ending in: @key.';
  200. $this->logger->get('netforum_auth')->error($message, ['@key' => $last_segment]);
  201. }
  202. elseif (!$response->result) {
  203. $message = 'Customer key ending in: @key has no membership information.';
  204. $this->logger->get('netforum_auth')->info($message, ['@key' => $last_segment]);
  205. }
  206. else {
  207. // This entire result is returned as an xml string, convert it.
  208. $xml = simplexml_load_string($response->result->any);
  209. $json = json_encode($xml);
  210. $response->result = json_decode($json, TRUE);
  211. }
  212.  
  213. return $response;
  214. }
  215.  
  216. /**
  217. * Filters out active roles from the membership information.
  218. */
  219. public function getActiveMemberTypesFromCustomerMembership($membership_information) : array {
  220. $roles = [];
  221.  
  222. if ($membership_information->result) {
  223. foreach ($membership_information->result['Membership'] as $membership) {
  224. if ($membership['MbrStatus'] == 'Active') {
  225. $roles[] = $membership['MbrType'];
  226. }
  227. }
  228. }
  229.  
  230. return $roles;
  231. }
  232.  
  233. }
  234.  
  235. <?php
  236.  
  237. namespace DrupalTestsnetforum_authKernel;
  238.  
  239. use Drupalnetforum_authNetforumNetforumAccountManager;
  240. use DrupalTeststokenKernelKernelTestBase;
  241. use orgbovigovfsvfsStream;
  242. use orgbovigovfsvisitorvfsStreamStructureVisitor;
  243.  
  244. /**
  245. * Class UserCreateTest
  246. *
  247. * @package DrupalTestsnetforum_authKernel
  248. * @group netforum_auth
  249. */
  250. class UserCreateTest extends KernelTestBase {
  251.  
  252. /**
  253. * Modules to enable.
  254. *
  255. * @var array
  256. */
  257. public static $modules = ['system', 'user', 'netforum_auth'];
  258.  
  259. protected function setUp() {
  260. parent::setUp();
  261.  
  262. // create roles
  263. }
  264.  
  265. public function testUserIsCreated() {
  266. $netforum_wsdl_response = new stdClass();
  267. $netforum_wsdl_response->result = new stdClass();
  268. $netforum_wsdl_response->result->Customer = new stdClass();
  269. $netforum_wsdl_response->result->Customer->cst_eml_address_dn = 'zztesting@example.comzz';
  270. $netforum_wsdl_response->result->Customer->cst_key = '123-456-789';
  271. $netforum_wsdl_response->result->Customer->cst_recno = '123456';
  272.  
  273. $result = new stdClass();
  274. $result->response = [
  275. 'Membership' => [
  276. 0 => [
  277. 'MbrStatus' => 'Active',
  278. 'MbrType' => 'Associate'
  279. ]
  280. ]
  281. ];
  282.  
  283. $prophecy = $this->prophesize(NetforumAccountManager::CLASS);
  284. $prophecy->getActiveMembershipsForCustomer($netforum_wsdl_response)->willReturn($result);
  285. $netforum_account_manager = $prophecy->reveal();
  286.  
  287. $uid = $netforum_account_manager->createUser($netforum_wsdl_response);
  288. // check $uid is a User
  289. // check role(s)
  290. }
  291.  
  292. }
  293.  
  294. Method call:
  295. - createUser(stdClass:00000000584be36000000000303910bf Object (
  296. 'result' => stdClass:00000000584be19c00000000303910bf Object (
  297. 'Customer' => stdClass:00000000584be05a00000000303910bf Object (
  298. 'cst_eml_address_dn' => 'zztesting@example.comzz'
  299. 'cst_key' => '123-456-789'
  300. 'cst_recno' => '123456'
  301. )
  302. )
  303. ))
  304. on DoubleDrupaliana_netforum_authNetforumNetforumAccountManagerP1 was not expected, expected calls were:
  305. - getActiveMembershipsForCustomer(exact(stdClass:00000000584be36000000000303910bf Object (
  306. 'result' => stdClass:00000000584be19c00000000303910bf Object (
  307. 'Customer' => stdClass:00000000584be05a00000000303910bf Object (
  308. 'cst_eml_address_dn' => 'zztesting@example.comzz'
  309. 'cst_key' => '123-456-789'
  310. 'cst_recno' => '123456'
  311. )
  312. )
  313. )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement