Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncAD
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
江博文
syncAD
Commits
789343dd
Commit
789343dd
authored
Jan 27, 2022
by
jiangbowen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test conn
parent
e516318b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
196 additions
and
1 deletion
+196
-1
LdapController.php
app/Http/Controllers/LdapController.php
+192
-0
web.php
routes/web.php
+4
-1
No files found.
app/Http/Controllers/LdapController.php
0 → 100644
View file @
789343dd
<?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
=
[
'hosts'
=>
[
'test.com'
],
'base_dn'
=>
'ou=Users_hoto,dc=test,dc=com'
,
'username'
=>
'hoto\hcmadmin'
,
'password'
=>
'hcmadmin123'
];
$ad
->
addProvider
(
$config
);
$this
->
provider
=
$ad
->
connect
();
}
public
function
handle
(
Request
$request
)
{
$data
=
$request
->
all
();
$data
=
[
'operation_type'
=>
'emp'
,
'operation'
=>
"add"
,
'data'
=>
[
'jiangbowen9090'
,
'jiangbowen90912'
]
];
// 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
);
$user
->
setDn
(
"cn=
{
$value
}
,cn=IT发展部,cn=Users_hoto,dc=test,dc=com"
);
$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
"
);
}
}
}
public
function
delEmp
(
$data
)
{
if
(
empty
(
$data
))
return
;
foreach
(
$data
as
$key
=>
$value
){
$user
=
$this
->
provider
->
search
()
->
find
(
$value
);
if
(
$user
->
exists
)
{
$user
->
delete
();
}
}
}
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
);
$result
=
$this
->
provider
->
search
()
->
setDn
(
"
$ou_str
,ou=department,dc=test,dc=com"
)
->
find
(
$v
);
// $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
);
$ou
->
setDn
(
"
$ou_str
,ou=department,dc=test,dc=com"
);
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
);
$result
=
$this
->
provider
->
search
()
->
setDn
(
"
$ou_str
,ou=department,dc=test,dc=com"
)
->
find
(
$depts
[
count
(
$depts
)
-
1
]);
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
;
$handleData
[]
=
$value
;
//ɾ��֮ǰ��bu
// $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);
// $result=$this->provider->search()->ous()->find($depts[0]);
// $result->setDn("$ou_str,ou=department,dc=test,dc=com");
// var_dump($result);die;
// if ($result->exists) {
// $result->rename("ou=".$hou_depts[0]);
// }
}
$this
->
saveDept
(
$handleData
);
}
}
\ No newline at end of file
routes/web.php
View file @
789343dd
...
...
@@ -47,3 +47,6 @@ Route::get('/ad', function () {
echo
$e
->
getMessage
();
}
});
Route
::
get
(
'/ad2'
,
'LdapController@handle'
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment