Search in sources :

Example 1 with PerunBean

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);
}
Also used : PerunBean(cz.metacentrum.perun.core.api.PerunBean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with PerunBean

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;
}
Also used : PerunBean(cz.metacentrum.perun.core.api.PerunBean) User(cz.metacentrum.perun.core.api.User)

Example 3 with PerunBean

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);
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) PerunBean(cz.metacentrum.perun.core.api.PerunBean) User(cz.metacentrum.perun.core.api.User) RichUser(cz.metacentrum.perun.core.api.RichUser) ServicesManager(cz.metacentrum.perun.core.api.ServicesManager) ArrayList(java.util.ArrayList) AuthzRoles(cz.metacentrum.perun.core.impl.AuthzRoles) Service(cz.metacentrum.perun.core.api.Service) Host(cz.metacentrum.perun.core.api.Host) BanOnFacility(cz.metacentrum.perun.core.api.BanOnFacility) Facility(cz.metacentrum.perun.core.api.Facility) Member(cz.metacentrum.perun.core.api.Member) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 4 with PerunBean

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;
}
Also used : PerunNotifTemplate(cz.metacentrum.perun.notif.entities.PerunNotifTemplate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DateTime(org.joda.time.DateTime) PerunBean(cz.metacentrum.perun.core.api.PerunBean) PerunNotifPoolMessage(cz.metacentrum.perun.notif.entities.PerunNotifPoolMessage)

Example 5 with PerunBean

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());
    }
}
Also used : PerunBean(cz.metacentrum.perun.core.api.PerunBean) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

PerunBean (cz.metacentrum.perun.core.api.PerunBean)14 ArrayList (java.util.ArrayList)7 Facility (cz.metacentrum.perun.core.api.Facility)5 Host (cz.metacentrum.perun.core.api.Host)4 User (cz.metacentrum.perun.core.api.User)4 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)3 Destination (cz.metacentrum.perun.core.api.Destination)3 Member (cz.metacentrum.perun.core.api.Member)3 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)3 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)3 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)2 Attribute (cz.metacentrum.perun.core.api.Attribute)2 BanOnFacility (cz.metacentrum.perun.core.api.BanOnFacility)2 Group (cz.metacentrum.perun.core.api.Group)2 Resource (cz.metacentrum.perun.core.api.Resource)2 Service (cz.metacentrum.perun.core.api.Service)2 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)2 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)2