How can I delete a status message added by the seam safety module?
I would like to show a different status message when the suspended user tries to log in. If the user is active, we return true from the authentication method, unless we add a special StatusMessage that mentions "User X has been suspended". Identity authentication also fails and adds StatusMessage. I tried to delete the popup statusMessage file using the following methods but it doesn't work and shows me 2 different status messages (my own message generated by the seam). What is the problem?
StatusMessages statusMessages;
statusMessages.clear()
statusMessages.clearGlobalMessages()
statusMessages.clearKeyedMessages(id)
EDIT1:
public boolean authenticate() {
log.info("Authenticating {0}", identity.getCredentials().getUsername());
String username = identity.getCredentials().getUsername();
String password = identity.getCredentials().getPassword();
// return true if the authentication was
// successful, false otherwise
try {
Query query = entityManager.createNamedQuery("user.by.login.id");
query.setParameter("loginId", username);
// only active users can log in
query.setParameter("status", "ACTIVE");
currentUser = (User)query.getSingleResult();
} catch (PersistenceException ignore) {
// Provide a status message for the locked account
statusMessages.clearGlobalMessages();
statusMessages.addFromResourceBundle(
"login.account.locked", new Object[] { username });
return false;
}
IdentityManager identityManager = IdentityManager.instance();
if (!identityManager.authenticate(username, "password")) {
return false;
} else {
log.info("Authenticated user {0} successfully", username);
}
}
a source to share
You can see the status messages used by Seam (you must define them in the resource bundle)
- org.jboss.seam.loginFailed
- org.jboss.seam.loginSuccessful
- org.jboss.seam.NotLoggedIn
This way you can override the key org.jboss.seam.loginFailed (remember to register your resource)
somePropertiesFile.properties
org.jboss.seam.loginFailed=<YOUR_CUSTOM_MESSAGE_GOES_HERE>
And use the following
<h:messages globalOnly="true"/>
To show authentication messages
UPDATE
If you want to create your own message, follow these steps
As of Seam 2.1, yours must authenticate the user by entering an account instead of Identity
@Name("authenticationManager")
public class AuthenticationManager {
private @In org.jboss.seam.security.Credentials credentials;
public boolean authenticate() {
private String username = credentials.getUsername();
private String password = credentials.getPassword();
try {
Query query = entityManager.createNamedQuery("user.by.login.id");
query.setParameter("loginId", username);
query.setParameter("status", "ACTIVE");
currentUser = (User) query.getSingleResult();
} catch (PersistenceException ignore) {
return false;
}
return true;
}
}
And inside your JSF form use credentials again instead of id
<h:inputText id="username" value="#{credentials.username}"/>
<h:inputText id="password" value="#{credentials.password}"/>
To show your own message , please do as follows
<h:outputText value="#{credentials.username} has been suspended" rendered="#{not identity.loggedIn}"/>
Now I hope everything will be fine!
a source to share
I had similar problems (this post thing was one of many to trust me!) And I just no longer use the seam safety module. It's not very flexible / extensible, even all the docs say (I think it's a lot of self-employment on the part of the developer who wrote the whole thing). A similar example I ran into the problem was handling a forced password change during the login process. I plucked out a lot of my hair before deciding that the entire seam safety module was simply out of order. It hurt a little, but I have no regrets at all. Good luck!
a source to share