use of de.tum.in.www1.artemis.exception.ArtemisAuthenticationException in project ArTEMiS by ls1intum.
the class JiraAuthenticationProvider method addUserToGroup.
/**
* Adds a JIRA user to a JIRA group. Ignores "user is already a member of" errors.
*
* @param username The JIRA username
* @param group The JIRA group name
* @throws ArtemisAuthenticationException if JIRA returns an error
*/
@Override
public void addUserToGroup(String username, String group) throws ArtemisAuthenticationException {
HttpHeaders headers = HeaderUtil.createAuthorization(JIRA_USER, JIRA_PASSWORD);
Map<String, Object> body = new HashMap<>();
body.put("name", username);
HttpEntity<?> entity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.exchange(JIRA_URL + "/rest/api/2/group/user?groupname=" + group, HttpMethod.POST, entity, Map.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.BAD_REQUEST) && e.getResponseBodyAsString().contains("user is already a member of")) {
// ignore the error if the user is already in the group
return;
}
log.error("Could not add JIRA user to group " + group, e);
throw new ArtemisAuthenticationException("Error while adding user to JIRA group");
}
}
use of de.tum.in.www1.artemis.exception.ArtemisAuthenticationException in project ArTEMiS by ls1intum.
the class JiraAuthenticationProvider method getUsernameForEmail.
/**
* Checks if an JIRA user for the given email address exists.
*
* @param email The JIRA user email address
* @return Optional String of JIRA username
* @throws ArtemisAuthenticationException
*/
@Override
public Optional<String> getUsernameForEmail(String email) throws ArtemisAuthenticationException {
HttpHeaders headers = HeaderUtil.createAuthorization(JIRA_USER, JIRA_PASSWORD);
HttpEntity<?> entity = new HttpEntity<>(headers);
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<ArrayList> authenticationResponse = restTemplate.exchange(JIRA_URL + "/rest/api/2/user/search?username=" + email, HttpMethod.GET, entity, ArrayList.class);
ArrayList results = authenticationResponse.getBody();
if (results.size() == 0) {
// no result
return Optional.empty();
}
Map firstResult = (Map) results.get(0);
return Optional.of((String) firstResult.get("name"));
} catch (HttpClientErrorException e) {
log.error("Could not get JIRA username for email address " + email, e);
throw new ArtemisAuthenticationException("Error while checking eMail address");
}
}
Aggregations