Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

Pull Newly Created ADS users

I have written the following code in our osticket installation, to pull newly created users in ADS and insert them into db of osticket as well as lock users who are disabled in ADS.
<?php
//RE012-NEWLY CREATED FOR LISTING ADS USERS WITH THEIR STATUS
if(!defined('OSTADMININC') || !$thisuser->isadmin()) die('Access Denied');
$groups=db_query('SELECT group_id,group_name FROM '.GROUP_TABLE);
$depts= db_query('SELECT dept_id,dept_name FROM '.DEPT_TABLE);
$SearchField="samaccountname";
$LDAPHost = "ldap://something.com";
$dn = "DC=something,DC=com";
$LDAPUserDomain = "@something.com";
$LDAPUser = "administrator";
$LDAPUserPassword = "xxxx";
$LDAPFieldsToFind = array("cn", "givenname","samaccountname","homedirectory", "telephonenumber", "mail","name","userAccountControl","countryCode");
$office=1;
$cnx = ldap_connect($LDAPHost) or die("Could not connect to LDAP");
ldap_set_option($cnx, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($cnx, LDAP_OPT_REFERRALS, 0);
ldap_bind($cnx,$LDAPUser.$LDAPUserDomain,$LDAPUserPassword) or die("Could not bind to LDAP");
error_reporting (E_ALL ^ E_NOTICE);
$filter="($SearchField=*)";
//echo $filter;
$sr=ldap_search($cnx, $dn, $filter,$LDAPFieldsToFind);
$info = ldap_get_entries($cnx, $sr);
?>

<?
for ($x=0; $x<$info["count"]; $x++) {
$sam=$info[$x]['samaccountname'][0];
$e=$info[$x]['useraccountcontrol'][0];
$fullname = $info[$x]['name'][0];
$mail=$info[$x]['mail'][0];
$country=$info[$x]['countrycode'][0];
if($e==514){
$status="Disabled";
$isactive=0;
$exec=db_query("UPDATE ost_staff SET isactive = 0 WHERE username = '$sam'");
}
else{
$status="Enabled";
//echo $sam;

if ((!preg_match('/[^a-zA-Z\.]+/', $sam)) ){
$usrList = "SELECT username FROM ost_staff WHERE username = '$sam'";
//echo $usrList;
$res=db_query($usrList);
if(!$res || !db_num_rows($res)&& (!empty($mail))){
//echo "failure";
$insert = "INSERT INTO ost_staff (username,firstname,lastname,email,office_id)values('$sam','$fullname','$fullname','$mail',$office)";
$exec = db_query($insert);
//echo $insert;
if(!$exec)
echo "wrong sql ".$insert;
}
//RE014
?>



<?
}
}}?>
User NameStatusEmail Address
<?echo $sam;?><?echo $status;?><?echo $mail;?><?echo $country;?>

Comments

Sign In or Register to comment.