我正在构建一个Symfony2应用程序,当用户在网站上注册时,我需要连接到我的AD服务器来添加用户。我使用SF2中包含的ZendLdap类。我还使用FOSUserBundle来处理注册和登录。与服务器的连接工作正常,但当我尝试添加一个条目时出现了这个错误:
0x10 (No such attribute; 00000057: LdapErr: DSID-0C090D87, comment: Error in attribute conversion operation, data 0, v2580): adding: cn=jordane,ou=CUBBYHOLE,dc=cubbyhole,dc=me"}]
我搜索了很多,想知道为什么我不能添加我的用户,我认为我所有的属性都可以,拼写正确,但可能我做了一个错误的配置。这是我的课:
class LdapRegistrationController extends BaseController {
protected $ldapDriver;
public function __construct()
{
$ldapOptions = array(
'host' => 'cubbyhole.me',
'port' => 389,
'useStartTls' => false,
'useSsl' => false,
'username' => 'CUBBYHOLE'Administrateur',
'accountCanonicalForm' => 3,
'password' => 'mypassword',
'accountDomainName' => 'cubbyhole.me',
'accountDomainNameShort' => 'CUBBYHOLE',
'baseDn' => 'CN=Users, dc=cubbyhole, dc=me',
'accountFilterFormat' => '(&(objectClass=Top:person;organizationalPerson;user))'
);
//connection
try
{
$this->ldapDriver = new Ldap($ldapOptions);
$this->ldapDriver->bind();
}
catch(Exception $e)
{
throw new LdapException("Error connecting to LDAP. Exception message: ".$e->getMessage());
}
}
/**
* @param User $user
* @throws 'Zend'Ldap'Exception'LdapException
*/
public function addUserInLdap(User $user=null)
{
if($user != null)
{
//add in ldap
$entry = array();
Attribute::setAttribute($entry, 'cn', $user->getUsername());
Attribute::setAttribute($entry, 'sn', $user->getUsername());
Attribute::setAttribute($entry, 'givenName', $user->getUsername());
Attribute::setAttribute($entry, 'displayName', $user->getUsername());
Attribute::setAttribute($entry, 'userPrincipalName', $user->getUsername());
Attribute::setAttribute($entry, 'distinguishedName', $user->getUsername());
$objectClass = array('top:person', 'organizationalPerson', 'user');
Attribute::setAttribute($entry, 'objectClass', $objectClass);
Attribute::setAttribute($entry, 'objectCategory', 'person');
Attribute::setAttribute($entry, 'mail', $user->getEmailCanonical());
Attribute::setAttribute($entry, 'userAccountControl', 66048);
Attribute::setPassword($entry, $user->getPlainPassword(), Attribute::PASSWORD_HASH_SHA);
try
{
$this->ldapDriver->add('cn='.$user->getUsername().',ou=CUBBYHOLE,dc=cubbyhole,dc=me', $entry);
}
catch(Exception $e)
{
throw new LdapException("LDAP Error. Exception message: ".$e->getMessage());
}
}
else
throw new LdapException("Object user is null");
}
}
有人知道可能出了什么问题吗?感谢:(
我认为问题在于distinguishedName
和userPrincipalName
属性的格式。用户主体名称应采用类似username@domain.local
的格式,而可分辨名称本质上是对象的LDAP路径,如cn=someUser,ou=someOU,dc=domain,dc=local
。
Attribute::setAttribute($entry, 'userPrincipalName', $user->getUsername() . '@cubbyhole.me');
Attribute::setAttribute($entry, 'distinguishedName', 'cn=' . $user->getUsername() . ',ou=CUBBYHOLE,dc=cubbyhole,dc=me');
好吧,多亏了ChadSikorra的帮助,我终于管理好了。现在一切都很好,我在AD中创建了一个实体,它有一个激活的用户帐户和一个永远不会过期的密码。问题出在我试图匹配的实体类型上,在AD中,用户被称为inetOrgPerson,因此无法工作。这里是那些可能遇到同样问题的人的最终代码。祝你今天愉快!
public function addUserInLdap(User $user=null)
{
if($user != null)
{
//add in AD
$entry = array();
Attribute::setAttribute($entry, 'cn', $user->getUsername());
Attribute::setAttribute($entry, 'sn', $user->getUsername());
Attribute::setAttribute($entry, 'userPrincipalName', $user->getEmailCanonical());
Attribute::setAttribute($entry, 'samAccountName', $user->getUsername());
Attribute::setAttribute($entry, 'objectClass', 'inetOrgPerson');
Attribute::setAttribute($entry, 'mail', $user->getEmailCanonical());
Attribute::setAttribute($entry, 'userAccountControl', 66080); //activated account with non-expire password
Attribute::setPassword($entry, $user->getPlainPassword(), Attribute::PASSWORD_HASH_SHA);
try
{
$this->ldapDriver->add('cn='.$user->getUsername().',ou=CUBBYHOLE,dc=cubbyhole,dc=me', $entry);
}
catch(Exception $e)
{
throw new LdapException("Error inserting in LDAP. Exception message: ".$e->getMessage());
}
}
else
throw new LdapException("Can't add user in LDAP. User is null");
}