use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_uid_namespace method checkAttributeValue.
@Override
public /**
* Checks the new UID of the user. The new UID must
* not be lower than the min UID or greater than the max UID. Also no collision between
* existing user and the new user is allowed.
*/
void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws WrongAttributeValueException, WrongReferenceAttributeValueException, InternalErrorException, WrongAttributeAssignmentException {
Integer uid = (Integer) attribute.getValue();
String uidNamespace = attribute.getFriendlyNameParameter();
if (uid == null) {
throw new WrongAttributeValueException(attribute, "Attribute was not filled, therefore there is nothing to be checked.");
}
Attribute minUidAttribute = null;
Attribute maxUidAttribute = null;
try {
minUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_minUID);
maxUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_maxUID);
} catch (AttributeNotExistsException e) {
throw new ConsistencyErrorException("minUid and maxUid attributes are required", e);
}
Integer min = (Integer) minUidAttribute.getValue();
Integer max = (Integer) maxUidAttribute.getValue();
if (min == null) {
throw new WrongReferenceAttributeValueException(attribute, minUidAttribute);
}
if (max == null) {
throw new WrongReferenceAttributeValueException(attribute, maxUidAttribute);
}
//uid is in proper range
if (uid < min || uid > max) {
throw new WrongAttributeValueException(attribute, "UID " + uid + " is not proper range (" + min + "," + max + ")");
}
// Get all users who have set attribute urn:perun:member:attribute-def:def:uid-namespace:[uid-namespace], with the value.
List<User> usersWithUid = sess.getPerunBl().getUsersManagerBl().getUsersByAttribute(sess, attribute);
//remove self
usersWithUid.remove(user);
if (!usersWithUid.isEmpty()) {
if (usersWithUid.size() > 1)
throw new ConsistencyErrorException("FATAL ERROR: Duplicated UID detected." + attribute + " " + usersWithUid);
throw new WrongAttributeValueException(attribute, "This UID " + attribute.getValue() + " is already occupied by" + usersWithUid.get(0) + ".");
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userPreferredCertDN method fillAttribute.
public Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
Attribute userCertDNs = null;
try {
userCertDNs = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
Map<String, String> certDNsValue = null;
if (userCertDNs.getValue() != null) {
certDNsValue = (Map<String, String>) userCertDNs.getValue();
Set<String> keys = certDNsValue.keySet();
for (String key : keys) {
if (key != null && !key.isEmpty()) {
Attribute attr = new Attribute(attribute);
attr.setValue(key);
return attr;
}
}
}
return new Attribute(attribute);
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userPreferredCertDN method checkAttributeValue.
public void checkAttributeValue(PerunSessionImpl sess, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException {
Attribute userCertDNs = null;
try {
userCertDNs = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
Map<String, String> certDNsValue = null;
if (userCertDNs.getValue() != null) {
certDNsValue = (Map<String, String>) userCertDNs.getValue();
} else {
if (attribute.getValue() != null)
throw new WrongReferenceAttributeValueException(attribute, userCertDNs, "There is no certificates for this user so preferred certificate can't be choose.");
else
return;
}
if (attribute.getValue() == null) {
if (certDNsValue != null || !certDNsValue.isEmpty())
throw new WrongAttributeValueException(attribute, user, "This attribute value can't be null because of notNull attribute userCertDNs");
} else {
String preferredCertDNValue = (String) attribute.getValue();
if (!certDNsValue.containsKey(preferredCertDNValue))
throw new WrongAttributeValueException(attribute, "This attribute value must be one of exsiting keys in userCertDNs.");
}
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class UsersManagerEntryIntegrationTest method getUsersByAttribute.
@Test
public void getUsersByAttribute() throws Exception {
System.out.println(CLASS_NAME + "getUsersByAttribute");
// Check if the attribute already exists
Attribute attr;
AttributeDefinition attrDef;
try {
attrDef = perun.getAttributesManagerBl().getAttributeDefinition(sess, "urn:perun:user:attribute-def:opt:user_test_attribute");
} catch (AttributeNotExistsException e) {
// Attribute doesn't exist, so create it
attrDef = new AttributeDefinition();
attrDef.setNamespace("urn:perun:user:attribute-def:opt");
attrDef.setFriendlyName("user-test-attribute");
attrDef.setType(String.class.getName());
attrDef = perun.getAttributesManagerBl().createAttribute(sess, attrDef);
}
attr = new Attribute(attrDef);
attr.setValue("UserAttribute");
// Set the attribute to the user
perun.getAttributesManagerBl().setAttribute(sess, user, attr);
assertTrue("results must contain user", usersManager.getUsersByAttribute(sess, attr).contains(user));
}
use of cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method sendPasswordResetLinkEmail.
public void sendPasswordResetLinkEmail(PerunSession sess, Member member, String namespace, String url) throws InternalErrorException {
User user = perunBl.getUsersManagerBl().getUserByMember(sess, member);
List<Attribute> logins = perunBl.getAttributesManagerBl().getLogins(sess, user);
boolean found = false;
for (Attribute a : logins) {
if (a.getFriendlyNameParameter().equals(namespace))
found = true;
}
if (!found)
throw new InternalErrorException(user.toString() + " doesn't have login in namespace: " + namespace);
String email = "";
try {
Attribute a = perunBl.getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredMail");
if (a != null && a.getValue() != null) {
email = (String) a.getValue();
}
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException(ex);
}
int id = getMembersManagerImpl().storePasswordResetRequest(sess, user, namespace);
Utils.sendPasswordResetEmail(user, email, namespace, url, id);
}
Aggregations