diff --git a/scaleph-api/src/main/java/cn/sliew/scaleph/api/controller/admin/SecAuthorizeController.java b/scaleph-api/src/main/java/cn/sliew/scaleph/api/controller/admin/SecAuthorizeController.java index b475864b2..b0cec2efb 100644 --- a/scaleph-api/src/main/java/cn/sliew/scaleph/api/controller/admin/SecAuthorizeController.java +++ b/scaleph-api/src/main/java/cn/sliew/scaleph/api/controller/admin/SecAuthorizeController.java @@ -22,11 +22,9 @@ import cn.sliew.scaleph.security.service.SecAuthorizeService; import cn.sliew.scaleph.security.service.dto.SecResourceWebWithAuthorizeDTO; import cn.sliew.scaleph.security.service.dto.SecRoleDTO; +import cn.sliew.scaleph.security.service.dto.SecUserDTO; import cn.sliew.scaleph.security.service.dto.UmiRoute; -import cn.sliew.scaleph.security.service.param.SecResourceWebBatchAuthorizeForRoleParam; -import cn.sliew.scaleph.security.service.param.SecResourceWebListByRoleParam; -import cn.sliew.scaleph.security.service.param.SecRoleBatchAuthorizeForResourceWebParam; -import cn.sliew.scaleph.security.service.param.SecRoleListByResourceWebParam; +import cn.sliew.scaleph.security.service.param.*; import cn.sliew.scaleph.system.model.ResponseVO; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import io.swagger.v3.oas.annotations.Operation; @@ -111,4 +109,68 @@ public ResponseEntity unauthorize(@Valid @RequestBody SecResourceWeb return new ResponseEntity<>(ResponseVO.success(), HttpStatus.OK); } + @Logging + @GetMapping("role/authorized-users") + @Operation(summary = "查询角色绑定用户列表", description = "查询角色绑定用户列表") + public ResponseEntity> listAuthorizedUsersByRoleId(@Valid SecUserListByRoleParam param) { + Page result = secAuthorizeService.listAuthorizedUsersByRoleId(param); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @Logging + @GetMapping("role/unauthorized-users") + @Operation(summary = "查询角色未绑定用户列表", description = "查询角色未绑定用户列表") + public ResponseEntity> listUnauthorizedUsersByRoleId(@Valid SecUserListByRoleParam param) { + Page result = secAuthorizeService.listUnauthorizedUsersByRoleId(param); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @Logging + @PutMapping("role/users") + @Operation(summary = "批量为角色绑定用户", description = "批量为角色绑定用户") + public ResponseEntity authorize(@Valid @RequestBody SecUserBatchAuthorizeForRoleParam param) { + secAuthorizeService.authorize(param); + return new ResponseEntity<>(ResponseVO.success(), HttpStatus.OK); + } + + @Logging + @DeleteMapping("role/users") + @Operation(summary = "批量为角色解除用户绑定", description = "批量为角色解除用户绑定") + public ResponseEntity unauthorize(@Valid @RequestBody SecUserBatchAuthorizeForRoleParam param) { + secAuthorizeService.unauthorize(param); + return new ResponseEntity<>(ResponseVO.success(), HttpStatus.OK); + } + + @Logging + @GetMapping("user/authorized-roles") + @Operation(summary = "查询用户绑定角色列表", description = "查询用户绑定角色列表") + public ResponseEntity> listAuthorizedRolesByUserId(@Valid SecRoleListByUserParam param) { + List result = secAuthorizeService.listAuthorizedRolesByUserId(param); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @Logging + @GetMapping("user/unauthorized-roles") + @Operation(summary = "查询用户未绑定角色列表", description = "查询用户未绑定角色列表") + public ResponseEntity> listUnauthorizedRolesByUserId(@Valid SecRoleListByUserParam param) { + List result = secAuthorizeService.listUnauthorizedRolesByUserId(param); + return new ResponseEntity<>(result, HttpStatus.OK); + } + + @Logging + @PutMapping("user/roles") + @Operation(summary = "批量为用户绑定角色", description = "批量为用户绑定角色") + public ResponseEntity authorize(@Valid @RequestBody SecRoleBatchAuthorizeForUserParam param) { + secAuthorizeService.authorize(param); + return new ResponseEntity<>(ResponseVO.success(), HttpStatus.OK); + } + + @Logging + @DeleteMapping("user/roles") + @Operation(summary = "批量为用户解除角色绑定", description = "批量为用户解除角色绑定") + public ResponseEntity unauthorize(@Valid @RequestBody SecRoleBatchAuthorizeForUserParam param) { + secAuthorizeService.unauthorize(param); + return new ResponseEntity<>(ResponseVO.success(), HttpStatus.OK); + } + } diff --git a/scaleph-dao/src/main/java/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.java b/scaleph-dao/src/main/java/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.java index b1d044580..4cc877770 100644 --- a/scaleph-dao/src/main/java/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.java +++ b/scaleph-dao/src/main/java/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.java @@ -18,10 +18,18 @@ package cn.sliew.scaleph.dao.mapper.master.security; +import cn.sliew.scaleph.common.dict.security.RoleStatus; +import cn.sliew.scaleph.common.dict.security.UserStatus; +import cn.sliew.scaleph.dao.entity.master.security.SecRole; +import cn.sliew.scaleph.dao.entity.master.security.SecUser; import cn.sliew.scaleph.dao.entity.master.security.SecUserRole; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; +import java.util.List; + /** *

* 用户角色关联表 Mapper 接口 @@ -33,4 +41,33 @@ @Repository public interface SecUserRoleMapper extends BaseMapper { + /** + * 查询角色关联的用户 + */ + Page selectRelatedUsersByRole(Page page, + @Param("roleId") Long roleId, + @Param("status") UserStatus status, + @Param("userName") String userName); + + /** + * 查询角色未关联的用户 + */ + Page selectUnrelatedUsersByRole(Page page, + @Param("roleId") Long roleId, + @Param("status") UserStatus status, + @Param("userName") String userName); + + /** + * 查询用户关联的角色 + */ + List selectRelatedRolesByUser(@Param("userId") Long userId, + @Param("status") RoleStatus status, + @Param("name") String name); + + /** + * 查询用户未关联的角色 + */ + List selectUnrelatedRolesByUser(@Param("userId") Long userId, + @Param("status") RoleStatus status, + @Param("name") String name); } diff --git a/scaleph-dao/src/main/resources/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.xml b/scaleph-dao/src/main/resources/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.xml index 900ded863..614cfd678 100644 --- a/scaleph-dao/src/main/resources/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.xml +++ b/scaleph-dao/src/main/resources/cn/sliew/scaleph/dao/mapper/master/security/SecUserRoleMapper.xml @@ -30,4 +30,76 @@ + + + + + + + + diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/SecAuthorizeService.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/SecAuthorizeService.java index c38cd1c82..418d9ab52 100644 --- a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/SecAuthorizeService.java +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/SecAuthorizeService.java @@ -20,11 +20,9 @@ import cn.sliew.scaleph.security.service.dto.SecResourceWebWithAuthorizeDTO; import cn.sliew.scaleph.security.service.dto.SecRoleDTO; +import cn.sliew.scaleph.security.service.dto.SecUserDTO; import cn.sliew.scaleph.security.service.dto.UmiRoute; -import cn.sliew.scaleph.security.service.param.SecResourceWebBatchAuthorizeForRoleParam; -import cn.sliew.scaleph.security.service.param.SecResourceWebListByRoleParam; -import cn.sliew.scaleph.security.service.param.SecRoleBatchAuthorizeForResourceWebParam; -import cn.sliew.scaleph.security.service.param.SecRoleListByResourceWebParam; +import cn.sliew.scaleph.security.service.param.*; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import java.util.List; @@ -63,6 +61,7 @@ public interface SecAuthorizeService { // ------------------------------------------------------------------------------------------- // role -> resource-web // ------------------------------------------------------------------------------------------- + /** * 查询所有 资源-web 和指定角色绑定状态 */ @@ -78,4 +77,52 @@ public interface SecAuthorizeService { */ void unauthorize(SecResourceWebBatchAuthorizeForRoleParam param); + // ------------------------------------------------------------------------------------------- + // role -> user + // ------------------------------------------------------------------------------------------- + + /** + * 查询角色绑定用户列表 + */ + Page listAuthorizedUsersByRoleId(SecUserListByRoleParam param); + + /** + * 查询角色未绑定用户列表 + */ + Page listUnauthorizedUsersByRoleId(SecUserListByRoleParam param); + + /** + * 批量为角色绑定用户 + */ + void authorize(SecUserBatchAuthorizeForRoleParam param); + + /** + * 批量为角色解除用户绑定 + */ + void unauthorize(SecUserBatchAuthorizeForRoleParam param); + + // ------------------------------------------------------------------------------------------- + // user -> role + // ------------------------------------------------------------------------------------------- + + /** + * 查询用户绑定角色列表 + */ + List listAuthorizedRolesByUserId(SecRoleListByUserParam param); + + /** + * 查询用户未绑定角色列表 + */ + List listUnauthorizedRolesByUserId(SecRoleListByUserParam param); + + /** + * 批量为用户绑定角色 + */ + void authorize(SecRoleBatchAuthorizeForUserParam param); + + /** + * 批量为用户解除角色绑定 + */ + void unauthorize(SecRoleBatchAuthorizeForUserParam param); + } diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/impl/SecAuthorizeServiceImpl.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/impl/SecAuthorizeServiceImpl.java index 935857fc0..1d340d538 100644 --- a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/impl/SecAuthorizeServiceImpl.java +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/impl/SecAuthorizeServiceImpl.java @@ -18,31 +18,30 @@ package cn.sliew.scaleph.security.service.impl; -import cn.sliew.scaleph.dao.entity.master.security.SecResourceWebRole; -import cn.sliew.scaleph.dao.entity.master.security.SecResourceWebVO; -import cn.sliew.scaleph.dao.entity.master.security.SecRole; +import cn.sliew.scaleph.dao.entity.master.security.*; import cn.sliew.scaleph.dao.mapper.master.security.SecResourceWebRoleMapper; +import cn.sliew.scaleph.dao.mapper.master.security.SecUserRoleMapper; +import cn.sliew.scaleph.security.authentication.UserDetailInfo; import cn.sliew.scaleph.security.service.SecAuthorizeService; import cn.sliew.scaleph.security.service.SecResourceWebService; import cn.sliew.scaleph.security.service.convert.SecResourceWebWithAuthorizeConvert; import cn.sliew.scaleph.security.service.convert.SecRoleConvert; -import cn.sliew.scaleph.security.service.dto.SecResourceWebDTO; -import cn.sliew.scaleph.security.service.dto.SecResourceWebWithAuthorizeDTO; -import cn.sliew.scaleph.security.service.dto.SecRoleDTO; -import cn.sliew.scaleph.security.service.dto.UmiRoute; -import cn.sliew.scaleph.security.service.param.SecResourceWebBatchAuthorizeForRoleParam; -import cn.sliew.scaleph.security.service.param.SecResourceWebListByRoleParam; -import cn.sliew.scaleph.security.service.param.SecRoleBatchAuthorizeForResourceWebParam; -import cn.sliew.scaleph.security.service.param.SecRoleListByResourceWebParam; +import cn.sliew.scaleph.security.service.convert.SecUserConvert; +import cn.sliew.scaleph.security.service.dto.*; +import cn.sliew.scaleph.security.service.param.*; +import cn.sliew.scaleph.security.util.SecurityUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Optional; @Service public class SecAuthorizeServiceImpl implements SecAuthorizeService { @@ -51,12 +50,19 @@ public class SecAuthorizeServiceImpl implements SecAuthorizeService { private SecResourceWebService secResourceWebService; @Autowired private SecResourceWebRoleMapper secResourceWebRoleMapper; + @Autowired + private SecUserRoleMapper secUserRoleMapper; /** * fixme 这里没有获取用户自己的资源,先获取的所有资源 */ @Override public List getWebRoute() { + Optional optional = SecurityUtil.getCurrentUserId(); + if (optional.isEmpty()) { + return Collections.emptyList(); + } + Long userId = optional.get(); return buildRouteByPid(0L); } @@ -159,4 +165,76 @@ public void unauthorize(SecResourceWebBatchAuthorizeForRoleParam param) { secResourceWebRoleMapper.delete(queryWrapper); } } + + @Override + public Page listAuthorizedUsersByRoleId(SecUserListByRoleParam param) { + Page page = new Page(param.getCurrent(), param.getPageSize()); + Page secUserPage = secUserRoleMapper.selectRelatedUsersByRole(page, param.getRoleId(), param.getStatus(), param.getUserName()); + Page result = new Page<>(secUserPage.getCurrent(), secUserPage.getSize(), secUserPage.getTotal()); + List secUserDTOS = SecUserConvert.INSTANCE.toDto(secUserPage.getRecords()); + result.setRecords(secUserDTOS); + return result; + } + + @Override + public Page listUnauthorizedUsersByRoleId(SecUserListByRoleParam param) { + Page page = new Page(param.getCurrent(), param.getPageSize()); + Page secUserPage = secUserRoleMapper.selectUnrelatedUsersByRole(page, param.getRoleId(), param.getStatus(), param.getUserName()); + Page result = new Page<>(secUserPage.getCurrent(), secUserPage.getSize(), secUserPage.getTotal()); + List secUserDTOS = SecUserConvert.INSTANCE.toDto(secUserPage.getRecords()); + result.setRecords(secUserDTOS); + return result; + } + + @Override + public void authorize(SecUserBatchAuthorizeForRoleParam param) { + for (Long userId : param.getUserIds()) { + SecUserRole record = new SecUserRole(); + record.setUserId(userId); + record.setRoleId(param.getRoleId()); + secUserRoleMapper.insert(record); + } + } + + @Override + public void unauthorize(SecUserBatchAuthorizeForRoleParam param) { + for (Long userId : param.getUserIds()) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(SecUserRole.class) + .eq(SecUserRole::getUserId, userId) + .eq(SecUserRole::getRoleId, param.getRoleId()); + secUserRoleMapper.delete(queryWrapper); + } + } + + @Override + public List listAuthorizedRolesByUserId(SecRoleListByUserParam param) { + List secRoleList = secUserRoleMapper.selectRelatedRolesByUser(param.getUserId(), param.getStatus(), param.getName()); + return SecRoleConvert.INSTANCE.toDto(secRoleList); + } + + @Override + public List listUnauthorizedRolesByUserId(SecRoleListByUserParam param) { + List secRoleList = secUserRoleMapper.selectUnrelatedRolesByUser(param.getUserId(), param.getStatus(), param.getName()); + return SecRoleConvert.INSTANCE.toDto(secRoleList); + } + + @Override + public void authorize(SecRoleBatchAuthorizeForUserParam param) { + for (Long roleId : param.getRoleIds()) { + SecUserRole record = new SecUserRole(); + record.setUserId(param.getUserId()); + record.setRoleId(roleId); + secUserRoleMapper.insert(record); + } + } + + @Override + public void unauthorize(SecRoleBatchAuthorizeForUserParam param) { + for (Long roleId : param.getRoleIds()) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(SecUserRole.class) + .eq(SecUserRole::getUserId, param.getUserId()) + .eq(SecUserRole::getRoleId, roleId); + secUserRoleMapper.delete(queryWrapper); + } + } } diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleBatchAuthorizeForUserParam.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleBatchAuthorizeForUserParam.java new file mode 100644 index 000000000..020dd5fa3 --- /dev/null +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleBatchAuthorizeForUserParam.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 cn.sliew.scaleph.security.service.param; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.util.List; + +@Data +public class SecRoleBatchAuthorizeForUserParam { + + @NotNull + @Schema(description = "用户 id") + private Long userId; + + @NotEmpty + @Schema(description = "待授权/取消授权角色 id 列表") + private List roleIds; +} diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleListByUserParam.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleListByUserParam.java new file mode 100644 index 000000000..bfef440c0 --- /dev/null +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecRoleListByUserParam.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 cn.sliew.scaleph.security.service.param; + +import cn.sliew.scaleph.common.dict.security.RoleStatus; +import cn.sliew.scaleph.common.dict.security.UserStatus; +import cn.sliew.scaleph.system.model.PaginationParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class SecRoleListByUserParam extends PaginationParam { + + @NotNull + @Schema(description = "用户 id") + private Long userId; + + @Schema(description = "用户状态") + private RoleStatus status; + + @Schema(description = "角色名称。支持搜索") + private String name; +} diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserBatchAuthorizeForRoleParam.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserBatchAuthorizeForRoleParam.java new file mode 100644 index 000000000..3a14b191f --- /dev/null +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserBatchAuthorizeForRoleParam.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 cn.sliew.scaleph.security.service.param; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; +import java.util.List; + +@Data +public class SecUserBatchAuthorizeForRoleParam { + + @NotNull + @Schema(description = "角色 id") + private Long roleId; + + @NotEmpty + @Schema(description = "待授权/取消授权用户 id 列表") + private List userIds; +} diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserListByRoleParam.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserListByRoleParam.java new file mode 100644 index 000000000..1f253bcb5 --- /dev/null +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/service/param/SecUserListByRoleParam.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 cn.sliew.scaleph.security.service.param; + +import cn.sliew.scaleph.common.dict.security.UserStatus; +import cn.sliew.scaleph.system.model.PaginationParam; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class SecUserListByRoleParam extends PaginationParam { + + @NotNull + @Schema(description = "角色 id") + private Long roleId; + + @Schema(description = "用户状态") + private UserStatus status; + + @Schema(description = "用户名称。支持搜索") + private String userName; +} diff --git a/scaleph-security/src/main/java/cn/sliew/scaleph/security/util/SecurityUtil.java b/scaleph-security/src/main/java/cn/sliew/scaleph/security/util/SecurityUtil.java index bdb4b33fa..cfc562a9d 100644 --- a/scaleph-security/src/main/java/cn/sliew/scaleph/security/util/SecurityUtil.java +++ b/scaleph-security/src/main/java/cn/sliew/scaleph/security/util/SecurityUtil.java @@ -18,11 +18,14 @@ package cn.sliew.scaleph.security.util; +import cn.sliew.scaleph.security.authentication.UserDetailInfo; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.util.ObjectUtils; +import java.util.Optional; + /** * @author gleiyu */ @@ -54,4 +57,17 @@ public static String getCurrentUserName() { } return null; } + + /** + * 获取当前登录的用户名称 + * + * @return username + */ + public static Optional getCurrentUserId() { + UserDetailInfo userDetailInfo = (UserDetailInfo) SecurityUtil.getCurrentUser(); + if (userDetailInfo != null) { + return Optional.of(userDetailInfo.getUser().getId()); + } + return Optional.empty(); + } } diff --git a/tools/docker/mysql/init.d/scaleph-security-mysql.sql b/tools/docker/mysql/init.d/scaleph-security-mysql.sql index 6d99e6db8..a089994db 100644 --- a/tools/docker/mysql/init.d/scaleph-security-mysql.sql +++ b/tools/docker/mysql/init.d/scaleph-security-mysql.sql @@ -619,6 +619,7 @@ create table sec_user_role -- init data insert into sec_user_role (id, user_id, role_id, creator, editor) values (1, 1, 1, 'sys', 'sys'); + /* 部门角色关联表 */ drop table if exists sec_dept_role; create table sec_dept_role