Page MenuHomeGRNET

grnet_ldap_interconnection.module
No OneTemporary

File Metadata

Created
Sat, Mar 21, 3:13 AM

grnet_ldap_interconnection.module

<?php
function grnet_ldap_interconnection_menu() {
$items = array();
$items['admin/config/grnet/ldap'] = array(
'title' => 'grnet Ldap settings',
'description' => 'Configure grnet Ldap Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('grnet_ldap_interconnection_admin_settings'),
'access arguments' => array('administer cas'),
'type' => MENU_NORMAL_ITEM,
'file' => 'grnet_ldap_interconnection.admin.inc',
);
$items['admin/config/grnet/ldap/settings'] = array(
'title' => 'grnet Ldap',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['request/alterinfo/delete/%'] = array(
'title' => 'Request Request for Change Delete',
'page callback' => '_request_alter_info_delete',
'page arguments' => array(3),
/*'access callback' => 'user_access',*/
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function _request_alter_info_delete($secretkey){
if($secretkey<>''){
$entity_id = grnet_core_get_entity_id_by_secretkey($secretkey);
}
if($entity_id>0){
$entity_instance = entity_load('entityform', array($entity_id));
$entity_instance = $entity_instance[$entity_id];
$entity_instance = _ldap_user_rollback_wrapper($entity_instance,'');
watchdog('grnet ldap interconnection', 'Ldap user record rolled back succesfully<br>
<b>Name/Surname :</b> '.$entity_instance->field_ldapregistr_name['und']['0']['value'].' '.$entity_instance->field_ldapregistr_surname['und']['0']['value'], NULL , WATCHDOG_INFO);
$entity_instance->save();
}
drupal_goto("/request/alterinfo");
}
function grnet_ldap_interconnection_entity_presave($entity, $type) {
if(grnet_core_user_has_role('ldapuser') && $entity->type=='ldapregistration'){
if($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='completed' ||
$_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='declined' ||
$_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='accepted' ||
$_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='accepted_minor_fixes'
){
$entity_loaded = $_SESSION['grnet']['ldapregistration'][$entity->entityform_id];
$entity_presave = $entity;
if(grnet_core_entity_has_changed($entity_loaded, $entity_presave)){
$entity->field_ldapregistr_status['und']['0']['value']='request_for_change';
}else{
}
}
}
if( (grnet_core_user_has_role('manager') || grnet_core_user_has_role('administrator')) &&
$entity->type=='ldapregistration'
){
if($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='request_for_change' &&
$entity->field_ldapregistr_status['und']['0']['value']=='declined'
){
unset($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]);
if($entity->field_ldapregistr_hidden_message['und']['0']['value']<>''){
$manager_comments = '| Σχόλια Διαχειριστή : '.$entity->field_ldapregistr_hidden_message['und']['0']['value'];
}
$entity->field_ldapregistr_hidden_message['und']['0']['value'] = 'Η αίτηση σας απορρίφθηκε.'.$manager_comments;
_ldap_user_rollback_wrapper($entity,$type);
}
if($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='request_for_change' &&
$entity->field_ldapregistr_status['und']['0']['value']=='accepted'
){
if($entity->field_ldapregistr_hidden_message['und']['0']['value']<>''){
$manager_comments = '| Σχόλια Διαχειριστή : '.$entity->field_ldapregistr_hidden_message['und']['0']['value'];
}
$entity->field_ldapregistr_hidden_message['und']['0']['value'] = 'Η αίτηση σας ενεκρίθη.'.$manager_comments;
}
if($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='request_for_change' &&
$entity->field_ldapregistr_status['und']['0']['value']=='accepted_minor_fixes'
){
if($entity->field_ldapregistr_hidden_message['und']['0']['value']<>''){
$manager_comments = '| Σχόλια Διαχειριστή : '.$entity->field_ldapregistr_hidden_message['und']['0']['value'];
}
$entity->field_ldapregistr_hidden_message['und']['0']['value'] = 'Η αίτηση σας ενεκρίθη με μικρές αλλαγές.'.$manager_comments;
}
}
}
function grnet_ldap_interconnection_entity_insert($entity, $type) {
if(grnet_core_user_has_role('administrator') &&
$type=='taxonomy_term' &&
($entity->vocabulary_machine_name='iatr_tmimata_monades_ana_tomea' ||
$entity->vocabulary_machine_name='iatr_tmimata_diatomeakou_epipedou' ||
$entity->vocabulary_machine_name='iatr_epikourika_loipa' ||
$entity->vocabulary_machine_name='dioik_tmimata_grafeia' ||
$entity->vocabulary_machine_name='specialities'
)
){
_ldap_unit_create_wrapper($entity,$type);
}
}
function grnet_ldap_interconnection_entity_update($entity, $type) {
if( (grnet_core_user_has_role('manager') || grnet_core_user_has_role('administrator')) &&
$entity->type=='ldapregistration' &&
( ($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='prepending' ||
$_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='pending'
) &&
$entity->field_ldapregistr_status['und']['0']['value']=='completed'
)
){
unset($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]);
_ldap_user_create_wrapper($entity,$type);
}
if( (grnet_core_user_has_role('manager') || grnet_core_user_has_role('administrator')) &&
$entity->type=='ldapregistration' &&
($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]->field_ldapregistr_status['und']['0']['value']=='request_for_change' &&
($entity->field_ldapregistr_status['und']['0']['value']=='accepted' ||
$entity->field_ldapregistr_status['und']['0']['value']=='accepted_minor_fixes'
)
)
){
unset($_SESSION['grnet']['ldapregistration'][$entity->entityform_id]);
_ldap_user_update_wrapper($entity,$type);
}
if( (grnet_core_user_has_role('manager') || grnet_core_user_has_role('administrator')) &&
$type=='taxonomy_term' &&
($entity->vocabulary_machine_name='iatr_tmimata_monades_ana_tomea' ||
$entity->vocabulary_machine_name='iatr_tmimata_diatomeakou_epipedou' ||
$entity->vocabulary_machine_name='iatr_epikourika_loipa' ||
$entity->vocabulary_machine_name='dioik_tmimata_grafeia' ||
$entity->vocabulary_machine_name='specialities')
){
_ldap_unit_update_wrapper($entity,$type);
}
}
function grnet_ldap_interconnection_entity_delete($entity_type, $id) {
if(grnet_core_user_has_role('administrator') && $id=='entityform' &&
$entity_type->type='ldapregistration' && $entity_type->field_ldapregistr_username['und'][0]['value']<>''){
_ldap_user_delete_wrapper($entity_type);
}
if(grnet_core_user_has_role('administrator') &&
$id=='taxonomy_term'
){
_ldap_unit_delete_wrapper($entity_type);
}
}
function _ldap_user_create_wrapper($entity, $type){
$ldapbasedn = variable_get('grnet_ldapbasedn');
$distinguishedName = '';
$dicomReferringPhysiciansName = '';
$description = '';
$seealso = '';
$proistamenos = '';
$eduPersonEntitlement = '';
if($entity->field_ldapregistr_personel_categ['und']['0']['value']=='medical'){
if($entity->field_ldapregistr_speciality['und']['0']['tid']>0){
$distinguishedName = 'uniqueIdentifier='.$entity->field_ldapregistr_speciality['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}
if($entity->field_ldapregistr_dicom['und']['0']['value']<>''){
$dicomReferringPhysiciansName = $entity->field_ldapregistr_dicom['und']['0']['value'];
}
$description = grnet_core_convert($entity->field_ldapregistr_personel_type['und']['0']['value']);
if($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_tmimata_monades_ana_tomea'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_tmim_mon_tomea['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}elseif($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_tmimata_diatomeakou_epipedou'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_tmim_diato['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}elseif($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_epikourika_loipa'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_epikour_loipa['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}
if($entity->field_ldapregistr_iatr_proist['und']['0']['value']=='1'){
$proistamenos = 'manager';
}
}elseif($entity->field_ldapregistr_personel_categ['und']['0']['value']=='administrative'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_dioik_tmimgraf['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
if($entity->field_ldapregistr_admin_proist['und']['0']['value']=='1'){
$proistamenos = 'manager';
}
}elseif($entity->field_ldapregistr_personel_categ['und']['0']['value']=='nursery'){
}
try{
$userinfo = array('objectClass' => array( 'top',
'extensibleObject',
'extendedAuthentication',
'grHealthPerson',
'inetOrgPerson'),
'cn'=>$entity->field_ldapregistr_name['und']['0']['value'].' '.$entity->field_ldapregistr_surname['und']['0']['value'],
'sn' => $entity->field_ldapregistr_surname['und']['0']['value'],
'schacPersonalUniqueID'=>$entity->field_ldapregistr_amka['und']['0']['value'],
'employeetype'=>grnet_core_convert($entity->field_ldapregistr_personel_categ['und']['0']['value']),
'givenName'=> $entity->field_ldapregistr_name['und']['0']['value'],
'mail'=>$entity->field_ldapregistr_email['und']['0']['email'],
'uid'=>$entity->field_ldapregistr_username['und']['0']['value']
);
if($entity->field_ldapregistr_mobile['und']['0']['value']<>''){
$userinfo['mobile'] = $entity->field_ldapregistr_mobile['und']['0']['value'];
}
if($entity->field_ldapregistr_phone['und']['0']['value']<>''){
$userinfo['telephoneNumber'] = $entity->field_ldapregistr_phone['und']['0']['value'];
}
if($entity->field_ldapregistr_dias_roles['und']['0']['tid']>0){
$userinfo['eduPersonEntitlement'] = grnet_core_conver_dias_roles($entity->field_ldapregistr_dias_roles['und']['0']['tid']);
}
if($entity->field_ldapregistr_personel_categ['und']['0']['value']=='medical'){
if($distinguishedName<>''){
$userinfo['distinguishedName'] = $distinguishedName;
}
if($dicomReferringPhysiciansName<>''){
$userinfo['dicomReferringPhysiciansName'] = $dicomReferringPhysiciansName;
}
if($proistamenos<>''){
$userinfo = array_merge($userinfo , array( 'title'=>$proistamenos));
}
$userinfo = array_merge($userinfo , array( 'description'=> $description,
'seealso'=>$seealso
)
);
}elseif($entity->field_ldapregistr_personel_categ['und']['0']['value']=='administrative'){
if($proistamenos<>''){
$userinfo = array_merge($userinfo , array( 'title'=>$proistamenos));
}
$userinfo = array_merge($userinfo , array( 'seealso'=>$seealso));
}
if(!_ldap_user_exists(array("uid" => $entity->field_ldapregistr_username['und']['0']['value']))){
$output = _ldap_user_create($userinfo,$entity->field_ldapregistr_pwd['und']['0']['password_field']['password_field']);
if($output==1){
watchdog('grnet ldap interconnection', 'Ldap user record created succesfully<br>
<b>Name/Surname :</b> '.$entity->field_ldapregistr_name['und']['0']['value'].' '.$entity->field_ldapregistr_surname['und']['0']['value'], NULL , WATCHDOG_INFO);
drupal_set_message(t('Ldap user record created succesfully!!'));
}
}else{
}
}catch (exception $e){
var_dump($e);
}
}
function grnet_ldap_interconnection_unit_read_wrapper($username , $attributes_array){
return _ldap_user_read_wrapper($username , $attributes_array);
}
function _ldap_user_read_wrapper($username , $attributes_array=null){
$ldapbasedn = variable_get('grnet_ldapbasedn');
try{
return _ldap_user_read($username , $attributes_array);
}catch (exception $e){
var_dump($e);
}
}
function _ldap_user_update_wrapper($entity, $type){
$ldapbasedn = variable_get('grnet_ldapbasedn');
$mobile = array();
$telephoneNumber = array();
$distinguishedName = array();
$dicomReferringPhysiciansName = array();
$description = array();
$seealso = array();
$proistamenos = array();
$eduPersonEntitlement = array();
if($entity->field_ldapregistr_mobile['und']['0']['value']<>''){
$mobile = $entity->field_ldapregistr_mobile['und']['0']['value'];
}
if($entity->field_ldapregistr_phone['und']['0']['value']<>''){
$telephoneNumber = $entity->field_ldapregistr_phone['und']['0']['value'];
}
if($entity->field_ldapregistr_dias_roles['und']['0']['tid']>0){
$eduPersonEntitlement = grnet_core_conver_dias_roles($entity->field_ldapregistr_dias_roles['und']['0']['tid']);
}
if($entity->field_ldapregistr_personel_categ['und']['0']['value']=='medical'){
if($entity->field_ldapregistr_speciality['und']['0']['tid']>0){
$distinguishedName = 'uniqueIdentifier='.$entity->field_ldapregistr_speciality['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}
if($entity->field_ldapregistr_dicom['und']['0']['value']<>''){
$dicomReferringPhysiciansName = $entity->field_ldapregistr_dicom['und']['0']['value'];
}
$description = grnet_core_convert($entity->field_ldapregistr_personel_type['und']['0']['value']);
if($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_tmimata_monades_ana_tomea'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_tmim_mon_tomea['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}elseif($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_tmimata_diatomeakou_epipedou'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_tmim_diato['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}elseif($entity->field_ldapregistr_iatr_top_mon['und']['0']['value']=='iatr_epikourika_loipa'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_epikour_loipa['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
}
if($entity->field_ldapregistr_iatr_proist['und']['0']['value']=='1'){
$proistamenos = 'manager';
}
}elseif($entity->field_ldapregistr_personel_categ['und']['0']['value']=='administrative'){
$seealso = 'uniqueIdentifier='.$entity->field_ldapregistr_dioik_tmimgraf['und']['0']['tid'].',ou=Units,'.$ldapbasedn;
if($entity->field_ldapregistr_admin_proist['und']['0']['value']=='1'){
$proistamenos = 'manager';
}
}elseif($entity->field_ldapregistr_personel_categ['und']['0']['value']=='nursery'){
}
try{
$userinfo = array(
'cn'=>$entity->field_ldapregistr_name['und']['0']['value'].' '.$entity->field_ldapregistr_surname['und']['0']['value'],
'sn' => $entity->field_ldapregistr_surname['und']['0']['value'],
'schacPersonalUniqueID'=>$entity->field_ldapregistr_amka['und']['0']['value'],
'employeetype'=>grnet_core_convert($entity->field_ldapregistr_personel_categ['und']['0']['value']),
'givenName'=> $entity->field_ldapregistr_name['und']['0']['value'],
'mail'=>$entity->field_ldapregistr_email['und']['0']['email'],
'mobile'=>$mobile,
'telephoneNumber'=>$telephoneNumber,
'eduPersonEntitlement'=>grnet_core_conver_dias_roles($entity->field_ldapregistr_dias_roles['und']['0']['tid']),
'uid'=>$entity->field_ldapregistr_username['und']['0']['value'],
'eduPersonEntitlement'=>$eduPersonEntitlement,
'dicomReferringPhysiciansName'=>$dicomReferringPhysiciansName,
'description'=> $description,
'distinguishedName'=>$distinguishedName,
'seealso'=>$seealso
);
if($proistamenos<>''){
$userinfo = array_merge($userinfo , array( 'title'=>$proistamenos));
}
if(_ldap_user_exists(array("uid" => $entity->field_ldapregistr_username['und']['0']['value']))){
$output = _ldap_user_update($userinfo);
if($output==1){
watchdog('grnet ldap interconnection', 'Ldap user record updated succesfully<br>
<b>Name/Surname :</b> '.$entity->field_ldapregistr_name['und']['0']['value'].' '.$entity->field_ldapregistr_surname['und']['0']['value'], NULL , WATCHDOG_INFO);
drupal_set_message(t('Ldap user record updated succesfully!!'));
}
}else{
}
}catch (exception $e){
var_dump($e);
}
}
function _ldap_user_delete_wrapper($entity_type){
if(grnet_core_user_has_role('administrator')){
try{
if(_ldap_user_exists(array("uid" => $entity_type->field_ldapregistr_username['und']['0']['value']))){
$output = _ldap_user_delete($entity_type->field_ldapregistr_username['und']['0']['value']);
if($output==1){
watchdog('grnet ldap interconnection', 'Ldap user record deleted succesfully<br>
<b>username :</b> '.$entity_type->field_ldapregistr_username['und']['0']['value'], NULL , WATCHDOG_INFO);
drupal_set_message(t("Ldap User record deleted succesfully!!"));
}
}else{
}
}catch (exception $e){
var_dump($e);
}
}else{
}
}
/**
* Method to rollback changes from a "request for change" an ldapuser ask for
* and a manager declined
* Technically..drupal database will be updated from the values that are stored in ldap
* (in a accepted or accepted with minor changes...sittuation...we have the oposite... The ldap is getting update from the values that are stored in drupal database)
*
* @access private
* @param object $entity
* @param string $type
* @return void
*/
function _ldap_user_rollback_wrapper($entity, $type){
$entity->field_ldapregistr_status['und']['0']['value']='declined';
$entity->field_ldapregistr_iatr_top_mon['und'] = array();
$entity->field_ldapregistr_dicom['und'] = array();
$entity->field_ldapregistr_personel_type['und'] = array();
$entity->field_ldapregistr_admin_proist['und']['0']['value']='0';
$entity->field_ldapregistr_iatr_proist['und']['0']['value']='0';
$entity->field_ldapregistr_speciality['und'] = array();
$entity->field_ldapregistr_tmim_mon_tomea = array();
$entity->field_ldapregistr_tmim_diato['und'] = array();
$entity->field_ldapregistr_epikour_loipa['und'] = array();
$entity->field_ldapregistr_dioik_tmimgraf = array();
$entity->field_ldapregistr_dias_roles['und'] = array();
$username = grnet_core_get_ldapinfo_user();
$ldapinfo_user = grnet_ldap_interconnection_unit_read_wrapper($username);
$entity->field_ldapregistr_name['und']['0']['value'] = $ldapinfo_user["givenname"][0];
$entity->field_ldapregistr_surname['und']['0']['value'] = $ldapinfo_user["sn"][0];
$entity->field_ldapregistr_amka['und']['0']['value'] = $ldapinfo_user["schacpersonaluniqueid"][0];
$entity->field_ldapregistr_email['und']['0']['email'] = $ldapinfo_user["mail"][0];
$entity->field_ldapregistr_phone['und']['0']['value'] = $ldapinfo_user["telephonenumber"][0];
$entity->field_ldapregistr_mobile['und']['0']['value'] = $ldapinfo_user["mobile"][0];
$entity->field_ldapregistr_personel_categ['und']['0']['value'] = grnet_core_convert($ldapinfo_user["employeetype"][0],TRUE);
if($ldapinfo_user["edupersonentitlement"][0] <> ''){
$entity->field_ldapregistr_dias_roles['und']['0']['tid'] = grnet_core_conver_dias_roles_revert_to_tid($ldapinfo_user["edupersonentitlement"][0]);
}
if(grnet_core_convert($ldapinfo_user["employeetype"][0],TRUE)=='medical'){
if($ldapinfo_user["seealso"][0]<>''){
$entity->field_ldapregistr_iatr_top_mon['und']['0']['value'] = taxonomy_vocabulary_load(grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->vid)->machine_name;
}
if($ldapinfo_user["distinguishedname"][0]<>''){
$entity->field_ldapregistr_speciality['und']['0']['tid'] = grnet_core_get_term_by_rdn($ldapinfo_user["distinguishedname"][0])->tid;
}
if($ldapinfo_user["dicomreferringphysiciansname"][0]<>''){
$entity->field_ldapregistr_dicom['und']['0']['value'] = $ldapinfo_user["dicomreferringphysiciansname"][0];
}
if($ldapinfo_user["title"][0]=='manager'){
$entity->field_ldapregistr_iatr_proist['und']['0']['value']='1';
}else{
$entity->field_ldapregistr_iatr_proist['und']['0']['value']='0';
}
$entity->field_ldapregistr_personel_type['und']['0']['value'] = grnet_core_convert($ldapinfo_user["description"][0],TRUE);
switch (grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->vid){
case 2:
$entity->field_ldapregistr_tmim_mon_tomea['und']['0']['tid'] = grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->tid;
break;
case 4:
$entity->field_ldapregistr_tmim_diato['und']['0']['tid'] = grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->tid;
break;
case 5:
$entity->field_ldapregistr_epikour_loipa['und']['0']['tid'] = grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->tid;
break;
}
}elseif(grnet_core_convert($ldapinfo_user["employeetype"][0],TRUE)=='administrative'){
if($ldapinfo_user["title"][0]=='manager'){
$entity->field_ldapregistr_admin_proist['und']['0']['value']='1';
}else{
$entity->field_ldapregistr_admin_proist['und']['0']['value']='0';
}
$entity->field_ldapregistr_dioik_tmimgraf['und']['0']['tid'] = grnet_core_get_term_by_rdn($ldapinfo_user["seealso"][0])->tid;
}
return $entity;
}
function grnet_ldap_interconnection_ldap_user_exists_wrapper($attributes){
return _ldap_user_exists($attributes);
}
function _ldap_user_create($user,$password) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
$res=false;
try{
$conn = ldap_connect($ldaphost,$ldapport) or die("Could not connect to server");
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$r = ldap_bind($conn,$ldapbinddn,$ldappass) or die("Could not bind to server");
$rdn = "uid=".$user['uid'].",$ldapbasedn";
module_load_include('module', 'password_field', 'password_field');
$user['userpassword']="{SHA}" . base64_encode( pack( "H*", sha1( password_field_decrypt($password) ) ) );
$res=ldap_add($conn, $rdn, $user);
ldap_close($conn);
return $res;
} catch(exception $e) {
var_dump($e);
}
}
function _ldap_user_read($username , $attributes_array=null){
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
$filter = "(uid=$username)";
try{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
if(count($attributes_array) > 0){
}else{
}
$info = ldap_get_entries($conn, $SearchResult);
if($info['count']>0) {
return $info[0];
}else{
return NULL;
}
}catch(Exception $e)
{
$log->error($e->getMessage());
return false;
}
}
function _ldap_user_update($userinfo) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
try{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$rdn = "uid=".$userinfo['uid'].",$ldapbasedn";
ldap_close($conn);
return $ldap_modify_message;
} catch(exception $e) {
var_dump($e);
}
}
function _ldap_user_delete($username) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
$res=false;
try{
$conn = ldap_connect($ldaphost,$ldapport) or die("Could not connect to server");
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$r = ldap_bind($conn,$ldapbinddn,$ldappass) or die("Could not bind to server");
$rdn = "uid=".$username.",$ldapbasedn";
$res=ldap_delete($conn,$rdn);
ldap_close($conn);
return $res;
} catch(exception $e) {
var_dump($e);
}
}
function _ldap_user_exists($attributes){
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
$filter = grnet_ldap_interconnection_construct_filter($attributes);
$returnAttributes = array("uid");
try{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$info = ldap_get_entries($conn, $SearchResult);
if($info['count']>0) {
return true;
}else{
return false;
}
}catch(Exception $e)
{
$log->error($e->getMessage());
return false;
}
}
function grnet_ldap_interconnection_unit_create_wrapper($entity){
return _ldap_unit_create_wrapper($entity);
}
function _ldap_unit_create_wrapper($entity, $type=NULL){
$ldapbasedn = variable_get('grnet_ldapbasedn_people').','.variable_get('grnet_ldapbasedn');
try{
$unit = array('objectClass' => array( 'top',
'extensibleObject',
'organization',
'organizationalUnit',
'pilotOrganization'),
'o' => $entity->name,
'ou' => $entity->field_backend_name['und']['0']['value'],
'businessCategory' => taxonomy_vocabulary_load($entity->vid)->description,
'uniqueIdentifier' => $entity->tid
);
if(reset($entity->parent)>0) {
$unit['seeAlso'] = "uniqueIdentifier=".reset($entity->parent).",ou=Units,".$ldapbasedn;
}
if( !_ldap_unit_exists($entity->tid)){
$output = _ldap_unit_create($unit);
if($output==1){
drupal_set_message(t("Ldap unit record created succesfully!!"));
return $output;
}
}else{
drupal_set_message(t("PROBLEM!! Ldap Unit record couldn't be inserted!!"), 'error');
}
}catch (exception $e){
var_dump($e);
}
}
function _ldap_unit_update_wrapper($entity, $type){
$unit_values = array();
$unit_values['tid'] = $entity->tid;
$unit_values['name'] = $entity->name;
$unit_values['backend_name'] = $entity->field_backend_name['und']['0']['value'];
$unit_values['parent'] = reset($entity->parent);
try{
$_SESSION['flafla'] = $type;
if(_ldap_unit_exists($entity->tid)){
$output = _ldap_unit_update($entity);
if($output==1){
drupal_set_message(t("Ldap unit record updated succesfully!!"));
}
}else{
drupal_set_message(t("PROBLEM!! Ldap Unit record couldn't be updated as it couldn't be found!!"), 'error');
drupal_set_message('output = '.$output);
}
}catch (exception $e){
var_dump($e);
}
}
function _ldap_unit_delete_wrapper($entity_type){
try{
if(_ldap_unit_exists($entity_type->tid)){
$output = _ldap_unit_delete($entity_type->tid);
if($output==1){
drupal_set_message(t("Ldap Unit record deleted succesfully!!"));
}
}else{
drupal_set_message(t("PROBLEM!! Ldap Unit record couldn't be deleted as it couldn't be found!!"), 'error');
drupal_set_message('output = '.$output);
}
}catch (exception $e){
var_dump($e);
}
}
function _ldap_unit_create($unit) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_units').','.variable_get('grnet_ldapbasedn');
$res=false;
try{
$conn = ldap_connect($ldaphost,$ldapport) or die("Could not connect to server");
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$r = ldap_bind($conn,$ldapbinddn,$ldappass) or die("Could not bind to server");
$rdn = "uniqueIdentifier=".$unit['uniqueIdentifier'].",$ldapbasedn";
$res=ldap_add($conn, $rdn, $unit);
ldap_close($conn);
return $res;
} catch(exception $e) {
var_dump($e);
}
}
function _ldap_unit_exists($tid){
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_units').','.variable_get('grnet_ldapbasedn');
$filter = "(uniqueIdentifier=$tid)";
$attributes = array("uniqueIdentifier");
try{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$info = ldap_get_entries($conn, $SearchResult);
if($info['count']>0) {
return true;
}else{
return false;
}
}catch(Exception $e)
{
$log->error($e->getMessage());
return false;
}
}
function _ldap_unit_update($unit) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_units').','.variable_get('grnet_ldapbasedn');
$info = array( "o" => $unit->name,
"ou" => $unit->field_backend_name['und']['0']['value'],
"businessCategory" => taxonomy_vocabulary_load($unit->vid)->description
);
$_SESSION['kko'] = reset($unit->parent);
if(reset($unit->parent)<>'0'){
$info['seeAlso'] = 'uniqueIdentifier='.reset($unit->parent).",$ldapbasedn";
}
try{
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$rdn = "uniqueIdentifier=".$unit->tid.",$ldapbasedn";
$_SESSION['unit_values'] = $info;
ldap_close($conn);
return $ldap_modify_message;
} catch(exception $e) {
var_dump($e);
}
}
function _ldap_unit_delete($tid) {
$ldaphost = variable_get('grnet_ldaphost');
$ldapport = variable_get('grnet_ldapport');
$ldapbinddn = variable_get('grnet_ldapbinddn');
$ldappass = variable_get('grnet_ldappass');
$ldapbasedn = variable_get('grnet_ldapbasedn_units').','.variable_get('grnet_ldapbasedn');
$res=false;
try{
$conn = ldap_connect($ldaphost,$ldapport) or die("Could not connect to server");
ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);
$r = ldap_bind($conn,$ldapbinddn,$ldappass) or die("Could not bind to server");
$rdn = "uniqueIdentifier=".$tid.",$ldapbasedn";
$res=ldap_delete($conn, $rdn);
ldap_close($conn);
return $res;
} catch(exception $e) {
var_dump($e);
}
}
function grnet_ldap_interconnection_construct_filter($attributes){
$counter=0;
$filterTemp ='';
$filter='';
foreach($attributes as $key => $value){
$filterTemp .= "($key=$value)";
$counter++;
}
if($counter>1){
$filter = "(&";
}
$filter .=$filterTemp;
if($counter>1){
$filter .= ")";
}
return $filter;
}
?>

Event Timeline