Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rbac #617

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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.TransactionSynchronizationManager;

import com.decathlon.ara.domain.Project;
import com.decathlon.ara.transaction.RunnableAfterCommitTransactionSynchronization;

@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(RunnableAfterCommitTransactionSynchronization evictRunnable) {
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(evictRunnable);
} else {
evictRunnable.run();
}
}

}
Loading