Search in sources :

Example 1 with InstanceSecurityRestriction

use of org.motechproject.mds.util.InstanceSecurityRestriction in project motech by motech.

the class DefaultMotechDataService method executeQuery.

@Override
@Transactional
public <R> R executeQuery(QueryExecution<R> queryExecution) {
    InstanceSecurityRestriction securityRestriction = validateCredentials();
    Query query = repository.getPersistenceManager().newQuery(repository.getClassType());
    return queryExecution.execute(query, securityRestriction);
}
Also used : Query(javax.jdo.Query) InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with InstanceSecurityRestriction

use of org.motechproject.mds.util.InstanceSecurityRestriction in project motech by motech.

the class MdsBundleIT method verifyCustomQuery.

private void verifyCustomQuery() throws Exception {
    List result = (List) service.executeQuery(new QueryExecution<List>() {

        @Override
        public List execute(Query query, InstanceSecurityRestriction restriction) {
            query.setFilter("someString == param0");
            query.declareParameters("java.lang.String param0");
            return (List) QueryExecutor.execute(query, "anotherString", restriction);
        }
    });
    assertEquals(1, result.size());
    Class objClass = result.get(0).getClass();
    assertInstanceOneDotOne(result.get(0), objClass);
    List<String> names = (List<String>) service.executeSQLQuery(new SqlQueryExecution<List<String>>() {

        @Override
        public List<String> execute(Query query) {
            Map<String, Integer> params = new HashMap<>();
            params.put("param", 4);
            return (List<String>) query.executeWithMap(params);
        }

        @Override
        public String getSqlQuery() {
            String driverName = sqlDBManager.getChosenSQLDriver();
            if (driverName.equals(Constants.Config.MYSQL_DRIVER_CLASSNAME)) {
                return "SELECT someString FROM MDS_FOO WHERE someInt = :param";
            } else {
                return "SELECT \"someString\" FROM \"MDS_FOO\" WHERE \"someInt\" = :param";
            }
        }
    });
    assertEquals(asList("notInSet"), names);
}
Also used : Query(javax.jdo.Query) HashMap(java.util.HashMap) InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TestClass(org.motechproject.mds.osgi.TestClass) SqlQueryExecution(org.motechproject.mds.query.SqlQueryExecution) SqlQueryExecution(org.motechproject.mds.query.SqlQueryExecution) QueryExecution(org.motechproject.mds.query.QueryExecution)

Example 3 with InstanceSecurityRestriction

use of org.motechproject.mds.util.InstanceSecurityRestriction in project motech by motech.

the class RestrictionPropertyTest method getProperty.

@Override
protected Property getProperty() {
    InstanceSecurityRestriction restriction = new InstanceSecurityRestriction();
    restriction.setByCreator(true);
    return new RestrictionProperty(restriction, "motech");
}
Also used : InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction)

Example 4 with InstanceSecurityRestriction

use of org.motechproject.mds.util.InstanceSecurityRestriction in project motech by motech.

the class TaskServiceImpl method findActiveTasksForTriggerSubject.

@Override
@Transactional
public List<Task> findActiveTasksForTriggerSubject(final String subject) {
    List<Task> list = null;
    if (isNotBlank(subject)) {
        List enabledTasks = tasksDataService.executeQuery(new QueryExecution<List<Task>>() {

            @Override
            public List<Task> execute(Query query, InstanceSecurityRestriction restriction) {
                String byTriggerSubject = "trigger.subject == param";
                String isTaskActive = "enabled == true";
                String isUsingTimeWindow = "useTimeWindow == true";
                String filter = String.format("((%s) || (%s)) && (%s)", isTaskActive, isUsingTimeWindow, byTriggerSubject);
                query.setFilter(filter);
                query.declareParameters("java.lang.String param");
                query.setOrdering("id asc");
                return (List) QueryExecutor.execute(query, subject, restriction);
            }
        });
        if (enabledTasks != null) {
            checkChannelAvailableInTasks(enabledTasks);
            list = checkTimeWindowInTasks(enabledTasks);
            CollectionUtils.filter(list, tasksWithRegisteredChannel());
        }
    }
    return list == null ? new ArrayList<>() : list;
}
Also used : Task(org.motechproject.tasks.domain.mds.task.Task) Query(javax.jdo.Query) InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction) List(java.util.List) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with InstanceSecurityRestriction

use of org.motechproject.mds.util.InstanceSecurityRestriction in project motech by motech.

the class DefaultMotechDataService method validateCredentials.

protected InstanceSecurityRestriction validateCredentials(T instance) {
    InstanceSecurityRestriction restriction = new InstanceSecurityRestriction();
    restriction.setByOwner(securityMode == SecurityMode.OWNER);
    restriction.setByCreator(securityMode == SecurityMode.CREATOR);
    if (!restriction.isEmpty() && instance != null) {
        restriction = checkInstanceAccess(instance, restriction);
    }
    return restriction;
}
Also used : InstanceSecurityRestriction(org.motechproject.mds.util.InstanceSecurityRestriction)

Aggregations

InstanceSecurityRestriction (org.motechproject.mds.util.InstanceSecurityRestriction)6 Query (javax.jdo.Query)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 TestClass (org.motechproject.mds.osgi.TestClass)1 QueryExecution (org.motechproject.mds.query.QueryExecution)1 SqlQueryExecution (org.motechproject.mds.query.SqlQueryExecution)1 Task (org.motechproject.tasks.domain.mds.task.Task)1