2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.frontend;
|
|
|
|
|
|
2013-03-30 19:06:32 +01:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import org.apache.wicket.Session;
|
|
|
|
|
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
|
|
|
|
|
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
|
|
|
|
|
import org.apache.wicket.request.Request;
|
|
|
|
|
|
2013-03-29 12:59:21 +01:00
|
|
|
import com.commafeed.backend.dao.UserRoleService;
|
2013-03-20 20:33:42 +01:00
|
|
|
import com.commafeed.backend.dao.UserService;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.User;
|
2013-03-30 19:06:32 +01:00
|
|
|
import com.commafeed.backend.model.UserRole.Role;
|
|
|
|
|
import com.google.common.collect.Sets;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-03-21 08:55:53 +01:00
|
|
|
@SuppressWarnings("serial")
|
2013-03-20 20:33:42 +01:00
|
|
|
public class CommaFeedSession extends AuthenticatedWebSession {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserService userService;
|
|
|
|
|
|
2013-03-29 12:59:21 +01:00
|
|
|
@Inject
|
|
|
|
|
UserRoleService userRoleService;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
private User user;
|
2013-03-29 12:59:21 +01:00
|
|
|
private Roles roles = new Roles();
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
public CommaFeedSession(Request request) {
|
|
|
|
|
super(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User getUser() {
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setUser(User user) {
|
|
|
|
|
this.user = user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommaFeedSession get() {
|
|
|
|
|
return (CommaFeedSession) Session.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Roles getRoles() {
|
2013-03-29 12:59:21 +01:00
|
|
|
return roles;
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean authenticate(String userName, String password) {
|
|
|
|
|
User user = userService.login(userName, password);
|
2013-03-29 12:59:21 +01:00
|
|
|
if (user == null) {
|
|
|
|
|
this.user = null;
|
|
|
|
|
this.roles = new Roles();
|
|
|
|
|
} else {
|
2013-03-30 19:06:32 +01:00
|
|
|
|
|
|
|
|
Set<String> roleSet = Sets.newHashSet();
|
|
|
|
|
for (Role role : userRoleService.getRoles(user)) {
|
|
|
|
|
roleSet.add(role.name());
|
|
|
|
|
}
|
2013-03-29 12:59:21 +01:00
|
|
|
this.user = user;
|
2013-03-30 19:06:32 +01:00
|
|
|
this.roles = new Roles(roleSet.toArray(new String[0]));
|
2013-03-29 12:59:21 +01:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
return user != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|