new fields in authoeization request
This commit is contained in:
parent
098537e067
commit
ee8e84b316
|
@ -1,6 +1,7 @@
|
||||||
package eu.dnetlib.organizations;
|
package eu.dnetlib.organizations;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -124,7 +125,13 @@ public class OAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
return (userRequest) -> {
|
return (userRequest) -> {
|
||||||
final OidcUser oidcUser = delegate.loadUser(userRequest);
|
final OidcUser oidcUser = delegate.loadUser(userRequest);
|
||||||
|
|
||||||
final String role = "ROLE_" + OpenOrgsConstants.OPENORGS_ROLE_PREFIX + databaseUtils.findUser(oidcUser.getEmail())
|
final Optional<User> user = databaseUtils.findUser(oidcUser.getEmail());
|
||||||
|
|
||||||
|
if (user.isPresent()) {
|
||||||
|
databaseUtils.updateUserDetails(oidcUser.getEmail(), oidcUser.getFullName(), oidcUser.getAttribute("organization"));
|
||||||
|
}
|
||||||
|
|
||||||
|
final String role = "ROLE_" + OpenOrgsConstants.OPENORGS_ROLE_PREFIX + user
|
||||||
.map(User::getRole)
|
.map(User::getRole)
|
||||||
.filter(StringUtils::isNotBlank)
|
.filter(StringUtils::isNotBlank)
|
||||||
.orElse(UserRole.NOT_AUTHORIZED.toString());
|
.orElse(UserRole.NOT_AUTHORIZED.toString());
|
||||||
|
|
|
@ -47,16 +47,18 @@ public class AdminController extends AbstractDnetController {
|
||||||
private DatabaseUtils dbUtils;
|
private DatabaseUtils dbUtils;
|
||||||
|
|
||||||
@PostMapping(value = "/registration_api/newUser")
|
@PostMapping(value = "/registration_api/newUser")
|
||||||
public Map<String, Integer> newUser(final @RequestBody List<String> countries, final Authentication authentication) {
|
public Map<String, Integer> newUser(final @RequestBody UserRegistration user, final Authentication authentication) {
|
||||||
|
|
||||||
final String email = UserInfo.getEmail(authentication);
|
final String email = UserInfo.getEmail(authentication);
|
||||||
|
final String fullname = UserInfo.getFullname(authentication);
|
||||||
|
final String organization = UserInfo.getOrganization(authentication);
|
||||||
|
|
||||||
final Map<String, Integer> res = new HashMap<>();
|
final Map<String, Integer> res = new HashMap<>();
|
||||||
|
|
||||||
if (!UserInfo.isNotAuthorized(authentication) || userRepository.existsById(email)) {
|
if (!UserInfo.isNotAuthorized(authentication) || userRepository.existsById(email)) {
|
||||||
res.put("status", 2);
|
res.put("status", 2);
|
||||||
} else {
|
} else {
|
||||||
dbUtils.newUser(email, countries);
|
dbUtils.newUser(email, fullname, organization, user.getReferencePerson(), user.getRequestMessage(), user.getCountries());
|
||||||
res.put("status", 1);
|
res.put("status", 1);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -65,6 +65,16 @@ public class HomeController extends AbstractDnetController {
|
||||||
return authentication != null ? UserInfo.getEmail(authentication) : null;
|
return authentication != null ? UserInfo.getEmail(authentication) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ModelAttribute("fullname")
|
||||||
|
public String getUserFullname(final Authentication authentication) {
|
||||||
|
return authentication != null ? UserInfo.getFullname(authentication) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ModelAttribute("organization")
|
||||||
|
public String getUserOrganization(final Authentication authentication) {
|
||||||
|
return authentication != null ? UserInfo.getOrganization(authentication) : null;
|
||||||
|
}
|
||||||
|
|
||||||
@ModelAttribute("sysconf")
|
@ModelAttribute("sysconf")
|
||||||
public SystemConfiguration getSysConf(final Authentication authentication) {
|
public SystemConfiguration getSysConf(final Authentication authentication) {
|
||||||
return systemConfigurationRepository.findById(SystemConfiguration.DEFAULT_ID).get();
|
return systemConfigurationRepository.findById(SystemConfiguration.DEFAULT_ID).get();
|
||||||
|
|
|
@ -92,4 +92,14 @@ public class UserInfo {
|
||||||
return user instanceof DefaultOidcUser ? ((DefaultOidcUser) user).getEmail() : authentication.getName();
|
return user instanceof DefaultOidcUser ? ((DefaultOidcUser) user).getEmail() : authentication.getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getFullname(final Authentication authentication) {
|
||||||
|
final Object user = authentication.getPrincipal();
|
||||||
|
return user instanceof DefaultOidcUser ? ((DefaultOidcUser) user).getFullName() : "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getOrganization(final Authentication authentication) {
|
||||||
|
final Object user = authentication.getPrincipal();
|
||||||
|
return user instanceof DefaultOidcUser ? ((DefaultOidcUser) user).getAttribute("organization") : "unknown";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package eu.dnetlib.organizations.controller;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class UserRegistration implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 4872798305803491565L;
|
||||||
|
|
||||||
|
private String referencePerson;
|
||||||
|
|
||||||
|
private String requestMessage;
|
||||||
|
|
||||||
|
private List<String> countries;
|
||||||
|
|
||||||
|
public String getReferencePerson() {
|
||||||
|
return referencePerson;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReferencePerson(final String referencePerson) {
|
||||||
|
this.referencePerson = referencePerson;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRequestMessage() {
|
||||||
|
return requestMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRequestMessage(final String requestMessage) {
|
||||||
|
this.requestMessage = requestMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getCountries() {
|
||||||
|
return countries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCountries(final List<String> countries) {
|
||||||
|
this.countries = countries;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ import eu.dnetlib.organizations.model.User;
|
||||||
public interface UserRepository extends JpaRepository<User, String> {
|
public interface UserRepository extends JpaRepository<User, String> {
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Query("update User set last_access = ?2 where email = ?1")
|
@Query("update User set fullname = ?2, organization = ?3, last_access = ?4 where email = ?1")
|
||||||
void updateLastAccess(final String email, OffsetDateTime now);
|
void updateDetails(final String email, final String fullname, final String organization, OffsetDateTime now);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -339,6 +339,10 @@ public class DatabaseUtils {
|
||||||
@Transactional
|
@Transactional
|
||||||
public void saveUser(@RequestBody final UserView userView) {
|
public void saveUser(@RequestBody final UserView userView) {
|
||||||
final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
|
final User user = userRepository.findById(userView.getEmail()).orElseThrow(() -> new RuntimeException("User not found"));
|
||||||
|
user.setFullname(userView.getFullname());
|
||||||
|
user.setOrganization(userView.getOrganization());
|
||||||
|
user.setReferencePerson(userView.getReferencePerson());
|
||||||
|
user.setRequestMessage(userView.getRequestMessage());
|
||||||
user.setRole(userView.getRole());
|
user.setRole(userView.getRole());
|
||||||
user.setValid(userView.isValid());
|
user.setValid(userView.isValid());
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
@ -356,12 +360,24 @@ public class DatabaseUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void newUser(final String email, final List<String> countries) {
|
public void newUser(final String email,
|
||||||
|
final String fullname,
|
||||||
|
final String organization,
|
||||||
|
final String referencePerson,
|
||||||
|
final String requestMessage,
|
||||||
|
final List<String> countries) {
|
||||||
|
|
||||||
final User user = new User();
|
final User user = new User();
|
||||||
user.setEmail(email);
|
user.setEmail(email);
|
||||||
|
user.setFullname(fullname);
|
||||||
|
user.setOrganization(organization);
|
||||||
|
user.setReferencePerson(referencePerson);
|
||||||
|
user.setRequestMessage(requestMessage);
|
||||||
user.setRole(UserRole.PENDING.name());
|
user.setRole(UserRole.PENDING.name());
|
||||||
user.setValid(false);
|
user.setValid(false);
|
||||||
|
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
if (countries != null) {
|
if (countries != null) {
|
||||||
userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
|
userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
@ -536,11 +552,12 @@ public class DatabaseUtils {
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Optional<User> findUser(final String email) {
|
public Optional<User> findUser(final String email) {
|
||||||
final Optional<User> user = userRepository.findById(email);
|
return userRepository.findById(email);
|
||||||
if (user.isPresent()) {
|
|
||||||
userRepository.updateLastAccess(email, OffsetDateTime.now());
|
|
||||||
}
|
}
|
||||||
return user;
|
|
||||||
|
@Transactional
|
||||||
|
public void updateUserDetails(final String email, final String fullname, final String organization) {
|
||||||
|
userRepository.updateDetails(email, fullname, organization, OffsetDateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String findFirstString(final List<OrganizationView> views, final Function<OrganizationView, String> mapper) {
|
private String findFirstString(final List<OrganizationView> views, final Function<OrganizationView, String> mapper) {
|
||||||
|
|
|
@ -50,6 +50,25 @@
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<form class="small">
|
<form class="small">
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Fullname</label>
|
||||||
|
<input type="text" readonly class="form-control-plaintext" th:value="${fullname}" />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Organization</label>
|
||||||
|
<input type="text" readonly class="form-control-plaintext" th:value="${organization}" />
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Reference person</label>
|
||||||
|
<input type="text" class="form-control" ng-model="referencePerson">
|
||||||
|
<small class="form-text text-muted">Please indicate your reference person (if available)</small>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label>Request message</label>
|
||||||
|
<textarea class="form-control" ng-model="requestMessage" cols="4"></textarea>
|
||||||
|
<small class="form-text text-muted">Specify the reasons for your request</small>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="card" style="margin-top: 25px">
|
<div class="card" style="margin-top: 25px">
|
||||||
<div class="card-header">Select your countries</div>
|
<div class="card-header">Select your countries</div>
|
||||||
|
|
||||||
|
@ -84,6 +103,8 @@
|
||||||
<script>
|
<script>
|
||||||
angular.module('authReqApp', ['checklist-model']).controller('authReqCtrl', function($scope, $http) {
|
angular.module('authReqApp', ['checklist-model']).controller('authReqCtrl', function($scope, $http) {
|
||||||
$scope.vocCountries = [];
|
$scope.vocCountries = [];
|
||||||
|
$scope.referencePerson = '';
|
||||||
|
$scope.requestMessage = '';
|
||||||
$scope.countries = [];
|
$scope.countries = [];
|
||||||
$scope.registrationStatus = 0;
|
$scope.registrationStatus = 0;
|
||||||
$scope.registrationMessage = '';
|
$scope.registrationMessage = '';
|
||||||
|
@ -94,11 +115,14 @@
|
||||||
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
|
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$scope.register = function () {
|
||||||
$scope.register = function (email) {
|
|
||||||
$('#btnRegister').attr("disabled", "disabled");
|
$('#btnRegister').attr("disabled", "disabled");
|
||||||
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
||||||
$http.post('registration_api/newUser', $scope.countries).then(function successCallback(res) {
|
$http.post('registration_api/newUser', {
|
||||||
|
'referencePerson' : $scope.referencePerson,
|
||||||
|
'requestMessage': $scope.requestMessage,
|
||||||
|
'countries': $scope.countries
|
||||||
|
}).then(function successCallback(res) {
|
||||||
$scope.registrationStatus = res.data.status;
|
$scope.registrationStatus = res.data.status;
|
||||||
if (res.data.status == 1) {
|
if (res.data.status == 1) {
|
||||||
$scope.registrationMessage = 'Registration saved !';
|
$scope.registrationMessage = 'Registration saved !';
|
||||||
|
|
Loading…
Reference in New Issue