use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userCertDNs method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
Attribute userPreferredCertDN = null;
try {
userPreferredCertDN = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userPreferredCertDN");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
String preferredCertDNValue = null;
if (userPreferredCertDN.getValue() != null)
preferredCertDNValue = (String) userPreferredCertDN.getValue();
Map<String, String> certDNs = null;
if (attribute.getValue() != null)
certDNs = (Map<String, String>) attribute.getValue();
if (certDNs == null || certDNs.isEmpty()) {
try {
session.getPerunBl().getAttributesManagerBl().removeAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
} else {
Set<String> certDNsKeys = certDNs.keySet();
String newPossibleCertDN = null;
for (String key : certDNsKeys) {
if (key != null && !key.isEmpty()) {
newPossibleCertDN = key;
break;
}
}
if (preferredCertDNValue == null) {
userPreferredCertDN.setValue(newPossibleCertDN);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
} else {
if (!certDNsKeys.contains(preferredCertDNValue)) {
userPreferredCertDN.setValue(newPossibleCertDN);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userPreferredCertDN method changedAttributeHook.
//TODO what dependencies of this attribute???
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
if (attribute.getValue() == null) {
Attribute userCertDNs = null;
try {
userCertDNs = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
Map<String, String> certDNsValue = null;
if (userCertDNs.getValue() != null) {
certDNsValue = (Map<String, String>) userCertDNs.getValue();
}
if (certDNsValue != null && !certDNsValue.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, "Can't remove preferredCert if there is any existing certDNs for the user.");
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_vsupMail method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
// map of reserved vsup mails
Attribute reservedMailsAttribute;
Map<String, String> reservedMailsAttributeValue;
// other vsup mail attributes to get values from
Attribute mailAliasAttribute;
Attribute mailAliasesAttribute;
Attribute vsupPreferredMailAttribute;
// output sets used for comparison
Set<String> reservedMailsOfUser = new HashSet<>();
Set<String> actualMailsOfUser = new HashSet<>();
try {
reservedMailsAttribute = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, usedMailsKeyVsup, usedMailsUrn);
mailAliasAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasUrn);
mailAliasesAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupMailAliasesUrn);
vsupPreferredMailAttribute = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, vsupPreferredMailUrn);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute doesn't exists.", ex);
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
if (attribute.getValue() == null && reservedMailsAttribute.getValue() == null) {
throw new ConsistencyErrorException("Entityless attribute 'urn:perun:entityless:attribute-def:def:usedMails' is empty, but we are removing 'vsupMail' value, so there should have been entry in entityless attribute.");
}
if (reservedMailsAttribute.getValue() == null) {
reservedMailsAttributeValue = new LinkedHashMap<>();
} else {
reservedMailsAttributeValue = (Map<String, String>) reservedMailsAttribute.getValue();
}
// if SET action and mail is already reserved by other user
if (attribute.getValue() != null) {
String ownersUserId = reservedMailsAttributeValue.get((String) attribute.getValue());
if (ownersUserId != null && !Objects.equals(ownersUserId, String.valueOf(user.getId()))) {
// TODO - maybe get actual owners attribute and throw WrongReferenceAttributeException to be nice in a GUI ?
throw new InternalErrorException("VŠUP mail: '" + attribute.getValue() + "' is already in use by User ID: " + ownersUserId + ".");
}
}
for (Map.Entry<String, String> entry : reservedMailsAttributeValue.entrySet()) {
if (Objects.equals(entry.getValue(), String.valueOf(user.getId()))) {
// reserved mails of a user
reservedMailsOfUser.add(entry.getKey());
}
}
if (mailAliasAttribute.getValue() != null) {
actualMailsOfUser.add((String) mailAliasAttribute.getValue());
}
if (vsupPreferredMailAttribute.getValue() != null) {
actualMailsOfUser.add((String) vsupPreferredMailAttribute.getValue());
}
if (mailAliasesAttribute.getValue() != null) {
actualMailsOfUser.addAll((ArrayList<String>) mailAliasesAttribute.getValue());
}
for (String mail : reservedMailsOfUser) {
if (!actualMailsOfUser.contains(mail)) {
// Remove mail, which is not in attributes anymore
reservedMailsAttributeValue.remove(mail);
// since this attribute holds single value, we can break the cycle here
break;
}
}
// Put in which is in attribute but not in a map
if (attribute.getValue() != null) {
reservedMailsAttributeValue.putIfAbsent((String) attribute.getValue(), String.valueOf(user.getId()));
}
// save changes in entityless attribute
try {
// always set value to attribute, since we might start with null in attribute and empty map in variable !!
reservedMailsAttribute.setValue(reservedMailsAttributeValue);
session.getPerunBl().getAttributesManagerBl().setAttribute(session, usedMailsKeyVsup, reservedMailsAttribute);
} catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
// if set, check vsupPreferredMail - if is empty, set vsupMail to vsupPreferredMail
if (vsupPreferredMailAttribute.getValue() == null && attribute.getValue() != null) {
vsupPreferredMailAttribute.setValue(attribute.getValue());
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, vsupPreferredMailAttribute);
} catch (WrongAttributeValueException | WrongAttributeAssignmentException e) {
throw new InternalErrorException("Unable to store generated vsupMail to vsupPreferredMail.", e);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_facility_attribute_def_virt_preferredUnixGroupName method getAttributeValue.
@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
Attribute attr = new Attribute(attributeDefinition);
Attribute preferredGroupNameAttribute = null;
try {
Attribute facilityGroupNameNamespaceAttr = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, A_FACILITY_DEF_UNIX_GROUPNAME_NAMESPACE);
if (facilityGroupNameNamespaceAttr.getValue() != null) {
String namespace = (String) facilityGroupNameNamespaceAttr.getValue();
preferredGroupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, A_USER_DEF_PREFERRED_UNIX_GROUPNAME_NAMESPACE + namespace);
attr = Utils.copyAttributeToVirtualAttributeWithValue(preferredGroupNameAttribute, attr);
} else {
attr.setValue(null);
}
} catch (AttributeNotExistsException e) {
throw new ConsistencyErrorException(e);
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
return attr;
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_facility_attribute_def_virt_shell method getAttributeValue.
public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
Attribute attr = new Attribute(attributeDefinition);
try {
Attribute attribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":shell");
if (attribute.getValue() != null) {
Utils.copyAttributeToVirtualAttributeWithValue(attribute, attr);
return attr;
}
} catch (WrongAttributeAssignmentException | AttributeNotExistsException ex) {
throw new InternalErrorException(ex);
}
try {
Attribute facilityShells = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":shells");
Attribute userPrefferedShells = (sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredShells"));
List<Resource> resources = sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user);
Set<String> resourcesShells = new HashSet<String>();
for (Resource resource : resources) {
List<String> resourcesShellsForTest = (List<String>) sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells").getValue();
if (resourcesShellsForTest != null)
resourcesShells.addAll(resourcesShellsForTest);
}
if (userPrefferedShells.getValue() != null) {
for (String pShell : (List<String>) userPrefferedShells.getValue()) {
if (resourcesShells.contains(pShell)) {
Utils.copyAttributeToViAttributeWithoutValue(userPrefferedShells, attr);
attr.setValue(pShell);
return attr;
}
}
}
if (facilityShells.getValue() != null) {
for (String fShell : (List<String>) facilityShells.getValue()) {
if (resourcesShells.contains(fShell)) {
Utils.copyAttributeToViAttributeWithoutValue(facilityShells, attr);
attr.setValue(fShell);
return attr;
}
}
}
} catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
return attr;
}
Aggregations