Make User available from Authentication via DaoAuthenticationProvider.
This commit is contained in:
@@ -19,6 +19,7 @@ import net.sf.acegisecurity.AccessDeniedException;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
@@ -88,7 +89,13 @@ public class ContactManagerFacade implements ContactManager, InitializingBean {
|
||||
Authentication auth = ((SecureContext) ContextHolder.getContext())
|
||||
.getAuthentication();
|
||||
|
||||
if (auth.getPrincipal().toString().equals(result.getOwner())) {
|
||||
String username = auth.getPrincipal().toString();
|
||||
|
||||
if (auth.getPrincipal() instanceof User) {
|
||||
username = ((User) auth.getPrincipal()).getUsername();
|
||||
}
|
||||
|
||||
if (username.equals(result.getOwner())) {
|
||||
return result;
|
||||
} else {
|
||||
throw new AccessDeniedException(
|
||||
|
||||
@@ -18,6 +18,7 @@ package sample.contact;
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.ConfigAttribute;
|
||||
import net.sf.acegisecurity.ConfigAttributeDefinition;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
import net.sf.acegisecurity.vote.AccessDecisionVoter;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
@@ -96,9 +97,15 @@ public class ContactSecurityVoter implements AccessDecisionVoter {
|
||||
}
|
||||
|
||||
if (passedOwner != null) {
|
||||
String username = authentication.getPrincipal().toString();
|
||||
|
||||
if (authentication.getPrincipal() instanceof User) {
|
||||
username = ((User) authentication.getPrincipal())
|
||||
.getUsername();
|
||||
}
|
||||
|
||||
// Check the authentication principal matches the passed owner
|
||||
if (passedOwner.equals(authentication.getPrincipal()
|
||||
.toString())) {
|
||||
if (passedOwner.equals(username)) {
|
||||
return ACCESS_GRANTED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import net.sf.acegisecurity.AuthenticationCredentialsNotFoundException;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
@@ -74,10 +75,17 @@ public class SecureIndexController implements Controller, InitializingBean {
|
||||
+ "SecureContext");
|
||||
}
|
||||
|
||||
final Authentication currentUser = secureContext.getAuthentication();
|
||||
// Lookup username. As we must accommodate DaoAuthenticationProvider,
|
||||
// CAS and container based authentication, we take care with casting
|
||||
Authentication auth = secureContext.getAuthentication();
|
||||
String username = auth.getPrincipal().toString();
|
||||
|
||||
if (auth.getPrincipal() instanceof User) {
|
||||
username = ((User) auth.getPrincipal()).getUsername();
|
||||
}
|
||||
|
||||
boolean supervisor = false;
|
||||
GrantedAuthority[] granted = currentUser.getAuthorities();
|
||||
GrantedAuthority[] granted = auth.getAuthorities();
|
||||
|
||||
for (int i = 0; i < granted.length; i++) {
|
||||
if (granted[i].getAuthority().equals("ROLE_SUPERVISOR")) {
|
||||
@@ -85,13 +93,12 @@ public class SecureIndexController implements Controller, InitializingBean {
|
||||
}
|
||||
}
|
||||
|
||||
Contact[] myContacts = contactManager.getAllByOwner(currentUser.getPrincipal()
|
||||
.toString());
|
||||
Contact[] myContacts = contactManager.getAllByOwner(username);
|
||||
|
||||
Map model = new HashMap();
|
||||
model.put("contacts", myContacts);
|
||||
model.put("supervisor", new Boolean(supervisor));
|
||||
model.put("user", currentUser.getPrincipal().toString());
|
||||
model.put("user", username);
|
||||
|
||||
return new ModelAndView("index", "model", model);
|
||||
}
|
||||
|
||||
@@ -15,8 +15,10 @@
|
||||
|
||||
package sample.contact;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.SecureContext;
|
||||
import net.sf.acegisecurity.providers.dao.User;
|
||||
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
@@ -54,8 +56,14 @@ public class WebContactAddController extends SimpleFormController {
|
||||
public ModelAndView onSubmit(Object command) throws ServletException {
|
||||
String name = ((WebContact) command).getName();
|
||||
String email = ((WebContact) command).getEmail();
|
||||
String owner = ((SecureContext) ContextHolder.getContext()).getAuthentication()
|
||||
.getPrincipal().toString();
|
||||
|
||||
Authentication auth = ((SecureContext) ContextHolder.getContext())
|
||||
.getAuthentication();
|
||||
String owner = auth.getPrincipal().toString();
|
||||
|
||||
if (auth.getPrincipal() instanceof User) {
|
||||
owner = ((User) auth.getPrincipal()).getUsername();
|
||||
}
|
||||
|
||||
Contact contact = new Contact(contactManager.getNextId(), name, email,
|
||||
owner);
|
||||
|
||||
Reference in New Issue
Block a user