-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertcsvtoldif-groups.php
106 lines (96 loc) · 3.3 KB
/
convertcsvtoldif-groups.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
$row = 1;
$limit = 120000;
$userTypeGroups = [];
$departMent = [];
if (($handle = fopen("/home/tran/mock.csv", "r")) !== FALSE) {
$members = "";
while ((($data = fgetcsv($handle, 1000, ",")) !== FALSE) && ($row <= $limit)){
$num = count($data);
if (!isset($userTypeGroups[$data[4]]) && $data[4] != "") $userTypeGroups[$data[4]] = $data[4];
if (!isset($departMent[$data[5]]) && $data[5] != "") $departMent[$data[5]] = $data[5];
$row++;
}
// Create groups:
foreach($userTypeGroups as $groupName){
/*
dn: cn=staff,ou=pluriel,ou=groups,dc=example,dc=org
changetype: add
objectClass: groupOfNames
objectClass: top
cn: staff
*/
echo "dn: cn=".$groupName.",ou=groups,dc=example,dc=org\n";
echo "changetype: add\n";
echo "objectClass: groupOfNames\n";
echo "objectClass: top\n";
echo "member: uid=billy,dc=example,dc=org";
echo "cn: ".$groupName."\n";
echo "\n";
}
// Create ou:
foreach($departMent as $dept){
/*
dn: ou=abc,ou=staff,ou=people,dc=example,dc=org
changetype: add
objectClass: organizationalUnit
objectClass: top
ou: abc
*/
echo "dn: ou=".$dept.",ou=people,dc=example,dc=org\n";
echo "changetype: add\n";
echo "objectClass: organizationalUnit\n";
echo "objectClass: top\n";
echo "ou: ".$dept."\n";
echo "\n";
}
fclose($handle);
}
///////////////////////////////
if (($handle = fopen("/home/tran/mock.csv", "r")) !== FALSE) {
$members = "";
$row = 1;
while ((($data = fgetcsv($handle, 1000, ",")) !== FALSE) && ($row <= $limit)){
$num = count($data);
echo "# New entry \n";
$userDn = "";
if ( isset($departMent[$data[5]]))
{
$userDn = "uid=".$data[2].",ou=".$departMent[$data[5]].",ou=people,dc=example,dc=org\n";
}else{
$userDn = "uid=".$data[2].",ou=staff,ou=people,dc=example,dc=org\n";
}
echo "dn: ".$userDn;
echo "changetype: add\n";
echo "gidNumber: 0\n";
echo "objectClass: inetOrgPerson\n";
echo "objectClass: organizationalPerson\n";
echo "objectClass: person\n";
echo "objectClass: top\n";
echo "objectClass: posixAccount\n";
echo "uidNumber: $row\n";
echo "uid: ".$data[2]."\n";
echo "homeDirectory: /home/".$data[2]."\n";
echo "sn: ".$data[3]."\n";
echo "cn: ".$data[3]."\n";
echo "mail: ".$data[1]."\n";
echo "employeeType: ".$data[4]."\n";
echo "displayName: Name ".$data[3]."\n";
if ( isset($userTypeGroups[$data[4]]) )
{
echo "memberOf: cn=".$userTypeGroups[$data[4]].",ou=groups,dc=example,dc=org\n";
}
echo "userPassword:: UEBzc3cwcmQ=\n";
echo "\n";
if ( isset($userTypeGroups[$data[4]]) )
{
echo "dn: cn=".$userTypeGroups[$data[4]].",ou=groups,dc=example,dc=org\n";
echo "changetype: modify\n";
echo "add: member\n";
echo "member: ".$userDn;
echo "\n";
}
$row++;
}
fclose($handle);
}