LdapController.php 5.97 KB
Newer Older
jiangbowen's avatar
jiangbowen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
namespace App\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class LdapController extends Controller
{
	public $provider=null;
	public function __construct()
	{
		$ad = new \Adldap\Adldap();
		$config = [
jiangbowen's avatar
jiangbowen committed
15
			'hosts'=>["172.20.100.201"],
jiangbowen's avatar
jiangbowen committed
16
			'base_dn'=>'ou=Users_hoto,dc=hoto,dc=test',
jiangbowen's avatar
jiangbowen committed
17 18 19 20 21 22 23 24 25
			'username' => 'hoto\hcmadmin',
			'password' =>'hcmadmin123'
		];
		$ad->addProvider($config);
		$this->provider = $ad->connect();
	}
    public function handle(Request $request)
    {
		$data = $request->all();
jiangbowen's avatar
jiangbowen committed
26
		//新增人员数据格式:
jiangbowen's avatar
jiangbowen committed
27 28 29 30 31
		$data = [
			'operation_type'=>'emp',
			'operation'=>"add",
			'data'=>['jiangbowen9090','jiangbowen90912']
		];
jiangbowen's avatar
jiangbowen committed
32 33 34 35 36
        //编辑人员数据格式:
        $data = [
            'operation_type'=>'emp',
            'operation'=>"edit",
            'data'=>['jiangbowen9090'=>'jiangbowen9090et','jiangbowen90912']
jiangbowen's avatar
jiangbowen committed
37 38 39 40 41 42
        ];
        //删除人员格式
        $data = [
            'operation_type'=>'emp',
            'operation'=>"del",
            'data'=>['jiangbowen90912']
jiangbowen's avatar
jiangbowen committed
43 44 45 46 47 48
        ];
        //添加部门数据格式
        $data = [
            'operation_type'=>'dept',
            'operation'=>'add',
            'data'=>['产品技术中心/测试部']
jiangbowen's avatar
jiangbowen committed
49 50 51 52 53
        ];
        //编辑部门数据格式
        $data = [
            'operation_type'=>'dept',
            'operation'=>'edit',
jiangbowen's avatar
jiangbowen committed
54
            'data'=>['产品技术中心/测试部'=>'产品技术中心/测试部3']
jiangbowen's avatar
jiangbowen committed
55
        ];
jiangbowen's avatar
jiangbowen committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		// var_dump($data);
		if ($data['operation_type'] == 'emp') {
			if ($data['operation'] == 'add') {
				$this->saveEmp($data['data']);
			} else if($data['operation'] == 'edit'){
				$this->editEmp($data['data']);
			} else if ($data['operation'] == 'del'){
				$this->delEmp($data['data']);
			}
		} else if ($data['operation_type'] == 'dept') {
			if ($data['operation'] == 'add') {
				$this->saveDept($data['data']);
			} else if ($data['operation'] == 'edit') {
				$this->editDept($data['data']);	
			} else if ($data['operation'] == 'del') {
				$this->delDept($data['data']);
			}
		}	
    }

    public function saveEmp($data)
    {

    	if (empty($data)) return;
    	foreach ($data as $key => $value) {
    		$entry=["cn"=>"{$value}"];
			$user=$this->provider->make()->user($entry);
jiangbowen's avatar
jiangbowen committed
83
			$user->setDn("cn={$value},ou=IT发展部,ou=Users_hoto,dc=hoto,dc=test"); //在IT发展部新增人员
jiangbowen's avatar
jiangbowen committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			$user->setAccountName("$value");
			$user->setCommonName("$value");
			if($user->create()){
				Log::info("{$value} created success....");
			} else {
				Log::info("{$value} created failed....");
			}
    	}
    }

    public function editEmp($data)
    {
    	if (empty($data)) return;
    	foreach ($data as $key => $value) {
    		$user=$this->provider->search()->find($key);
    		if ($user->exists) {
    			$user->rename("cn=$value");
jiangbowen's avatar
jiangbowen committed
101 102 103 104
                Log::info("{$key} edit success....");
    		}else{
                Log::info("{$key} not exist....");
            }
jiangbowen's avatar
jiangbowen committed
105 106 107 108 109 110 111 112 113 114
    	}
    }

    public function delEmp($data)
    {
    	if (empty($data)) return;
    	foreach($data as $key=>$value){
	    	$user=$this->provider->search()->find($value);
			if ($user->exists) {
				$user->delete();
jiangbowen's avatar
jiangbowen committed
115 116 117 118
                Log::info("{$value} delete success....");
			}else{
                Log::info("{$value} delete success....");
            }
jiangbowen's avatar
jiangbowen committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    	}
    }


    public function saveDept($data)
    {
    	/**
    	 *数据格式
         *$data = ['一级部门/二级部门/三级部门']
         */
    	if (empty($data)) return;
    	foreach ($data as $key => $value) {
    		$depts = [];
    		$arr = explode('/',$value);
    		foreach ($arr as $key => $v) {
    				
    			// var_dump($result);
    			$depts[] = $v;
    			$depts2=array_reverse($depts);
				foreach ($depts2 as $key => $value) {
					$ous[] = "ou={$value}";
				}
				$ou_str = implode(',',$ous);
jiangbowen's avatar
jiangbowen committed
142
				$result=$this->provider->search()->setDn("$ou_str,ou=IT发展部,ou=Users_hoto,dc=hoto,dc=test")->find($v);
jiangbowen's avatar
jiangbowen committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    			// $dn = $result->getDnBuilder();->ous()
    			// $dn->addOu("$ou_str,ou=department,dc=test,dc=com");
				// var_dump($ou_str);die;
    			// $result->setDn();
    			if (is_null($result)) {
    				$this->generateOu($v,$depts);
    			}
    		}
    	}
    }

    public function generateOu($value,$depts)
    {
    	$entry=[
			"name"=>"{$value}"
		];
		$ou=$this->provider->make()->ou($entry);
		$depts=array_reverse($depts);
		foreach ($depts as $key => $value) {
			$ous[] = "ou={$value}";
		}
		$ou_str = implode(',',$ous);
jiangbowen's avatar
jiangbowen committed
165
		$ou->setDn("$ou_str,ou=IT发展部,ou=Users_hoto,dc=hoto,dc=test");
jiangbowen's avatar
jiangbowen committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		if($ou->create()){
			Log::info("{$value} created success....");
		} else {
			Log::info("{$value} created failed....");
		}
    }


    public function delDept($data)
    {
    	if (empty($data)) return;
    	foreach ($data as $key => $value) {
    			$depts = explode('/',$value);
    			$depts2=array_reverse($depts);
				foreach ($depts2 as $v) {
					$ous[] = "ou={$v}";
				}
				$ou_str = implode(',',$ous);
jiangbowen's avatar
jiangbowen committed
184
				$result=$this->provider->search()->setDn("$ou_str,ou=IT发展部,ou=Users_hoto,dc=hoto,dc=test")->find($depts[count($depts)-1]);
jiangbowen's avatar
jiangbowen committed
185 186 187 188 189 190 191 192 193 194 195 196 197
    		if (is_null($result)) return;
    		if ($result->exists) {
    			$result->delete();
    		}
    	}
    }


    public function editDept($data)
    {
    	if (empty($data)) return;
    	foreach ($data as $key => $value) {
    		if ($key === $value) continue;
jiangbowen's avatar
jiangbowen committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
     		$depts=explode('/',$key);
     		$hou_depts = explode('/',$value);
     		$depts=array_reverse($depts);
     		$hou_depts=array_reverse($hou_depts);
			 foreach ($depts as $k => $v) {
			 	$ous[] = "ou={$v}";
			 }
			 foreach ($hou_depts as $k => $v) {
			 	$hou_ous[] = "ou={$v}";
			 }
			$ou_str = implode(',',$ous);
			 var_export($depts[0]);
    		$result=$this->provider->search()->ous()->find($depts[0]);
    		var_export($result);die;
    		$result->setDn("$ou_str,ou=IT发展部,ou=Users_hoto,dc=hoto,dc=test");
//    		 var_dump($result);die;
    		if ($result->exists) {
    			$result->rename("ou=".$hou_depts[0]);
    		}
jiangbowen's avatar
jiangbowen committed
217
    	}
jiangbowen's avatar
jiangbowen committed
218
//    	$this->saveDept($handleData);
jiangbowen's avatar
jiangbowen committed
219 220
    }
}