...
|
...
|
@@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j; |
19
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
20
|
20
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
21
|
21
|
import org.springframework.stereotype.Repository;
|
|
22
|
+import org.thingsboard.server.common.data.EntityType;
|
22
|
23
|
import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
|
23
|
24
|
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
|
24
|
25
|
import org.thingsboard.server.common.data.alarm.AlarmStatus;
|
...
|
...
|
@@ -63,19 +64,19 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository { |
63
|
64
|
}
|
64
|
65
|
|
65
|
66
|
public static final String SELECT_ORIGINATOR_NAME = " CASE" +
|
66
|
|
- " WHEN a.originator_type = 0" +
|
|
67
|
+ " WHEN a.originator_type = "+ EntityType.TENANT.ordinal() +
|
67
|
68
|
" THEN (select title from tenant where id = a.originator_id)" +
|
68
|
|
- " WHEN a.originator_type = 1 " +
|
|
69
|
+ " WHEN a.originator_type = "+ EntityType.CUSTOMER.ordinal() +
|
69
|
70
|
" THEN (select title from customer where id = a.originator_id)" +
|
70
|
|
- " WHEN a.originator_type = 2" +
|
|
71
|
+ " WHEN a.originator_type = " + EntityType.USER.ordinal() +
|
71
|
72
|
" THEN (select CONCAT (first_name, ' ', last_name) from tb_user where id = a.originator_id)" +
|
72
|
|
- " WHEN a.originator_type = 3" +
|
|
73
|
+ " WHEN a.originator_type = " + EntityType.DASHBOARD.ordinal() +
|
73
|
74
|
" THEN (select title from dashboard where id = a.originator_id)" +
|
74
|
|
- " WHEN a.originator_type = 4" +
|
|
75
|
+ " WHEN a.originator_type = " + EntityType.ASSET.ordinal() +
|
75
|
76
|
" THEN (select name from asset where id = a.originator_id)" +
|
76
|
|
- " WHEN a.originator_type = 5" +
|
|
77
|
+ " WHEN a.originator_type = " + EntityType.DEVICE.ordinal() +
|
77
|
78
|
" THEN (select name from device where id = a.originator_id)" +
|
78
|
|
- " WHEN a.originator_type = 9" +
|
|
79
|
+ " WHEN a.originator_type = " + EntityType.ENTITY_VIEW.ordinal() +
|
79
|
80
|
" THEN (select name from entity_view where id = a.originator_id)" +
|
80
|
81
|
" END as originator_name";
|
81
|
82
|
|
...
|
...
|
@@ -114,8 +115,11 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository { |
114
|
115
|
if (pageLink.isSearchPropagatedAlarms()) {
|
115
|
116
|
selectPart.append(" r.from_id as entity_id ");
|
116
|
117
|
fromPart.append(JOIN_RELATIONS);
|
|
118
|
+ wherePart.append(buildPermissionsQuery(tenantId, customerId, ctx));
|
|
119
|
+ addAnd = true;
|
117
|
120
|
} else {
|
118
|
121
|
selectPart.append(" a.originator_id as entity_id ");
|
|
122
|
+ //No need to check permissions if we select by originator.
|
119
|
123
|
}
|
120
|
124
|
EntityDataSortOrder sortOrder = pageLink.getSortOrder();
|
121
|
125
|
if (sortOrder != null && sortOrder.getKey().getType().equals(EntityKeyType.ALARM_FIELD)) {
|
...
|
...
|
@@ -126,8 +130,9 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository { |
126
|
130
|
if (pageLink.isSearchPropagatedAlarms()) {
|
127
|
131
|
fromPart.append(" and r.from_id in (:entity_ids)");
|
128
|
132
|
} else {
|
129
|
|
- wherePart.append(" a.originator_id in (:entity_ids)");
|
|
133
|
+ addAndIfNeeded(wherePart, addAnd);
|
130
|
134
|
addAnd = true;
|
|
135
|
+ wherePart.append(" a.originator_id in (:entity_ids)");
|
131
|
136
|
}
|
132
|
137
|
} else {
|
133
|
138
|
fromPart.append(" left join (select * from (VALUES");
|
...
|
...
|
@@ -202,6 +207,31 @@ public class DefaultAlarmQueryRepository implements AlarmQueryRepository { |
202
|
207
|
return AlarmDataAdapter.createAlarmData(pageLink, rows, totalElements);
|
203
|
208
|
}
|
204
|
209
|
|
|
210
|
+ private String buildPermissionsQuery(TenantId tenantId, CustomerId customerId, QueryContext ctx) {
|
|
211
|
+ StringBuilder permissionsQuery = new StringBuilder();
|
|
212
|
+ ctx.addUuidParameter("permissions_tenant_id", tenantId.getId());
|
|
213
|
+ permissionsQuery.append(" a.tenant_id = :permissions_tenant_id ");
|
|
214
|
+ if (customerId != null && !customerId.isNullUid()) {
|
|
215
|
+ ctx.addUuidParameter("permissions_customer_id", customerId.getId());
|
|
216
|
+ ctx.addUuidParameter("permissions_device_customer_id", customerId.getId());
|
|
217
|
+ ctx.addUuidParameter("permissions_asset_customer_id", customerId.getId());
|
|
218
|
+ ctx.addUuidParameter("permissions_user_customer_id", customerId.getId());
|
|
219
|
+ ctx.addUuidParameter("permissions_entity_view_customer_id", customerId.getId());
|
|
220
|
+ permissionsQuery.append(" and (");
|
|
221
|
+ permissionsQuery.append("(a.originator_type = '").append(EntityType.DEVICE.ordinal()).append("' and exists (select 1 from device cd where cd.id = a.originator_id and cd.customer_id = :permissions_device_customer_id))");
|
|
222
|
+ permissionsQuery.append(" or ");
|
|
223
|
+ permissionsQuery.append("(a.originator_type = '").append(EntityType.ASSET.ordinal()).append("' and exists (select 1 from asset ca where ca.id = a.originator_id and ca.customer_id = :permissions_device_customer_id))");
|
|
224
|
+ permissionsQuery.append(" or ");
|
|
225
|
+ permissionsQuery.append("(a.originator_type = '").append(EntityType.CUSTOMER.ordinal()).append("' and exists (select 1 from customer cc where cc.id = a.originator_id and cc.id = :permissions_customer_id))");
|
|
226
|
+ permissionsQuery.append(" or ");
|
|
227
|
+ permissionsQuery.append("(a.originator_type = '").append(EntityType.USER.ordinal()).append("' and exists (select 1 from tb_user cu where cu.id = a.originator_id and cu.customer_id = :permissions_user_customer_id))");
|
|
228
|
+ permissionsQuery.append(" or ");
|
|
229
|
+ permissionsQuery.append("(a.originator_type = '").append(EntityType.ENTITY_VIEW.ordinal()).append("' and exists (select 1 from entity_view cv where cv.id = a.originator_id and cv.customer_id = :permissions_entity_view_customer_id))");
|
|
230
|
+ permissionsQuery.append(")");
|
|
231
|
+ }
|
|
232
|
+ return permissionsQuery.toString();
|
|
233
|
+ }
|
|
234
|
+
|
205
|
235
|
private Set<AlarmStatus> toStatusSet(List<AlarmSearchStatus> statusList) {
|
206
|
236
|
Set<AlarmStatus> result = new HashSet<>();
|
207
|
237
|
for (AlarmSearchStatus searchStatus : statusList) {
|
...
|
...
|
|