InitialContext initialContext = new InitialContext();
SessionFactory sessionFactory = (SessionFactory)initialContext.lookup("java:/portal/IdentitySessionFactory");
session = sessionFactory.openSession();
tx = session.beginTransaction();
User user = this.userModule.findUserByUserName(username);
if(user != null)
{
//Check and make sure the user account is enabled
Boolean enabled = (Boolean)this.profileModule.getProperty(user, User.INFO_USER_ENABLED);
if(enabled != null && enabled.booleanValue())
{
//Check and make sure user has proper role setup
if(this.havingRole != null && this.havingRole.trim().length()>0)
{
boolean hasTheRole = false;
Set roles = this.membershipModule.getRoles(user);
for (Iterator i = roles.iterator(); i.hasNext();)
{
Role role = (Role)i.next();
if (this.havingRole.equals(role.getName()))
{
hasTheRole = true;
break;
}
}
if (!hasTheRole)
{
return false;
}
}
//Now perform validation
status = user.validatePassword(password);
}
}
return status;
}