Search in sources :

Example 31 with ConditionSortLimit

use of com.infiniteautomation.mango.db.query.ConditionSortLimit in project ma-core-public by MangoAutomation.

the class EventInstanceServiceTest method testQueryReadPermissionEnforcement.

@Override
@Test
public void testQueryReadPermissionEnforcement() {
    EventInstanceVO vo = newVO(editUser);
    setReadPermission(MangoPermission.requireAnyRole(editRole), vo);
    EventInstanceVO saved = service.insert(vo);
    runAs.runAs(readUser, () -> {
        ConditionSortLimit conditions = new ConditionSortLimit(null, null, 1, 0);
        AtomicInteger count = new AtomicInteger();
        getService().customizedQuery(conditions, (item) -> {
            count.getAndIncrement();
        });
        assertEquals(0, count.get());
    });
    runAs.runAs(editUser, () -> {
        Condition c = getDao().getIdField().eq(saved.getId());
        ConditionSortLimit conditions = new ConditionSortLimit(c, null, null, null);
        AtomicInteger count = new AtomicInteger();
        getService().customizedQuery(conditions, (item) -> {
            count.getAndIncrement();
            assertEquals(saved.getId(), item.getId());
        });
        assertEquals(1, count.get());
    });
}
Also used : Condition(org.jooq.Condition) EventInstanceVO(com.serotonin.m2m2.vo.event.EventInstanceVO) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) Test(org.junit.Test)

Example 32 with ConditionSortLimit

use of com.infiniteautomation.mango.db.query.ConditionSortLimit in project ma-core-public by MangoAutomation.

the class AbstractVOServiceWithPermissionsTest method countForName.

public int countForName(String name) {
    Condition c = getDao().getNameField().eq(name);
    ConditionSortLimit conditions = new ConditionSortLimit(c, null, null, 0);
    return getService().customizedCount(conditions);
}
Also used : Condition(org.jooq.Condition) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit)

Example 33 with ConditionSortLimit

use of com.infiniteautomation.mango.db.query.ConditionSortLimit in project ma-core-public by MangoAutomation.

the class AbstractBasicVOServiceWithPermissionsTestBase method testCountQueryEditPermissionEnforcement.

@Test
public void testCountQueryEditPermissionEnforcement() {
    VO vo = newVO(readUser);
    setEditPermission(MangoPermission.requireAnyRole(editRole), vo);
    service.insert(vo);
    runAs.runAs(readUser, () -> {
        ConditionSortLimit conditions = new ConditionSortLimit(null, null, null, 0);
        int count = getService().customizedCount(conditions);
        assertEquals(0, count);
    });
}
Also used : AbstractBasicVO(com.serotonin.m2m2.vo.AbstractBasicVO) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) Test(org.junit.Test)

Example 34 with ConditionSortLimit

use of com.infiniteautomation.mango.db.query.ConditionSortLimit in project ma-core-public by MangoAutomation.

the class FileStoreService method getStores.

/**
 * List all file-stores that the user has read permission for
 */
public List<FileStore> getStores() {
    List<FileStore> stores = new ArrayList<>();
    this.customizedQuery(new ConditionSortLimit(null, null, null, null), (item) -> stores.add(item));
    PermissionHolder user = Common.getUser();
    Collection<FileStoreDefinition> moduleDefs = ModuleRegistry.getFileStoreDefinitions().values();
    for (FileStoreDefinition def : moduleDefs) {
        if (this.permissionService.hasPermission(user, def.getReadPermission())) {
            stores.add(def.toFileStore());
        }
    }
    return stores;
}
Also used : FileStore(com.serotonin.m2m2.vo.FileStore) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) FileStoreDefinition(com.serotonin.m2m2.module.FileStoreDefinition)

Example 35 with ConditionSortLimit

use of com.infiniteautomation.mango.db.query.ConditionSortLimit in project ma-core-public by MangoAutomation.

the class DataPointTagsDao method getTagValuesForKey.

public Set<String> getTagValuesForKey(String tagKey, PermissionHolder user) {
    SelectJoinStep<Record1<String>> query = this.create.selectDistinct(table.tagValue).from(table);
    SelectConditionStep<Record1<String>> conditional;
    if (!permissionService.hasAdminRole(user)) {
        query = query.join(dataPointTable).on(table.dataPointId.eq(dataPointTable.id));
        ConditionSortLimit csl = new ConditionSortLimit(table.tagKey.eq(tagKey), null, null, null);
        query = DataPointDao.getInstance().joinPermissions(query, user);
        conditional = query.where(csl.getCondition());
    } else {
        conditional = query.where(table.tagKey.eq(tagKey));
    }
    try (Stream<Record1<String>> stream = conditional.stream()) {
        return stream.map(Record1::value1).collect(Collectors.toSet());
    }
}
Also used : ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) Record1(org.jooq.Record1)

Aggregations

ConditionSortLimit (com.infiniteautomation.mango.db.query.ConditionSortLimit)35 Condition (org.jooq.Condition)14 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)10 AbstractBasicVO (com.serotonin.m2m2.vo.AbstractBasicVO)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)7 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)4 Common (com.serotonin.m2m2.Common)4 ModuleRegistry (com.serotonin.m2m2.module.ModuleRegistry)4 ApiOperation (io.swagger.annotations.ApiOperation)3 List (java.util.List)3 Record (org.jooq.Record)3 Record1 (org.jooq.Record1)3 SelectJoinStep (org.jooq.SelectJoinStep)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3