Skip to content

Commit

Permalink
* [FIX] Fixes #504. Added an option to select which attribute should …
Browse files Browse the repository at this point in the history
…be used for user's login when importing from LDAP. Thanks to @maxdie for the feedback
  • Loading branch information
nuxsmin committed Mar 15, 2017
1 parent 58e902a commit 373ba30
Show file tree
Hide file tree
Showing 21 changed files with 1,796 additions and 1,667 deletions.
8 changes: 7 additions & 1 deletion inc/SP/Controller/ItemActionController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use SP\Mgmt\Users\UserUtil;
use SP\Util\Checks;
use SP\Util\Json;
use SP\Util\Util;

/**
* Class AjaxSaveController
Expand Down Expand Up @@ -1071,7 +1072,12 @@ protected function ldapImportAction()
{
$this->LogMessage->setAction(__('Importar usuarios de LDAP', false));

if (UserLdapSync::run()) {
$options = [
'loginAttribute' => Request::analyze('ldap_loginattribute'),
'isADS' => Util::boolval(Request::analyze('ldap_ads'))
];

if (UserLdapSync::run($options)) {
$this->LogMessage->addDescription(__('Importación de usuarios de LDAP realizada', false));
$this->LogMessage->addDetails(__('Usuarios importados', false), sprintf('%d/%d', UserLdapSync::$syncedObjects, UserLdapSync::$totalObjects));
$this->LogMessage->addDetails(__('Errores', false), UserLdapSync::$errorObjects);
Expand Down
3 changes: 2 additions & 1 deletion inc/SP/Mgmt/Users/UserLdap.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ public function checkDuplicatedOnAdd()
$query = /** @lang SQL */
'SELECT user_login, user_email
FROM usrData
WHERE LOWER(user_login) = LOWER(?) OR LOWER(user_email) = LOWER(?)';
WHERE LOWER(user_login) = LOWER(?) OR (? <> \'\' AND LOWER(user_email) = LOWER(?))';

$Data = new QueryData();
$Data->setQuery($query);
$Data->addParam($this->itemData->getUserLogin());
$Data->addParam($this->itemData->getUserEmail());
$Data->addParam($this->itemData->getUserEmail());

DB::getQuery($Data);

Expand Down
46 changes: 27 additions & 19 deletions inc/SP/Mgmt/Users/UserLdapSync.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* sysPass
*
* @author nuxsmin
* @link http://syspass.org
* @author nuxsmin
* @link http://syspass.org
* @copyright 2012-2017, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
Expand Down Expand Up @@ -55,18 +55,24 @@ class UserLdapSync
/**
* Sincronizar usuarios de LDAP
*
* @param array $options
* @return bool
* @throws \phpmailer\phpmailerException
*/
public static function run()
public static function run(array &$options)
{
$Log = new Log();
$LogMessage = $Log->getLogMessage();
$LogMessage->setAction(__('Sincronización LDAP', false));

$Ldap = Config::getConfig()->isLdapAds() ? new LdapMsAds() : new LdapStd();
$Ldap = Config::getConfig()->isLdapAds() || $options['isADS'] ? new LdapMsAds() : new LdapStd();

$ldapObjects = $Ldap->findObjects();

if (!$ldapObjects) {
return false;
}

self::$totalObjects = (int)$ldapObjects['count'];

$LogMessage->addDetails(__('Objetos encontrados', false), self::$totalObjects);
Expand All @@ -75,9 +81,9 @@ public static function run()
$UserData = new UserData();

foreach ($ldapObjects as $result) {
$User = clone $UserData;

if (is_array($result)) {
$User = clone $UserData;

foreach ($result as $attribute => $values) {

$value = $values[0];
Expand All @@ -87,27 +93,29 @@ public static function run()
case 'fullname':
$User->setUserName($value);
break;
case 'login':
case 'samaccountname':
case 'uid':
$User->setUserLogin(strtolower($value));
case $options['loginAttribute']:
$User->setUserLogin($value);
break;
case 'mail':
$User->setUserEmail(strtolower($value));
$User->setUserEmail($value);
break;
}
}

$User->setUserPass(Util::generateRandomBytes());
if (!empty($User->getUserName())
&& !empty($User->getUserLogin())
) {
$User->setUserPass(Util::generateRandomBytes());

try {
$LogMessage->addDetails(__('Usuario', false), sprintf('%s (%s)', $User->getUserName(), $User->getUserLogin()));
UserLdap::getItem($User)->add();
try {
$LogMessage->addDetails(__('Usuario', false), sprintf('%s (%s)', $User->getUserName(), $User->getUserLogin()));
UserLdap::getItem($User)->add();

self::$syncedObjects++;
} catch (SPException $e) {
self::$errorObjects++;
$LogMessage->addDescription($e->getMessage());
self::$syncedObjects++;
} catch (SPException $e) {
self::$errorObjects++;
$LogMessage->addDescription($e->getMessage());
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion inc/SP/Util/Util.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public static function getAppInfo($index = null)
*/
public static function getVersion($retBuild = false, $normalized = false)
{
$build = 17031401;
$build = 17031501;
$version = [2, 1, 2];

if ($normalized === true) {
Expand Down
Binary file modified inc/locales/de_DE/LC_MESSAGES/messages.mo
Binary file not shown.
Loading

0 comments on commit 373ba30

Please sign in to comment.