Files
sysPass/lib/SP/Services/Ldap/LdapCheckService.php
nuxsmin 3504e66c9c * [MOD] Improved LDAP import
* [ADD] Groups import from LDAP
* [MOD] Improved LDAP auth behavior
* [MOD] Config data is always cloned on every request
* [MOD] Improved non RSA encrypted data detection
* [MOD] Code refactoring
2018-03-01 00:49:00 +01:00

99 lines
2.6 KiB
PHP

<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Services\Ldap;
use SP\Providers\Auth\Ldap\LdapBase;
use SP\Providers\Auth\Ldap\LdapMsAds;
use SP\Providers\Auth\Ldap\LdapParams;
use SP\Providers\Auth\Ldap\LdapStd;
use SP\Services\Service;
/**
* Class LdapCheckService
*
* @package SP\Services\Ldap
*/
class LdapCheckService extends Service
{
/**
* @var LdapBase
*/
protected $ldap;
/**
* @param LdapParams $ldapParams
* @throws \SP\Providers\Auth\Ldap\LdapException
*/
public function checkConnection(LdapParams $ldapParams)
{
if ($ldapParams->isAds()) {
$this->ldap = new LdapMsAds($ldapParams, $this->eventDispatcher, true);
} else {
$this->ldap = new LdapStd($ldapParams, $this->eventDispatcher, true);
}
$this->ldap->checkConnection();
}
/**
* @return array
* @throws \SP\Providers\Auth\Ldap\LdapException
*/
public function getUsersAndGroups()
{
$users = $this->ldapResultsMapper($this->ldap->findUsersByGroupFilter(['dn']));
$groups = $this->ldapResultsMapper($this->ldap->findGroups(['dn']));
return [
'count' => count($users) + count($groups),
'users' => $users,
'groups' => $groups
];
}
/**
* Obtener los datos de una búsqueda de LDAP de un atributo
*
* @param array $data
* @param string $attribute
* @return array
*/
public function ldapResultsMapper($data, $attribute = 'dn')
{
$out = [];
foreach ($data as $result) {
if (is_array($result)) {
foreach ($result as $ldapAttribute => $value) {
if (strtolower($ldapAttribute) === $attribute) {
$out[] = $value;
}
}
}
}
return $out;
}
}