use of io.gravitee.el.TemplateEngine in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResource method getGroupsToAddUser.
private Set<GroupEntity> getGroupsToAddUser(String username, List<ExpressionMapping> mappings, String userInfo) {
Set<GroupEntity> groupsToAdd = new HashSet<>();
for (ExpressionMapping mapping : mappings) {
TemplateEngine templateEngine = TemplateEngine.templateEngine();
templateEngine.getTemplateContext().setVariable("profile", userInfo);
boolean match = templateEngine.getValue(mapping.getCondition(), boolean.class);
trace(username, match, mapping);
// get groups
if (match) {
for (String groupName : mapping.getGroupNames()) {
List<GroupEntity> groupEntities = groupService.findByName(groupName);
if (groupEntities.isEmpty()) {
LOGGER.error("Unable to create user, missing group in repository : {}", groupName);
throw new InternalServerErrorException();
} else if (groupEntities.size() > 1) {
LOGGER.warn("There's more than a group found in repository for name : {}", groupName);
}
GroupEntity groupEntity = groupEntities.get(0);
groupsToAdd.add(groupEntity);
}
}
}
return groupsToAdd;
}
Aggregations