-
1. Re: IdentityManager : useful for non default use case ?
shane.bryzak Aug 27, 2008 1:04 AM (in response to gaboo.gael.livre-rare-book.com)You can extend UserAction to include additional fields if you need them. Also, if you're using JpaIdentityStore, then it raises the event org.jboss.seam.security.management.prePersistUser before persisting the user entity, giving you the opportunity to set additional fields on the entity object before persisting to the database. You simply need to add an observer for this event to your extended UserAction component, and set the additional field values there.
-
2. Re: IdentityManager : useful for non default use case ?
tognado Feb 11, 2009 12:13 PM (in response to gaboo.gael.livre-rare-book.com)
You can extend UserAction to include additional fields if you need them. Also, if you're using JpaIdentityStore, then it raises the event org.jboss.seam.security.management.prePersistUser before persisting the user entity, giving you the opportunity to set additional fields on the entity object before persisting to the database. You simply need to add an observer for this event to your extended UserAction component, and set the additional field values there.Hello, Shane, that is exactly what i am looking for, but all I can find is WHAT i should do to get those extra fields in my user form, not exactly HOW.
I did:
@Observer("org.jboss.seam.security.management.prePersistUser") public void onPrePersist(User user) { } @Observer("org.jboss.seam.security.management.userCreated") public void onUserCreated(User user) { }
and that's all i got.
How do I persist myemail
extra field extending UserAction ?if i code
<h:outputLabel for="email" value="Email" styleClass="formLabel"/> <h:inputText id="email" value="#{userAction.email}" readonly="#{identityManager.userExists(userAction.email)}"/> <h:message for="email" styleClass="validationError"/>
i got a problem, since userAction doesn't have this field.
So, how to display fulfilled fields when an user try to view/update it ?
Thiago
-
3. Re: IdentityManager : useful for non default use case ?
radu Apr 27, 2009 5:51 PM (in response to gaboo.gael.livre-rare-book.com)I also spent one day with this issue.
I wanted a way to add user account confirmation and to use the default forms generated by seam-get, UserAccountHome and UserAccountList with all my custom fields and without much modifications to the xhtml files.
I have relations with another tables in my UserAccount component, so I can not use UserAction class.The easiest solution for me was to create a method in UserAccountHome class which will:
- check for password confirmation
- call identityManager.createUser(getInstance().getUsername(), password) (you will have the entity persisted after this call)
- call identityManager.grantRole(getInstance().getUsername(), role);
- retrieve the new user from the database: UserAccount userAccount = userAccountDAO.findUser(usernameSaved, false, false);
- set your new fields as wanted (like userAccount.setActivationCode(activationCode);)
- persist the user with the new values (getEntityManager().persist(userAccount);)
You will need to add the fieleds private List<String> roles, private String password and private String confirm to your UserAccountHome class and use them in UserAccountEdit.xhtml (instead of userAccountHome.instance.roles)
The save button will call userAccountHome.save method
Code example:
@Name("userAccountHome") public class UserAccountHome extends EntityHome<UserAccount> { .... @In IdentityManager identityManager; @In UserAccountDAO userAccountDAO; private List<String> roles; private String password; private String confirm; .... public String save() { if ((getInstance() == null) || (getInstance().getUsername().trim().length() < 2) || (identityManager.userExists(getInstance().getUsername()))) { FacesMessages.instance().addToControl("username", "Failed to create user"); log.debug("user '{0}' already exists", getInstance().getUsername()); return "failure"; } String usernameSaved = getInstance().getUsername(); boolean isEnabled = getInstance().isEnabled(); boolean isActivated = getInstance().isActivated(); if (!password.equals(confirm)) { FacesMessages.instance().addToControl("password", "Passwords do not match"); log.debug("Password do not match: {0} - {1}", password, confirm); return "failure"; } boolean success = identityManager.createUser(usernameSaved, password); log.info("User created: {0}", usernameSaved); if (success) { for (String role : roles) { identityManager.grantRole(usernameSaved, role); log.info("Adding role to user {0}: {1}", usernameSaved, role); } if (!isEnabled || !isActivated) { identityManager.disableUser(usernameSaved); } Date currentDate = new Date(); String raw = "md5".concat(getInstance().getUsername()).concat(currentDate.toString()); String activationCode = DigestUtils.md5Hex(raw); log.info("Setting activation code to: {0}", usernameSaved); // Retrieve back entity saved by identityManager and set additional values UserAccount userAccount = userAccountDAO.findUser(usernameSaved, false, false); userAccount.setActivationCode(activationCode); userAccount.setActivated(isActivated); this.getEntityManager().persist(userAccount); this.clearInstance(); return "success"; } return "failure"; } ....
Example of UserAccountEdit.xhtml
.... <s:decorate id="usernameField" template="/layout/edit.xhtml"> <ui:define name="label">Username</ui:define> <h:inputText id="username" value="#{userAccountHome.instance.username}" readonly="#{identityManager.userExists(userAccountHome.instance.username)}"/> </s:decorate> <s:decorate id="passwordField" template="/layout/edit.xhtml"> <ui:define name="label">Password</ui:define> <h:inputSecret id="password" value="#{userAccountHome.password}"/> </s:decorate> <s:decorate id="confirmPasswordField" template="/layout/edit.xhtml"> <ui:define name="label">Confirm Password</ui:define> <h:inputSecret id="confirm" value="#{userAccountHome.confirm}"/> </s:decorate> <s:decorate id="rolesField" template="/layout/edit.xhtml"> <ui:define name="label">Member of roles</ui:define> <div class="selectMany"> <h:selectManyCheckbox id="roles" value="#{userAccountHome.roles}" layout="pageDirection" styleClass="roles"> <s:selectItems value="#{identityManager.listGrantableRoles()}" var="_role" label="#{_role}"/> </h:selectManyCheckbox> </div> </s:decorate> <s:decorate id="enabledField" template="/layout/edit.xhtml"> <ui:define name="label">Account enabled</ui:define> <h:selectBooleanCheckbox id="enabled" value="#{userAccountHome.instance.enabled}"/> </s:decorate> .... <h:commandButton id="save" value="Save" action="#{userAccountHome.save}" rendered="#{!identityManager.userExists(userAccountHome.instance.username)}"/>
I hope this will save someone time...