Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function mymodule_form_alter(&$form, $form_state, $form_id) {
- if ($form_id == 'user_login_block' || $form_id == 'user_login') {
- $form['#validate'] = array('user_login_name_validate', 'mymodule_login_validate', 'user_login_final_validate');
- }
- }
- function mymodule_login_validate( $form, &$form_state )
- {
- global $user;
- $username = $form_state['values']['name'];
- $password = $form_state['values']['pass'];
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,"http://localhost/MYAPI/oauth/authenticate");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS,"username=".$username."&grant_type=password");
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","Authorization:Basic xxxxxxxxxxxxxxx"));
- // receive server response ...
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $server_output = curl_exec ($ch);
- curl_close ($ch);
- $postData = drupal_json_decode($server_output);
- $count = db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' =>$username))->fetchField();
- if($count == 0 )
- {
- if (!empty($postData['access_token']))
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,"http://localhost/MYAPI/rest/user/1/get_user_details");
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded","Authorization:Bearer ".$postData['access_token']));
- // receive server response ...
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $server_output = curl_exec ($ch);
- curl_close ($ch);
- $postData = drupal_json_decode($server_output);
- $resRoleArr = $postData['userDetail']['role'];
- $resMail = $postData['userDetail']['email'];
- $roleArr = user_roles();
- $common_roles = array_intersect($resRoleArr,$roleArr);
- if (count($common_roles) > 0) {
- user_external_login_register($username, 'mymodule');
- $user_fields = [
- 'pass' => $password,
- 'status' => 1,
- 'mail' => $resMail,
- 'init' => $resMail,
- 'roles' =>
- [DRUPAL_AUTHENTICATED_RID => 'authenticated user'],
- ];
- user_save((object) ['uid' => $user->uid], (array) $user_fields);
- $account = user_external_load($username);
- $form_state['uid'] = $account->uid;
- if ($role = user_role_load_by_name($common_roles)) {
- user_multiple_role_edit(array($user->uid), 'add_role', $role->rid);
- }
- }
- }
- }
- else {
- user_login_authenticate_validate($form,$form_state);
- }
- }
- user_external_login_register($username, 'mymodule');
- user_login_authenticate_validate($form,$form_state);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement