User's Manual

120 Chapter 10. Kernel Tutorial
8. How do I log in a user using a username and password?
KernelHelper.getKernelRequestContext(request).getUserContext().login(username,
password, forever), where forever should be true if the user should be granted a permanent
login cookie, false if the cookie should only last for the current session. For an example, see
com.arsdigita.ui.login.UserRegistrationForm.
Tip
There are several subtle security issues in managing passwords safely, so developers are encour-
aged to use the provided UI code rather than creating their own. This ensures that developers auto-
matically benefit from fixes in future releases.
9. How do I log in as another user (Administrators only)?
Three methods allow an administrator to assume the role of another user the "\" is used to break
the lines for printing purposes only::
1. KernelHelper.getKernelRequestContext(request).getUserContext().\
login(username)
2. KernelHelper.getKernelRequestContext(request).getUserContext().\
login(userID)
3. KernelHelper.getKernelRequestContext(request).getUserContext().\
login(User)
The first two versions of this method are convenience wrappers for the third. These methods require
that the current user be an administrator.
After calling any of these methods, the UserContext is updated with the target user’s information;
however, cached user information (such as that provided by UserAuthenticationListener) will
still contain the administrator’s information.
Immediately after calling any of these methods, the client should be redirected to a new page (such as
the user workspace). See com.arsdigita.ui.admin.UserLoginPage for an example.
10. How do I log out a user?
KernelHelper.getKernelRequestContext(request).getUserContext().logout() logs
out the current user. com.arsdigita.ui.login.UserLogoutListener is an ActionListener
that logs out the user by calling this method.
Developers can link to this action listener using an ActionLink. Alternately, developers can link
to a com.arsdigita.ui.login.UserLogoutPage; this page logs out the user and immediately
redirects the client to the registration page.
11. How do I get a user’s email address?
The user’s User object given to the method user.getPrimaryEmail().getEmailAddress()
returns the user’s primary email address as a string. It is possible that getPrimaryEmail() or
getEmailAddress() might return null.
com.arsdigita.ui.login.EmailInitListener is a FormInitListener that
initializes a form parameter with the current user’s email address, if possible. If the
user is not logged in or has no email address, it leaves the parameter unchanged. See
com.arsdigita.ui.login.UserRegistrationForm for an example.