sionsmith wrote:
here is a simple example of what i have:

user = userManager.getUserByUsername(getRequest().getRemoteUser());

I've found there are times when I need basic info from the current user object (which acegi stores in the session, so going to the DB is not necessary), and times when I want that object to be part of the current hibernate session so that I can follow various lazy linkages:

  /**
   * Convenience method to get the current User object.
   *
   * @param userManager Service with which to look up user by username,
   *                    if necessary
   * @param forceReloadInCurrentSession if lazy collections are to be
   *        referenced, the user must be re-fetched
   * @return The current User object, null if not logged in.
   */
  public static User getCurrentUser(UserManager userManager,
                                  boolean forceReloadInCurrentSession) {
    SecurityContext ctx = SecurityContextHolder.getContext();
    Authentication auth = ctx.getAuthentication();
    if (auth == null) {
      return null;
    }
    Object principal = auth.getPrincipal();
    User user;

    // the Acegi assigned principal is typically an instance of User
    // except for certain cases where the user programatically updates
    // the authentication token (eg users' password) by calling code the
    // code below in which case the principal will be an instance of
    // String for the rest of the duration of the current
    // request-response.
// sc.setAuthentication(new UsernamePasswordAuthenticationToken("user", "password"));

    if (principal instanceof User) {
      if (forceReloadInCurrentSession) {
user = userManager.getUserByUsernamePrivacyOverride(((User)principal).getUsername());
      } else {
        user = (User) principal;
      }
    } else if (principal instanceof String) {
      if (((String) principal).equalsIgnoreCase("anonymous")) {
        user = null;
      } else {
user = userManager.getUserByUsernamePrivacyOverride((String) principal);
      }
    } else {
      throw new IllegalArgumentException(
                         "Unrecognized principal type : " + principal);
    }

    return user;
  }

-Dale

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to