login a user programatically.
anjalipv@yahoo.com - November 16, 2008 - 16:40
Hello,
Objective :In my site I want to create a user programatically and make that user login when i submit a content type form .
Current status : I have created a user programatically using user_save, which actually does create user in database and I also added roles to it. Now how do i make that user login , I do have the user name and password values . how to make that user login ?
Urgent help required.
thanks in advance

I wrote a URL login module
I wrote a URL login module for a customer who didn't pay.
I guess I'll open source it shortly if payment isn't received. (It's been many months already so I'm 99% sure I'll open source it around xmas sometime)
Ray Smith
http://RaymondSmith.com
not sure if this function
not sure if this function may help u ...
......user_external_login()
u may find this in user module.
Thanks for the reply.But
Thanks for the reply.
But could not find sucha a function "user_external_login()".(Drupal 5.x)
Here are some functions to help
I created these functions to do exactly what you're looking form. There was talk once about CRUD functions library. But nothing seemed to be able to do this at the time. so here is a breakdown
<?php
// will programmatically create a new user
function create_new_user($username, $pass, $email, $roles = array(), $status = 1){
$array = array();
$array["name"] = $username;
$array["pass"] = $pass;
$array["mail"] = $email;
$array["status"] = $status;
$array["roles"] = $roles;
$userObj = user_save("", $array); // this function is documented in the API
return $userObj;
}
// updates a user based on the $username
function update_user($username, $pass, $email, $roles = array(), $status = 1){
$user_obj = user_load(array('name' => $username));
$array = array();
$array["name"] = $username;
$array["pass"] = $pass;
$array["mail"] = $email;
$array["status"] = $status;
$array["roles"] = $roles;
$userObj = user_save($user_obj, $array);
return $userObj;
}
// this function will log in a user and set him as the current user
function login_user($username, $password){
$form_values = array();
$form_values["name"] = $username;
$form_values["pass"] = $password;
$form_values["op"] = "Log in";
return user_authenticate($form_values); // not sure if this is documented
}
// this function will authenticate a user and return a user Object but won't log him in, meaning it doesn't change the current user
function authenticate_user($username, $password, $hashed = false){
$user_obj = user_load(array('name' => $username, 'pass' => trim($password), 'status' => 1));
return $user_obj;
}
?>
hope this helps
It is not working
Hi,
I am calling your function through webservice which returns all the variables resulting through user_authenticate function but the user was not found logged in when we open the url in other tab
But when we check log reports we found that user session has been created when we called that function.
So please if any one can help me telling the reason why is it so?
Whereas when we directly give link to a page on which we call drupal_execute('user_login', $form_state); function it works.
after doing this when we chnage tab we found the user logged in ?????
but is not working when want it to do through web service.
Cookies
When you say that you are running through a webservice, do you mean in the browser through javascript/AJAX/etc?
If so, I don't think you the server can set the cookies through AJAX. So you won't stay logged in through subsequent calls.
If you are using PHP/server side stuff.. to consume the webservice, you need to implement the cookies manually...
Off the bat for javascript/ajax, I see a few ways to get around this.
1. Have the user authenticate previously through the regular way, I.E. not through a webservice, create a simple log in page, which will redirect him to the page that you want to user to use the webservice on. This set the proper cookies in the browser, and will then send it over to the server in the AJAX calls. Just make sure that the domain names are the same, for the log in page, and the webservice endpoint.
In such a case you won't need to call user_authentication. Downside, is that you can only be logged in as one user at one time.
2. Implement one of the fancy webservice authentication modules such as OAuth, http://drupal.org/project/oauth which is the way to do authentication with webservices, this is how Google and the big boys accomplish authentication. In such a case calling user_authentication won't work... :-(, this process will work for PHP consumption too.
Unlike the previous suggestion, you can be logged in as multiple users if you implement this correctly.
PS> the purpose of the above function, or the reason why I created them initially, I wanted to perform some modification on the system, like, add/remove/update node, If I did it directly with the database or with the superuser I would not have any record of who created the nodes etc. The functions allow me to create a script that will login as a specific user (say autobot) and then perform the changes and drupal will track the user and I can then troubleshoot it later if necessary, or assign specific permissions to that user to limit it's functionality.
All the best.
How to set cookies
Hi,
Thanks yfreeman for your quick reponse
I am actually calling a web service from a .NET Site to php drupal site
The function which the web service calling is defined in a page on root of drupal
You are correct,the cookie not getting set,but how to do that-:
My code is -:
global $user;
global $account;
if ($user->uid) {
//user is already logged in
return services_error(t('Already logged in as !user.', array('!user' => $user->name)));
}
$user = user_authenticate(array('name' => $username, 'pass' => $password));
if ($user->uid) {
session_start();
$return = new stdClass();
$return->sessid = session_id();
$return->user = $user;
setcookie(session_name(), session_id(), 0, '/'); // I added this to make sure the cookie is set
return $return;
}
session_destroy();
return services_error(t('Wrong username or password.'));
But it is not working,session is generating but the cookies content is different,not the session id
How to solve it,I want to do it through web service only,just my job is to return successfull after loging in
Then users in . NET site will just click on the menu in their site and will directly land in drupal site
I can do it by directly giving the link to any page in drupal site and again redirect to .NET site but this is not the correct way to do that
Plz show me the correct way
Thanks
Cookies cont.
If I understand correctly you have a .NET backend communcating with a drupal site, as a webservice (the .NET part)
When drupal sends the cookies over to .NET, they do not get passed over to the browser. Rather .NET will pass it's own set of cookies to the browser.
This is the problem that webservices had when the stated out. So they came out with OAuth. http://en.wikipedia.org/wiki/OAuth . google has something else called ClientLogin Authentication, but it addressed the same principle.
This allows for a separate set of credentials to run side by side, across multiple sites.
Setting the cookie in drupal won't help.
Yeah I know it's complicated. Just the way it works.
The only way I see around it is if you cache the drupal cookies in the .NET code/db (don't send it to the browse) and in the subsequent drupal calls in .NET you set the cookies in the HTTP requests to the ones previously received.
I'm not sure how to do this in .NET, i'm not as proficient
Good luck
Again
I re-read you reply, and I'm still confused about a couple of point.
All you want to do is log the user in to the drupal site., through .NET backend? that's it?
Yes,Only LOGIN
Hi yfreeman,
At present,my only concern is to make the user logged in-:
I was doing this by giving a link from .NET site to page on Drupal site where my code is written.
<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $base_url;
global $user;
$form_state = array();
$form_state['values']['name'] = $_GET['user_name'];
$form_state['values']['pass'] = $_GET['password'];
$form_state['values']['op'] = t('Log In');
drupal_execute('user_login', $form_state);
header('location:http://localhost/blabla');
?>
This is functioning properly,no problem
BUT I DONT WANT THIS
i was told to call this code in a function through web service and the result is as discussed previously "cookie is not set" .
oauth module is something to get the user data to other site and the user is not necessary to be present there.(I think so)
That can be achieved by any way,if security is not a concern.