experimental: temp save role if assign fails
This commit is contained in:
parent
0ad86025b3
commit
e9fedc90d4
|
@ -0,0 +1,53 @@
|
||||||
|
package eu.dnetlib.repo.manager.domain;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class PendingUserRole {
|
||||||
|
@Id
|
||||||
|
long id;
|
||||||
|
int coPersonId;
|
||||||
|
int couId;
|
||||||
|
|
||||||
|
public PendingUserRole() {
|
||||||
|
// no-arg constructor
|
||||||
|
}
|
||||||
|
|
||||||
|
public PendingUserRole(int coPersonId, int couId) {
|
||||||
|
this.coPersonId = coPersonId;
|
||||||
|
this.couId = couId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCoPersonId() {
|
||||||
|
return coPersonId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCoPersonId(int coPersonId) {
|
||||||
|
this.coPersonId = coPersonId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCouId() {
|
||||||
|
return couId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCouId(int couId) {
|
||||||
|
this.couId = couId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PendingUserRole{" +
|
||||||
|
"coPersonId=" + coPersonId +
|
||||||
|
", couId=" + couId +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package eu.dnetlib.repo.manager.repository;
|
||||||
|
|
||||||
|
import eu.dnetlib.repo.manager.domain.PendingUserRole;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface PendingUserRoleRepository extends CrudRepository<PendingUserRole, Long> {
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
package eu.dnetlib.repo.manager.service;
|
||||||
|
|
||||||
|
import eu.dnetlib.repo.manager.domain.PendingUserRole;
|
||||||
|
import eu.dnetlib.repo.manager.repository.PendingUserRoleRepository;
|
||||||
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PendingUserRoleService {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(PendingUserRoleService.class);
|
||||||
|
private final PendingUserRoleRepository pendingUserRoleRepository;
|
||||||
|
private final AaiRegistryService aaiRegistryService;
|
||||||
|
|
||||||
|
public PendingUserRoleService(PendingUserRoleRepository pendingUserRoleRepository,
|
||||||
|
AaiRegistryService aaiRegistryService) {
|
||||||
|
this.pendingUserRoleRepository = pendingUserRoleRepository;
|
||||||
|
this.aaiRegistryService = aaiRegistryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 3_600_000)
|
||||||
|
public void assignRoles() {
|
||||||
|
Iterable<PendingUserRole> roles = pendingUserRoleRepository.findAll();
|
||||||
|
for (PendingUserRole role : roles) {
|
||||||
|
logger.debug("Attempt to assign role: {}", role);
|
||||||
|
try {
|
||||||
|
aaiRegistryService.assignMemberRole(role.getCoPersonId(), role.getCouId());
|
||||||
|
pendingUserRoleRepository.deleteById(role.getId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("Could not assign role to user. Pending Role: {}\n", role, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
package eu.dnetlib.repo.manager.service.security;
|
package eu.dnetlib.repo.manager.service.security;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
import eu.dnetlib.repo.manager.domain.PendingUserRole;
|
||||||
import eu.dnetlib.repo.manager.domain.dto.Role;
|
import eu.dnetlib.repo.manager.domain.dto.Role;
|
||||||
import eu.dnetlib.repo.manager.domain.dto.User;
|
import eu.dnetlib.repo.manager.domain.dto.User;
|
||||||
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
|
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
|
||||||
|
import eu.dnetlib.repo.manager.repository.PendingUserRoleRepository;
|
||||||
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
|
||||||
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
||||||
import org.mitre.openid.connect.model.UserInfo;
|
import org.mitre.openid.connect.model.UserInfo;
|
||||||
|
@ -30,13 +32,16 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||||
private final RoleMappingService roleMappingService;
|
private final RoleMappingService roleMappingService;
|
||||||
private final AaiRegistryService aaiRegistryService;
|
private final AaiRegistryService aaiRegistryService;
|
||||||
private final AuthoritiesUpdater authoritiesUpdater;
|
private final AuthoritiesUpdater authoritiesUpdater;
|
||||||
|
private final PendingUserRoleRepository pendingUserRoleRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
AuthorizationServiceImpl(RoleMappingService roleMappingService, AaiRegistryService aaiRegistryService,
|
AuthorizationServiceImpl(RoleMappingService roleMappingService, AaiRegistryService aaiRegistryService,
|
||||||
AuthoritiesUpdater authoritiesUpdater) {
|
AuthoritiesUpdater authoritiesUpdater,
|
||||||
|
PendingUserRoleRepository pendingUserRoleRepository) {
|
||||||
this.roleMappingService = roleMappingService;
|
this.roleMappingService = roleMappingService;
|
||||||
this.aaiRegistryService = aaiRegistryService;
|
this.aaiRegistryService = aaiRegistryService;
|
||||||
this.authoritiesUpdater = authoritiesUpdater;
|
this.authoritiesUpdater = authoritiesUpdater;
|
||||||
|
this.pendingUserRoleRepository = pendingUserRoleRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String mapType(String type) {
|
private String mapType(String type) {
|
||||||
|
@ -148,7 +153,12 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||||
// Assign new role to the current authenticated user
|
// Assign new role to the current authenticated user
|
||||||
Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
|
Integer coPersonId = aaiRegistryService.getCoPersonIdByIdentifier();
|
||||||
if (couId != null) {
|
if (couId != null) {
|
||||||
aaiRegistryService.assignMemberRole(coPersonId, couId);
|
|
||||||
|
try {
|
||||||
|
aaiRegistryService.assignMemberRole(coPersonId, couId);
|
||||||
|
} catch (Exception e) {
|
||||||
|
pendingUserRoleRepository.save(new PendingUserRole(coPersonId, couId));
|
||||||
|
}
|
||||||
|
|
||||||
// Add role to current user authorities
|
// Add role to current user authorities
|
||||||
authoritiesUpdater.addRole(roleMappingService.repositoryIdToAuthority(resourceId));
|
authoritiesUpdater.addRole(roleMappingService.repositoryIdToAuthority(resourceId));
|
||||||
|
|
Loading…
Reference in New Issue