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-08-16 17:40:02 +02:00
|
|
|
import lombok.Getter;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
public class CommaFeedSession extends AuthenticatedWebSession {
|
|
|
|
|
|
2013-04-15 21:50:36 +02:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
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
|
|
|
|
2013-08-16 17:40:02 +02:00
|
|
|
@Getter(lazy = true)
|
|
|
|
|
private final CommaFeedSessionServices services = newServices();
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
public CommaFeedSession(Request request) {
|
|
|
|
|
super(request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User getUser() {
|
|
|
|
|
return 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) {
|
2013-08-16 17:40:02 +02:00
|
|
|
User user = getServices().getUserService().login(userName, password);
|
2013-05-01 21:56:59 +02:00
|
|
|
setUser(user);
|
|
|
|
|
return user != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setUser(User user) {
|
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();
|
2013-08-16 17:40:02 +02:00
|
|
|
for (Role role : getServices().getUserRoleDAO().findRoles(user)) {
|
2013-03-30 19:06:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2013-08-16 17:40:02 +02:00
|
|
|
private CommaFeedSessionServices newServices() {
|
|
|
|
|
return new CommaFeedSessionServices();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|