use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class AttributesManagerImpl method getAttributes.
public List<Attribute> getAttributes(PerunSession sess, Member member, List<String> attrNames) throws InternalErrorException {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("mId", member.getId());
parameters.addValue("nSC", AttributesManager.NS_MEMBER_ATTR_CORE);
parameters.addValue("nSO", AttributesManager.NS_MEMBER_ATTR_OPT);
parameters.addValue("nSD", AttributesManager.NS_MEMBER_ATTR_DEF);
parameters.addValue("nSV", AttributesManager.NS_MEMBER_ATTR_VIRT);
parameters.addValue("attrNames", attrNames);
try {
return namedParameterJdbcTemplate.query("select " + getAttributeMappingSelectQuery("mem") + " from attr_names " + "left join member_attr_values mem on id=mem.attr_id and member_id=:mId " + "where namespace in ( :nSC,:nSO,:nSD,:nSV ) and attr_names.attr_name in ( :attrNames )", parameters, new AttributeRowMapper(sess, this, member));
} catch (EmptyResultDataAccessException ex) {
return new ArrayList<Attribute>();
} catch (RuntimeException ex) {
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class SearcherImpl method getUsers.
public List<User> getUsers(PerunSession sess, Map<Attribute, String> attributesWithSearchingValues) throws InternalErrorException {
StringBuilder query = new StringBuilder();
query.append("select distinct " + UsersManagerImpl.userMappingSelectQuery + " from users ");
List<String> whereClauses = new ArrayList<String>();
MapSqlParameterSource parameters = new MapSqlParameterSource();
int counter = 0;
for (Attribute key : attributesWithSearchingValues.keySet()) {
counter++;
String value = attributesWithSearchingValues.get(key);
query.append("left join user_attr_values val" + counter + " ");
query.append("on val" + counter + ".user_id=users.id and val" + counter + ".attr_id=" + key.getId() + " ");
query.append("left join attr_names nam" + counter + " on val" + counter + ".attr_id=nam" + counter + ".id ");
if (value == null || value.isEmpty()) {
if (key.getType().equals(LinkedHashMap.class.getName()) || key.getType().equals(BeansUtils.largeStringClassName) || key.getType().equals(BeansUtils.largeArrayListClassName)) {
whereClauses.add("val" + counter + ".attr_value_text IS NULL ");
} else {
whereClauses.add("val" + counter + ".attr_value IS NULL ");
}
} else {
if (key.getType().equals(Integer.class.getName())) {
key.setValue(Integer.valueOf(value));
whereClauses.add("val" + counter + ".attr_value=:v" + counter + " ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, Integer.class.getName().toString());
parameters.addValue("v" + counter, BeansUtils.attributeValueToString(key));
} else if (key.getType().equals(String.class.getName())) {
key.setValue(value);
whereClauses.add("lower(" + Compatibility.convertToAscii("val" + counter + ".attr_value") + ")=lower(" + Compatibility.convertToAscii(":v" + counter) + ") ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, String.class.getName().toString());
parameters.addValue("v" + counter, BeansUtils.attributeValueToString(key));
} else if (key.getType().equals(BeansUtils.largeStringClassName)) {
key.setValue(value);
whereClauses.add("lower(" + Compatibility.convertToAscii("val" + counter + ".attr_value_text") + ") LIKE lower(" + Compatibility.convertToAscii(":v" + counter) + ") ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, BeansUtils.largeStringClassName);
parameters.addValue("v" + counter, BeansUtils.attributeValueToString(key));
} else if (key.getType().equals(Boolean.class.getName())) {
key.setValue(value);
whereClauses.add("lower(" + Compatibility.convertToAscii("val" + counter + ".attr_value") + ")=lower(" + Compatibility.convertToAscii(":v" + counter) + ") ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, Boolean.class.getName().toString());
parameters.addValue("v" + counter, BeansUtils.attributeValueToString(key));
} else if (key.getType().equals(ArrayList.class.getName())) {
List<String> list = new ArrayList<String>();
list.add(value);
key.setValue(list);
whereClauses.add("val" + counter + ".attr_value LIKE :v" + counter + " ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, ArrayList.class.getName().toString());
parameters.addValue("v" + counter, '%' + BeansUtils.attributeValueToString(key).substring(0, BeansUtils.attributeValueToString(key).length() - 1) + '%');
} else if (key.getType().equals(BeansUtils.largeArrayListClassName)) {
List<String> list = new ArrayList<String>();
list.add(value);
key.setValue(list);
whereClauses.add("val" + counter + ".attr_value_text LIKE :v" + counter + " ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, BeansUtils.largeArrayListClassName);
parameters.addValue("v" + counter, '%' + BeansUtils.attributeValueToString(key).substring(0, BeansUtils.attributeValueToString(key).length() - 1) + '%');
} else if (key.getType().equals(LinkedHashMap.class.getName())) {
String[] splitMapItem = value.split("=");
if (splitMapItem.length == 0)
throw new InternalErrorException("Value can't be split by char '='.");
String splitKey = splitMapItem[0];
StringBuilder splitValue = new StringBuilder();
if (splitMapItem.length > 1) {
for (int i = 1; i < splitMapItem.length; i++) {
if (i != 1)
splitValue.append('=');
splitValue.append(splitMapItem[i]);
}
}
Map<String, String> map = new LinkedHashMap<String, String>();
map.put(splitKey, splitValue.length() == 0 ? null : splitValue.toString());
key.setValue(map);
whereClauses.add("val" + counter + ".attr_value_text LIKE :v" + counter + " or val" + counter + ".attr_value_text LIKE :vv" + counter + " ");
whereClauses.add("nam" + counter + ".type=:n" + counter + " ");
parameters.addValue("n" + counter, LinkedHashMap.class.getName().toString());
parameters.addValue("v" + counter, BeansUtils.attributeValueToString(key) + '%');
parameters.addValue("vv" + counter, "%," + BeansUtils.attributeValueToString(key) + '%');
} else {
throw new InternalErrorException(key + " is not type of integer, string, boolean, array or hashmap.");
}
}
}
//Add Where clauses at end of sql query
boolean first = true;
for (String whereClause : whereClauses) {
if (first) {
query.append("where ");
query.append(whereClause);
first = false;
} else {
query.append("and ");
query.append(whereClause);
}
}
try {
return jdbc.query(query.toString(), parameters, UsersManagerImpl.USER_MAPPER);
} catch (EmptyResultDataAccessException e) {
return new ArrayList<User>();
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class urn_perun_group_attribute_def_def_unixGID_namespace method checkAttributeValue.
public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
try {
String gidNamespace = attribute.getFriendlyNameParameter();
//Special behaviour if gid is null
if (attribute.getValue() == null) {
List<Facility> groupFacilities = new ArrayList<Facility>();
for (Resource r : sess.getPerunBl().getResourcesManagerBl().getAssignedResources(sess, group)) {
groupFacilities.add(sess.getPerunBl().getResourcesManagerBl().getFacility(sess, r));
}
Set<String> namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere = sess.getPerunBl().getModulesUtilsBl().getSetOfGroupNameNamespacesWhereFacilitiesHasTheSameGIDNamespace(sess, groupFacilities, attribute);
for (String namespace : namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere) {
Attribute unixGroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, A_G_unixGroupName_namespace + ":" + namespace);
if (unixGroupName.getValue() != null) {
throw new WrongAttributeValueException(attribute, group, "Group is propagated to the facility where it have set unix group name so it must have unix GID too.");
}
}
//Group is not propagated to any facility in this GID namespace or it doesn't have set unix name there so it doesn't need to have unix GID.
return;
}
//Special behaviour if gid is null
Integer attrValue = null;
if (attribute.getValue() == null) {
throw new WrongAttributeValueException(attribute, group, "Unix GID must be set");
} else {
attrValue = (Integer) attribute.getValue();
}
//check if gid is not already depleted
Attribute usedGids = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, gidNamespace, A_E_usedGids);
//null in value means there is no depleted or used gids
if (usedGids.getValue() != null) {
Map<String, String> usedGidsValue = (Map<String, String>) usedGids.getValue();
//Dx, where x is GID means depleted value for GID x
if (usedGidsValue.containsKey("D" + attrValue.toString())) {
throw new WrongReferenceAttributeValueException(attribute, usedGids, group, null, gidNamespace, null, "This GID is already depleted.");
}
}
//Check if gid GID is within allowed range
sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute);
//Prepare lists for all groups and resources with same GID in the same namespace
List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<Group>();
List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<Resource>();
//Prepare attributes for searching through groups and resources
Attribute groupGIDAttribute = attribute;
Attribute resourceGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGID_namespace + ":" + gidNamespace));
resourceGIDAttribute.setValue(groupGIDAttribute.getValue());
//Fill lists of Groups and Resources by data
allGroupsWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute));
allResourcesWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute));
//remove this group
allGroupsWithSameGIDInSameNamespace.remove(group);
//Prepare list of GroupName attributes of this group
List<Attribute> groupNamesOfGroup = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGroupName_namespace + ":");
//Searching through groups
if (!allGroupsWithSameGIDInSameNamespace.isEmpty()) {
for (Group g : allGroupsWithSameGIDInSameNamespace) {
for (Attribute a : groupNamesOfGroup) {
int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a);
if (compare > 0) {
//This is problem, there is the same attribute but have other value
throw new WrongReferenceAttributeValueException(attribute, a, "There is a group with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + g + " " + group);
}
//Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
}
}
}
//Searching through resources
if (!allResourcesWithSameGIDInSameNamespace.isEmpty()) {
for (Resource r : allResourcesWithSameGIDInSameNamespace) {
for (Attribute a : groupNamesOfGroup) {
//Prepare resource version of this group attribute
Attribute resourceGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + a.getFriendlyNameParameter()));
resourceGroupName.setValue(a.getValue());
int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, resourceGroupName);
if (compare > 0) {
//This is problem, there is the same attribute but have other value
throw new WrongReferenceAttributeValueException(attribute, a, "There is a resource with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + r + " " + group);
}
//Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
}
}
}
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class urn_perun_group_attribute_def_def_unixGID_namespace method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
String gidNamespace = attribute.getFriendlyNameParameter();
//get attribute with usedGids for update
//IMPORTANT: for update lock row in table of attr values, be careful when using
Attribute usedGids;
try {
usedGids = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, gidNamespace, A_E_usedGids);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
//Get Map of gids (if there is no value, use empty map
Map<String, String> usedGidsValue = new LinkedHashMap<>();
if (usedGids.getValue() != null)
usedGidsValue = (Map<String, String>) usedGids.getValue();
//initial settings
String key = "G" + group.getId();
String oldGid = usedGidsValue.get(key);
//for removing gid
if (attribute.getValue() == null) {
//remove record from map
if (oldGid != null) {
usedGidsValue.remove(key);
//looking for another oldGid value, if not exists, add depleted record
if (!usedGidsValue.containsValue(oldGid)) {
usedGidsValue.put("D" + oldGid, oldGid);
}
}
//for setting gid
} else {
String newUnixGid = ((Integer) attribute.getValue()).toString();
//add new record to map
usedGidsValue.put(key, newUnixGid);
//looking for another oldGid value, if not exists, add depleted record
if (oldGid != null && !usedGidsValue.containsValue(oldGid)) {
usedGidsValue.put("D" + oldGid, oldGid);
}
}
//set new attribute value for usedGids
usedGids.setValue(usedGidsValue);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, gidNamespace, usedGids);
} catch (WrongAttributeValueException ex) {
throw new WrongReferenceAttributeValueException(attribute, usedGids, ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class urn_perun_group_attribute_def_def_unixGroupName_namespace method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
//prepare namespace and groupName value variables
String groupName = null;
if (attribute.getValue() != null)
groupName = (String) attribute.getValue();
String groupNameNamespace = attribute.getFriendlyNameParameter();
if (groupName == null) {
// if this is group attribute, its ok
return;
}
//Check attribute regex
sess.getPerunBl().getModulesUtilsBl().checkAttributeRegex(attribute, "^[-._a-zA-Z0-9]+$");
//Check reserved unix group names
sess.getPerunBl().getModulesUtilsBl().checkReservedUnixGroupNames(attribute);
try {
//prepare attributes group and resource unixGroupName
Attribute groupUnixGroupName = attribute;
Attribute resourceUnixGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + groupNameNamespace));
resourceUnixGroupName.setValue(attribute.getValue());
//prepare lists of groups and resources with the same groupName value in the same namespace
List<Group> groupsWithSameGroupNameInTheSameNamespace = new ArrayList<Group>();
List<Resource> resourcesWithSameGroupNameInTheSameNamespace = new ArrayList<Resource>();
//Fill lists of groups and resources
groupsWithSameGroupNameInTheSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupUnixGroupName));
resourcesWithSameGroupNameInTheSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceUnixGroupName));
//If there is no group or resource with same GroupNameInTheSameNamespace, its ok
if (groupsWithSameGroupNameInTheSameNamespace.isEmpty() && resourcesWithSameGroupNameInTheSameNamespace.isEmpty())
return;
//First need to know that i have right to write any of duplicit groupName-namespace attribute
boolean haveRights = sess.getPerunBl().getModulesUtilsBl().haveRightToWriteAttributeInAnyGroupOrResource(sess, groupsWithSameGroupNameInTheSameNamespace, resourcesWithSameGroupNameInTheSameNamespace, groupUnixGroupName, resourceUnixGroupName);
if (!haveRights)
throw new WrongReferenceAttributeValueException(attribute, "This groupName is already used for other group or resource and user has no rights to use it.");
//Now if rights are ok, prepare lists of UnixGIDs attributes of this group (also equivalent resource GID)
List<Attribute> groupUnixGIDs = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGID_namespace + ":");
List<Attribute> resourceVersionOfUnixGIDs = sess.getPerunBl().getModulesUtilsBl().getListOfResourceGIDsFromListOfGroupGIDs(sess, groupUnixGIDs);
//In list of duplicit groups looking for GID in same namespace but with different value, thats not correct
if (!groupsWithSameGroupNameInTheSameNamespace.isEmpty()) {
for (Group g : groupsWithSameGroupNameInTheSameNamespace) {
for (Attribute a : groupUnixGIDs) {
int compare;
compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a);
if (compare > 0) {
throw new WrongReferenceAttributeValueException(attribute, a, "One of the group GIDs is from the same namespace like other group GID but with different values.");
}
}
}
}
//In list of duplicit resources looking for GID in same namespace but with different value, thats not correct
if (!resourcesWithSameGroupNameInTheSameNamespace.isEmpty()) {
for (Resource r : resourcesWithSameGroupNameInTheSameNamespace) {
for (Attribute a : resourceVersionOfUnixGIDs) {
int compare;
compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, a);
if (compare > 0) {
throw new WrongReferenceAttributeValueException(attribute, a, "One of the group GIDs is from the same namespace like other resource GIDs but with different values.");
}
}
}
}
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
Aggregations