Hi,
First of all excuse me , my English is not that well
I have a drupal 7 website. This website have one internet part, and one intranet part (only accessible if you are from my Institute).
I'am currently creating a authentication module. I will explain why.
Server 1 : My own website, + other.
Server 2 : a IT server which authenticate student, teacher, ... (i don't have access to this one).
Server X : other website.
actually, on other website, when i need to log in, 2 solution are available :
1) i use a login link on a webpage, i'm redirect to a login page on server 2, i enter my login and password and if it is correct, i'm redirect on my previous website, and i'm logged.
2) I try to go to a secured webpage, and i'm redirect to a login page on server 2, i enter my login and password and if it is correct, i'm redirect on my previous website, and i'm logged. (it's done with the apache conf, some url path can be protected (exemple: www.site.com, www.site.com/node/... are not proteced, but www.site.com/forum/... is protected)).
if i go to an other website, and need to be logged in, i use one of the two ways describe just above.
But, in fact,i'm already connected and known by the server 2, so i don't need to give ma login and password again.
The website, on server 1, or server X can retrieve info on user with a specific cookie. And i can retrieve some user info with a ldap (not all).
actually, i have made a module, with the admin form (which cookie to read, i can ask ldap server info)
My question is:
how to specify page, where my module will be used.
how to create user if not exist with the specified cookie info and ldap info.
how to log in user if user exist.
i do not need a whole code module, just ways to solve my problem, or a already made module, but, i've search several times.
Thank you in advance to any one who may be able to give me some ideas.
Comments
Hi, how to specify page,
Hi,
how to specify page, where my module will be used.
You can use hook_menu to specify a page that you want to use.
how to create user if not exist with the specified cookie info and ldap info.
You can create a function that call user_save, this function allows you to create a new user and fill the user account with the info you got.
how to log in user if user exist.
If you want to login a user and you know the username and password you can use user_authenticate. if you only know the username or uid you can use user_load or user_load_by_name after loading the user you can set the global var $user.
You will get something like this:
<?phpglobal $user;
$user = user_load(2);//2 is the uid(user ID)
?>
By setting the global you will logging the user right away.
<?phpglobal $user;
$user = user_load_by_name('sdariunpsail');
?>
hi, i made this. <?php//
hi,
i made this.
<?php
// $Id$
/**
* Implement hook_help().
*/
function test_login_help($path, $arg) {
switch ($path) {
case 'admin/help#test_login' :
return t('test Login module allows User to log in.');
}
}
/**
* Implement hook_menu().
*/
function test_login_menu() {
$items = array();
$items['admin/config/people/test_login'] = array(
'title' => 'test Login',
'description' => 'Conf module for test_login.',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_login_form'),
'access arguments' => array('administer users'),
'type' => MENU_NORMAL_ITEM,
);
$items['test_login'] = array(
'title' => 'test Login',
'page callback' => '_test_login',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Form builder; Create and display the test_login configuration
* settings form.
*/
function test_login_form($form, &$form_state) {
$form = array();
$form['test_login_cookie_name'] = array(
'#type' => 'textfield',
'#title' => t('cookie name'),
'#default_value' => variable_get('test_login_cookie_name','test_ticket'),
'#description' => t('cookie name to search'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
);
$form['test_login_cookie_id'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('test_login_cookie_id','id'),
'#title' => t('term to search'),
'#description' => t('term to search in cookie'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
/**
* Save configuration settings for test_login module.
*/
function test_login_form_submit($form, &$form_state) {
variable_set('test_login_cookie_name',$form_state['values']['test_login_cookie_name']);
variable_set('test_login_cookie_id', $form_state['values']['test_login_cookie_id']);
drupal_set_message(t('The settings have been saved'));
}
function _test_login($ghi = 0, $jkl = '') {
$cookiename = variable_get('user_login_cookie_name') ;
$cookieitemtofind = variable_get('user_login_cookie_id');
$usertestid = "";
$cookie = NULL;
if( isset( $_COOKIE[$cookiename] ) )
{
$cookie = $_COOKIE[$cookiename];
}
debug($cookie);
if(!empty($cookie)){
$cookie = preg_split('/&/',$cookie);
for($i=0; $i<count($cookie); $i++){
$num = $i+1;
if($cookie[$i] == $cookieitemtofind){
if($cookie[$num]){
$usertestid = $cookie[$num];
}
}
}
}
if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $usertestid))) {
// User doesn't exist
$new_user = getLdapInfo($usertestid);
if($new_user != null)
{
$account = user_save(null, $new_user);
}
}
else
{
global $user;
$user = user_load_by_name($usertestid);
}
}
function getLdapInfo($name)
{
if(isset($surname)){
$user = "id=xxxxxxx,ou=SpecialUsers,dc=test,dc=en";
$pass = "XXXXXXXX";
$filter = "(&(id=$name))";
$new_user = null;
//connection ldap
$ds = ldap_connect("ldap://ldap.test.en");
if($ds){
$r = ldap_bind($ds,$user,$pass);
//recherche
$sr = ldap_search($ds,"ou=People,dc=test,dc=en",$filter);
$info = ldap_get_entries($ds, $sr);
if(count($info)>1)
{
$new_user = array(
'name' => $info[0]["sn"][0],
'firstname' => $info[0]["givenname"][0],
'pass' => 'maxellultrium1',
'mail' => $info[0]["mail"][0],
'init' => $info[0]["mail"][0],
'status' => 1,
);
}
ldap_close($ds);
return $new_user;
}
}
}
At first, i got a page not found. (seems legit so i've create a page with 'test_login' alias).
but now, when i got to test_login page, noting append. No user are created, nothing.
Am i doing something wrong.
Place a return in
Place a return in _test_login, drupal expects a page to be returnend.
<?phpreturn "some var here";
?>
when you placed the return with a string, and no page is show, try to clear the cache, found onder preformance in the admin. Drupal menu has a hard cache which sometimes need to be cleared to get it to work.
when you get a page back, but the code doesn't work, try entering some var_dump($value); to see if the all the values match that you expect.