forked from 22decembre/ldap_login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.ldap.php
More file actions
executable file
·230 lines (164 loc) · 4.24 KB
/
Copy pathclass.ldap.php
File metadata and controls
executable file
·230 lines (164 loc) · 4.24 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
// Require the updated LDAP class
include_once('include/adLDAP.php');
// Define Global variable
global $conf;
/**
* Extend the Piwigo LDAP class using the adLDAP class.
* That way we take advantage of code someone a lot smarter
* than me has already written.
*/
class Ldap extends adLDAP {
/**
* The configuration variable
*
* @var array
*/
public $config;
/**
* Path to the configuration file
*
* @var array
*/
public $config_file_path;
/**
* Load the configuration parameters
* and call the parent constructor.
*/
function __construct( $options=array()) {
$this -> config_file_path = rtrim(LDAP_LOGIN_PATH, '/') . '/config/data.dat';
$this -> load_config();
parent::__construct( $this -> config );
}
/**
* Load the default LDAP configuration
*/
public function load_default_config() {
// For LDAP only
$d['account_suffix'] = "@mydomain.local";
$d['base_dn'] = "DC=mydomain,DC=local";
$d['ad_username'] = "pjenkins";
$d['ad_password'] = "mySecretPass";
$d['domain_controllers'] = array ("dc01.mydomain.local");
$d['use_ssl'] = False;
// Other options
$d['allow_newusers'] = False;
$d['advertise_admin_new_ldapuser'] = False;
$d['send_password_by_mail_ldap'] = False;
// Group Mappings
$d['group_mapping'] = '';
return $d;
}
/*
* Search for user
*
*@return Bool
*/
public function get_ldap_info($param, $filter) {
$res = ldap_search(
$this->_conn,
$this->_base_dn,
$filter,
array(
'samaccountname',
'mail',
'memberof',
'department',
'displayname',
'telephonenumber',
$this->config['user_primary_groupid'],
'objectsid',
$this->config['login_attr'],
$this->config['username_attr'],
)
);
$entries = ldap_get_entries($this->_conn, $res);
if(isset($entries[0])) {
return $entries;
}
return null;
}
/*
* Load the saved configuration
*
*@return Bool
*/
public function load_config() {
$conf_file = @file_get_contents( $this -> config_file_path );
$defaults = $this -> load_default_config();
if ($conf_file!==false)
{
$config = unserialize($conf_file);
foreach ($config as $key => $value) {
$defaults[$key] = $value;
}
}
$this -> config = $defaults;
}
/*
* Function to save the user entered configuration
*
* @var array
*
* @return void
*/
public function save_config()
{
$file = fopen( $this -> config_file_path, 'w' );
fwrite($file, serialize($this -> config) );
fclose( $file );
}
/*
* Load the Ldap admin menu
*/
public function ldap_admin_menu($menu)
{
array_push($menu,
array(
'NAME' => 'Ldap Login',
'URL' => get_admin_plugin_menu_link(LDAP_LOGIN_PATH.'/admin.php') )
);
return $menu;
}
/**
* Validate a user's login credentials
*
* @param string $username A user's AD username
* @param string $password A user's AD password
* @param bool optional $prevent_rebind
* @return bool
*/
function authenticate2($username, $password, $prevent_rebind = false) {
// Prevent null binding
if ($username === NULL || $password === NULL) { return false; }
if (empty($username) || empty($password)) { return false; }
// Bind as the user
$this->_bind = @ldap_bind($this->_conn, $username, $password);
if (!$this->_bind){
return false;
} else {
return true;
}
}
public function query_group_memberships($info) {
$id = $this->config['group_use_fulldn'] ? $info['dn'] : $info[$this->config['username_attr']][0];
$groupid_attr = $this->config['groupid_attr'];
$query = ldap_search($this->_conn, $this->config['group_base_dn'], $this->config['group_user_attr'].'='.$id, array($groupid_attr));
$result = ldap_get_entries($this->_conn, $query);
unset($result['count']);
$result = array_map(
function ($entry) use ($groupid_attr) {
return $entry[$groupid_attr][0];
}, $result
);
if ($this->config['user_primary_groupid'] && $info[$this->config['user_primary_groupid']]) {
$query = ldap_search($this->_conn, $this->config['group_base_dn'], 'gidnumber='.$info[$this->config['user_primary_groupid']][0], array($groupid_attr));
$r = ldap_get_entries($this->_conn, $query);
if (sizeof($r)) {
$result[] = $r[0][$groupid_attr][0];
}
}
return $result;
}
}
?>