Commit 879eadf5 authored by Matija Obreza's avatar Matija Obreza
Browse files

Use User#uuid as 'username' for Principal

parent 622d0999
......@@ -18,6 +18,7 @@ package org.genesys2.server.model.impl;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
......@@ -26,6 +27,7 @@ import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import net.sf.oval.constraint.Email;
......@@ -44,6 +46,9 @@ public class User extends BusinessModel {
*/
private static final long serialVersionUID = 4564013753931115445L;
@Column(length = 36, unique = true)
private String uuid;
// validation
@NotNull(message = "sample.error.not.null")
@NotEmpty(message = "sample.error.not.empty")
......@@ -79,6 +84,14 @@ public class User extends BusinessModel {
@Column(nullable = false, updatable = false, name = "sys")
private boolean systemAccount;
@PrePersist
void ensureUUID() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID().toString();
}
}
public String getEmail() {
return email;
}
......@@ -145,6 +158,16 @@ public class User extends BusinessModel {
public String toString() {
return "User id=" + id + " email=" + email;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public void setSystemAccount(boolean systemAccount) {
this.systemAccount = systemAccount;
......
......@@ -30,4 +30,7 @@ public interface UserPersistence extends JpaRepository<User, Long> {
@Query("select u from User u where u.email = ?1 and u.systemAccount = true")
User findSystemUser(String username);
@Query("select u from User u where u.uuid = ?1 and u.systemAccount = false")
User findByUuid(String uuid);
}
......@@ -45,6 +45,8 @@ public interface UserService {
User getUserByEmail(String email);
User getUserByUuid(String uuid);
User getUserById(long userId) throws UserException;
boolean exists(String username) throws UserException;
......@@ -58,4 +60,5 @@ public interface UserService {
User getSystemUser(String string);
}
......@@ -83,7 +83,7 @@ public class AclAssignerServiceImpl implements AclAssignerService {
return;
}
String uuid = authUser.getUser().getId().toString();
String uuid = authUser.getUser().getUuid();
// it's ok if it is null
// it can be pre-authorized Admin
......
......@@ -47,7 +47,7 @@ public class AuthUserDetailsService implements UserDetailsService {
return null;
}
AuthUserDetails userDetails = new AuthUserDetails(email, user.getPassword(), getGrantedAuthorities(user));
AuthUserDetails userDetails = new AuthUserDetails(user.getUuid(), user.getPassword(), getGrantedAuthorities(user));
// set actual DB user for possible further purposes
userDetails.setUser(user);
......
......@@ -227,6 +227,18 @@ public class UserServiceImpl implements UserService {
user.getRoles().size();
return user;
}
@Override
public User getUserByUuid(String uuid) {
User user = userPersistence.findByUuid(uuid);
if (user == null) {
throw new UsernameNotFoundException(uuid);
}
user.getRoles().size();
return user;
}
@Override
public User getSystemUser(String username) {
......@@ -247,7 +259,7 @@ public class UserServiceImpl implements UserService {
throw new UserException(e);
}
}
@Override
public boolean exists(String username) throws UserException {
return userPersistence.findByEmail(username) != null;
......
......@@ -68,9 +68,9 @@ public class UserProfileController extends BaseController {
return "redirect:/profile/" + user.getEmail();
}
@RequestMapping("/{email:.+}")
public String someProfile(ModelMap model, @PathVariable("email") String email) {
User user = userService.getUserByEmail(email);
@RequestMapping("/{uuid:.+}")
public String someProfile(ModelMap model, @PathVariable("uuid") String uuid) {
User user = userService.getUserByUuid(uuid);
if (user == null) {
throw new ResourceNotFoundException();
}
......@@ -81,11 +81,10 @@ public class UserProfileController extends BaseController {
return "/user/profile";
}
@RequestMapping("/{email:.+}/edit")
@RequestMapping("/{uuid:.+}/edit")
@PreAuthorize("hasRole('ADMINISTRATOR') || principal.user.email == #email")
public String edit(ModelMap model, @PathVariable("email") String email) {
System.err.println("email=" +email);
someProfile(model, email);
public String edit(ModelMap model, @PathVariable("uuid") String uuid) {
someProfile(model, uuid);
return "/user/edit";
}
......
......@@ -28,3 +28,6 @@ log4j.rootLogger=info, stdout
#log4j.category.org.hibernate.search=debug
log4j.category.org.apache.tomcat.jdbc.pool=debug
log4j.category.org.springframework.security.oauth2=trace
#log4j.category.org.springframework.security.access=trace
#log4j.category.org.springframework.security.acl=trace
#log4j.category.org.springframework.expression=trace
......@@ -53,7 +53,7 @@
</security:authorize>
<security:authorize access="isAuthenticated()">
<ul class="nav">
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" title="<spring:message code="locale.language.change"/>"><spring:message code="user.pulldown.heading" arguments="${user.username}" /> <b class="caret"></b>
<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" title="<spring:message code="locale.language.change"/>"><spring:message code="user.pulldown.heading" arguments="${user.user.name}" /> <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<security:authorize access="hasRole('ADMINISTRATOR')">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment