Skip to content

Commit

Permalink
fix: retrieve users authorities in user management page (reactive)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdns committed Mar 28, 2021
1 parent cc5d620 commit 55cdc09
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
3 changes: 3 additions & 0 deletions generators/server/templates/build.gradle.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ dependencies {
<%_ if (databaseType === 'sql') { _%>
implementation "org.hibernate:hibernate-core"
implementation "com.zaxxer:HikariCP"
<%_ if (reactive) { _%>
implementation "commons-beanutils:commons-beanutils:1.9.4"
<%_ } _%>
<%_ } _%>
implementation "org.apache.commons:commons-lang3"
<%_ if (databaseType === 'cassandra' || databaseType === 'couchbase' || applicationType === 'gateway') { _%>
Expand Down
6 changes: 6 additions & 0 deletions generators/server/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
<%_ } else { _%>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<%_ } _%>
<%_ } _%>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ import com.datastax.oss.driver.api.querybuilder.insert.RegularInsert;
<%_ if (searchEngine === 'couchbase') { _%>
import <%= packageName %>.repository.search.SearchCouchbaseRepository;
<%_ } _%>
<%_ if (databaseType === 'sql' && reactive) { _%>
import org.apache.commons.beanutils.BeanComparator;
<%_ } _%>
<%_ if (cacheManagerIsAvailable === true) { _%>
import org.springframework.cache.annotation.Cacheable;
<%_ } _%>
Expand All @@ -72,7 +75,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
<%_ } else { _%>
import org.springframework.data.domain.Sort;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.data.r2dbc.convert.R2dbcConverter;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
Expand Down Expand Up @@ -110,6 +113,7 @@ import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ if (databaseType === 'sql') { _%>
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;
<%_ } _%>
<%_ } _%>
Expand Down Expand Up @@ -285,6 +289,8 @@ interface UserRepositoryInternal<% if (authenticationType !== 'oauth2') { %> ext

Mono<User> create(User user);
<%_ } _%>

Flux<User> findAllWithAuthorities(Pageable pageable);
}

class UserRepositoryInternalImpl implements UserRepositoryInternal {
Expand All @@ -311,32 +317,25 @@ class UserRepositoryInternalImpl implements UserRepositoryInternal {
}

<%_ } _%>
private Mono<User> findOneWithAuthoritiesBy(String fieldName, Object fieldValue) {
return db.sql("SELECT * FROM <%= jhiTablePrefix %>_user u LEFT JOIN <%= jhiTablePrefix %>_user_authority ua ON u.id=ua.user_id WHERE u." + fieldName + " = :" + fieldName)
.bind(fieldName, fieldValue)
.map((row, metadata) ->
Tuples.of(
r2dbcConverter.read(User.class, row, metadata),
Optional.ofNullable(row.get("authority_name", String.class))
)
@Override
public Flux<User> findAllWithAuthorities(Pageable pageable) {
String property = pageable.getSort().stream().map(Sort.Order::getProperty).findFirst().get();
String direction = String.valueOf(pageable.getSort().stream().map(Sort.Order::getDirection).findFirst().get());
long page = pageable.getPageNumber();
long size = pageable.getPageSize();

return db
.sql("SELECT * FROM <%= jhiTablePrefix %>_user u LEFT JOIN <%= jhiTablePrefix %>_user_authority ua ON u.id=ua.user_id")
.map(
(row, metadata) ->
Tuples.of(r2dbcConverter.read(User.class, row, metadata), Optional.ofNullable(row.get("authority_name", String.class)))
)
.all()
.collectList()
.filter(l -> !l.isEmpty())
.map(l -> {
User user = l.get(0).getT1();
user.setAuthorities(
l.stream()
.filter(t -> t.getT2().isPresent())
.map(t -> {
Authority authority = new Authority();
authority.setName(t.getT2().get());
return authority;
})
.collect(Collectors.toSet())
);
return user;
});
.groupBy(t -> t.getT1().getLogin())
.flatMap(l -> l.collectList().map(t -> updateUserWithAuthorities(t.get(0).getT1(), t)))
.sort(Sort.Direction.fromString(direction) == Sort.DEFAULT_DIRECTION ? new BeanComparator<>(property) : new BeanComparator<>(property).reversed())
.skip(page * size)
.take(size);
}

<%_ if (authenticationType !== 'oauth2') { _%>
Expand All @@ -358,6 +357,38 @@ class UserRepositoryInternalImpl implements UserRepositoryInternal {
}

<%_ } _%>
private Mono<User> findOneWithAuthoritiesBy(String fieldName, Object fieldValue) {
return db.sql("SELECT * FROM <%= jhiTablePrefix %>_user u LEFT JOIN <%= jhiTablePrefix %>_user_authority ua ON u.id=ua.user_id WHERE u." + fieldName + " = :" + fieldName)
.bind(fieldName, fieldValue)
.map((row, metadata) ->
Tuples.of(
r2dbcConverter.read(User.class, row, metadata),
Optional.ofNullable(row.get("authority_name", String.class))
)
)
.all()
.collectList()
.filter(l -> !l.isEmpty())
.map(l -> updateUserWithAuthorities(l.get(0).getT1(), l));
}

private User updateUserWithAuthorities(User user, List<Tuple2<User, Optional<String>>> tuples) {
user.setAuthorities(
tuples
.stream()
.filter(t -> t.getT2().isPresent())
.map(
t -> {
Authority authority = new Authority();
authority.setName(t.getT2().get());
return authority;
}
)
.collect(Collectors.toSet())
);

return user;
}
}

class UserSqlHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ public class UserService {
<%_ } _%>
<%_ if (['sql', 'mongodb', 'neo4j', 'couchbase'].includes(databaseType)) { _%>
public <% if (reactive) { %>Flux<% } else { %>Page<% } %><<%= asDto('AdminUser') %>> getAllManagedUsers(Pageable pageable) {
return userRepository.findAll<% if (reactive) { %>ByIdNotNull<% } %>(pageable).map(<%= asDto('AdminUser') %>::new);
return userRepository.findAll<% if (reactive) { %><% if (databaseType === 'sql') { %>WithAuthorities<% } else { %>ByIdNotNull<% }} %>(pageable).map(<%= asDto('AdminUser') %>::new);
}
<%_ if (databaseType === 'sql') { _%>
Expand Down

0 comments on commit 55cdc09

Please sign in to comment.