use of cz.metacentrum.perun.core.api.exceptions.PasswordDeletionFailedException in project perun by CESNET.
the class UsersManagerBlImpl method deleteAlternativePassword.
@Override
public void deleteAlternativePassword(PerunSession sess, User user, String loginNamespace, String passwordId) throws PasswordDeletionFailedException, LoginNotExistsException {
log.info("Deleting alternative password for {} in login-namespace {} with passwordId {}.", user, loginNamespace, passwordId);
try {
Attribute userAlternativePassword = getPerunBl().getAttributesManagerBl().getAttribute(sess, user, A_USER_DEF_ALT_PASSWORD_NAMESPACE + loginNamespace);
Map<String, String> altPassValue = new LinkedHashMap<>();
// Set not null value from altPassword attribute of this user
if (userAlternativePassword.getValue() != null)
altPassValue = userAlternativePassword.valueAsMap();
// If password already exists, throw an exception
if (!altPassValue.containsValue(passwordId))
throw new PasswordDeletionFailedException("Password not found by ID.");
// remove key with this value from map
Set<String> keys = altPassValue.keySet();
String description = null;
for (String key : keys) {
String valueOfKey = altPassValue.get(key);
if (valueOfKey.equals(passwordId)) {
if (description != null)
throw new ConsistencyErrorException("There is more than 1 password with same ID in value for user " + user);
description = key;
}
}
if (description == null)
throw new InternalErrorException("Password not found by ID.");
altPassValue.remove(description);
// set new value for altPassword attribute for this user
userAlternativePassword.setValue(altPassValue);
getPerunBl().getAttributesManagerBl().setAttribute(sess, user, userAlternativePassword);
} catch (WrongAttributeAssignmentException | WrongReferenceAttributeValueException | WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
// actually delete password in the backend
PasswordManagerModule module = getPasswordManagerModule(sess, loginNamespace);
try {
module.deleteAlternativePassword(sess, user, passwordId);
} catch (PasswordDeletionFailedRuntimeException ex) {
throw new PasswordDeletionFailedException(ex);
} catch (LoginNotExistsRuntimeException ex) {
throw new LoginNotExistsException(ex);
} catch (Exception ex) {
// fallback for exception compatibility
throw new PasswordDeletionFailedException("Alternative password deletion failed for " + loginNamespace + ":" + passwordId + " of " + user + ".", ex);
}
}
Aggregations