Add configurable support for admin profile.
This commit is contained in:
@@ -25,7 +25,6 @@ public class User
|
||||
extends Collaborator
|
||||
implements Serializable {
|
||||
|
||||
private static final String ADMIN_EMAIL = "test@wisemapping.org";
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private String password;
|
||||
@@ -132,8 +131,4 @@ public class User
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return ADMIN_EMAIL.equals(this.getEmail());
|
||||
}
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ public class AdminController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/xml", "application/json"})
|
||||
@RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/xml", "application/json","text/html"})
|
||||
@ResponseBody
|
||||
public ModelAndView getUserById(@PathVariable int id) throws IOException {
|
||||
final User userBy = userService.getUserBy(id);
|
||||
@@ -28,22 +28,24 @@ public class AdminController {
|
||||
return new ModelAndView("userView", "user", new RestUser(userBy));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/xml", "application/json"})
|
||||
@RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/xml", "application/json","text/html"})
|
||||
@ResponseBody
|
||||
public ModelAndView getUserByEmail(@PathVariable String email) throws IOException {
|
||||
final User userBy = userService.getUserBy(email);
|
||||
if (userBy == null) {
|
||||
throw new IllegalArgumentException("User could not be found");
|
||||
throw new IllegalArgumentException("User '" + email + "' could not be found" );
|
||||
}
|
||||
return new ModelAndView("userView", "user", new RestUser(userBy));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"})
|
||||
public void getUserByEmail(@RequestBody RestUser user) throws IOException, WiseMappingException {
|
||||
public ModelAndView getUserByEmail(@RequestBody RestUser user) throws IOException, WiseMappingException {
|
||||
if (user == null) {
|
||||
throw new IllegalArgumentException("User could not be found");
|
||||
}
|
||||
userService.createUser(user.getDelegated(), false);
|
||||
return new ModelAndView("responseView", "message", "User created successfully");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,10 +1,8 @@
|
||||
package com.wisemapping.security;
|
||||
|
||||
|
||||
import com.wisemapping.dao.UserManager;
|
||||
import com.wisemapping.model.User;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.security.authentication.AbstractAuthenticationToken;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.authentication.encoding.PasswordEncoder;
|
||||
@@ -13,8 +11,7 @@ import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
|
||||
public class AuthenticationProvider implements org.springframework.security.authentication.AuthenticationProvider {
|
||||
private UserManager userManager;
|
||||
|
||||
private UserDetailsService userDetailsService;
|
||||
private PasswordEncoder encoder;
|
||||
|
||||
@Override()
|
||||
@@ -23,13 +20,12 @@ public class AuthenticationProvider implements org.springframework.security.auth
|
||||
// All your user authentication needs
|
||||
final String email = auth.getName();
|
||||
|
||||
final User user = userManager.getUserBy(email);
|
||||
final UserDetails userDetails = getUserDetailsService().loadUserByUsername(email);
|
||||
final User user = userDetails.getUser();
|
||||
final String credentials = (String) auth.getCredentials();
|
||||
if (user == null || credentials == null || !encoder.isPasswordValid(user.getPassword(), credentials, null)) {
|
||||
throw new BadCredentialsException("Username/Password does not match for " + auth.getPrincipal());
|
||||
}
|
||||
|
||||
final UserDetails userDetails = new UserDetails(user);
|
||||
return new UsernamePasswordAuthenticationToken(userDetails, credentials, userDetails.getAuthorities());
|
||||
}
|
||||
|
||||
@@ -42,8 +38,11 @@ public class AuthenticationProvider implements org.springframework.security.auth
|
||||
this.encoder = encoder;
|
||||
}
|
||||
|
||||
public void setUserManager(UserManager userManager) {
|
||||
this.userManager = userManager;
|
||||
public UserDetailsService getUserDetailsService() {
|
||||
return userDetailsService;
|
||||
}
|
||||
|
||||
public void setUserDetailsService(UserDetailsService userDetailsService) {
|
||||
this.userDetailsService = userDetailsService;
|
||||
}
|
||||
}
|
||||
|
@@ -28,14 +28,16 @@ import java.util.Collection;
|
||||
|
||||
public class UserDetails implements org.springframework.security.core.userdetails.UserDetails {
|
||||
private com.wisemapping.model.User user;
|
||||
private boolean isAdmin;
|
||||
|
||||
public UserDetails(@NotNull final com.wisemapping.model.User user) {
|
||||
public UserDetails(@NotNull final com.wisemapping.model.User user, boolean isAdmin) {
|
||||
this.user = user;
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
final Collection<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
|
||||
if(this.getUser().isAdmin()) {
|
||||
if (this.isAdmin) {
|
||||
final SimpleGrantedAuthority role_admin = new SimpleGrantedAuthority("ROLE_ADMIN");
|
||||
result.add(role_admin);
|
||||
}
|
||||
|
@@ -20,25 +20,31 @@ package com.wisemapping.security;
|
||||
|
||||
import com.wisemapping.dao.UserManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
|
||||
public class UserDetailService
|
||||
public class UserDetailsService
|
||||
implements org.springframework.security.core.userdetails.UserDetailsService {
|
||||
private UserManager userManager;
|
||||
private String adminUser;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(@NotNull String email) throws UsernameNotFoundException, DataAccessException {
|
||||
final com.wisemapping.model.User model = userManager.getUserBy(email);
|
||||
|
||||
if (model != null) {
|
||||
return new UserDetails(model);
|
||||
return new UserDetails(model, isAdmin(email));
|
||||
} else {
|
||||
throw new UsernameNotFoundException(email);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAdmin(@Nullable String email) {
|
||||
return email != null && adminUser != null && email.trim().endsWith(adminUser);
|
||||
}
|
||||
|
||||
public UserManager getUserManager() {
|
||||
return userManager;
|
||||
}
|
||||
@@ -47,4 +53,11 @@ public class UserDetailService
|
||||
this.userManager = userManager;
|
||||
}
|
||||
|
||||
public String getAdminUser() {
|
||||
return adminUser;
|
||||
}
|
||||
|
||||
public void setAdminUser(String adminUser) {
|
||||
this.adminUser = adminUser;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user