new fields in authoeization request

This commit is contained in:
Michele Artini 2022-09-19 14:33:22 +02:00
parent 098537e067
commit ee8e84b316
8 changed files with 124 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package eu.dnetlib.organizations;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
@ -124,7 +125,13 @@ public class OAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
return (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)
.filter(StringUtils::isNotBlank)
.orElse(UserRole.NOT_AUTHORIZED.toString());

View File

@ -47,16 +47,18 @@ public class AdminController extends AbstractDnetController {
private DatabaseUtils dbUtils;
@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 fullname = UserInfo.getFullname(authentication);
final String organization = UserInfo.getOrganization(authentication);
final Map<String, Integer> res = new HashMap<>();
if (!UserInfo.isNotAuthorized(authentication) || userRepository.existsById(email)) {
res.put("status", 2);
} else {
dbUtils.newUser(email, countries);
dbUtils.newUser(email, fullname, organization, user.getReferencePerson(), user.getRequestMessage(), user.getCountries());
res.put("status", 1);
}
return res;

View File

@ -65,6 +65,16 @@ public class HomeController extends AbstractDnetController {
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")
public SystemConfiguration getSysConf(final Authentication authentication) {
return systemConfigurationRepository.findById(SystemConfiguration.DEFAULT_ID).get();

View File

@ -92,4 +92,14 @@ public class UserInfo {
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";
}
}

View File

@ -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;
}
}

View File

@ -11,7 +11,7 @@ import eu.dnetlib.organizations.model.User;
public interface UserRepository extends JpaRepository<User, String> {
@Modifying
@Query("update User set last_access = ?2 where email = ?1")
void updateLastAccess(final String email, OffsetDateTime now);
@Query("update User set fullname = ?2, organization = ?3, last_access = ?4 where email = ?1")
void updateDetails(final String email, final String fullname, final String organization, OffsetDateTime now);
}

View File

@ -339,6 +339,10 @@ public class DatabaseUtils {
@Transactional
public void saveUser(@RequestBody final UserView userView) {
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.setValid(userView.isValid());
userRepository.save(user);
@ -356,12 +360,24 @@ public class DatabaseUtils {
}
@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();
user.setEmail(email);
user.setFullname(fullname);
user.setOrganization(organization);
user.setReferencePerson(referencePerson);
user.setRequestMessage(requestMessage);
user.setRole(UserRole.PENDING.name());
user.setValid(false);
userRepository.save(user);
if (countries != null) {
userCountryRepository.saveAll(countries.stream().map(c -> new UserCountry(email, c)).collect(Collectors.toList()));
}
@ -536,11 +552,12 @@ public class DatabaseUtils {
@Transactional
public Optional<User> findUser(final String email) {
final Optional<User> user = userRepository.findById(email);
if (user.isPresent()) {
userRepository.updateLastAccess(email, OffsetDateTime.now());
return userRepository.findById(email);
}
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) {

View File

@ -50,6 +50,25 @@
</p>
<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-header">Select your countries</div>
@ -84,6 +103,8 @@
<script>
angular.module('authReqApp', ['checklist-model']).controller('authReqCtrl', function($scope, $http) {
$scope.vocCountries = [];
$scope.referencePerson = '';
$scope.requestMessage = '';
$scope.countries = [];
$scope.registrationStatus = 0;
$scope.registrationMessage = '';
@ -94,11 +115,14 @@
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
});
$scope.register = function (email) {
$scope.register = function () {
$('#btnRegister').attr("disabled", "disabled");
$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;
if (res.data.status == 1) {
$scope.registrationMessage = 'Registration saved !';