//if not Hibernate user try to obtain it using userName
Session session = getCurrentSession();
Query query = session.createQuery("from HibernateUserImpl where userName=:userName");
query.setParameter("userName", user.getUserName());
query.setCacheable(true);
HibernateUserImpl hu = (HibernateUserImpl)query.uniqueResult();
if (hu != null )
{
return hu;
}
else if (!isSynchronizeNonExistingUsers())
{
throw new IdentityException("No user in DB - set synchronizeNonExistingUsers option to true");
}
else
{
try
{
hu = new HibernateUserImpl(user.getUserName());
//user.updatePassword(user.getPassword());
if (defaultSynchronizePassword != null)
{
hu.updatePassword(getDefaultSynchronizePassword());
}
//really dummy password generation
//TODO: make something more sophisticated (risk of this part is documented)
else if (randomSynchronizePassword)
{
Random r = new Random();
StringBuffer password = new StringBuffer();
for (int i = 0; i < 10; i++)
{
password.append(r.nextDouble());
}
hu.updatePassword(password.toString());
}
session = getCurrentSession();
//so if we synchronize from LDAP lets make the user enabled
if (isEnableSynchronizedUsers())
{
hu.setEnabled(true);
}
session.save(hu);
return hu;
}