php - 创建用户,但使用 PHP 检查 Active Directory 中是否存在

标签 php symfony active-directory doctrine ldap

我正在使用 Symfony 2.8.2、Doctrine。

我需要在应用程序数据库中创建用户,但首先我需要检查这些用户是否存在于外部 Active Directory 数据库中。仅当用户存在时,才可以在应用程序数据库上创建用户。我的代码适用于 creat 用户,但现在我正在尝试实现“首先检查用户是否存在”部分,我正在遵循一些示例,但我不确定我这样做的方式是否顺利。我读到 LDAP 的通常顺序是连接、绑定(bind)、搜索、解释搜索结果、关闭连接。有人可以给我一个提示,告诉我应该如何处理我的代码,我应该首先在哪里进行搜索,在绑定(bind)之前还是之后?以及原因。

public function createAction(Request $request){
    $em = $this->getDoctrine()->getManager();
    $post_send = $request->request->get('userbundle_user');

    if (array_key_exists('id', $post_send)) {
        $ldap_success = true;
        $entity = $em->getRepository('UserBundle:User')->find($post_send['id']);
    }else{
        $ldapconn = ldap_connect("10.0.0.230");
        $Pass= "XXXXXX";
        $searchUser = "YYYYYY";
        $ldap_success = false;
        if (@ldap_bind($ldapconn, $searchUser, $Pass)) {
            try{
                $post_send['password'] = $Pass;
                $attributes = ['cn'];
                $filter = "(&(objectClass=user)(objectCategory=person)(userPrincipalName=".ldap_escape($post_send['username'], null, LDAP_ESCAPE_FILTER)."))";
                $baseDn = "DC=XXX,DC=XX,DC=cl";
                $results = @ldap_search($ldapconn, $baseDn, $filter);
                //$info = @ldap_get_entries($ldapconn, $results);
                if ( $results ) {
                    $ldap_success = true;
                } else {
                    $ldap_success = false;//false, lo deje en true para que pudiera avanzar
                }
            }
            catch(\Exception $e){
                $ldap_success = false;
            }
        }
        $entity = new User();
    }

    if( $ldap_success ){
        $entity->setUsername($post_send['username']);
        $entity->setRut($post_send['rut']);
        //$entity->setSalt("ssss");
        if (array_key_exists('password', $post_send)) {
            if ( $post_send['password'] != "" ) {
                $entity->setPassword($post_send['password']);
                $this->setSecurePassword($entity);
            }
        }
        $entity->setEmail($post_send['email']);
        $entity->setDateAdded(new \DateTime());
        $entity->setIsActive(true);
        $entity->setIsAdmin(true);
        $entity->setUserRoles($em->getRepository('UserBundle:Role')->find( $post_send['admin_roles_id'] ));
        $entity->setWizardCompleted(true);
        $entity->setPath("s");
        $em->persist($entity);
        $em->flush();
        $json = new JsonUtils();
        return $json->arrayToJson(array("id"=>$entity->getId()));
    }
    return $json->arrayToJson( array("sueccess"=>false ) );
}

这里有一个问题How do I make a ldap search with anonymous binding?与我的问题相关,根本没有答案。

最佳答案

关于您的 AD 查询来检查用户是否存在(从 else 语句的开头开始),我会将逻辑设为如下所示:

$ldapconn = ldap_connect("10.0.0.230");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0)
// Use a LDAP service account with only read access...
$searchUser = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="186d6b7d6a5875615c7775797176367b7775" rel="noreferrer noopener nofollow">[email protected]</a>';
$searchPass = '12345';

$ldap_success = false;
if (@ldap_bind($ldapconn, $searchUser, $searchPass)) {
    $attributes = ['cn'];
    $filter = "(&(objectClass=user)(objectCategory=person)(userPrincipalName=".ldap_escape($post_send['username'], null, LDAP_ESCAPE_FILTER)."))";
    $baseDn = "DC=myDomain,DC=com";
    $results = @ldap_search($ldapconn, $baseDn, $filter, $attributes);
    $info = @ldap_get_entries($ldapconn, $results);

    $ldap_success = ($info && $info['count'] === 1);
}

在这种情况下,只有当 AD 中存在 UPN 的用户时,$ldap_success 才会为 true指定的。

但是,如果您需要用户的密码并绑定(bind)到 AD,则没有理由在绑定(bind)后还验证是否可以通过查询找到该用户。在这种情况下,简单地与 AD 结合就足以证明它们存在于 AD 中。

关于php - 创建用户,但使用 PHP 检查 Active Directory 中是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106940/

相关文章:

php - 通过 <a href> 使用 mysql 主机在 PHP 中打开新页面

java - 在 ssl (ldaps) 的支持下连接 Activity 目录

c++ - 如何判断路径是否来自 Active Directory 重定向文件夹?

php - 从 php 中的 Zip 下载中排除隐藏文件

php - 本地 WordPress 开发问题 index.php 和管理工作 - 没有其他页面响应

php - 如何在 MySQL UPDATE 查询期间连接两个字符串?

php - 如何在没有 --prefer-source 的情况下使用 Composer 克隆存储库? (使用 Symfony 2)

mysql - Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

php - 谷歌索引 API - 403 'Forbidden Response'

authentication - Apache Shiro - 使用 cn 以外的属性进行身份验证?