committed by
GitHub
179 changed files with 620 additions and 3559 deletions
@ -1,58 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.data; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.edqs.fields.EntityGroupFields; |
|
||||
|
|
||||
import java.util.Collection; |
|
||||
import java.util.UUID; |
|
||||
import java.util.concurrent.ConcurrentHashMap; |
|
||||
import java.util.concurrent.ConcurrentMap; |
|
||||
|
|
||||
public class EntityGroupData extends BaseEntityData<EntityGroupFields> { |
|
||||
|
|
||||
private final ConcurrentMap<UUID, EntityData<?>> entitiesById = new ConcurrentHashMap<>(); |
|
||||
|
|
||||
public EntityGroupData(UUID entityId) { |
|
||||
super(entityId); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public EntityType getEntityType() { |
|
||||
return EntityType.ENTITY_GROUP; |
|
||||
} |
|
||||
|
|
||||
public Collection<EntityData<?>> getEntities() { |
|
||||
return entitiesById.values(); |
|
||||
} |
|
||||
|
|
||||
public boolean addOrUpdate(EntityData<?> ed) { |
|
||||
return entitiesById.put(ed.getId(), ed) == null; |
|
||||
} |
|
||||
|
|
||||
public boolean remove(EntityData<?> ed) { |
|
||||
return entitiesById.remove(ed.getId()) != null; |
|
||||
} |
|
||||
|
|
||||
public EntityData<?> getEntity(UUID entityId) { |
|
||||
return entitiesById.get(entityId); |
|
||||
} |
|
||||
|
|
||||
public boolean remove(UUID toId) { |
|
||||
return entitiesById.remove(toId) != null; |
|
||||
} |
|
||||
} |
|
||||
@ -1,75 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.EntityFilter; |
|
||||
import org.thingsboard.server.edqs.data.EntityGroupData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.query.SortableEntityData; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.HashMap; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
import java.util.UUID; |
|
||||
|
|
||||
public abstract class AbstractEntityGroupQueryProcessor<T extends EntityFilter> extends AbstractSingleEntityTypeQueryProcessor<T> { |
|
||||
|
|
||||
public AbstractEntityGroupQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query, T filter) { |
|
||||
super(repo, ctx, query, filter); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected List<SortableEntityData> processCustomerGenericReadWithGroups(UUID customerId, boolean readAttrPermissions, boolean readTsPermissions, List<GroupPermissions> groupPermissions) { |
|
||||
var genericReadResults = processCustomerGenericRead(customerId, readAttrPermissions, readTsPermissions); |
|
||||
Map<UUID, SortableEntityData> mergedResult = new HashMap<>(genericReadResults.size()); |
|
||||
for (SortableEntityData sd : genericReadResults) { |
|
||||
mergedResult.put(sd.getId(), sd); |
|
||||
} |
|
||||
for (GroupPermissions permissions : groupPermissions) { |
|
||||
SortableEntityData alreadyAdded = mergedResult.get(permissions.groupId); |
|
||||
if (alreadyAdded != null) { |
|
||||
alreadyAdded.setReadAttrs(alreadyAdded.isReadAttrs() || permissions.readAttrs); |
|
||||
alreadyAdded.setReadTs(alreadyAdded.isReadTs() || permissions.readTs); |
|
||||
} else { |
|
||||
EntityGroupData egData = repository.getEntityGroup(permissions.groupId); |
|
||||
if (matches(egData)) { |
|
||||
SortableEntityData sortData = toSortData(egData, permissions); |
|
||||
mergedResult.put(egData.getId(), sortData); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return new ArrayList<>(mergedResult.values()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected CombinedPermissions getCombinedPermissionsInternal(UUID id, boolean read, boolean readAttrs, boolean readTs, List<GroupPermissions> groupPermissions) { |
|
||||
for (GroupPermissions eg : groupPermissions) { |
|
||||
if (read && readAttrs && readTs) { |
|
||||
break; |
|
||||
} |
|
||||
if (eg.groupId.equals(id)) { |
|
||||
read = true; |
|
||||
readAttrs = readAttrs || eg.readAttrs; |
|
||||
readTs = readTs || eg.readTs; |
|
||||
} |
|
||||
} |
|
||||
return new CombinedPermissions(read, readAttrs, readTs); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,25 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import lombok.Data; |
|
||||
|
|
||||
@Data |
|
||||
public class CombinedPermissions implements Permissions { |
|
||||
private final boolean read; |
|
||||
private final boolean readAttrs; |
|
||||
private final boolean readTs; |
|
||||
} |
|
||||
@ -1,121 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.edqs.fields.EntityGroupFields; |
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.EntitiesByGroupNameFilter; |
|
||||
import org.thingsboard.server.edqs.data.CustomerData; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.query.SortableEntityData; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
import org.thingsboard.server.edqs.util.RepositoryUtils; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.Collection; |
|
||||
import java.util.HashSet; |
|
||||
import java.util.List; |
|
||||
import java.util.UUID; |
|
||||
import java.util.function.Consumer; |
|
||||
import java.util.regex.Pattern; |
|
||||
|
|
||||
import static org.thingsboard.server.common.data.EntityType.ENTITY_GROUP; |
|
||||
import static org.thingsboard.server.edqs.util.RepositoryUtils.getSortValue; |
|
||||
|
|
||||
public class EntitiesByGroupNameQueryProcessor extends AbstractSingleEntityTypeQueryProcessor<EntitiesByGroupNameFilter> { |
|
||||
|
|
||||
private final String groupType; |
|
||||
private final UUID ownerId; |
|
||||
private final Pattern pattern; |
|
||||
|
|
||||
public EntitiesByGroupNameQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (EntitiesByGroupNameFilter) query.getEntityFilter()); |
|
||||
this.groupType = filter.getGroupType().name(); |
|
||||
this.ownerId = filter.getOwnerId() != null ? filter.getOwnerId().getId() : null; |
|
||||
this.pattern = RepositoryUtils.toSqlLikePattern(filter.getEntityGroupNameFilter()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processCustomerGenericRead(UUID customerId, Consumer<EntityData<?>> processor) { |
|
||||
var customers = repository.getEntityMap(EntityType.CUSTOMER); |
|
||||
for (UUID cId : repository.getAllCustomers(customerId)) { |
|
||||
var customerData = (CustomerData) customers.get(cId); |
|
||||
if (customerData != null) { |
|
||||
process(customerData.getEntities(ENTITY_GROUP), processor); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected List<SortableEntityData> processCustomerGenericReadWithGroups(UUID customerId, boolean readAttrPermissions, boolean readTsPermissions, List<GroupPermissions> groupPermissions) { |
|
||||
List<SortableEntityData> result = new ArrayList<>(getProbableResultSize()); |
|
||||
var customers = repository.getAllCustomers(customerId); |
|
||||
processAll(ed -> { |
|
||||
CombinedPermissions permissions = getCombinedPermissions(ed.getId(), checkCustomerHierarchy(customers, ed), readAttrPermissions, readTsPermissions, groupPermissions); |
|
||||
if (permissions.isRead()) { |
|
||||
SortableEntityData sortData = new SortableEntityData(ed); |
|
||||
sortData.setSortValue(getSortValue(ed, sortKey)); |
|
||||
sortData.setReadAttrs(permissions.isReadAttrs()); |
|
||||
sortData.setReadTs(permissions.isReadTs()); |
|
||||
result.add(sortData); |
|
||||
} |
|
||||
}); |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processGroupsOnly(List<GroupPermissions> groupPermissions, Consumer<EntityData<?>> processor) { |
|
||||
Collection<EntityData<?>> entities = new HashSet<>(getProbableResultSize()); |
|
||||
for (GroupPermissions groupPermission : groupPermissions) { |
|
||||
entities.add(repository.getEntityGroup(groupPermission.groupId)); |
|
||||
} |
|
||||
process(entities, processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processAll(Consumer<EntityData<?>> processor) { |
|
||||
process(repository.getEntitySet(ENTITY_GROUP), processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void process(Collection<EntityData<?>> entities, Consumer<EntityData<?>> processor) { |
|
||||
for (EntityData<?> ed : entities) { |
|
||||
if (matches(ed)) { |
|
||||
Collection<EntityData<?>> groupEntities = repository.getEntityGroup(ed.getId()).getEntities(); |
|
||||
for (EntityData<?> groupEntity : groupEntities) { |
|
||||
processor.accept(groupEntity); |
|
||||
} |
|
||||
return; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected boolean matches(EntityData ed) { |
|
||||
EntityGroupFields fields = (EntityGroupFields)ed.getFields(); |
|
||||
return super.matches(ed) && groupType.equals(fields.getType()) |
|
||||
&& (pattern == null || pattern.matcher(fields.getName()).matches()) |
|
||||
&& (ownerId == null || ownerId.equals(fields.getOwnerId())); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int getProbableResultSize() { |
|
||||
return 1024; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,96 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.EntityGroupFilter; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.data.EntityGroupData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.query.SortableEntityData; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
import java.util.UUID; |
|
||||
import java.util.function.Consumer; |
|
||||
|
|
||||
import static org.thingsboard.server.edqs.util.RepositoryUtils.getSortValue; |
|
||||
|
|
||||
public class EntitiesByGroupQueryProcessor extends AbstractSingleEntityTypeQueryProcessor<EntityGroupFilter> { |
|
||||
|
|
||||
private final String groupType; |
|
||||
private final UUID groupId; |
|
||||
|
|
||||
public EntitiesByGroupQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (EntityGroupFilter) query.getEntityFilter()); |
|
||||
groupId = UUID.fromString(filter.getEntityGroup()); |
|
||||
groupType = filter.getGroupType().name(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processCustomerGenericRead(UUID customerId, Consumer<EntityData<?>> processor) { |
|
||||
var customers = repository.getAllCustomers(customerId); |
|
||||
processAll(ed -> { |
|
||||
if (checkCustomerHierarchy(customers, ed)) { |
|
||||
processor.accept(ed); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected List<SortableEntityData> processCustomerGenericReadWithGroups(UUID customerId, boolean readAttrPermissions, boolean readTsPermissions, List<GroupPermissions> groupPermissions) { |
|
||||
List<SortableEntityData> result = new ArrayList<>(getProbableResultSize()); |
|
||||
var customers = repository.getAllCustomers(customerId); |
|
||||
processAll(ed -> { |
|
||||
CombinedPermissions permissions = getCombinedPermissions(ed.getId(), checkCustomerHierarchy(customers, ed), readAttrPermissions, readTsPermissions, groupPermissions); |
|
||||
if (permissions.isRead()) { |
|
||||
SortableEntityData sortData = new SortableEntityData(ed); |
|
||||
sortData.setSortValue(getSortValue(ed, sortKey)); |
|
||||
sortData.setReadAttrs(permissions.isReadAttrs()); |
|
||||
sortData.setReadTs(permissions.isReadTs()); |
|
||||
result.add(sortData); |
|
||||
} |
|
||||
}); |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processGroupsOnly(List<GroupPermissions> groupPermissions, Consumer<EntityData<?>> processor) { |
|
||||
processAll(processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processAll(Consumer<EntityData<?>> processor) { |
|
||||
EntityGroupData entityGroup = repository.getEntityGroup(groupId); |
|
||||
if (matches(entityGroup)) { |
|
||||
for (EntityData<?> ed : entityGroup.getEntities()) { |
|
||||
processor.accept(ed); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected boolean matches(EntityData ed) { |
|
||||
return super.matches(ed) && groupType.equals(ed.getFields().getType()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int getProbableResultSize() { |
|
||||
return 1024; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,84 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.EntityGroupListFilter; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.data.EntityGroupData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
|
|
||||
import java.util.List; |
|
||||
import java.util.Set; |
|
||||
import java.util.UUID; |
|
||||
import java.util.function.Consumer; |
|
||||
import java.util.stream.Collectors; |
|
||||
|
|
||||
public class EntityGroupListQueryProcessor extends AbstractEntityGroupQueryProcessor<EntityGroupListFilter> { |
|
||||
|
|
||||
private final String groupType; |
|
||||
private final Set<UUID> groupIds; |
|
||||
|
|
||||
public EntityGroupListQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (EntityGroupListFilter) query.getEntityFilter()); |
|
||||
this.groupType = filter.getGroupType().name(); |
|
||||
this.groupIds = filter.getEntityGroupList().stream().map(UUID::fromString).collect(Collectors.toSet()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processCustomerGenericRead(UUID customerId, Consumer<EntityData<?>> processor) { |
|
||||
var customers = repository.getAllCustomers(customerId); |
|
||||
processAll(ed -> { |
|
||||
if (checkCustomerHierarchy(customers, ed)) { |
|
||||
processor.accept(ed); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processGroupsOnly(List<GroupPermissions> groupPermissions, Consumer<EntityData<?>> processor) { |
|
||||
Set<UUID> allowedGroupIds = groupPermissions.stream().map(GroupPermissions::getGroupId) |
|
||||
.filter(this.groupIds::contains).collect(Collectors.toSet()); |
|
||||
|
|
||||
checkGroupIds(allowedGroupIds, processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processAll(Consumer<EntityData<?>> processor) { |
|
||||
checkGroupIds(groupIds, processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int getProbableResultSize() { |
|
||||
return groupIds.size(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected boolean matches(EntityData ed) { |
|
||||
return super.matches(ed) && groupType.equals(ed.getFields().getType()); |
|
||||
} |
|
||||
|
|
||||
private void checkGroupIds(Set<UUID> allowedGroupIds, Consumer<EntityData<?>> processor) { |
|
||||
for (UUID groupId : allowedGroupIds) { |
|
||||
EntityGroupData entityGroup = repository.getEntityGroup(groupId); |
|
||||
if (matches(entityGroup)) { |
|
||||
processor.accept(entityGroup); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,83 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.EntityGroupNameFilter; |
|
||||
import org.thingsboard.server.edqs.data.CustomerData; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.data.EntityGroupData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
import org.thingsboard.server.edqs.util.RepositoryUtils; |
|
||||
|
|
||||
import java.util.List; |
|
||||
import java.util.UUID; |
|
||||
import java.util.function.Consumer; |
|
||||
import java.util.regex.Pattern; |
|
||||
|
|
||||
import static org.thingsboard.server.common.data.EntityType.ENTITY_GROUP; |
|
||||
|
|
||||
public class EntityGroupNameQueryProcessor extends AbstractEntityGroupQueryProcessor<EntityGroupNameFilter> { |
|
||||
|
|
||||
private final String groupType; |
|
||||
private final Pattern groupNamePattern; |
|
||||
|
|
||||
public EntityGroupNameQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (EntityGroupNameFilter) query.getEntityFilter()); |
|
||||
this.groupType = filter.getGroupType().name(); |
|
||||
this.groupNamePattern = RepositoryUtils.toSqlLikePattern(filter.getEntityGroupNameFilter()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processCustomerGenericRead(UUID customerId, Consumer<EntityData<?>> processor) { |
|
||||
var customers = repository.getEntityMap(EntityType.CUSTOMER); |
|
||||
for (UUID cId : repository.getAllCustomers(customerId)) { |
|
||||
var customerData = (CustomerData) customers.get(cId); |
|
||||
if (customerData != null) { |
|
||||
process(customerData.getEntities(ENTITY_GROUP), processor); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processGroupsOnly(List<GroupPermissions> groupPermissions, Consumer<EntityData<?>> processor) { |
|
||||
for (GroupPermissions groupPermission : groupPermissions) { |
|
||||
EntityGroupData entityGroup = repository.getEntityGroup(groupPermission.groupId); |
|
||||
if (matches(entityGroup)) { |
|
||||
processor.accept(entityGroup); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processAll(Consumer<EntityData<?>> processor) { |
|
||||
process(repository.getEntitySet(ENTITY_GROUP), processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected boolean matches(EntityData ed) { |
|
||||
return super.matches(ed) && (groupNamePattern == null || groupNamePattern.matcher(ed.getFields().getName()).matches()) |
|
||||
&& groupType.equals(ed.getFields().getType()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int getProbableResultSize() { |
|
||||
return 1024; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,29 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import lombok.Data; |
|
||||
|
|
||||
import java.util.UUID; |
|
||||
|
|
||||
@Data |
|
||||
public class GroupPermissions implements Permissions { |
|
||||
|
|
||||
protected final UUID groupId; |
|
||||
protected final boolean readAttrs; |
|
||||
protected final boolean readTs; |
|
||||
|
|
||||
} |
|
||||
@ -1,24 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
public interface Permissions { |
|
||||
|
|
||||
boolean isReadAttrs(); |
|
||||
|
|
||||
boolean isReadTs(); |
|
||||
|
|
||||
} |
|
||||
@ -1,33 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import lombok.Builder; |
|
||||
import lombok.Getter; |
|
||||
|
|
||||
import java.util.List; |
|
||||
|
|
||||
@Getter |
|
||||
@Builder |
|
||||
public class RelationQueryPermissions implements Permissions { |
|
||||
|
|
||||
private final boolean readEntity; |
|
||||
private final boolean readAttrs; |
|
||||
private final boolean readTs; |
|
||||
private final boolean hasGroups; |
|
||||
private final List<GroupPermissions> groupPermissions; |
|
||||
|
|
||||
} |
|
||||
@ -1,37 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.SchedulerEventFilter; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
|
|
||||
public class SchedulerEventQueryProcessor extends AbstractSimpleQueryProcessor<SchedulerEventFilter> { |
|
||||
|
|
||||
public SchedulerEventQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (SchedulerEventFilter) query.getEntityFilter(), EntityType.SCHEDULER_EVENT); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected boolean matches(EntityData<?> ed) { |
|
||||
return super.matches(ed) && (filter.getEventType() == null || filter.getEventType().equals(ed.getFields().getType())) |
|
||||
&& (filter.getOriginator() == null || filter.getOriginator().equals(ed.getFields().getOriginatorId())); |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
@ -1,91 +0,0 @@ |
|||||
/** |
|
||||
* Copyright © 2016-2024 ThingsBoard, Inc. |
|
||||
* |
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
* you may not use this file except in compliance with the License. |
|
||||
* You may obtain a copy of the License at |
|
||||
* |
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||
* |
|
||||
* Unless required by applicable law or agreed to in writing, software |
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
* See the License for the specific language governing permissions and |
|
||||
* limitations under the License. |
|
||||
*/ |
|
||||
package org.thingsboard.server.edqs.query.processor; |
|
||||
|
|
||||
import org.thingsboard.server.common.data.EntityType; |
|
||||
import org.thingsboard.server.common.data.id.EntityId; |
|
||||
import org.thingsboard.server.common.data.permission.QueryContext; |
|
||||
import org.thingsboard.server.common.data.query.StateEntityOwnerFilter; |
|
||||
import org.thingsboard.server.edqs.data.EntityData; |
|
||||
import org.thingsboard.server.edqs.query.EdqsQuery; |
|
||||
import org.thingsboard.server.edqs.query.SortableEntityData; |
|
||||
import org.thingsboard.server.edqs.repo.TenantRepo; |
|
||||
|
|
||||
import java.util.Collections; |
|
||||
import java.util.List; |
|
||||
import java.util.UUID; |
|
||||
import java.util.function.Consumer; |
|
||||
|
|
||||
public class StateEntityOwnerQueryProcessor extends AbstractSingleEntityTypeQueryProcessor<StateEntityOwnerFilter> { |
|
||||
|
|
||||
private final EntityId entityId; |
|
||||
|
|
||||
public StateEntityOwnerQueryProcessor(TenantRepo repo, QueryContext ctx, EdqsQuery query) { |
|
||||
super(repo, ctx, query, (StateEntityOwnerFilter) query.getEntityFilter()); |
|
||||
this.entityId = filter.getSingleEntity(); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processCustomerGenericRead(UUID customerId, Consumer<EntityData<?>> processor) { |
|
||||
EntityData ed = repository.getEntityMap(entityId.getEntityType()).get(entityId.getId()); |
|
||||
if (ed != null && ed.getCustomerId() != null && matches(ed)) { |
|
||||
if (customerId.equals(ed.getCustomerId()) || repository.getAllCustomers(customerId).contains(ed.getCustomerId())) { |
|
||||
processor.accept(ed); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
|
||||
@Override |
|
||||
protected List<SortableEntityData> processCustomerGenericReadWithGroups(UUID customerId, boolean readAttrPermissions, boolean readTsPermissions, List<GroupPermissions> groupPermissions) { |
|
||||
EntityData ed = repository.getEntityMap(entityId.getEntityType()).get(entityId.getId()); |
|
||||
if (!matches(ed)) { |
|
||||
return Collections.emptyList(); |
|
||||
} else { |
|
||||
boolean genericRead = customerId.equals(ed.getCustomerId()) || repository.getAllCustomers(customerId).contains(ed.getCustomerId()); |
|
||||
CombinedPermissions permissions = getCombinedPermissions(ed.getId(), genericRead, readAttrPermissions, readTsPermissions, groupPermissions); |
|
||||
if (permissions.isRead()) { |
|
||||
SortableEntityData sortData = toSortData(ed, permissions); |
|
||||
return Collections.singletonList(sortData); |
|
||||
} else { |
|
||||
return Collections.emptyList(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processGroupsOnly(List<GroupPermissions> groupPermissions, Consumer<EntityData<?>> processor) { |
|
||||
processAll(processor); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected void processAll(Consumer<EntityData<?>> processor) { |
|
||||
EntityData ed = repository.getEntityMap(entityId.getEntityType()).get(entityId.getId()); |
|
||||
if (ed != null) { |
|
||||
if (ed.getCustomerId() != null) { |
|
||||
processor.accept(repository.getEntityMap(EntityType.CUSTOMER).get(ed.getCustomerId())); |
|
||||
} else { |
|
||||
processor.accept(repository.getEntityMap(EntityType.TENANT).get(repository.getTenantId().getId())); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
protected int getProbableResultSize() { |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue