Skip to content

Commit

Permalink
feat: rbac
Browse files Browse the repository at this point in the history
  • Loading branch information
z15lross committed May 6, 2022
1 parent 2d897b8 commit 2837118
Show file tree
Hide file tree
Showing 98 changed files with 7,521 additions and 381 deletions.
19 changes: 19 additions & 0 deletions code/api/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
</dependency>

<!-- Cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<!-- LOGS -->
<dependency>
Expand Down Expand Up @@ -96,6 +108,13 @@
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.32.0</version>
<scope>test</scope>
</dependency>

<!-- Security -->
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions code/api/api/src/main/java/com/decathlon/ara/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ public final class Messages {
private Messages() {
}

public static final String ALREADY_EXIST = "The %s already exist";

public static final String PARAMETER_IS_MISSING = "One or more parameters is missing or null";
public static final String PARAMETER_HAS_ONE_OR_MORE_MISSING_FIELDS = "A parameter has one or more missing field";

public static final String NOT_FOUND = "The %s does not exist: it has perhaps been removed.";
public static final String NOT_FOUND_COMMUNICATION = "The communication does not exist: it has perhaps been removed.";
public static final String NOT_FOUND_COUNTRY = "The country does not exist: it has perhaps been removed.";
public static final String NOT_FOUND_CYCLE_DEFINITION = "The cycle definition does not exist: it has perhaps been removed.";
Expand Down
104 changes: 104 additions & 0 deletions code/api/api/src/main/java/com/decathlon/ara/cache/CacheService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.decathlon.ara.cache;

import java.util.function.Predicate;

import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.decathlon.ara.domain.Project;

@Service
public class CacheService {

private static final String USER_PROJECTS_CACHE_NAME = "security.user.projects";
private static final String USER_PROJECT_ROLES_CACHE_NAME = "security.user.project.roles";

private CacheManager cacheManager;

public CacheService(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}

public void evictCaches(Project project) {
evictCacheAfterCommit(() -> {
evictCache(USER_PROJECT_ROLES_CACHE_NAME, key -> key.toString().startsWith(project.getCode()));

Cache cache = cacheManager.getCache(USER_PROJECTS_CACHE_NAME);
if (cache != null) {
cache.clear();
}
});
}

public void evictCaches(Project project, String userName) {
evictsUserProjectRolesCache(project, userName);
evictsUserProjectsCache(userName);
}

public void evictsUserProjectRolesCache(Project project, String userName) {
evictCacheAfterCommit(() -> {
Cache cache = cacheManager.getCache(USER_PROJECT_ROLES_CACHE_NAME);
if (cache != null) {
cache.evict(project.getCode().concat(userName));
}
});
}

public void evictsUserProjectRolesCache(String userName) {
evictCacheAfterCommit(() -> evictCache(USER_PROJECT_ROLES_CACHE_NAME, key -> key.toString().endsWith(userName)));
}

public void evictsUserProjectsCache(String userName) {
evictCacheAfterCommit(() -> {
Cache cache = cacheManager.getCache(USER_PROJECTS_CACHE_NAME);
if (cache != null) {
cache.evict(userName);
}
});
}

public void evictCaches(String userName) {
evictsUserProjectRolesCache(userName);
evictsUserProjectsCache(userName);
}

@SuppressWarnings("unchecked")
private void evictCache(String cacheName, Predicate<Object> keyPredicate) {
Cache cache = cacheManager.getCache(cacheName);
if (cache != null) {
if (cache instanceof EhCacheCache ehCacheCache) {
ehCacheCache.getNativeCache().getKeys().stream().filter(keyPredicate).forEach(cache::evict);
} else {
cache.clear();
}
}
}

private void evictCacheAfterCommit(Runnable evictRunnable) {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new EvictCacheSynchronization(evictRunnable));
} else {
evictRunnable.run();
}
}

private static class EvictCacheSynchronization implements TransactionSynchronization {

private Runnable evictRunnable;

public EvictCacheSynchronization(Runnable evictRunnable) {
this.evictRunnable = evictRunnable;
}

@Override
public void afterCommit() {
evictRunnable.run();
}

}

}
Loading

0 comments on commit 2837118

Please sign in to comment.