/**
* @see org.apache.cocoon.auth.ApplicationManager#login(String, java.util.Map)
*/
public User login(final String appName, final Map loginContext) throws Exception {
final Map objectModel = this.processInfoProvider.getObjectModel();
final Application application = this.getApplication(appName);
final String securityHandlerKey = this.getKey(application.getSecurityHandler());
User user = null;
// first check, if we are already logged in
if ( this.isLoggedIn(appName) ) {
user = ApplicationUtil.getUser(objectModel);
} else {
final Request req = ObjectModelHelper.getRequest(objectModel);
HttpSession session = req.getSession(false);
LoginInfo info = null;
Map loginInfos = null;
if ( session != null ) {
// is the user already logged in on the security handler?
loginInfos = (Map)session.getAttribute(LOGIN_INFO_KEY);
if ( loginInfos != null && loginInfos.containsKey(securityHandlerKey)) {
info = (LoginInfo)loginInfos.get(securityHandlerKey);
user = info.user;
}
}
if ( user == null ) {
user = application.getSecurityHandler().login(loginContext);
if ( user != null ) {
// create new login info
session = req.getSession();
loginInfos = (Map)session.getAttribute(LOGIN_INFO_KEY);
if ( loginInfos == null ) {
loginInfos = new HashMap();
}
info = new LoginInfo(user);
loginInfos.put(securityHandlerKey, info);
}
}
// user can be null, if login failed
if ( user != null ) {
info.incUsageCounter(appName);
session.setAttribute(LOGIN_INFO_KEY, loginInfos);
// set the user in the session
session.setAttribute(USER + '-' + appName, user);
objectModel.put(ApplicationManager.USER, user);
// set the application in the object model
objectModel.put(ApplicationManager.APPLICATION, application);
// set the application data in the session
Object data = ObjectUtils.NULL;
if ( application.getApplicationStore() != null ) {
data = application.getApplicationStore().loadApplicationData(user, application);
}
session.setAttribute(APPLICATION_KEY_PREFIX + appName, data);
objectModel.put(ApplicationManager.APPLICATION_DATA, data);
// notify the application about successful login
application.userDidLogin(user, loginContext);
// notify the application about accessing
application.userIsAccessing(user);
}
}
return user;
}