Search in sources :

Example 11 with ConditionSortLimit

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

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)

Example 12 with ConditionSortLimit

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

the class EventDao method joinPermissions.

public <R extends Record> SelectJoinStep<R> joinPermissions(SelectJoinStep<R> select, ConditionSortLimit conditions, PermissionHolder user) {
    if (!permissionService.hasAdminRole(user)) {
        List<Integer> roleIds = permissionService.getAllInheritedRoles(user).stream().map(r -> r.getId()).collect(Collectors.toList());
        Condition roleIdsIn = MintermsRoles.MINTERMS_ROLES.roleId.in(roleIds);
        Table<?> mintermsGranted = this.create.select(MintermsRoles.MINTERMS_ROLES.mintermId).from(MintermsRoles.MINTERMS_ROLES).groupBy(MintermsRoles.MINTERMS_ROLES.mintermId).having(DSL.count().eq(DSL.count(DSL.case_().when(roleIdsIn, DSL.inline(1)).else_(DSL.inline((Integer) null))))).asTable("mintermsGranted");
        Table<?> permissionsGranted = this.create.selectDistinct(PermissionsMinterms.PERMISSIONS_MINTERMS.permissionId).from(PermissionsMinterms.PERMISSIONS_MINTERMS).join(mintermsGranted).on(mintermsGranted.field(MintermsRoles.MINTERMS_ROLES.mintermId).eq(PermissionsMinterms.PERMISSIONS_MINTERMS.mintermId)).asTable("permissionsGranted");
        select = select.join(permissionsGranted).on(permissionsGranted.field(PermissionsMinterms.PERMISSIONS_MINTERMS.permissionId).in(table.readPermissionId));
    }
    return select;
}
Also used : SelectJoinStep(org.jooq.SelectJoinStep) AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) UserCommentVO(com.serotonin.m2m2.vo.comment.UserCommentVO) Arrays(java.util.Arrays) DSL(org.jooq.impl.DSL) EventInstance(com.serotonin.m2m2.rt.event.EventInstance) Users(com.infiniteautomation.mango.db.tables.Users) EventTypeNames(com.serotonin.m2m2.rt.event.type.EventType.EventTypeNames) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) LoggerFactory(org.slf4j.LoggerFactory) Table(org.jooq.Table) Autowired(org.springframework.beans.factory.annotation.Autowired) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) AuditEventInstanceVO(com.serotonin.m2m2.vo.event.audit.AuditEventInstanceVO) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) Events(com.infiniteautomation.mango.db.tables.Events) Condition(org.jooq.Condition) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) JsonException(com.serotonin.json.JsonException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) Repository(org.springframework.stereotype.Repository) Record(org.jooq.Record) Logger(org.slf4j.Logger) Common(com.serotonin.m2m2.Common) ReturnCause(com.serotonin.m2m2.rt.event.ReturnCause) JsonSerializableUtility(com.serotonin.m2m2.util.JsonSerializableUtility) DatabaseProxy(com.serotonin.m2m2.db.DatabaseProxy) LazyInitSupplier(com.infiniteautomation.mango.util.LazyInitSupplier) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Field(org.jooq.Field) MintermsRoles(com.infiniteautomation.mango.db.tables.MintermsRoles) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) Collectors(java.util.stream.Collectors) ModuleRegistry(com.serotonin.m2m2.module.ModuleRegistry) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) List(java.util.List) UserComments(com.infiniteautomation.mango.db.tables.UserComments) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) TransactionStatus(org.springframework.transaction.TransactionStatus) EventsRecord(com.infiniteautomation.mango.db.tables.records.EventsRecord) TransactionCallback(org.springframework.transaction.support.TransactionCallback) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels) PermissionsMinterms(com.infiniteautomation.mango.db.tables.PermissionsMinterms) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Condition(org.jooq.Condition)

Example 13 with ConditionSortLimit

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

the class AbstractBasicVOService method customizedQuery.

/**
 * Query for VOs, filtering within the database is supported
 * by the conditions input, if DAO does not use database filtering you must manually filter on permissions
 *
 * @param condition SQL conditions to restrict results
 * @return list of results
 */
public List<T> customizedQuery(Condition condition) {
    List<T> list = new ArrayList<>();
    customizedQuery(new ConditionSortLimit(condition, null, null, null), list::add);
    return list;
}
Also used : ArrayList(java.util.ArrayList) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit)

Example 14 with ConditionSortLimit

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

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 15 with ConditionSortLimit

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

the class AbstractBasicVOServiceWithPermissionsTestBase method testCountQueryReadPermissionEnforcement.

@Test
public void testCountQueryReadPermissionEnforcement() {
    VO vo = newVO(editUser);
    setReadPermission(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);
    });
    VO vo2 = newVO(editUser);
    setReadPermission(MangoPermission.requireAnyRole(editRole), vo2);
    service.insert(vo2);
    runAs.runAs(editUser, () -> {
        ConditionSortLimit conditions = new ConditionSortLimit(null, null, null, 0);
        int count = getService().customizedCount(conditions);
        assertEquals(2, count);
    });
}
Also used : AbstractBasicVO(com.serotonin.m2m2.vo.AbstractBasicVO) ConditionSortLimit(com.infiniteautomation.mango.db.query.ConditionSortLimit) Test(org.junit.Test)

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