Explorar el Código

增加ClearUserAuthorizationCacheEvent

zhouhao hace 7 años
padre
commit
7ae5a5e6b2

+ 17 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-api/src/main/java/org/hswebframework/web/service/authorization/events/ClearUserAuthorizationCacheEvent.java

@@ -0,0 +1,17 @@
+package org.hswebframework.web.service.authorization.events;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author zhouhao
+ * @see org.springframework.context.event.EventListener
+ * @since 3.0.0-RC
+ */
+@AllArgsConstructor
+@Getter
+public class ClearUserAuthorizationCacheEvent {
+    private String userId;
+
+    private boolean all;
+}

+ 22 - 0
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java

@@ -34,11 +34,14 @@ import org.hswebframework.web.service.DefaultDSLQueryService;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.authorization.*;
 import org.hswebframework.web.service.authorization.AuthorizationSettingTypeSupplier.SettingInfo;
+import org.hswebframework.web.service.authorization.events.ClearUserAuthorizationCacheEvent;
 import org.hswebframework.web.validator.group.CreateGroup;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.event.TransactionalEventListener;
 import org.springframework.util.StringUtils;
 
 import java.util.*;
@@ -268,6 +271,25 @@ public class SimpleAuthorizationSettingService extends GenericEntityService<Auth
                         StringUtils.isEmpty(menuEntity.getParentId()) || "-1".equals(menuEntity.getParentId()));
     }
 
+    @TransactionalEventListener(condition = "#event.all")
+    @CacheEvict(cacheNames = USER_MENU_CACHE_NAME, allEntries = true)
+    public void clearAllUserCache(ClearUserAuthorizationCacheEvent event) {
+        logger.debug("clear all user authorization cache");
+    }
+
+    @TransactionalEventListener(condition = "!#event.all")
+    @Caching(
+            evict = {
+                    @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "#event.userId"),
+                    @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'user-menu-list:'+#event.userId"),
+                    @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'menu-tree:'+#event.userId")
+            }
+    )
+    public void clearUserCache(ClearUserAuthorizationCacheEvent event) {
+        logger.debug("clear user:{} authorization cache", event.getUserId());
+    }
+
+
     @Override
     public Authentication initUserAuthorization(String userId) {
         if (null == userId) {

+ 21 - 1
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimplePermissionService.java

@@ -6,11 +6,15 @@ import org.hswebframework.web.entity.authorization.PermissionEntity;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.GenericEntityService;
 import org.hswebframework.web.service.authorization.PermissionService;
+import org.hswebframework.web.service.authorization.events.ClearUserAuthorizationCacheEvent;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationEventPublisher;
 import org.springframework.stereotype.Service;
 
+import java.util.Optional;
+
 /**
- * TODO 完成注释
+ * 权限管理
  *
  * @author zhouhao
  */
@@ -20,6 +24,9 @@ public class SimplePermissionService extends GenericEntityService<PermissionEnti
     @Autowired
     private PermissionDao permissionDao;
 
+    @Autowired
+    private ApplicationEventPublisher eventPublisher;
+
     @Override
     protected IDGenerator<String> getIDGenerator() {
         return IDGenerator.MD5;
@@ -36,4 +43,17 @@ public class SimplePermissionService extends GenericEntityService<PermissionEnti
         return super.insert(entity);
     }
 
+    @Override
+    public int updateByPk(String id, PermissionEntity entity) {
+        int len = super.updateByPk(id, entity);
+        eventPublisher.publishEvent(new ClearUserAuthorizationCacheEvent(null, true));
+        return len;
+    }
+
+    @Override
+    public int deleteByPk(String id) {
+        int len = super.deleteByPk(id);
+        eventPublisher.publishEvent(new ClearUserAuthorizationCacheEvent(null, true));
+        return len;
+    }
 }

+ 7 - 4
hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java

@@ -14,6 +14,7 @@ import org.hswebframework.web.entity.authorization.bind.BindRoleUserEntity;
 import org.hswebframework.web.id.IDGenerator;
 import org.hswebframework.web.service.AbstractService;
 import org.hswebframework.web.service.DefaultDSLQueryService;
+import org.hswebframework.web.service.authorization.events.ClearUserAuthorizationCacheEvent;
 import org.hswebframework.web.service.authorization.events.UserModifiedEvent;
 import org.hswebframework.web.validate.ValidationException;
 import org.hswebframework.utils.ListUtils;
@@ -165,10 +166,10 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
 
     @Override
     @Caching(evict = {
-            @CacheEvict(value = CacheConstants.USER_CACHE_NAME, key = "#userId"),
-            @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "#userId"),
-            @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'user-menu-list:'+#userId"),
-            @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'menu-tree:'+#userId")
+            @CacheEvict(value = CacheConstants.USER_CACHE_NAME, key = "#userId")
+//           , @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "#userId"),
+//            @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'user-menu-list:'+#userId"),
+//            @CacheEvict(value = CacheConstants.USER_AUTH_CACHE_NAME, key = "'menu-tree:'+#userId")
     })
     public void update(String userId, UserEntity userEntity) {
         userEntity.setId(userId);
@@ -210,6 +211,8 @@ public class SimpleUserService extends AbstractService<UserEntity, String>
         if (excludeProperties.contains(UserEntity.password)) {
             publisher.publishEvent(new UserModifiedEvent(userEntity, passwordModified, roleModified));
         }
+        publisher.publishEvent(new ClearUserAuthorizationCacheEvent(userEntity.getId(),false));
+
     }
 
     @Override