Advertisement
matthewpoer

Drupal Testing

Sep 11th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.60 KB | None | 0 0
  1. <?php
  2. /**
  3.  * An implementation of hook_form_FORM_ID_alter()
  4.  * Only allow this once the users table and user admin name are set.
  5.  * @param $form array
  6.  * @param $form_state
  7.  */
  8. function sugarcrm_users_form_user_login_alter(&$form, &$form_state) {
  9.     die('got here!');
  10.     if (variable_get('sugarcrm_users_sugar_url', false) && variable_get('sugarcrm_users_drupal_admin', false)) {
  11.         $form['#validate'][1] = 'sugarcrm_users_authenticate_validate';
  12.     }
  13. }
  14.  
  15. /**
  16.  * Validate the user in SugarCRM, or if it's the Drupal admin, use normal Drupal stuffs
  17.  * @param $form
  18.  * @param $form_state
  19.  */
  20. function sugarcrm_users_authenticate_validate($form, &$form_state) {
  21.     if (drupal_strtolower($form_state['values']['name']) == drupal_strtolower(variable_get('sugarcrm_users_drupal_admin', ''))) {
  22.         user_login_authenticate_validate($form, $form_state);
  23.     } else {
  24.         $username = _sugarcrm_users_check_user($form_state['values']['name'], $form_state['values']['pass']);
  25.         if ($username != false) {
  26.             user_external_login_register($username, 'sugarcrm_users');
  27.             $account = user_external_load($username);
  28.             $form_state['uid'] = $account->uid;
  29.         } // else drop through to the end and return nothing - Drupal will handle the rejection for us.
  30.     }
  31. }
  32.  
  33. /**
  34.  * Authenticate the user in the MediaWiki users DB based on username or email.
  35.  * Utilize the SugarCRM API for this one
  36.  * @param $username string
  37.  * @param $password string
  38.  * @return bool success is true
  39.  */
  40. function _sugarcrm_users_check_user($username, $password) {
  41.     $user_information = _sugarcrm_users_sugar_login($username,$password);
  42.  
  43.     if(isset($user_information->number)){
  44.         return false;
  45.     } elseif(isset($user_information->name_value_list->user_name->value)) {
  46.         return $user_information->name_value_list->user_name->value;
  47.     } else {
  48.         return false;
  49.     }
  50. }
  51.  
  52. /**
  53.  * Implementation of hook_user_insert().
  54.  * Used to copy the user email from SugarCRM to Drupal.
  55.  * @param $edit
  56.  * @param $account
  57.  * @param null $category
  58.  * @return bool success
  59.  */
  60. function sugarcrm_users_user_insert(&$edit, &$account, $category = null) {
  61.     if (empty($account->mail) && variable_get('sugarcrm_users_sugar_url', false)) {
  62.         $user_information = _sugarcrm_users_sugar_login();
  63.  
  64.         // if we failed to authenticate or something
  65.         if(!isset($user_information->id)){
  66.             return false;
  67.         }
  68.  
  69.         $session_id = $user_information->id;
  70.  
  71.         $user_email_data = array(
  72.             'session' => $session_id,
  73.             'module_name' => 'Users',
  74.             'query' => " id = '{$user_information->name_value_list->user_id->value}' ",
  75.             'order_by' => 'last_name',
  76.             'offset' => "0",
  77.             'select_fields' => array('id','email1'),
  78.             'link_name_to_fields_array' => '',
  79.             'max_results' => "5",
  80.             'deleted' => 0,
  81.             'favorites' => 0,
  82.         );
  83.  
  84.         $user_email = _sugarcrm_users_api_query($url,'get_entry_list',$user_email_data);
  85.         if(isset($user_email->entry_list[0]->name_value_list->email1->value)){
  86.             $email = $user_email->entry_list[0]->name_value_list->email1->value;
  87.         } else {
  88.             $email = "";
  89.         }
  90.  
  91.         if (valid_email_address($email)) {
  92.             db_update('users')
  93.                 ->fields(array('mail' => $email))
  94.                 ->condition('uid', $account->uid, '=')
  95.                 ->execute();
  96.         }
  97.     }
  98. }
  99.  
  100. function _sugarcrm_users_sugar_login($username = NULL, $password = NULL){
  101.     if(is_null($username)){
  102.         variable_get('sugarcrm_users_sugar_username', false);
  103.     }
  104.     if(is_null($password)){
  105.         variable_get('sugarcrm_users_sugar_password', false);
  106.     }
  107.  
  108.     $login_data = array(
  109.         'user_auth'=>array(
  110.             'user_name'=>$username,
  111.             'password'=>md5($password),
  112.             'version'=>'.01'),
  113.         'application_name'=>'SoapTest',
  114.         'name_value_list' => array(
  115.             array(
  116.                 'name' => 'notifyonsave',
  117.                 'value' => 'false')
  118.         )
  119.     );
  120.     return _sugarcrm_users_api_query('login',$login_data);
  121. }
  122.  
  123. /**
  124.  * Handle the API calls to SugarCRM
  125.  * @param $method
  126.  * @param $data
  127.  * @return mixed
  128.  */
  129. function _sugarcrm_users_api_query($method, $data) {
  130.     // setup URL
  131.     $url = drupal_strtolower(variable_get('sugarcrm_users_sugar_url', ''));
  132.     $url .= 'service/v2/rest.php';
  133.  
  134.     // create headers
  135.     $headers = (function_exists('getallheaders')) ? getallheaders(): array();
  136.     $_headers = array();
  137.     foreach($headers as $k=>$v){
  138.         $_headers[strtolower($k)] = $v;
  139.     }
  140.  
  141.     // setup cURL
  142.     $ch = curl_init();
  143.     curl_setopt($ch, CURLOPT_URL, $url);
  144.     curl_setopt($ch, CURLOPT_POST, 1);
  145.     curl_setopt($ch, CURLOPT_HTTPHEADER, $_headers);
  146.     curl_setopt($ch, CURLOPT_HEADER, 1);
  147.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  148.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  149.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  150.     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
  151.  
  152.     // setup postdata, finish and execute cURL
  153.     $post_data = 'method=' . $method . '&input_type=json&response_type=json';
  154.     $jsonEncodedData = json_encode($data);
  155.     $post_data = $post_data . "&rest_data=" . $jsonEncodedData;
  156.     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  157.     $result = curl_exec($ch);
  158.     curl_close($ch);
  159.  
  160.     // process the results
  161.     $result = explode("\r\n\r\n", $result, 2);
  162.     $response_data = json_decode($result[1]);
  163.     return $response_data;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement