if (principal == null) {
return null;
}
// Read user from DB
User user = getUserFromDB(principal.getName());
// Create new user object
if (user == null) {
user = createUser(principal.getName());
}
// Get attributes
HttpSession session = request.getSession();
@SuppressWarnings("unchecked")
Map<String, List<Object>> attributes = (Map<String, List<Object>>) session
.getAttribute("SESSION_ATTRIBUTE_MAP");
String email = null;
String firstName = null;
String lastName = null;
if (attributes != null) {
// Set transient data from attributes
List<Object> firstNameList = attributes.get("firstName");
if (firstNameList != null) {
if (firstNameList.size() > 0) {
firstName = (String) firstNameList.get(0);
}
}
List<Object> lastNameList = attributes.get("lastName");
if (lastNameList != null) {
if (lastNameList.size() > 0) {
lastName = (String) lastNameList.get(0);
}
}
List<Object> emailList = attributes.get("email");
if (emailList != null) {
if (emailList.size() > 0) {
email = (String) emailList.get(0);
}
}
}
ArrayList<Role> roles = new ArrayList<Role>();
if (request.isUserInRole("authenticated")) {
Role role = new Role();
role.setName("authenticated");
roles.add(role);
}
if (request.isUserInRole("admin")) {
Role role = new Role();
role.setName("admin");
roles.add(role);
}
user.setRoles(roles);
boolean update = false;
if (email != null && !email.equals("") && !email.equals(user.getEmail())) {
user.setEmail(email);
update = true;
}
if (firstName != null && !firstName.equals("") && !firstName.equals(user.getFirstName())) {
user.setFirstName(firstName);
update = true;
}
if (lastName != null && !lastName.equals("") && !lastName.equals(user.getLastName())) {
user.setLastName(lastName);
update = true;
}
if (update) {
em.merge(user);
log.debug("Updating email address of user " + user.getUsername());
}
// try {
// Subject caller = (Subject) PolicyContext
// .getContext("javax.security.auth.Subject.container");