Don't unassign deleted user alarms on tenant deletion
This commit is contained in:
		
							parent
							
								
									02dc49fd3a
								
							
						
					
					
						commit
						473681c343
					
				@ -0,0 +1,20 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2024 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * 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.dao.eventsourcing;
 | 
			
		||||
 | 
			
		||||
public enum ActionCause {
 | 
			
		||||
    TENANT_DELETION
 | 
			
		||||
}
 | 
			
		||||
@ -17,18 +17,20 @@ package org.thingsboard.server.dao.eventsourcing;
 | 
			
		||||
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EdgeId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
 | 
			
		||||
@Builder
 | 
			
		||||
@Data
 | 
			
		||||
public class DeleteEntityEvent<T> {
 | 
			
		||||
 | 
			
		||||
    private final TenantId tenantId;
 | 
			
		||||
    private final EntityId entityId;
 | 
			
		||||
    private final T entity;
 | 
			
		||||
    private final String body;
 | 
			
		||||
    private final ActionCause cause;
 | 
			
		||||
 | 
			
		||||
    @Builder.Default
 | 
			
		||||
    private final long ts = System.currentTimeMillis();
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.housekeeper.HousekeeperTask;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.msg.housekeeper.HousekeeperClient;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.ActionCause;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationService;
 | 
			
		||||
 | 
			
		||||
@ -59,7 +60,7 @@ public class CleanUpService {
 | 
			
		||||
            if (!skippedEntities.contains(entityType)) {
 | 
			
		||||
                cleanUpRelatedData(tenantId, entityId);
 | 
			
		||||
            }
 | 
			
		||||
            if (entityType == EntityType.USER) {
 | 
			
		||||
            if (entityType == EntityType.USER && event.getCause() != ActionCause.TENANT_DELETION) {
 | 
			
		||||
                submitTask(HousekeeperTask.unassignAlarms((User) event.getEntity()));
 | 
			
		||||
            }
 | 
			
		||||
        } catch (Throwable e) {
 | 
			
		||||
 | 
			
		||||
@ -53,6 +53,7 @@ import org.thingsboard.server.common.data.settings.UserSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.settings.UserSettingsType;
 | 
			
		||||
import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
 | 
			
		||||
import org.thingsboard.server.dao.entity.EntityCountService;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.ActionCause;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.ActionEntityEvent;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
 | 
			
		||||
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent;
 | 
			
		||||
@ -293,6 +294,10 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public void deleteUser(TenantId tenantId, User user) {
 | 
			
		||||
        deleteUser(tenantId, user, null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void deleteUser(TenantId tenantId, User user, ActionCause cause) {
 | 
			
		||||
        Objects.requireNonNull(user, "User is null");
 | 
			
		||||
        UserId userId = user.getId();
 | 
			
		||||
        log.trace("[{}] Executing deleteUser [{}]", tenantId, userId);
 | 
			
		||||
@ -307,7 +312,9 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
 | 
			
		||||
        eventPublisher.publishEvent(DeleteEntityEvent.builder()
 | 
			
		||||
                .tenantId(tenantId)
 | 
			
		||||
                .entityId(userId)
 | 
			
		||||
                .entity(user).build());
 | 
			
		||||
                .entity(user)
 | 
			
		||||
                .cause(cause)
 | 
			
		||||
                .build());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -564,7 +571,7 @@ public class UserServiceImpl extends AbstractCachedEntityService<UserCacheKey, U
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        protected void removeEntity(TenantId tenantId, User user) {
 | 
			
		||||
            deleteUser(tenantId, user);
 | 
			
		||||
            deleteUser(tenantId, user, ActionCause.TENANT_DELETION);
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user