Skip to content

Commit b47eece

Browse files
authored
Add user profile schema (#580)
1 parent 39f1d7b commit b47eece

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fr.insee.onyxia.api.controller.api.profile;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import fr.insee.onyxia.api.services.JsonSchemaProfileRegistryService;
5+
import fr.insee.onyxia.api.services.UserProvider;
6+
import fr.insee.onyxia.model.User;
7+
import fr.insee.onyxia.model.region.Region;
8+
import io.swagger.v3.oas.annotations.Parameter;
9+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
10+
import io.swagger.v3.oas.annotations.tags.Tag;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@Tag(name = "Profile", description = "Info on profile configuration feature")
17+
@RequestMapping("/profile")
18+
@RestController
19+
@SecurityRequirement(name = "auth")
20+
public class ProfileController {
21+
22+
private final UserProvider userProvider;
23+
24+
private final JsonSchemaProfileRegistryService jsonSchemaProfileRegistryService;
25+
26+
@Autowired
27+
public ProfileController(
28+
UserProvider userProvider,
29+
JsonSchemaProfileRegistryService jsonSchemaProfileRegistryService) {
30+
this.userProvider = userProvider;
31+
this.jsonSchemaProfileRegistryService = jsonSchemaProfileRegistryService;
32+
}
33+
34+
@GetMapping("schema")
35+
public JsonNode profileSchema(@Parameter(hidden = true) Region region) {
36+
User user = userProvider.getUser(region);
37+
return jsonSchemaProfileRegistryService.getProfileSchema();
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package fr.insee.onyxia.api.services;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import javax.annotation.PostConstruct;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.stereotype.Service;
14+
15+
@Service
16+
public class JsonSchemaProfileRegistryService {
17+
18+
private static final Logger LOGGER =
19+
LoggerFactory.getLogger(JsonSchemaProfileRegistryService.class);
20+
21+
@Value("${profile.schema.directory:/userProfile}") // External directory path
22+
private String userProfileDirectory;
23+
24+
private JsonNode defaultProfileSchema;
25+
26+
private ObjectMapper objectMapper = new ObjectMapper();
27+
28+
public JsonSchemaProfileRegistryService() {}
29+
30+
public JsonNode getProfileSchema() {
31+
return defaultProfileSchema;
32+
}
33+
34+
@PostConstruct
35+
private void loadProfileSchema() {
36+
Path roleSchemaPath = Paths.get(userProfileDirectory);
37+
if (Files.exists(roleSchemaPath) && Files.exists(roleSchemaPath.resolve("default"))) {
38+
try {
39+
JsonNode schema = objectMapper.readTree(roleSchemaPath.resolve("default").toFile());
40+
defaultProfileSchema = schema;
41+
} catch (IOException e) {
42+
LOGGER.error("User profile : failed to load default profile", e);
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)