Search in sources :

Example 11 with PerunBean

use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method createMessageToSend.

/**
	 * Creates message which can be send using different ways defined in
	 * typeOfReceiver Every typeOfReceiver creates own Instance of
	 * PerunNotifMessageToSendDto, so the messages can be later send in
	 * batch
	 * <p/>
	 * Allowed syntax of variables in text -
	 * #{cz.metacentrum.perun.core.api.Destination.getId#} - #{for(regexId)
	 * "repeatable message
	 * ${cz.metacentrum.perun.core.api.Destination.getId$}" #}
	 *
	 * @param template
	 * @param dto
	 * @return
	 */
private List<PerunNotifMessageDto> createMessageToSend(PerunNotifTemplate template, PoolMessage dto) throws IOException, TemplateException, InternalErrorException {
    Map<String, Object> container = new HashMap<String, Object>();
    Map<String, Map<String, PerunBean>> resultMapOfBeans = new HashMap<String, Map<String, PerunBean>>();
    for (PerunNotifPoolMessage message : dto.getList()) {
        List<PerunBean> retrievedObjects = AuditParser.parseLog(message.getNotifMessage());
        Map<String, PerunBean> normalizedBeans = new HashMap<String, PerunBean>();
        for (PerunBean retrievedObject : retrievedObjects) {
            normalizedBeans.put(normalizeName(retrievedObject.getClass().toString()), retrievedObject);
        }
        resultMapOfBeans.put(message.getRegexId().toString(), normalizedBeans);
    }
    container.put("retrievedObjects", resultMapOfBeans);
    container.put("perunSession", session);
    container.put("perun", perun);
    // We get all used pool ids so we can erase them later
    Set<Integer> usedPoolIds = new HashSet<Integer>();
    for (PerunNotifPoolMessage message : dto.getList()) {
        usedPoolIds.add(message.getId());
    }
    // resolve sender
    String sender = resolveSender(template.getSender(), container);
    // Create of message based on receiver
    List<PerunNotifMessageDto> result = new ArrayList<PerunNotifMessageDto>();
    for (PerunNotifReceiver receiver : template.getReceivers()) {
        PerunNotifMessageDto messageDto = new PerunNotifMessageDto();
        Locale locale = interpretLocale(receiver.getLocale(), receiver.getTarget(), dto.getKeyAttributes());
        String messageContent = compileTemplate(Integer.toString(template.getId()), locale, container);
        String subjectContent = compileTemplate(Integer.toString(template.getId()) + "-subject", locale, container);
        messageDto.setMessageToSend(messageContent);
        messageDto.setPoolMessage(dto);
        messageDto.setUsedPoolIds(usedPoolIds);
        messageDto.setReceiver(receiver);
        messageDto.setTemplate(template);
        messageDto.setSubject(subjectContent);
        messageDto.setSender(sender);
        result.add(messageDto);
    }
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) PerunBean(cz.metacentrum.perun.core.api.PerunBean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with PerunBean

use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method testPerunNotifMessageText.

@Override
public String testPerunNotifMessageText(String template, Map<Integer, List<PerunBean>> regexIdsBeans) throws IOException, TemplateException {
    //Initialization of freemarker
    StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
    Configuration myConfiguration = createFreemarkerConfiguration(stringTemplateLoader);
    stringTemplateLoader.putTemplate("test", template);
    Template freeMarkerTemplate = myConfiguration.getTemplate(template, new Locale("en"));
    StringWriter stringWriter = new StringWriter(4096);
    Map<String, Object> container = new HashMap<String, Object>();
    Map<String, Map<String, PerunBean>> resultMapOfBeans = new HashMap<String, Map<String, PerunBean>>();
    for (Integer regexId : regexIdsBeans.keySet()) {
        Map<String, PerunBean> normalizedBeans = new HashMap<String, PerunBean>();
        List<PerunBean> perunBeans = regexIdsBeans.get(regexId);
        for (PerunBean retrievedObject : perunBeans) {
            normalizedBeans.put(normalizeName(retrievedObject.getClass().toString()), retrievedObject);
        }
        resultMapOfBeans.put(regexId.toString(), normalizedBeans);
    }
    container.put("retrievedObjects", resultMapOfBeans);
    container.put("perunSession", session);
    container.put("perun", perun);
    freeMarkerTemplate.process(container, stringWriter);
    return stringWriter.toString();
}
Also used : Configuration(freemarker.template.Configuration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Template(freemarker.template.Template) StringTemplateLoader(cz.metacentrum.perun.notif.StringTemplateLoader) PerunBean(cz.metacentrum.perun.core.api.PerunBean) StringWriter(java.io.StringWriter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 13 with PerunBean

use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.

the class PerunNotifRegexManagerImpl method getIdsOfRegexesMatchingMessage.

@Override
public Set<Integer> getIdsOfRegexesMatchingMessage(PerunNotifAuditMessage auditMessage) throws InternalErrorException {
    Set<PerunNotifObject> setOfObjects = new HashSet<PerunNotifObject>();
    for (PerunBean bean : auditMessage.getPerunBeanList()) {
        for (PerunNotifObject object : allObjects) {
            if (object.getObjectClass() != null) {
                if (bean.getClass().isAssignableFrom(object.getObjectClass())) {
                    setOfObjects.add(object);
                }
            }
        }
    }
    Set<Integer> result = new HashSet<Integer>();
    for (PerunNotifRegex regex : allRegex) {
        if (auditMessage.getMessage().matches(regex.getRegex())) {
            //We test whether message has all objects
            boolean matches = true;
            for (PerunNotifObject object : regex.getObjects()) {
                if (!setOfObjects.contains(object)) {
                    matches = false;
                }
            }
            if (matches) {
                result.add(regex.getId());
            }
        }
    }
    return result;
}
Also used : PerunBean(cz.metacentrum.perun.core.api.PerunBean) PerunNotifRegex(cz.metacentrum.perun.notif.entities.PerunNotifRegex) HashSet(java.util.HashSet) PerunNotifObject(cz.metacentrum.perun.notif.entities.PerunNotifObject)

Example 14 with PerunBean

use of cz.metacentrum.perun.core.api.PerunBean in project perun by CESNET.

the class AttributesManagerBlImpl method getAttributesDefinitionWithRights.

public List<AttributeDefinition> getAttributesDefinitionWithRights(PerunSession sess, List<PerunBean> entities) throws InternalErrorException, AttributeNotExistsException {
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    //if there is no entities, so no attribute definition will be returned => empty array list of ADs
    if (entities == null || entities.isEmpty())
        return attributeDefinitions;
    else
        //or fill list by all attributeDefinitions
        attributeDefinitions = this.getAttributesDefinition(sess);
    //Prepare possible objects
    User user = null;
    Member member = null;
    Vo vo = null;
    Resource resource = null;
    Group group = null;
    Facility facility = null;
    Host host = null;
    UserExtSource ues = null;
    //Iterate through all entities and fill those which are in list of entities
    for (PerunBean entity : entities) {
        if (entity instanceof User)
            user = (User) entity;
        else if (entity instanceof Member)
            member = (Member) entity;
        else if (entity instanceof Vo)
            vo = (Vo) entity;
        else if (entity instanceof Resource)
            resource = (Resource) entity;
        else if (entity instanceof Group)
            group = (Group) entity;
        else if (entity instanceof Facility)
            facility = (Facility) entity;
        else if (entity instanceof Host)
            host = (Host) entity;
        else if (entity instanceof UserExtSource)
            ues = (UserExtSource) entity;
        else
            //Else skip not identified entity (log it)
            log.debug("In method GetAttributesDefinitionWithRights there are entity which is not identified correctly and will be skipped: " + entity);
    }
    //Iterate through all attributesDefinitions and remove those which are not in the possible namespace or user in session has no rights to read them
    Iterator<AttributeDefinition> iterator = attributeDefinitions.iterator();
    while (iterator.hasNext()) {
        AttributeDefinition attrDef = iterator.next();
        if (this.isFromNamespace(sess, attrDef, NS_USER_FACILITY_ATTR) && user != null && facility != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, user, facility)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, user, facility));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_MEMBER_RESOURCE_ATTR) && member != null && resource != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, member, resource)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, member, resource));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_MEMBER_GROUP_ATTR) && member != null && group != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, member, group)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, member, group));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_GROUP_RESOURCE_ATTR) && group != null && resource != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, group, resource)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, group, resource));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_USER_ATTR) && user != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, user, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, user, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_MEMBER_ATTR) && member != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, member, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, member, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_VO_ATTR) && vo != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, vo, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, vo, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_RESOURCE_ATTR) && resource != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, resource, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, resource, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_GROUP_ATTR) && group != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, group, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, group, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, NS_FACILITY_ATTR) && facility != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, facility, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, facility, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, AttributesManager.NS_HOST_ATTR) && host != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, host, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, host, null));
            }
        } else if (this.isFromNamespace(sess, attrDef, AttributesManager.NS_UES_ATTR) && ues != null) {
            if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attrDef, ues, null)) {
                iterator.remove();
            } else {
                attrDef.setWritable(AuthzResolver.isAuthorizedForAttribute(sess, ActionType.WRITE, attrDef, ues, null));
            }
        } else {
            //if there is another namespace or if there are no entities (which are needed for the namespace) remove this attributeDefinition
            iterator.remove();
        }
    }
    return attributeDefinitions;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) User(cz.metacentrum.perun.core.api.User) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) Host(cz.metacentrum.perun.core.api.Host) PerunBean(cz.metacentrum.perun.core.api.PerunBean) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility) Member(cz.metacentrum.perun.core.api.Member)

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