|
以下为全量组织架构从AD域中获取,并以树形结构方式存在本地,先创建一个ou_tree的表,
创建表
CREATE TABLE `ou_tree` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`guid` varchar(255) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`islast` varchar(255) DEFAULT NULL,
`line` varchar(255) DEFAULT NULL COMMENT '树形',
`ou` varchar(255) DEFAULT NULL COMMENT '本级',
`ou_p` varchar(255) DEFAULT NULL COMMENT '上级',
`dept_no` varchar(255) DEFAULT NULL COMMENT '部门编号',
`dn` varchar(255) DEFAULT NULL COMMENT '全量ou',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4956 DEFAULT CHARSET=utf8;
程序
<?php
include_once("inc/conn.php"); // 如需要连接数据库则包含,如已包含auth.inc.php,则无须包含conn.php
include_once("inc/utility_all.php"); // 如需要使用公用函数则包含
include_once "inc/utility_user.php";
include_once "inc/ldap/adLDAP.php";
$SYNC_CONFIG = get_sys_para("DOMAIN_SYNC_CONFIG");
$SYNC_CONFIG = unserialize($SYNC_CONFIG["DOMAIN_SYNC_CONFIG"]);
$option = get_ldap_option($SYNC_CONFIG);
$adldap = new adLDAP($option);
if (!$adldap->authenticate($SYNC_CONFIG["AD_USER"], $SYNC_CONFIG["AD_PWD"])) {
Message("", _("域相关参数设置有误") . "(" . $adldap->get_last_error() . ")");
exit();
}
$folder_list = $adldap->folder_list(NULL, ADLDAP_FOLDER, NULL, "folder");
$org_array = get_org_array($folder_list, $option["base_dn"]);
for ($i = 0; $i < count($org_array); $i++) {
$name =$org_array[$i]["name"];
//echo 'name:'.$name."</br>";
$level =$org_array[$i]["level"];
$islast =$org_array[$i]["islast"];
$line =$org_array[$i]["line"];
$guid =$org_array[$i]["guid"];
//ou字符串获取
$str = $org_array[$i]["dn"];
//定义截取",DC=wingtech"字符串前面的所有内容
$searchString = ",DC=wingtech";
// 使用strpos查找$searchString在$str中的位置
$position = strpos($str, $searchString);
//截获出要的字符串
$ou = substr($str, 0, $position);
//定义要上级部门字符解惑标识“,”
$commaPos = strpos($ou, ',');
if ($commaPos !== false) {
$ou_p = substr($ou, $commaPos + 1);
}
// 假设我们有一个字符串
//用guid查询是否重复,重复则跳出
$query="SELECT * FROM ou_tree WHERE `guid` = '$guid' ";
$cursor = TD::DB()->prepareQuery($query);
$count=count($cursor);
if($count>=1){
echo "更新";
}else{
if($level==0){
$parent_id="NULL";
$query="INSERT INTO `TD_OA`.`ou_tree` (`name`, `guid`, `parent_id`, `level`, `islast`, `line`, `ou`, `ou_p`,`dn`) VALUES
('$name', '$guid', '$parent_id', '$level', '$islast', '$line', '$ou', '$ou_p','$str')";
TD::DB()->prepareQuery($query);
//用parent_id=0查询并循环出id来,在用id进行更新
$query="SELECT * FROM `TD_OA`.`ou_tree` WHERE `parent_id` = '0' ";
$cursor = exequery(TD::conn(), $query);
$nu=1;
while ($ROW = mysqli_fetch_array($cursor)) {
$id= $ROW["id"];
$dept_no="000".$nu++;
$dept_no=substr($dept_no, -3);
$sql="UPDATE `TD_OA`.`ou_tree` SET `dept_no` = '$dept_no' WHERE `id` = '$id'";
exequery(TD::conn(), $sql);
}
}else{
$query="SELECT * FROM `TD_OA`.`ou_tree` WHERE `ou` = '$ou_p' ";
$cursor = exequery(TD::conn(), $query);
while ($ROW = mysqli_fetch_array($cursor)) {
$parent_id= $ROW["id"];
$num=$ROW["dept_no"];
$query="INSERT INTO `TD_OA`.`ou_tree` (`name`, `guid`, `parent_id`, `level`, `islast`, `line`, `ou`, `ou_p`,`dn`) VALUES ('$name', '$guid', '$parent_id', '$level', '$islast', '$line', '$ou', '$ou_p','$str')";
TD::DB()->prepareQuery($query);
//
$querya="SELECT * FROM `TD_OA`.`ou_tree` WHERE `parent_id` = '$parent_id' ";
$cursora = exequery(TD::conn(), $querya);
$nua=1;
while ($ROWa = mysqli_fetch_array($cursora)) {
$ida= $ROWa["id"];
$dept_noa="000".$nua++;
$dept_noa=$num.substr($dept_noa,-3);
$sqla="UPDATE `TD_OA`.`ou_tree` SET `dept_no` = '$dept_noa' WHERE `id` = '$ida'";
exequery(TD::conn(), $sqla);
}
//
}
}
}
}
在写一个脚本把你要的部门创建到department的中,在同步的dept_map
|
|