diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/listener/AuthorizationListenerDispatcher.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/listener/AuthorizationListenerDispatcher.java index d01af7b5a..760468666 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/listener/AuthorizationListenerDispatcher.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/listener/AuthorizationListenerDispatcher.java @@ -35,15 +35,17 @@ public class AuthorizationListenerDispatcher { } @SuppressWarnings("unchecked") - public void doEvent(Class eventType, E event) { + public int doEvent(Class eventType, E event) { List> store = (List) listenerStore.get(eventType); if (null != store) { store.forEach(listener -> listener.on(event)); + return store.size(); } + return 0; } @SuppressWarnings("unchecked") - public void doEvent(E event) { - doEvent((Class) event.getClass(), event); + public int doEvent(E event) { + return doEvent((Class) event.getClass(), event); } } diff --git a/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/TreeSupportEntity.java b/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/TreeSupportEntity.java index 084b346c7..837070104 100644 --- a/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/TreeSupportEntity.java +++ b/hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/TreeSupportEntity.java @@ -87,6 +87,12 @@ public interface TreeSupportEntity extends GenericEntity { parent.setPath(RandomUtil.randomChar(4)); if (parent.getPath() != null) parent.setLevel(parent.getPath().split("-").length); + if (parent instanceof SortSupportEntity) { + Long index = ((SortSupportEntity) parent).getSortIndex(); + if (null == index) { + ((SortSupportEntity) parent).setSortIndex(1L); + } + } } if (children != null) { PK pid = parent.getId(); diff --git a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java index 43243d37f..653383830 100644 --- a/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java +++ b/hsweb-commons/hsweb-commons-service/hsweb-commons-service-simple/src/main/java/org/hswebframework/web/service/GenericEntityService.java @@ -25,6 +25,7 @@ import org.hswebframework.web.id.IDGenerator; import org.hswebframework.utils.ClassUtils; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; /** @@ -117,6 +118,7 @@ public abstract class GenericEntityService, PK> @Override public List selectByPk(List id) { + if (id == null || id.isEmpty()) return new ArrayList<>(); return createQuery().where().in(GenericEntity.id, id).listNoPaging(); } } diff --git a/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java b/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java index f2e29b613..0e65f61b7 100644 --- a/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java +++ b/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java @@ -89,7 +89,9 @@ import java.util.stream.Stream; @EnableCaching @EnableAspectJAutoProxy @EnableAccessLogger -public class SpringBootExample implements CommandLineRunner { +public class SpringBootExample +// implements CommandLineRunner +{ @Bean public AccessLoggerListener accessLoggerListener() { @@ -172,7 +174,7 @@ public class SpringBootExample implements CommandLineRunner { SpringApplication.run(SpringBootExample.class); } - @Override +// @Override public void run(String... strings) throws Exception { //只能查询自己创建的数据 DataAccessEntity accessEntity = new DataAccessEntity(); @@ -213,6 +215,7 @@ public class SpringBootExample implements CommandLineRunner { // permission.setDataAccess(Arrays.asList(accessEntity, updateAccessEntity, denyUpdateFields, denyUpdateFields, onlyDepartmentData)); permissionService.insert(permission); + //角色 BindPermissionRoleEntity roleEntity = entityFactory.newInstance(BindPermissionRoleEntity.class); SimplePermissionRoleEntity permissionRoleEntity = new SimplePermissionRoleEntity(); diff --git a/hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml b/hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml index 9f9895401..6a12ca7ab 100644 --- a/hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml +++ b/hsweb-examples/hsweb-examples-simple/src/main/resources/application.yml @@ -4,7 +4,7 @@ spring: auto: true proxy-target-class: true datasource: - url : jdbc:h2:mem:examples + url : jdbc:h2:file:./data username : sa password : type: com.alibaba.druid.pool.DruidDataSource diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java index a874acfbb..253f0a195 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationController.java @@ -32,6 +32,8 @@ import org.hswebframework.web.controller.message.ResponseMessage; import org.hswebframework.web.entity.authorization.UserEntity; import org.hswebframework.web.logging.AccessLogger; import org.hswebframework.web.service.authorization.UserService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -49,6 +51,8 @@ import static org.hswebframework.web.controller.message.ResponseMessage.ok; @Api(tags = "hsweb-authorization", description = "提供基本的授权功能") public class AuthorizationController { + private Logger logger = LoggerFactory.getLogger(this.getClass()); + @Autowired private UserService userService; @@ -107,7 +111,10 @@ public class AuthorizationController { // 验证通过 Authentication authentication = authenticationInitializeService.initUserAuthorization(entity.getId()); AuthorizationSuccessEvent event = new AuthorizationSuccessEvent(authentication, parameterGetter); - authorizationListenerDispatcher.doEvent(event); + int size = authorizationListenerDispatcher.doEvent(event); + if (size == 0) { + logger.warn("not found any AuthorizationSuccessEvent,access control maybe disabled!"); + } return ok(entity.getId()); } catch (Exception e) { AuthorizationFailedEvent failedEvent = new AuthorizationFailedEvent(username, password, parameterGetter, reason); diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationSettingController.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationSettingController.java index 5f694c6f2..9ae1152ec 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationSettingController.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/AuthorizationSettingController.java @@ -17,13 +17,18 @@ package org.hswebframework.web.controller.authorization; +import org.hswebframework.web.authorization.Permission; import org.hswebframework.web.authorization.annotation.Authorize; import org.hswebframework.web.commons.entity.param.QueryParamEntity; import org.hswebframework.web.controller.GenericEntityController; +import org.hswebframework.web.controller.SimpleGenericEntityController; +import org.hswebframework.web.controller.message.ResponseMessage; import org.hswebframework.web.entity.authorization.AuthorizationSettingEntity; import org.hswebframework.web.logging.AccessLogger; import org.hswebframework.web.service.authorization.AuthorizationSettingService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -36,15 +41,10 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("${hsweb.web.mappings.autz-setting:autz-setting}") @Authorize(permission = "autz-setting") @AccessLogger("权限设置") -public class AuthorizationSettingController implements GenericEntityController { +public class AuthorizationSettingController implements SimpleGenericEntityController { private AuthorizationSettingService authorizationSettingService; - @Override - public AuthorizationSettingEntity modelToEntity(AuthorizationSettingEntity model, AuthorizationSettingEntity entity) { - return model; - } - @Autowired public void setAuthorizationSettingService(AuthorizationSettingService authorizationSettingService) { this.authorizationSettingService = authorizationSettingService; @@ -54,4 +54,11 @@ public class AuthorizationSettingController implements GenericEntityController select(@PathVariable String type, @PathVariable String settingFor) { + return ResponseMessage.ok(authorizationSettingService.select(type, settingFor)); + } } diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/MenuController.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/MenuController.java index 96496e792..ad40ab1a7 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/MenuController.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-controller/src/main/java/org/hswebframework/web/controller/authorization/MenuController.java @@ -55,8 +55,6 @@ public class MenuController implements SimpleGenericEntityController + + diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMapper.xml b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMapper.xml index f4662bf59..d3d3cd67a 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMapper.xml +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMapper.xml @@ -21,10 +21,12 @@ - - - - + + + + + + @@ -32,8 +34,8 @@ - - + + diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMenuMapper.xml b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMenuMapper.xml index 0defa7d1c..3c74d5f99 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMenuMapper.xml +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-dao/hsweb-system-authorization-dao-mybatis/src/main/resources/org/hswebframework/web/dao/mybatis/mappers/authorization/AuthorizationSettingMenuMapper.xml @@ -46,6 +46,11 @@ delete from s_autz_menu where u_id =#{id} + + + diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/AuthorizationSettingMenuEntity.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/AuthorizationSettingMenuEntity.java index aee2f6ded..d68c01c25 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/AuthorizationSettingMenuEntity.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/AuthorizationSettingMenuEntity.java @@ -16,7 +16,6 @@ */ package org.hswebframework.web.entity.authorization; -import org.hswebframework.web.commons.entity.GenericEntity; import org.hswebframework.web.commons.entity.TreeSortSupportEntity; import java.util.List; diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/SimpleAuthorizationSettingMenuEntity.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/SimpleAuthorizationSettingMenuEntity.java index f7589d54d..a222cef7f 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/SimpleAuthorizationSettingMenuEntity.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-entity/src/main/java/org/hswebframework/web/entity/authorization/SimpleAuthorizationSettingMenuEntity.java @@ -100,7 +100,7 @@ public class SimpleAuthorizationSettingMenuEntity extends SimpleTreeSortSupportE } @Override - public void setChildren(List chidren) { + public void setChildren(List children) { this.children = children; } } \ No newline at end of file diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java index bdad074a0..e2cc91237 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthorizationSettingService.java @@ -88,14 +88,15 @@ public class SimpleAuthorizationSettingService extends GenericEntityService(); //获取全部菜单,并创建缓存备用 Map menuCache = menuService .selectByPk(menuIdList) .stream() .collect(Collectors.toMap(MenuEntity::getId, Function.identity())); + //根据配置,重新构造菜单结构 List reBuildMenu = new LinkedList<>(); - for (AuthorizationSettingMenuEntity entity : menuEntities) { - MenuEntity cache = menuCache.get(entity.getId()); - if (null != cache && cache.getStatus() == 1) { - UserMenuEntity menu = entityFactory.newInstance(UserMenuEntity.class, cache); - menu.setSortIndex(entity.getSortIndex()); - menu.setLevel(entity.getLevel()); - menu.setId(entity.getId()); - menu.setParentId(entity.getParentId()); - menu.setMenuId(cache.getId()); - reBuildMenu.add(menu); - } + for (MenuEntity menuEntity : menuCache.values()) { + UserMenuEntity menu = entityFactory.newInstance(UserMenuEntity.class, menuEntity); + menu.setSortIndex(menuEntity.getSortIndex()); + menu.setLevel(menuEntity.getLevel()); + menu.setId(menuEntity.getId()); + menu.setParentId(menuEntity.getParentId()); + menu.setMenuId(menuEntity.getId()); + reBuildMenu.add(menu); } + +// for (AuthorizationSettingMenuEntity entity : menuEntities) { +// MenuEntity cache = menuCache.get(entity.getMenuId()); +// if (null != cache && DataStatus.STATUS_ENABLED.equals(cache.getStatus())) { +// UserMenuEntity menu = entityFactory.newInstance(UserMenuEntity.class, cache); +// menu.setSortIndex(entity.getSortIndex()); +// menu.setLevel(entity.getLevel()); +// menu.setId(entity.getId()); +// menu.setParentId(entity.getParentId()); +// menu.setMenuId(cache.getId()); +// reBuildMenu.add(menu); +// } +// } + Collections.sort(reBuildMenu); return reBuildMenu; } @@ -262,7 +277,7 @@ public class SimpleAuthorizationSettingService extends GenericEntityService> settings = detailList .stream() diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java index 82b8f74ae..9ba7a3b3c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleUserService.java @@ -77,7 +77,14 @@ public class SimpleUserService extends AbstractService @Transactional(readOnly = true) public UserEntity selectByPk(String id) { tryValidateProperty(StringUtils.hasLength(id), UserEntity.id, "id:{not_be_null}"); - return createQuery().where(UserEntity.id, id).single(); + UserEntity userEntity=createQuery().where(UserEntity.id, id).single(); + if(null!=userEntity){ + List roleId= userRoleDao.selectByUserId(id).stream().map(UserRoleEntity::getRoleId).collect(Collectors.toList()); + BindRoleUserEntity roleUserEntity=entityFactory.newInstance(BindRoleUserEntity.class,userEntity); + roleUserEntity.setRoles(roleId); + return roleUserEntity; + } + return null; } @Override diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js index 36e235fc5..2434e5995 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/main/resources/hsweb-starter.js @@ -69,8 +69,8 @@ function install(context) { database.createOrAlter("s_permission_role") .addColumn().name("role_id").varchar(32).notNull().comment("角色ID").commit() .addColumn().name("permission_id").varchar(32).notNull().comment("权限ID").commit() - .addColumn().name("actions").clob().notNull().comment("可选操作").commit() - .addColumn().name("data_access").clob().notNull().comment("数据级控制配置").commit() + .addColumn().name("actions").clob().comment("可选操作").commit() + .addColumn().name("data_access").clob().comment("数据级控制配置").commit() .comment("权限与角色关联表").commit(); database.createOrAlter("s_user_role") @@ -83,8 +83,8 @@ function install(context) { .addColumn().name("u_id").varchar(32).notNull().primaryKey().comment("uid").commit() .addColumn().name("type").varchar(32).notNull().comment("权限类型").commit() .addColumn().name("setting_for").varchar(64).notNull().comment("设置给谁").commit() - .addColumn().name("describe").varchar(256).notNull().comment("备注").commit() - .addColumn().name("status").number(4, 0).notNull().comment("设置给谁").commit() + .addColumn().name("describe").varchar(256).comment("备注").commit() + .addColumn().name("status").number(4, 0).comment("设置给谁").commit() .comment("权限设置表").commit(); database.createOrAlter("s_autz_detail")