repository terms functionality

This commit is contained in:
Konstantinos Spyrou 2022-03-15 11:33:49 +00:00
parent 2a57236c9a
commit 982d299b2d
6 changed files with 117 additions and 27 deletions

View File

@ -3,6 +3,7 @@ package eu.dnetlib.repo.manager.controllers;
import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.domain.data.RepositoryInterface;
import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.domain.dto.RepositoryTerms;
import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.RepositoryService;
@ -24,6 +25,7 @@ import org.springframework.web.bind.annotation.*;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -64,7 +66,24 @@ public class RepositoryController {
@ResponseBody
@PreAuthorize("hasAuthority('REGISTERED_USER')")
public List<RepositorySnippet> getRepositoriesSnippetsOfUser() throws Exception {
return repositoryService.getRepositoriesSnippetsOfUser("0", "100");
return repositoryService.getRepositoriesSnippetsOfUser("0", "100"); // FIXME
}
@RequestMapping(value = "/terms", method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasAuthority('REGISTERED_USER')")
public void updateRepositoriesTerms(@RequestBody List<RepositoryTerms> repositoriesTerms) throws Exception {
Date date = new Date();
if (repositoriesTerms != null) {
for (RepositoryTerms terms : repositoriesTerms) {
Repository repository = repositoryService.getRepositoryById(terms.getId());
repository.setConsentTermsOfUse(terms.getConsentTermsOfUse());
repository.setFullTextDownload(terms.getFullTextDownload());
repository.setConsentTermsOfUseDate(date);
repositoryService.updateRepository(repository, SecurityContextHolder.getContext().getAuthentication());
}
}
}
@RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}", method = RequestMethod.GET,

View File

@ -0,0 +1,55 @@
package eu.dnetlib.repo.manager.domain.dto;
import java.util.Date;
public class RepositoryTerms {
private String id;
private String name;
private Boolean consentTermsOfUse;
private Boolean fullTextDownload;
private Date consentTermsOfUseDate;
public RepositoryTerms() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getConsentTermsOfUse() {
return consentTermsOfUse;
}
public void setConsentTermsOfUse(Boolean consentTermsOfUse) {
this.consentTermsOfUse = consentTermsOfUse;
}
public Boolean getFullTextDownload() {
return fullTextDownload;
}
public void setFullTextDownload(Boolean fullTextDownload) {
this.fullTextDownload = fullTextDownload;
}
public Date getConsentTermsOfUseDate() {
return consentTermsOfUseDate;
}
public void setConsentTermsOfUseDate(Date consentTermsOfUseDate) {
this.consentTermsOfUseDate = consentTermsOfUseDate;
}
}

View File

@ -981,7 +981,7 @@ LOGGER.debug("json: " + jsonArray);
private UriComponents searchDatasource(String page, String size) {
return UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/search/")
.fromHttpUrl(baseAddress + "/ds/searchdetails/")
.path("/{page}/{size}/")
.queryParam("requestSortBy", "officialname")
.queryParam("order", "ASCENDING")

View File

@ -10,7 +10,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.exceptions.UnauthorizedClientException;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.session.Session;
//import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.stereotype.Service;
@ -27,10 +26,10 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository {
@Autowired
FindByIndexNameSessionRepository sessions;
public void update(String email, Collection<? extends GrantedAuthority> authorities) {
public void update(String id, Update update) {
if (sessions != null) {
Map<String, Session> map = sessions.
findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, email);
findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, id);
if (map != null) {
logger.debug(map.values().toArray().length);
for (Session session : map.values()) {
@ -40,9 +39,9 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository {
Authentication authentication = securityContext.getAuthentication();
if (authentication instanceof OIDCAuthenticationToken) {
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
logger.debug(authorities);
logger.debug(update.authorities(authOIDC.getAuthorities()));
securityContext.setAuthentication(new OIDCAuthenticationToken(authOIDC.getSub(), authOIDC.getIssuer(),
authOIDC.getUserInfo(), authorities, authOIDC.getIdToken(),
authOIDC.getUserInfo(), update.authorities(authOIDC.getAuthorities()), authOIDC.getIdToken(),
authOIDC.getAccessTokenValue(), authOIDC.getRefreshTokenValue()));
logger.debug("Update authorities");
session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext);
@ -54,13 +53,8 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository {
}
}
public void update(String email, Update update) {
Collection<? extends GrantedAuthority> authorities = update.authorities(SecurityContextHolder.getContext().getAuthentication().getAuthorities());
this.update(email, authorities);
}
public void addRole(String email, GrantedAuthority role) {
this.update(email, old -> {
public void addRole(String id, GrantedAuthority role) {
this.update(id, old -> {
HashSet<GrantedAuthority> authorities = new HashSet<>(old);
authorities.add(role);
return authorities;
@ -77,8 +71,8 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository {
}
}
public void removeRole(String email, GrantedAuthority role) {
this.update(email, old -> {
public void removeRole(String id, GrantedAuthority role) {
this.update(id, old -> {
HashSet<GrantedAuthority> authorities = new HashSet<>(old);
authorities.remove(role);
return authorities;

View File

@ -5,19 +5,26 @@ import com.google.gson.JsonElement;
import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.mitre.openid.connect.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@Service("authorizationService")
public class AuthorizationServiceImpl implements AuthorizationService {
private static final Logger logger = LogManager.getLogger(AuthorizationServiceImpl.class);
public static final String SUPER_ADMINISTRATOR = "SUPER_ADMINISTRATOR";
public static final String CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR = "CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR";
public static final String REGISTERED_USER = "REGISTERED_USER";
@ -131,17 +138,11 @@ public class AuthorizationServiceImpl implements AuthorizationService {
@Override
public Collection<String> getUserRoles() {
List<String> roles;
JsonArray entitlements;
Collection<String> roles;
UserInfo userInfo = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo();
if (userInfo.getSource().getAsJsonArray("edu_person_entitlements") != null) {
entitlements = userInfo.getSource().getAsJsonArray("edu_person_entitlements");
} else if (userInfo.getSource().getAsJsonArray("eduperson_entitlement") != null) {
entitlements = userInfo.getSource().getAsJsonArray("eduperson_entitlement");
} else {
entitlements = new JsonArray();
}
roles = AuthoritiesMapper.entitlementRoles(entitlements);
roles = getUserRoles(userInfo.getEmail());
logger.debug(String.format("User Roles: %s", String.join(",", roles)));
return roles;
}

View File

@ -31,7 +31,8 @@ public class Converter {
Repository repository = new Repository();
JSONObject datasource = repositoryObject.getJSONObject("datasource");
// JSONObject datasource = repositoryObject.getJSONObject("datasource");
JSONObject datasource = repositoryObject;
//if( datasource.equals(null))
// return null;
@ -103,11 +104,23 @@ public class Converter {
repository.setCountryCode(countryCode);
}
repository.setConsentTermsOfUse(convertStringToBoolean(datasource.get("consentTermsOfUse").toString()));
try {
repository.setConsentTermsOfUseDate(convertStringToDate(datasource.get("consentTermsOfUseDate").toString()));
} catch (JSONException e) {
repository.setConsentTermsOfUseDate(null);
}
repository.setFullTextDownload(convertStringToBoolean(datasource.get("fullTextDownload").toString()));
/* identities field */
return repository;
}
public static Boolean convertStringToBoolean(String value) {
return value.equals("null") ? null : Boolean.valueOf(value);
}
public static Date convertStringToDate(String date) {
if (Objects.equals(date, "null"))
@ -169,6 +182,11 @@ public class Converter {
repositorySnippet.setRegisteredby(repositorySnippetObject.get("registeredby").toString());
if (repositorySnippet.getRegisteredby().equals("null"))
repositorySnippet.setRegisteredby("");
repositorySnippet.setConsentTermsOfUse(repositorySnippetObject.get("consenttermsofuse").toString());
repositorySnippet.setFullTextDownload(repositorySnippetObject.get("fulltextdownload").toString());
repositorySnippet.setConsentTermsOfUseDate(convertStringToDate(repositorySnippetObject.get("consenttermsofusedate").toString()));
return repositorySnippet;
}
@ -296,6 +314,9 @@ public class Converter {
}
repositoryMap.put("subjects", "");
repositoryMap.put("consentTermsOfUse", repository.getConsentTermsOfUse());
repositoryMap.put("fullTextDownload", repository.getFullTextDownload());
repositoryMap.put("consentTermsOfUseDate", convertDateToString(repository.getConsentTermsOfUseDate()));
return mapper.writeValueAsString(repositoryMap);
}