Commit 0598634e authored by Matija Obreza's avatar Matija Obreza
Browse files

Included querydsl, updated code to resolve generics in AuditTrailInterceptor,...

Included querydsl, updated code to resolve generics in AuditTrailInterceptor, User renamed to BasicUser
parent f234bcc5
......@@ -52,6 +52,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
......@@ -399,23 +400,17 @@ public class AuditTrailInterceptor extends EmptyInterceptor implements Initializ
LOG.trace("Finding property type for {}.{}", class1.getName(), propertyName);
// Field
try {
final Field field = ReflectionUtils.findField(class1, propertyName);
if (field != null) {
LOG.debug("Found field: " + field + "\n\ttype=" + field.getType() + "\n\tgeneric=" + field.getGenericType() + "\n\tgenericTN=" + field.getGenericType().getTypeName());
if (field.getGenericType() instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType) field.getGenericType();
// LOG.trace("\traw=" + pt.getRawType());
// LOG.trace("\tata0=" + pt.getActualTypeArguments()[0]);
return Class.forName(pt.getActualTypeArguments()[0].getTypeName());
} else {
LOG.trace("No idea!");
return Class.forName(field.getGenericType().getTypeName());
}
final Field field = ReflectionUtils.findField(class1, propertyName);
if (field != null) {
LOG.debug("Found field: " + field + "\n\ttype=" + field.getType() + "\n\tgeneric=" + field.getGenericType() + "\n\tgenericTN=" + field.getGenericType().getTypeName());
ResolvableType t = ResolvableType.forField(field, class1);
if (t.hasGenerics()) {
LOG.trace("\tResoved={} returning={}", t.toString(), t.resolveGeneric(0));
return t.resolveGeneric(0);
} else {
LOG.trace("Returning class itself={}", t.getRawClass());
return t.getRawClass();
}
} catch (SecurityException | ClassNotFoundException e) {
e.printStackTrace();
}
// Getter
......
......@@ -33,7 +33,33 @@
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<includes>
<!-- List packages to be processed -->
<include>org.genesys.blocks.security.model</include>
</includes>
<outputDirectory>target/generated-sources/querydsl</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>4.1.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
......
package org.genesys.blocks.security;
import org.genesys.blocks.security.model.User;
import org.genesys.blocks.security.model.BasicUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
......@@ -20,21 +20,21 @@ public class SecurityContextUtil {
/**
* @return current user
*/
public static User getMe() {
public static <T extends BasicUser<?>> T getMe() {
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.getPrincipal() instanceof User) {
return (User) auth.getPrincipal();
if (auth != null && auth.getPrincipal() instanceof BasicUser) {
return (T) auth.getPrincipal();
} else {
return null;
}
}
public static User getCurrentUser() {
public static <T extends BasicUser<?>> T getCurrentUser() {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof User) {
return (User) authentication.getPrincipal();
if (authentication != null && authentication.getPrincipal() instanceof BasicUser) {
return (T) authentication.getPrincipal();
}
return null;
......
......@@ -17,7 +17,7 @@ package org.genesys.blocks.security;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys.blocks.security.model.User;
import org.genesys.blocks.security.model.BasicUser;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
......@@ -27,7 +27,7 @@ public class SpringSecurityAuditorAware implements AuditorAware<Long> {
@Override
public Long getCurrentAuditor() {
final User user = SecurityContextUtil.getCurrentUser();
final BasicUser<?> user = SecurityContextUtil.getCurrentUser();
if (user == null) {
SpringSecurityAuditorAware.LOG.trace("No User in security context, can't specify createdBy/lastUpdatedBy");
......
......@@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
......@@ -39,6 +40,7 @@ import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.JsonViews;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
//@Cacheable
......@@ -46,7 +48,7 @@ import org.springframework.security.core.userdetails.UserDetails;
//@Table(name = "user")
//@Audited
@MappedSuperclass
public abstract class User extends AuditedVersionedModel implements UserDetails {
public abstract class BasicUser<R> extends AuditedVersionedModel implements UserDetails {
private static final long serialVersionUID = -5318892732608111516L;
......@@ -88,7 +90,7 @@ public abstract class User extends AuditedVersionedModel implements UserDetails
@Enumerated(EnumType.STRING)
@CollectionTable(name = "userrole", joinColumns = @JoinColumn(name = "userId"))
@Column(name = "role")
private Set<UserRole> roles = new HashSet<>();
private Set<R> roles = new HashSet<>();
@JsonView(JsonViews.Protected.class)
@Enumerated(EnumType.STRING)
......@@ -148,11 +150,11 @@ public abstract class User extends AuditedVersionedModel implements UserDetails
this.password = password;
}
public Set<UserRole> getRoles() {
public Set<R> getRoles() {
return roles;
}
public void setRoles(final Set<UserRole> roles) {
public void setRoles(final Set<R> roles) {
this.roles = roles;
}
......@@ -164,7 +166,7 @@ public abstract class User extends AuditedVersionedModel implements UserDetails
@Transient
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return roles;
return getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role)).collect(Collectors.toSet());
}
@Override
......
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.security.model;
import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority {
USER, ADMINISTRATOR;
/**
* GrantedAuthorities start with ROLE_
*/
@Override
public String getAuthority() {
return "ROLE_" + toString();
}
}
Supports Markdown
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