/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.web;
import java.util.Locale;
import org.apache.wicket.Request;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.bricket.plugin.authentication.service.AuthenticationService;
import org.bricket.plugin.language.domain.Language;
import org.bricket.plugin.language.service.LanguagePluginService;
import org.bricket.plugin.language.service.LanguageService;
import org.bricket.plugin.user.domain.User;
import org.bricket.service.BricketServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* @author Henning Teek
* @author Ingo Renner
*/
public class BricketWebSession extends AuthenticatedWebSession {
private final Logger log = LoggerFactory.getLogger(BricketWebSession.class);
@SpringBean
private AuthenticationService authenticationService;
@SpringBean
private LanguagePluginService languagePluginService;
@SpringBean
private LanguageService languageService;
private User user;
public BricketWebSession(Request request) {
super(request);
InjectorHolder.getInjector().inject(this);
}
@Override
public boolean authenticate(String username, String password) {
try {
this.user = authenticationService.authenticateUser(username, password);
return true;
} catch (BricketServiceException e) {
log.error("authentication failed", e);
}
return false;
}
@Override
public Roles getRoles() {
Roles roles = new Roles();
if (isSignedIn()) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
log.error("Unexpected: Authentication not found. No roles granted. SecurityContext="
+ SecurityContextHolder.getContext().toString());
} else {
for (GrantedAuthority authority : authentication.getAuthorities()) {
roles.add(authority.getAuthority());
}
}
}
return roles;
}
public User getUser() {
return user;
}
public String getI18nLink(String path) {
String locale = getLocale().toString();
if (locale.indexOf('_') >= 0) {
locale = locale.substring(0, locale.indexOf('_'));
}
Language lang = languageService.loadLanguageByLocale(locale);
if (lang == null) {
locale = languagePluginService.loadLanguagePlugin().getDefaultLanguage().getLocale();
setLocale(new Locale(locale));
}
return "/" + locale + path;
}
}