Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. function mymodule_form_alter(&$form, $form_state, $form_id) {
  2.  
  3. if ($form_id == 'user_login_block' || $form_id == 'user_login') {
  4.  
  5. $form['#validate'] = array('user_login_name_validate', 'mymodule_login_validate', 'user_login_final_validate');
  6.  
  7. }
  8. }
  9.  
  10. function mymodule_login_validate( $form, &$form_state )
  11. {
  12. global $user;
  13.  
  14.  
  15. $username = $form_state['values']['name'];
  16. $password = $form_state['values']['pass'];
  17.  
  18.  
  19. $ch = curl_init();
  20.  
  21. curl_setopt($ch,CURLOPT_URL,"http://localhost/MYAPI/oauth/authenticate");
  22.  
  23. curl_setopt($ch, CURLOPT_POST, 1);
  24. curl_setopt($ch, CURLOPT_POSTFIELDS,"username=".$username."&grant_type=password");
  25. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","Authorization:Basic xxxxxxxxxxxxxxx"));
  26.  
  27.  
  28. // receive server response ...
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30.  
  31. $server_output = curl_exec ($ch);
  32.  
  33. curl_close ($ch);
  34.  
  35.  
  36. $postData = drupal_json_decode($server_output);
  37.  
  38. $count = db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' =>$username))->fetchField();
  39.  
  40. if($count == 0 )
  41. {
  42. if (!empty($postData['access_token']))
  43. {
  44.  
  45. $ch = curl_init();
  46.  
  47. curl_setopt($ch, CURLOPT_URL,"http://localhost/MYAPI/rest/user/1/get_user_details");
  48. curl_setopt($ch, CURLOPT_POST, 1);
  49. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","Authorization:Bearer ".$postData['access_token']));
  50.  
  51.  
  52. // receive server response ...
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54.  
  55. $server_output = curl_exec ($ch);
  56.  
  57. curl_close ($ch);
  58.  
  59. $postData = drupal_json_decode($server_output);
  60.  
  61.  
  62. $resRoleArr = $postData['userDetail']['role'];
  63. $resMail = $postData['userDetail']['email'];
  64.  
  65. $roleArr = user_roles();
  66.  
  67. $common_roles = array_intersect($resRoleArr,$roleArr);
  68.  
  69.  
  70. if (count($common_roles) > 0) {
  71.  
  72. user_external_login_register($username, 'mymodule');
  73.  
  74. $user_fields = [
  75. 'pass' => $password,
  76. 'status' => 1,
  77. 'mail' => $resMail,
  78. 'init' => $resMail,
  79. 'roles' =>
  80. [DRUPAL_AUTHENTICATED_RID => 'authenticated user'],
  81.  
  82. ];
  83.  
  84.  
  85. user_save((object) ['uid' => $user->uid], (array) $user_fields);
  86.  
  87.  
  88.  
  89. $account = user_external_load($username);
  90. $form_state['uid'] = $account->uid;
  91.  
  92. if ($role = user_role_load_by_name($common_roles)) {
  93. user_multiple_role_edit(array($user->uid), 'add_role', $role->rid);
  94. }
  95.  
  96.  
  97.  
  98. }
  99. }
  100. }
  101. else {
  102.  
  103. user_login_authenticate_validate($form,$form_state);
  104.  
  105. }
  106.  
  107. }
  108.  
  109. user_external_login_register($username, 'mymodule');
  110.  
  111. user_login_authenticate_validate($form,$form_state);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement