Project

General

Profile

« Previous | Next » 

Revision 57320

View differences:

modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/repository/OrganizationRepository.java
18 18
	@Query("update Organization set modified_by = ?2, modification_date = ?3 where id = ?1")
19 19
	void updateModificationDate(String id, String user, OffsetDateTime now);
20 20

  
21
	@Query(value = "select count(o.country) > 0 from organizations o left outer join user_countries uc on (o.country = uc.country) where o.id = ?1 and uc.email = ?2", nativeQuery = true)
22
	boolean verifyAuthorizationForId(String id, String user);
23

  
24
	@Query(value = "select count(country) > 0 from user_countries  where country = ?1 and email = ?2", nativeQuery = true)
25
	boolean verifyAuthorizationForCountry(String country, String user);
26

  
21 27
}
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java
5 5

  
6 6
import javax.transaction.Transactional;
7 7

  
8
import org.apache.commons.lang3.StringUtils;
9 8
import org.springframework.beans.factory.annotation.Autowired;
10 9
import org.springframework.cache.annotation.Cacheable;
11 10
import org.springframework.jdbc.core.JdbcTemplate;
......
57 56

  
58 57
		final Organization org = new Organization(update ? orgView.getId() : null,
59 58
				orgView.getName(),
60
				StringUtils.isNotBlank(orgView.getType()) ? orgView.getType() : "UNKNOWN",
59
				orgView.getType(),
61 60
				orgView.getLat(), orgView.getLng(),
62 61
				orgView.getCity(), orgView.getCountry());
63 62

  
......
115 114
		relationshipRepository.delete(r2);
116 115
	}
117 116

  
117
	@Cacheable("countries_for_user")
118
	public List<String> listCountriesForUser(final String name) {
119
		return jdbcTemplate.queryForList("select country from user_countries where email = ?", String.class, name);
120
	}
121

  
118 122
}
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/VocabulariesController.java
7 7
import java.util.stream.Collectors;
8 8

  
9 9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.security.core.Authentication;
10 11
import org.springframework.web.bind.annotation.RequestMapping;
11 12
import org.springframework.web.bind.annotation.RequestMethod;
12 13
import org.springframework.web.bind.annotation.RestController;
......
24 25
	private DatabaseUtils databaseUtils;
25 26

  
26 27
	@RequestMapping(value = "", method = RequestMethod.GET)
27
	public Map<String, List<String>> ListVocabularies() {
28
	public Map<String, List<String>> ListVocabularies(final Authentication authentication) {
28 29
		final Map<String, List<String>> vocs = new HashMap<>();
29 30
		vocs.put("orgTypes", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.org_types));
30 31
		vocs.put("idTypes", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.id_types));
31 32
		vocs.put("languages", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.languages));
32
		vocs.put("countries", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.countries));
33

  
33 34
		vocs.put("relTypes", Arrays.stream(RelationType.values()).map(Object::toString).collect(Collectors.toList()));
34 35
		vocs.put("similaritiesType", Arrays.stream(SimilarityType.values()).map(Object::toString).collect(Collectors.toList()));
36

  
37
		if (User.isSuperUser(authentication)) {
38
			vocs.put("countries", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.countries));
39
		} else {
40
			vocs.put("countries", databaseUtils.listCountriesForUser(authentication.getName()));
41
		}
42

  
35 43
		return vocs;
36 44
	}
37 45

  
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java
21 21
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
22 22
import eu.dnetlib.organizations.model.view.OrganizationView;
23 23
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
24
import eu.dnetlib.organizations.repository.OrganizationRepository;
24 25
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
25 26
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
26 27
import eu.dnetlib.organizations.utils.DatabaseUtils;
......
31 32
public class OrganizationController {
32 33

  
33 34
	@Autowired
35
	private OrganizationRepository organizationRepository;
36
	@Autowired
34 37
	private OrganizationViewRepository organizationViewRepository;
35 38
	@Autowired
36 39
	private OrganizationSimpleViewRepository organizationSimpleViewRepository;
......
41 44

  
42 45
	@RequestMapping(value = "/save", method = RequestMethod.POST)
43 46
	public List<String> save(@RequestBody final OrganizationView org, final Authentication authentication) {
44
		final String user = authentication != null ? authentication.getName() : "anonymous";
45
		final String orgId = databaseUtils.insertOrUpdateOrganization(org, user, StringUtils.isNotBlank(org.getId()));
46
		return Arrays.asList(orgId);
47
		if (StringUtils.isBlank(org.getName())) {
48
			throw new RuntimeException("Missing field: name");
49
		} else if (StringUtils.isBlank(org.getCountry())) {
50
			throw new RuntimeException("Missing field: country");
51
		} else if (StringUtils.isBlank(org.getType())) {
52
			throw new RuntimeException("Missing field: type");
53
		} else if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
54
			final String orgId = databaseUtils.insertOrUpdateOrganization(org, authentication.getName(), StringUtils.isNotBlank(org.getId()));
55
			return Arrays.asList(orgId);
56
		} else {
57
			throw new RuntimeException("User not authorized");
58
		}
47 59
	}
48 60

  
49 61
	@RequestMapping(value = "/get", method = RequestMethod.GET)
50
	public OrganizationView findById(@RequestParam final String id) {
51
		return organizationViewRepository.findById(id).get();
62
	public OrganizationView findById(@RequestParam final String id, final Authentication authentication) {
63
		final OrganizationView org = organizationViewRepository.findById(id).get();
64

  
65
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(org.getCountry(), authentication.getName())) {
66
			return org;
67
		} else {
68
			throw new RuntimeException("User not authorized");
69
		}
52 70
	}
53 71

  
54 72
	@RequestMapping(value = "/relations", method = RequestMethod.GET)
55
	public List<RelationByOrg> findRelationsById(@RequestParam final String id) {
56
		return organizationViewRepository.findRelations(id);
73
	public List<RelationByOrg> findRelationsById(@RequestParam final String id, final Authentication authentication) {
74
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
75
			return organizationViewRepository.findRelations(id);
76
		} else {
77
			throw new RuntimeException("User not authorized");
78
		}
57 79
	}
58 80

  
59 81
	@RequestMapping(value = "/relations", method = RequestMethod.PUT)
60
	public List<RelationByOrg> addRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) {
61
		if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); }
82
	public List<RelationByOrg> addRelation(@RequestParam final String from,
83
			@RequestParam final String to,
84
			@RequestParam final RelationType type,
85
			final Authentication authentication) {
86
		if (from.equals(to)) {
87
			throw new IllegalArgumentException("Invalid relation !!!");
88
		} else if (User.isSuperUser(authentication)
89
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
90
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
62 91

  
63
		databaseUtils.addRelation(from, to, type);
64
		return organizationViewRepository.findRelations(from);
92
			databaseUtils.addRelation(from, to, type);
93

  
94
			return organizationViewRepository.findRelations(from);
95
		} else {
96
			throw new RuntimeException("User not authorized");
97
		}
98

  
65 99
	}
66 100

  
67 101
	@RequestMapping(value = "/relations", method = RequestMethod.DELETE)
68
	public List<RelationByOrg> deleteRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) {
69
		if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); }
102
	public List<RelationByOrg> deleteRelation(@RequestParam final String from,
103
			@RequestParam final String to,
104
			@RequestParam final RelationType type,
105
			final Authentication authentication) {
106
		if (from.equals(to)) {
107
			throw new IllegalArgumentException("Invalid relation !!!");
108
		} else if (User.isSuperUser(authentication)
109
				|| organizationRepository.verifyAuthorizationForId(to, authentication.getName())
110
				|| organizationRepository.verifyAuthorizationForId(from, authentication.getName())) {
70 111

  
71
		databaseUtils.deleteRelation(from, to, type);
72
		return organizationViewRepository.findRelations(from);
112
			databaseUtils.deleteRelation(from, to, type);
113

  
114
			return organizationViewRepository.findRelations(from);
115
		} else {
116
			throw new RuntimeException("User not authorized");
117
		}
118

  
73 119
	}
74 120

  
75 121
	@RequestMapping(value = "/similarities", method = RequestMethod.GET)
76
	public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id) {
77
		return openaireSimRelRepository.findByLocalId(id);
122
	public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id, final Authentication authentication) {
123
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForId(id, authentication.getName())) {
124
			return openaireSimRelRepository.findByLocalId(id);
125
		} else {
126
			throw new RuntimeException("User not authorized");
127
		}
78 128
	}
79 129

  
80 130
	@RequestMapping(value = "/similarities", method = RequestMethod.POST)
81
	public List<OpenaireSimRel> saveSimilarities(@RequestBody final List<OpenaireSimRel> simrels) {
82
		return openaireSimRelRepository.saveAll(simrels);
131
	public List<OpenaireSimRel> saveSimilarities(@RequestBody final List<OpenaireSimRel> simrels, final Authentication authentication) {
132

  
133
		final boolean b = simrels.stream()
134
				.map(OpenaireSimRel::getLocalId)
135
				.distinct()
136
				.allMatch(id -> organizationRepository.verifyAuthorizationForId(id, authentication.getName()));
137

  
138
		if (b) {
139
			return openaireSimRelRepository.saveAll(simrels);
140
		} else {
141
			throw new RuntimeException("User not authorized");
142
		}
83 143
	}
84 144

  
85 145
	@RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
......
93 153
	}
94 154

  
95 155
	@RequestMapping(value = "/byCountry/{code}/{page}/{size}", method = RequestMethod.GET)
96
	public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code, @PathVariable final int page, @PathVariable final int size) {
97

  
98
		return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
156
	public Page<OrganizationSimpleView> findByCountry(@PathVariable final String code,
157
			@PathVariable final int page,
158
			@PathVariable final int size,
159
			final Authentication authentication) {
160
		if (User.isSuperUser(authentication) || organizationRepository.verifyAuthorizationForCountry(code, authentication.getName())) {
161
			return organizationSimpleViewRepository.findByCountry(code, PageRequest.of(page, size));
162
		} else {
163
			throw new RuntimeException("User not authorized");
164
		}
99 165
	}
100 166

  
101 167
	@RequestMapping(value = "/byType/{type}/{page}/{size}", method = RequestMethod.GET)
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/User.java
44 44
		}
45 45
		return false;
46 46
	}
47

  
47 48
}

Also available in: Unified diff