use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.
the class AttributesManagerImpl method setAttribute.
@Override
public boolean setAttribute(final PerunSession sess, final Object object, final Attribute attribute) throws InternalErrorException, WrongAttributeAssignmentException {
String tableName;
String columnName;
Object identificator;
String namespace;
// check whether the object is String or Perun Bean:
if (object instanceof String) {
tableName = "entityless_attr_values";
columnName = "subject";
identificator = (String) object;
namespace = AttributesManager.NS_ENTITYLESS_ATTR;
} else if (object instanceof PerunBean) {
PerunBean bean = (PerunBean) object;
// Add underscore between two letters where first is lowercase and second is uppercase, then lowercase BeanName
String name = bean.getBeanName().replaceAll("(\\p{Ll})(\\p{Lu})", "$1_$2").toLowerCase();
// same behaviour for rich objects as for the simple ones -> cut off "rich_" prefix
if (name.startsWith("rich")) {
name = name.replaceFirst("rich_", "");
}
// get namespace of the perun bean
namespace = NAMESPACES_BEANS_MAP.get(name);
if (namespace == null) {
// perun bean is not in the namespace map
throw new InternalErrorException(new IllegalArgumentException("Setting attribute for perun bean " + bean + " is not allowed."));
}
tableName = name + "_attr_values";
columnName = name + "_id";
identificator = bean.getId();
} else {
throw new InternalErrorException(new IllegalArgumentException("Object " + object + " must be either String or PerunBean."));
}
// check that given object is consistent with the attribute
checkNamespace(sess, attribute, namespace);
// create map of parameters for the where clause of the SQL query
Map<String, Object> params = new HashMap<>();
params.put("attr_id", attribute.getId());
params.put(columnName, identificator);
// save attribute
return setAttributeInDB(sess, attribute, tableName, params);
}
use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.
the class urn_perun_user_attribute_def_virt_userCertDNs method getUserFromMessage.
/**
* Get User from message if exists or if there is only one. In other case return null instead.
*
* @param message
* @return user or null
* @throws InternalErrorException
*/
private User getUserFromMessage(String message) throws InternalErrorException {
User user = null;
List<PerunBean> perunBeans = AuditParser.parseLog(message);
for (PerunBean pb : perunBeans) {
if (pb instanceof User) {
if (user != null) {
return null;
} else {
user = (User) pb;
}
}
}
return user;
}
use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.
the class FacilitiesManagerEntryIntegrationTest method addHostAndDestinationSameNameSameAdmin.
@Test
public void addHostAndDestinationSameNameSameAdmin() throws Exception {
System.out.println(CLASS_NAME + "addHostAndDestinationSameNameSameAdmin");
// Initialize host, destination and service
String hostName = "TestHost";
Host hostOne = new Host(0, hostName);
Destination destination = new Destination(0, hostName, Destination.DESTINATIONHOSTTYPE);
Service service = new Service(0, "testService");
ServicesManager servicesManagerEntry = perun.getServicesManager();
service = servicesManagerEntry.createService(sess, service);
// Creates second facility
Facility secondFacility = new Facility(0, "TestSecondFacility", "TestDescriptionText");
assertNotNull(perun.getFacilitiesManager().createFacility(sess, secondFacility));
// Set up two members
Member memberOne = setUpMember(vo);
// Set userOne as admin for both facilities
User userOne = perun.getUsersManagerBl().getUserByMember(sess, memberOne);
facilitiesManagerEntry.addAdmin(sess, facility, userOne);
facilitiesManagerEntry.addAdmin(sess, secondFacility, userOne);
// Sets userOne as actor in this test with role facility admin for facility
List<PerunBean> list = new ArrayList<PerunBean>();
list.add(facility);
list.add(secondFacility);
AuthzRoles authzRoles = new AuthzRoles(Role.FACILITYADMIN, list);
sess.getPerunPrincipal().setRoles(authzRoles);
sess.getPerunPrincipal().setUser(userOne);
// Adds host to facility
facilitiesManagerEntry.addHost(sess, hostOne, facility);
assertTrue(facilitiesManagerEntry.getHosts(sess, facility).size() == 1);
// Adds destination with same name as host to facility
servicesManagerEntry.addDestination(sess, service, facility, destination);
assertTrue(servicesManagerEntry.getDestinations(sess, service, facility).size() == 1);
// Adds same host to second facility
facilitiesManagerEntry.addHost(sess, hostOne, secondFacility);
assertTrue(facilitiesManagerEntry.getHosts(sess, secondFacility).size() == 1);
// Adds destination with same name as host to secondFacility
servicesManagerEntry.addDestination(sess, service, secondFacility, destination);
assertTrue(servicesManagerEntry.getDestinations(sess, service, secondFacility).size() == 1);
}
use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.
the class PerunNotifPoolMessageManagerImpl method createPerunNotifPoolMessagesForTemplates.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<PerunNotifPoolMessage> createPerunNotifPoolMessagesForTemplates(Map<Integer, List<PerunNotifTemplate>> templatesWithRegexIds, PerunNotifAuditMessage perunAuditMessage) throws InternalErrorException {
List<PerunNotifPoolMessage> result = new ArrayList<PerunNotifPoolMessage>();
// We parse recieved message from auditer to get all objects
//List<PerunBean> retrievedObjects = ParseUtils.parseMessage(perunMessage.getMessage());
List<PerunBean> retrievedObjects = AuditParser.parseLog(perunAuditMessage.getMessage());
// Objects which can be later used when proccessing managerCalls
Map<String, Object> usableObjects = parseRetrievedObjects(retrievedObjects);
usableObjects.put(parseClassName(PerunSession.class.toString()), session);
Map<String, String> retrievedProperties = new HashMap<String, String>();
for (Integer regexId : templatesWithRegexIds.keySet()) {
// We list through every regexId recognized in message
List<PerunNotifTemplate> templates = templatesWithRegexIds.get(regexId);
if ((templates != null) && (!templates.isEmpty())) {
for (PerunNotifTemplate template : templates) {
// We list through every template which uses regexId
Map<String, String> retrievedPrimaryProperties = new HashMap<String, String>();
Set<String> classNames = new HashSet<String>();
classNames.addAll(template.getPrimaryProperties().keySet());
for (String className : classNames) {
if (className != null && !className.equals(METHOD_CLASSNAME)) {
// Listing through all classNames
try {
logger.debug("Resolving class with name: " + className);
Class resolvedClass = Class.forName(className);
Object matchingObject = null;
for (Object myObject : retrievedObjects) {
if (resolvedClass.isAssignableFrom(myObject.getClass())) {
matchingObject = myObject;
logger.debug("Parsed object: " + matchingObject.toString() + " from message recognized for class: " + className);
}
}
if (matchingObject != null) {
if (template.getPrimaryProperties().get(className) != null) {
List<String> methods = template.getPrimaryProperties().get(className);
retrieveProperties(methods, className, retrievedPrimaryProperties, retrievedProperties, matchingObject);
}
} else {
logger.error("No object recognized in objects from message for class: " + className);
}
} catch (ClassNotFoundException ex) {
logger.error("Class from template cannot be resolved: " + className, ex);
}
}
}
if (template.getPrimaryProperties().get(METHOD_CLASSNAME) != null) {
for (String methodName : template.getPrimaryProperties().get(METHOD_CLASSNAME)) {
String value = retrieveMethodProperty(retrievedProperties, methodName, usableObjects);
retrievedPrimaryProperties.put(methodName, value);
}
}
if (retrievedPrimaryProperties != null && !retrievedPrimaryProperties.isEmpty()) {
PerunNotifPoolMessage poolMessage = new PerunNotifPoolMessage();
poolMessage.setCreated(new DateTime());
poolMessage.setKeyAttributes(retrievedPrimaryProperties);
poolMessage.setRegexId(regexId);
poolMessage.setTemplateId(template.getId());
poolMessage.setNotifMessage(perunAuditMessage.getMessage());
result.add(poolMessage);
}
}
} else {
logger.info("No template for regex id: " + regexId + " found.");
}
}
return result;
}
use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.
the class AttributesManagerEntryIntegrationTest method getAttributeDefinitionWithRights.
@Test
public void getAttributeDefinitionWithRights() throws Exception {
System.out.println(CLASS_NAME + "getAttributeDefinitionWithRights");
vo = setUpVo();
facility = setUpFacility();
resource = setUpResource();
group = setUpGroup();
member = setUpMember();
perun.getResourcesManagerBl().assignGroupToResource(sess, group, resource);
perun.getGroupsManagerBl().addMember(sess, group, member);
Attribute attr = setUpSpecificMemberResourceAttribute(member, resource);
List<PerunBean> perunBeans = new ArrayList<PerunBean>();
perunBeans.add(member);
perunBeans.add(resource);
List<AttributeDefinition> attrDefs = attributesManager.getAttributesDefinitionWithRights(sess, perunBeans);
List<AttributeDefinition> allAttrDef = attributesManager.getAttributesDefinition(sess);
assertFalse(attrDefs.isEmpty());
assertFalse(attrDefs.containsAll(allAttrDef));
assertTrue(allAttrDef.containsAll(attrDefs));
assertTrue(attrDefs.contains(attr));
for (AttributeDefinition ad : attrDefs) {
assertTrue(attributesManager.isFromNamespace(sess, ad, AttributesManager.NS_MEMBER_ATTR) || attributesManager.isFromNamespace(sess, ad, AttributesManager.NS_RESOURCE_ATTR) || attributesManager.isFromNamespace(sess, ad, AttributesManager.NS_MEMBER_RESOURCE_ATTR));
assertTrue(ad.getWritable());
}
}
Aggregations