Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 05f5d6e

Browse files
author
Jamie Snape
committed
Migrate ldap module settings to database
1 parent 64961b7 commit 05f5d6e

File tree

17 files changed

+445
-401
lines changed

17 files changed

+445
-401
lines changed

modules/ldap/Notification.php

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21-
/** Notification manager for the ldap module */
21+
/**
22+
* Notification manager for the ldap module
23+
*
24+
* @property Ldap_UserModel $Ldap_User
25+
*/
2226
class Ldap_Notification extends MIDAS_Notification
2327
{
24-
public $_models = array('User');
28+
public $_models = array('Setting', 'User');
2529
public $_moduleModels = array('User');
2630
public $moduleName = 'ldap';
2731

@@ -83,12 +87,12 @@ public function userSettingsChanged($params)
8387

8488
$ldapUser = $this->Ldap_User->getByUser($user);
8589
if ($ldapUser) {
86-
if ($ldapLogin == '') {
90+
if (empty($ldapLogin)) {
8791
$this->Ldap_User->delete($ldapUser);
8892
} else {
8993
$ldapUser->setLogin($ldapLogin);
9094
}
91-
} elseif ($ldapLogin != '') {
95+
} elseif (!empty($ldapLogin)) {
9296
$ldapUserDao = MidasLoader::newDao('UserDao', 'ldap');
9397
$ldapUserDao->setUserId($user->getKey());
9498
$ldapUserDao->setLogin($ldapLogin);
@@ -102,37 +106,36 @@ public function userSettingsChanged($params)
102106
/** generate admin Dashboard information */
103107
public function getDashboard()
104108
{
105-
$config = Zend_Registry::get('configsModules');
106-
$hostname = $config['ldap']->ldap->hostname;
107-
$port = (int) $config['ldap']->ldap->port;
108-
$proxybasedn = $config['ldap']->ldap->proxyBasedn;
109-
$protocolVersion = $config['ldap']->ldap->protocolVersion;
110-
$backupServer = $config['ldap']->ldap->backup;
111-
$bindn = $config['ldap']->ldap->bindn;
112-
$bindpw = $config['ldap']->ldap->bindpw;
113-
$proxyPassword = $config['ldap']->ldap->proxyPassword;
114-
115-
$ldap = ldap_connect($hostname, $port);
109+
$hostName = $this->Setting->getValueByName(LDAP_HOST_NAME_KEY, $this->moduleName);
110+
$port = (int) $this->Setting->getValueByName(LDAP_PORT_KEY, $this->moduleName);
111+
$proxyBaseDn = $this->Setting->getValueByName(LDAP_PROXY_BASE_DN_KEY, $this->moduleName);
112+
$protocolVersion = $this->Setting->getValueByName(LDAP_PROTOCOL_VERSION_KEY, $this->moduleName);
113+
$backupServer = $this->Setting->getValueByName(LDAP_BACKUP_SERVER_KEY, $this->moduleName);
114+
$bindRdn = $this->Setting->getValueByName(LDAP_BIND_RDN_KEY, $this->moduleName);
115+
$bindPassword = $this->Setting->getValueByName(LDAP_BIND_PASSWORD_KEY, $this->moduleName);
116+
$proxyPassword = $this->Setting->getValueByName(LDAP_PROXY_PASSWORD_KEY, $this->moduleName);
117+
118+
$ldap = ldap_connect($hostName, $port);
116119
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $protocolVersion);
117120

118121
$server = false;
119122
$backup = false;
120123

121124
if (isset($ldap) && $ldap !== false) {
122-
if ($proxybasedn != '') {
123-
ldap_bind($ldap, $proxybasedn, $proxyPassword);
125+
if ($proxyBaseDn != '') {
126+
ldap_bind($ldap, $proxyBaseDn, $proxyPassword);
124127
}
125128

126-
$ldapbind = ldap_bind($ldap, $bindn, $bindpw);
127-
if ($ldapbind != false) {
129+
$ldapBind = ldap_bind($ldap, $bindRdn, $bindPassword);
130+
if ($ldapBind != false) {
128131
$server = true;
129132
}
130133

131134
if (!empty($backupServer)) {
132135
$ldap = ldap_connect($backupServer);
133136
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $protocolVersion);
134-
$ldapbind = ldap_bind($ldap, $bindn, $bindpw);
135-
if ($ldapbind != false) {
137+
$ldapBind = ldap_bind($ldap, $bindRdn, $bindPassword);
138+
if ($ldapBind != false) {
136139
$backup = true;
137140
}
138141
}
@@ -172,53 +175,52 @@ public function ldapLogin($params)
172175
$email = $params['email'];
173176
$password = $params['password'];
174177

175-
$config = Zend_Registry::get('configsModules');
176-
$baseDn = $config['ldap']->ldap->basedn;
177-
$hostname = $config['ldap']->ldap->hostname;
178-
$port = (int) $config['ldap']->ldap->port;
179-
$protocolVersion = $config['ldap']->ldap->protocolVersion;
180-
$autoAddUnknownUser = $config['ldap']->ldap->autoAddUnknownUser;
181-
$searchTerm = $config['ldap']->ldap->search;
182-
$useActiveDirectory = $config['ldap']->ldap->useActiveDirectory;
183-
$proxybasedn = $config['ldap']->ldap->proxyBasedn;
184-
$backup = $config['ldap']->ldap->backup;
185-
$bindn = $config['ldap']->ldap->bindn;
186-
$bindpw = $config['ldap']->ldap->bindpw;
187-
$proxyPassword = $config['ldap']->ldap->proxyPassword;
178+
$hostName = $this->Setting->getValueByName(LDAP_HOST_NAME_KEY, $this->moduleName);
179+
$port = (int) $this->Setting->getValueByName(LDAP_PORT_KEY, $this->moduleName);
180+
$proxyBaseDn = $this->Setting->getValueByName(LDAP_PROXY_BASE_DN_KEY, $this->moduleName);
181+
$protocolVersion = $this->Setting->getValueByName(LDAP_PROTOCOL_VERSION_KEY, $this->moduleName);
182+
$backupServer = $this->Setting->getValueByName(LDAP_BACKUP_SERVER_KEY, $this->moduleName);
183+
$bindRdn = $this->Setting->getValueByName(LDAP_BIND_RDN_KEY, $this->moduleName);
184+
$bindPassword = $this->Setting->getValueByName(LDAP_BIND_PASSWORD_KEY, $this->moduleName);
185+
$proxyPassword = $this->Setting->getValueByName(LDAP_PROXY_PASSWORD_KEY, $this->moduleName);
186+
$baseDn = $this->Setting->getValueByName(LDAP_BASE_DN_KEY, $this->moduleName);
187+
$autoAddUnknownUser = $this->Setting->getValueByName(LDAP_AUTO_ADD_UNKNOWN_USER_KEY, $this->moduleName);
188+
$searchTerm = $this->Setting->getValueByName(LDAP_SEARCH_TERM_KEY, $this->moduleName);
189+
$useActiveDirectory = $this->Setting->getValueByName(LDAP_USE_ACTIVE_DIRECTORY_KEY, $this->moduleName);
188190

189191
if ($searchTerm == 'uid') {
190192
$atCharPos = strpos($email, '@');
191193
if ($atCharPos === false) {
192-
$ldapsearch = 'uid='.$email;
194+
$ldapSearch = 'uid='.$email;
193195
} else {
194-
$ldapsearch = 'uid='.substr($email, 0, $atCharPos);
196+
$ldapSearch = 'uid='.substr($email, 0, $atCharPos);
195197
}
196198
} else {
197-
$ldapsearch = $searchTerm.'='.$email;
199+
$ldapSearch = $searchTerm.'='.$email;
198200
}
199201

200-
$ldap = ldap_connect($hostname, $port);
202+
$ldap = ldap_connect($hostName, $port);
201203

202204
if ($ldap !== false) {
203205
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $protocolVersion);
204206
if ($useActiveDirectory) {
205207
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
206208
}
207-
if ($proxybasedn != '') {
208-
$proxybind = ldap_bind($ldap, $proxybasedn, $proxyPassword);
209-
if (!$proxybind) {
209+
if ($proxyBaseDn != '') {
210+
$proxyBind = ldap_bind($ldap, $proxyBaseDn, $proxyPassword);
211+
if (!$proxyBind) {
210212
throw new Zend_Exception('Cannot bind proxy');
211213
}
212214
}
213215

214-
$ldapbind = ldap_bind($ldap, $bindn, $bindpw);
215-
if (!$ldapbind && $backup) {
216-
$ldap = ldap_connect($backup);
217-
ldap_bind($ldap, $bindn, $bindpw);
216+
$ldapBind = ldap_bind($ldap, $bindRdn, $bindPassword);
217+
if (!$ldapBind && $backupServer) {
218+
$ldap = ldap_connect($backupServer);
219+
ldap_bind($ldap, $bindRdn, $bindPassword);
218220
}
219221

220222
// do an ldap search for the specified user
221-
$result = ldap_search($ldap, $baseDn, $ldapsearch, array('uid', 'cn', 'mail'));
223+
$result = ldap_search($ldap, $baseDn, $ldapSearch, array('uid', 'cn', 'mail'));
222224
$someone = false;
223225
if ($result != 0) {
224226
$entries = ldap_get_entries($ldap, $result);
@@ -237,8 +239,8 @@ public function ldapLogin($params)
237239
$someone = $someone->getUser();
238240
} elseif ($autoAddUnknownUser) {
239241
// If the user doesn't exist we add it
240-
$givenname = $entries[0]['cn'][0];
241-
if (!isset($givenname)) {
242+
$givenName = $entries[0]['cn'][0];
243+
if (!isset($givenName)) {
242244
throw new Zend_Exception(
243245
'No common name (cn) set in LDAP, cannot register user into Midas'
244246
);
@@ -253,24 +255,24 @@ public function ldapLogin($params)
253255
}
254256
}
255257

256-
$names = explode(' ', $givenname);
257-
$firstname = ' ';
258+
$names = explode(' ', $givenName);
259+
$firstName = ' ';
258260
$namesCount = count($names);
259261
if ($namesCount > 1) {
260-
$firstname = $names[0];
261-
$lastname = $names[1];
262+
$firstName = $names[0];
263+
$lastName = $names[1];
262264
for ($i = 2; $i < $namesCount; $i++) {
263-
$lastname .= ' '.$names[$i];
265+
$lastName .= ' '.$names[$i];
264266
}
265267
} else {
266-
$lastname = $names[0];
268+
$lastName = $names[0];
267269
}
268270
$someone = $this->Ldap_User->createLdapUser(
269271
$ldapEmail,
270272
$email,
271273
$password,
272-
$firstname,
273-
$lastname
274+
$firstName,
275+
$lastName
274276
);
275277
$someone = $someone->getUser(); // convert to core user dao
276278
}
@@ -285,7 +287,7 @@ public function ldapLogin($params)
285287

286288
return $someone;
287289
} else {
288-
throw new Zend_Exception('Could not connect to LDAP at '.$hostname);
290+
throw new Zend_Exception('Could not connect to LDAP at '.$hostName);
289291
}
290292
}
291293

@@ -306,13 +308,12 @@ public function handleResetPassword($params)
306308
{
307309
$ldapUser = $this->Ldap_User->getByUser($params['user']);
308310
if ($ldapUser !== false) {
309-
$config = Zend_Registry::get('configsModules');
310-
$ldapServer = $config['ldap']->ldap->hostname;
311+
$hostName = $this->Setting->getValueByName(LDAP_HOST_NAME_KEY, $this->moduleName);
311312
$email = $params['user']->getEmail();
312313
$subject = "Password Request";
313314
$body = "You have requested a new password for Midas Platform.<br/><br/>";
314315
$body .= "We could not fulfill this request because your user account is managed by an external LDAP server.<br/><br/>";
315-
$body .= "Please contact the administrator of the LDAP server at <b>".$ldapServer."</b> to have your password changed.";
316+
$body .= "Please contact the administrator of the LDAP server at <b>".$hostName."</b> to have your password changed.";
316317
$result = Zend_Registry::get('notifier')->callback(
317318
'CALLBACK_CORE_SEND_MAIL_MESSAGE',
318319
array(

modules/ldap/configs/module.ini

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,4 @@ fullname = "LDAP Authentication"
55
description = "Authenticate users against an LDAP server"
66
category = "Authentication"
77
uuid = "93ac38e2-01d8-4214-84cb-900accc2da48"
8-
version = "1.0.1"
9-
10-
ldap.hostname = "localhost"
11-
ldap.port = "389"
12-
ldap.backup = ""
13-
ldap.basedn = "ou=people,dc=myorganization,dc=com"
14-
ldap.bindn = "cn=user,ou=people,dc=myorganization,dc=com"
15-
ldap.bindpw = "set_your_password"
16-
ldap.protocolVersion = "3"
17-
ldap.autoAddUnknownUser = "1"
18-
ldap.search = "uid"
19-
ldap.useActiveDirectory = "0"
20-
ldap.proxyBasedn = ""
21-
ldap.proxyPassword = ""
8+
version = "1.1.0"

modules/ldap/constant/module.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
define('LDAP_HOST_NAME_KEY', 'host_name');
22+
define('LDAP_HOST_NAME_DEFAULT_VALUE', 'localhost');
23+
define('LDAP_PORT_KEY', 'port');
24+
define('LDAP_PORT_DEFAULT_VALUE', '389');
25+
define('LDAP_BACKUP_SERVER_KEY', 'backup_server');
26+
define('LDAP_BACKUP_SERVER_DEFAULT_VALUE', '');
27+
define('LDAP_BIND_RDN_KEY', 'bind_rdn');
28+
define('LDAP_BIND_RDN_DEFAULT_VALUE', 'cn=user,ou=people,dc=myorganization,dc=com');
29+
define('LDAP_BIND_PASSWORD_KEY', 'bind_password');
30+
define('LDAP_BIND_PASSWORD_DEFAULT_VALUE', '');
31+
define('LDAP_BASE_DN_KEY', 'base_dn');
32+
define('LDAP_BASE_DN_DEFAULT_VALUE', 'ou=people,dc=myorganization,dc=com');
33+
define('LDAP_PROTOCOL_VERSION_KEY', 'protocol_version');
34+
define('LDAP_PROTOCOL_VERSION_DEFAULT_VALUE', 3);
35+
define('LDAP_SEARCH_TERM_KEY', 'search_term');
36+
define('LDAP_SEARCH_TERM_DEFAULT_VALUE', 'uid');
37+
define('LDAP_PROXY_BASE_DN_KEY', 'proxy_base_dn');
38+
define('LDAP_PROXY_BASE_DN_DEFAULT_VALUE', '');
39+
define('LDAP_PROXY_PASSWORD_KEY', 'proxy_password');
40+
define('LDAP_PROXY_PASSWORD_DEFAULT_VALUE', '');
41+
define('LDAP_USE_ACTIVE_DIRECTORY_KEY', 'use_active_directory');
42+
define('LDAP_USE_ACTIVE_DIRECTORY_DEFAULT_VALUE', 0);
43+
define('LDAP_AUTO_ADD_UNKNOWN_USER_KEY', 'auto_add_unknown_user');
44+
define('LDAP_AUTO_ADD_UNKNOWN_USER_DEFAULT_VALUE', 1);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Admin controller for the ldap module. */
22+
class Ldap_AdminController extends Ldap_AppController
23+
{
24+
/** @var array */
25+
public $_models = array('Setting');
26+
27+
/** Index action */
28+
public function indexAction()
29+
{
30+
$this->requireAdminPrivileges();
31+
32+
$this->view->pageTitle = 'LDAP Module Configuration';
33+
$form = new Ldap_Form_Admin();
34+
35+
if ($this->getRequest()->isPost()) {
36+
$data = $this->getRequest()->getPost();
37+
38+
if ($form->isValid($data)) {
39+
$values = $form->getValues();
40+
41+
foreach ($values as $key => $value) {
42+
$this->Setting->setConfig($key, $value, $this->moduleName);
43+
}
44+
}
45+
46+
$form->populate($data);
47+
} else {
48+
$elements = $form->getElements();
49+
50+
foreach ($elements as $element) {
51+
$name = $element->getName();
52+
53+
if ($name !== 'csrf' && $name !== 'submit') {
54+
$value = $this->Setting->getValueByName($name, $this->moduleName);
55+
56+
if (!is_null($value)) {
57+
$form->setDefault($name, $value);
58+
}
59+
}
60+
}
61+
}
62+
63+
$this->view->form = $form;
64+
session_start();
65+
}
66+
}

0 commit comments

Comments
 (0)