问题
So I'm using the FOSUserBundle with LDAP as my authentication method, and I'm wondering whether there is a way to remove/ignore the password
field for my FOSUser
entity?
I realize removal might not be ideal (in case it messes with internal logic), but the column is never used, and I'm forced to fill it when I'm creating FOSUsers
from fixtures:
$user = new FOSUser();
$user->setDn($item["dn"]);
$user->setEnabled(1);
$user->setUsername($item["samaccountname"][0]);
$user->setUsernameCanonical(strtolower($item["samaccountname"][0]));
$user->setEmail($item["mail"][0]);
$user->setEmailCanonical(strtolower($item["mail"][0]));
$user->setDepartment($ldap_groups[$matches[1]]);
$user->setDepartmentDn($group);
$user->setPassword('blank'); // Is there away to avoid this?
$manager->persist($user);
回答1:
You could actually remove the password, but you have to keep a getPassword()
as that is defined in the UserInterface. In case you want to keep it, e.g. when you allow for multiple login types where you will need it again. I recommend setting the field as nullable. If you are using annotations it's as simple as adding nullable=true
to the column:
/**
* @ORM\Column(type="string", name="password", nullable=true)
*/
回答2:
So to override a column doctrine allows for Inheritance Mapping:
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="password",
* column=@ORM\Column(
* name = "password",
* type = "string",
* nullable=true
* )
* )
* })
*/
class FOSUser extends BaseUser implements LdapUserInterface
{
// Extended logic.
}
The password field still resides in BaseUser
, but is overwritten by the @ORM\AttributeOverride
annotation.
来源:https://stackoverflow.com/questions/43108632/fosuserbundle-ignore-password-field