1
|
package eu.dnetlib.organizations.controller;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Map;
|
5
|
|
6
|
import org.apache.commons.codec.digest.DigestUtils;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.web.bind.annotation.GetMapping;
|
9
|
import org.springframework.web.bind.annotation.PostMapping;
|
10
|
import org.springframework.web.bind.annotation.RequestParam;
|
11
|
import org.springframework.web.bind.annotation.RestController;
|
12
|
|
13
|
import eu.dnetlib.organizations.model.User;
|
14
|
import eu.dnetlib.organizations.model.view.UserView;
|
15
|
import eu.dnetlib.organizations.repository.UserRepository;
|
16
|
import eu.dnetlib.organizations.repository.readonly.UserViewRepository;
|
17
|
import eu.dnetlib.organizations.utils.OpenOrgsConstants;
|
18
|
|
19
|
@RestController
|
20
|
public class UserController {
|
21
|
|
22
|
private static final String DEFAULT_PASSWORD = "dnet";
|
23
|
|
24
|
@Autowired
|
25
|
private UserRepository userRepository;
|
26
|
@Autowired
|
27
|
private UserViewRepository userViewRepository;
|
28
|
|
29
|
@PostMapping(value = "/public_api/newUser")
|
30
|
public Map<String, Integer> newUser(final @RequestParam String email) {
|
31
|
final Map<String, Integer> res = new HashMap<>();
|
32
|
if (userRepository.existsById(email)) {
|
33
|
res.put("status", 2);
|
34
|
} else {
|
35
|
final User user = new User();
|
36
|
user.setEmail(email);
|
37
|
user.setPassword("{MD5}" + DigestUtils.md5Hex(DEFAULT_PASSWORD));
|
38
|
user.setRole(OpenOrgsConstants.pendingRole);
|
39
|
user.setValid(false);
|
40
|
userRepository.save(user);
|
41
|
res.put("status", 1);
|
42
|
}
|
43
|
return res;
|
44
|
}
|
45
|
|
46
|
@GetMapping("/api/users")
|
47
|
public Iterable<UserView> users() {
|
48
|
return userViewRepository.findAll();
|
49
|
}
|
50
|
|
51
|
}
|