【auth】【system】重新整理登录逻辑

pull/3/head
fengshuonan 2020-12-26 19:07:12 +08:00
parent effff8c6e0
commit 26f692459b
29 changed files with 389 additions and 476 deletions

View File

@ -54,33 +54,4 @@ public interface LoginUserApi {
*/
boolean getSuperAdminFlag();
/**
*
*
* @return truefalse
* @author fengshuonan
* @date 2020/10/17 11:02
*/
boolean hasLogin();
/**
* 访
*
* @param requestUri url: /userInfo/list
* @return 访truefalse
* @author fengshuonan
* @date 2020/10/17 11:03
*/
boolean hasPermission(String requestUri);
/**
*
*
* @param roleCode
* @return truefalse
* @author fengshuonan
* @date 2020/10/17 11:04
*/
boolean hasRole(String roleCode);
}

View File

@ -2,11 +2,12 @@ package cn.stylefeng.roses.kernel.auth.api.pojo.login;
import cn.hutool.core.lang.Dict;
import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;
/**
@ -25,84 +26,42 @@ public class LoginUser implements Serializable {
*/
private Long userId;
/**
*
*/
private String realName;
/**
*
*/
private String account;
/**
* /id
*/
private Long organizationId;
/**
* 访url
*/
private String avatar;
/**
*
*/
private Date birthday;
/**
* SexEnumM-F-
*/
private String sex;
/**
*
*/
private String email;
/**
*
*/
private String mobilePhone;
/**
*
*/
private String tel;
/**
* true-
*/
private Boolean superAdmin;
/**
*
*
*/
private Set<DataScopeTypeEnum> dataScopeTypes;
private SimpleUserInfo simpleUserInfo;
/**
* userId
* <p>
*
*
*/
private Set<Long> userIdDataScope;
private List<SimpleRoleInfo> simpleRoleInfoList;
/**
* id
* <p>
*
* /id
*/
private Set<Long> organizationIdDataScope;
private Long organizationId;
/**
*
*
*/
private Set<SimpleDict> apps;
private Long positionId;
/**
*
*
*/
private Set<SimpleDict> roles;
private Set<DataScopeTypeEnum> dataScopeTypeEnums;
private Set<Long> dataScopeUserIds;
private Set<Long> dataScopeOrganizationIds;
/**
*

View File

@ -0,0 +1,29 @@
package cn.stylefeng.roses.kernel.auth.api.pojo.login.basic;
import lombok.Data;
/**
*
*
* @author fengshuonan
* @date 2020/12/26 18:14
*/
@Data
public class SimpleRoleInfo {
/**
*
*/
private Long roleId;
/**
*
*/
private String roleName;
/**
*
*/
private String roleCode;
}

View File

@ -0,0 +1,56 @@
package cn.stylefeng.roses.kernel.auth.api.pojo.login.basic;
import lombok.Data;
import java.util.Date;
/**
*
*
* @author fengshuonan
* @date 2020/12/26 18:14
*/
@Data
public class SimpleUserInfo {
/**
*
*/
private String nickName;
/**
*
*/
private String realName;
/**
*
*/
private Long avatar;
/**
*
*/
private Date birthday;
/**
* M-F-
*/
private String sex;
/**
*
*/
private String email;
/**
*
*/
private String phone;
/**
*
*/
private String tel;
}

View File

@ -0,0 +1,32 @@
package cn.stylefeng.roses.kernel.auth.api.prop;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
/**
*
* <p>
* LoginUser便set
* <p>
* 使
* <p>
* LoginUser
* <p>
* expandAction
* <p>
* 使 LoginContext
*
* @author fengshuonan
* @date 2020/12/22 14:18
*/
public interface LoginUserPropExpander {
/**
* loginUser
*
* @param loginUser
* @author fengshuonan
* @date 2020/12/22 14:26
*/
void expandAction(LoginUser loginUser);
}

View File

@ -6,13 +6,11 @@ import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Set;
import static cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum.TOKEN_GET_ERROR;
@ -91,52 +89,4 @@ public class LoginUserImpl implements LoginUserApi {
return loginUser.getSuperAdmin();
}
@Override
public boolean hasLogin() {
// 获取用户的token
String token = null;
try {
token = getToken();
} catch (Exception e) {
return false;
}
// 获取是否在会话中有
return sessionManagerApi.haveSession(token);
}
@Override
public boolean hasPermission(String requestUri) {
LoginUser loginUser = getLoginUser();
Set<String> resourceUrls = loginUser.getResourceUrls();
if (resourceUrls != null && resourceUrls.size() > 0) {
return resourceUrls.contains(requestUri);
} else {
return false;
}
}
@Override
public boolean hasRole(String roleCode) {
LoginUser loginUser = getLoginUser();
Set<SimpleDict> roles = loginUser.getRoles();
if (roles != null && roles.size() > 0) {
for (SimpleDict role : roles) {
if (role.getCode().equals(roleCode)) {
return true;
}
}
}
return false;
}
}

View File

@ -1,6 +1,9 @@
package cn.stylefeng.roses.kernel.system;
import cn.stylefeng.roses.kernel.system.pojo.organization.DataScopeResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import java.util.List;
/**
*
@ -17,11 +20,12 @@ public interface DataScopeApi {
* <p>
*
*
* @param userId id
* @param userId id
* @param sysRoles
* @return
* @author majianguo
* @date 2020/11/5 11:44
*/
DataScopeResponse getDataScope(Long userId);
DataScopeResponse getDataScope(Long userId, List<SysRoleResponse> sysRoles);
}

View File

@ -4,6 +4,7 @@ import cn.stylefeng.roses.kernel.resource.api.pojo.resource.ResourceDefinition;
import cn.stylefeng.roses.kernel.resource.api.pojo.resource.ResourceUrlParam;
import java.util.List;
import java.util.Set;
/**
*
@ -24,23 +25,13 @@ public interface ResourceServiceApi {
ResourceDefinition getResourceByUrl(ResourceUrlParam resourceUrlReq);
/**
* id
* urlcode
*
* @param resourceCodes code
* @return
* @author fengshuonan
* @date 2020/11/29 19:49
*/
List<ResourceDefinition> getResourceListByIds(List<String> resourceCodes);
/**
* urlids
*
* @param resourceIds id
* @param resourceCodes
* @return url
* @author fengshuonan
* @date 2020/11/29 19:49
*/
List<String> getResourceUrlsListByIds(List<String> resourceIds);
Set<String> getResourceUrlsListByCodes(List<String> resourceCodes);
}

View File

@ -53,13 +53,13 @@ public interface RoleServiceApi {
List<Long> getMenuIdsByRoleIds(List<Long> roleIds);
/**
* id
* code
*
* @param roleIdList id
* @return id
* @return code
* @author majianguo
* @date 2020/11/5 11:17
*/
List<String> getRoleResourceList(List<Long> roleIdList);
List<String> getRoleResourceCodeList(List<Long> roleIdList);
}

View File

@ -1,5 +1,6 @@
package cn.stylefeng.roses.kernel.system.exception;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.system.constants.SystemConstants;
@ -12,6 +13,10 @@ import cn.stylefeng.roses.kernel.system.constants.SystemConstants;
*/
public class SystemModularException extends ServiceException {
public SystemModularException(AbstractExceptionEnum exception, Object... params) {
super(SystemConstants.SYSTEM_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params));
}
public SystemModularException(AbstractExceptionEnum exception, String userTip) {
super(SystemConstants.SYSTEM_MODULE_NAME, exception.getErrorCode(), userTip);
}

View File

@ -41,7 +41,7 @@ public enum SysUserExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
USER_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "51", "用户不存在"),
USER_NOT_EXIST(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "51", "用户不存在用户id或账号为{}"),
/**
*
@ -86,7 +86,17 @@ public enum SysUserExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
USER_NOT_HAVE_MENUS(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "60", "当前用户未分配菜单");
USER_NOT_HAVE_MENUS(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "60", "当前用户未分配菜单"),
/**
*
*/
USER_NOT_BIND_ROLE(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "61", "用户未绑定角色"),
/**
*
*/
ACCOUNT_HAVE_MANY(RuleConstants.BUSINESS_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "62", "系统错误,账号存在多个,账号为:{}");
/**
*

View File

@ -62,7 +62,7 @@ public class SysMenuRequest extends BaseRequest {
/**
*
*/
@NotBlank(message = "应用分类不能为空", groups = {add.class, edit.class, getAppMenus.class})
@NotBlank(message = "appCode不能为空", groups = {add.class, edit.class, getAppMenus.class})
private String appCode;
/**

View File

@ -1,10 +1,8 @@
package cn.stylefeng.roses.kernel.system.pojo.organization;
import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import lombok.Data;
import java.util.List;
import java.util.Set;
/**
@ -31,9 +29,4 @@ public class DataScopeResponse {
*/
private Set<Long> organizationIds;
/**
*
*/
private List<SysRoleResponse> sysRoleResponses;
}

View File

@ -37,7 +37,7 @@ public class DataScopeUtil {
return tips.append("空:获取不到当前用户").toString();
}
Set<DataScopeTypeEnum> dataScopeTypes = loginUser.getDataScopeTypes();
Set<DataScopeTypeEnum> dataScopeTypes = loginUser.getDataScopeTypeEnums();
if (dataScopeTypes == null) {
return tips.append("空:数据范围为空").toString();
}
@ -49,7 +49,7 @@ public class DataScopeUtil {
}
}
Set<Long> userDataScope = loginUser.getUserIdDataScope();
Set<Long> userDataScope = loginUser.getDataScopeUserIds();
tips.append("用户userId数据范围");
if (userDataScope != null && !userDataScope.isEmpty()) {
for (Long id : userDataScope) {
@ -57,7 +57,7 @@ public class DataScopeUtil {
}
}
Set<Long> organizationDataScope = loginUser.getOrganizationIdDataScope();
Set<Long> organizationDataScope = loginUser.getDataScopeOrganizationIds();
tips.append("用户organizationId数据范围");
if (organizationDataScope != null && !organizationDataScope.isEmpty()) {
for (Long id : organizationDataScope) {
@ -91,8 +91,8 @@ public class DataScopeUtil {
}
// 获取用户的数据范围类型user数据范围组织机构数据范围
Set<DataScopeTypeEnum> dataScopeTypes = loginUser.getDataScopeTypes();
Set<Long> organizationDataScope = loginUser.getOrganizationIdDataScope();
Set<DataScopeTypeEnum> dataScopeTypes = loginUser.getDataScopeTypeEnums();
Set<Long> organizationDataScope = loginUser.getDataScopeOrganizationIds();
// 如果数据范围类型为空,则返回没权限
if (dataScopeTypes == null || dataScopeTypes.isEmpty()) {

View File

@ -29,6 +29,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.db.api.DbOperatorApi;
import cn.stylefeng.roses.kernel.menu.modular.entity.SysMenu;
import cn.stylefeng.roses.kernel.menu.modular.factory.MenuFactory;
@ -38,7 +39,6 @@ import cn.stylefeng.roses.kernel.rule.enums.StatusEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.factory.DefaultTreeBuildFactory;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.system.MenuServiceApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.constants.SymbolConstant;
@ -293,7 +293,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
// 获取当前用户的角色id集合
LoginUser loginUser = LoginContext.me().getLoginUser();
List<Long> roleIdList = loginUser.getRoles().stream().map(SimpleDict::getId).collect(Collectors.toList());
List<Long> roleIdList = loginUser.getSimpleRoleInfoList().stream().map(SimpleRoleInfo::getRoleId).collect(Collectors.toList());
// 当前用户角色为空,则没菜单
if (ObjectUtil.isEmpty(roleIdList)) {

View File

@ -42,7 +42,7 @@ public class DataScopeService implements DataScopeApi {
private DbOperatorApi dbOperatorApi;
@Override
public DataScopeResponse getDataScope(Long userId) {
public DataScopeResponse getDataScope(Long userId, List<SysRoleResponse> sysRoles) {
// 初始化返回结果
DataScopeResponse dataScopeResponse = new DataScopeResponse();
@ -54,27 +54,20 @@ public class DataScopeService implements DataScopeApi {
throw new DataScopeException(DataScopeExceptionEnum.USER_ID_EMPTY_ERROR, userTip);
}
// 获取用户的所有角色
List<Long> userRoleIdList = userServiceApi.getUserRoleIdList(userId);
if (userRoleIdList.isEmpty()) {
String userTip = StrUtil.format(DataScopeExceptionEnum.ROLE_EMPTY_ERROR.getUserTip(), userId);
throw new DataScopeException(DataScopeExceptionEnum.ROLE_EMPTY_ERROR, userTip);
}
// 获取角色信息详情
List<SysRoleResponse> sysRoles = roleServiceApi.getRolesByIds(userRoleIdList);
dataScopeResponse.setSysRoleResponses(sysRoles);
// 获取用户的主要部门信息
SysUserOrgResponse sysUserOrgResponse = userOrgServiceApi.getUserOrgInfo(userId);
// 获取角色中的数据范围类型
Set<DataScopeTypeEnum> dataScopeTypeEnums = sysRoles.stream().map(SysRoleResponse::getDataScopeTypeEnum).collect(Collectors.toSet());
dataScopeResponse.setDataScopeTypeEnums(dataScopeTypeEnums);
// 包含全部数据类型的,直接不用设置组织机构,直接放开
// 1.根据数据范围类型的不同,填充角色拥有的 organizationIds 和 userIds 范围
// 1.1 包含全部数据类型的,直接不用设置组织机构,直接放开
if (dataScopeTypeEnums.contains(DataScopeTypeEnum.ALL)) {
return dataScopeResponse;
}
// 包含指定部门数据将指定数据范围增加到Set中
// 1.2 包含指定部门数据将指定数据范围增加到Set中
if (dataScopeTypeEnums.contains(DataScopeTypeEnum.DEFINE)) {
// 获取角色对应的组织机构范围
@ -83,10 +76,7 @@ public class DataScopeService implements DataScopeApi {
organizationIds.addAll(orgIds);
}
// 获取用户的主要部门信息
SysUserOrgResponse sysUserOrgResponse = userOrgServiceApi.getUserOrgInfo(userId);
// 本部门和本部门以下查出用户的主要部门并且查询该部门本部门及以下的组织机构id列表
// 1.3 本部门和本部门以下查出用户的主要部门并且查询该部门本部门及以下的组织机构id列表
if (dataScopeTypeEnums.contains(DataScopeTypeEnum.DEPT_WITH_CHILD)) {
// 获取部门及以下部门的id列表
@ -96,7 +86,7 @@ public class DataScopeService implements DataScopeApi {
organizationIds.addAll(subOrgIds);
}
// 如果是本部门,则查出本部门并添加到组织机构列表
// 1.4 如果是本部门,则查出本部门并添加到组织机构列表
if (dataScopeTypeEnums.contains(DataScopeTypeEnum.DEPT)) {
// 获取本部门的id
@ -104,15 +94,16 @@ public class DataScopeService implements DataScopeApi {
organizationIds.add(organizationId);
}
// 如果只是本用户数据将用户本身装进userId数据范围
// 1.5 如果只是本用户数据将用户本身装进userId数据范围
if (dataScopeTypeEnums.contains(DataScopeTypeEnum.SELF)) {
userIds.add(userId);
}
// 获取用户单独绑定的组织机构id
// 2. 获取用户单独绑定的组织机构id
List<Long> userBindDataScope = userServiceApi.getUserBindDataScope(userId);
organizationIds.addAll(userBindDataScope);
// 3. 组装返回结果
dataScopeResponse.setUserIds(userIds);
dataScopeResponse.setOrganizationIds(organizationIds);

View File

@ -179,7 +179,7 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
// 如果是超级管理员 或 数据范围是所有,则不过滤数据范围
boolean needToDataScope = true;
if (LoginContext.me().getSuperAdminFlag()) {
Set<DataScopeTypeEnum> dataScopeTypes = LoginContext.me().getLoginUser().getDataScopeTypes();
Set<DataScopeTypeEnum> dataScopeTypes = LoginContext.me().getLoginUser().getDataScopeTypeEnums();
if (dataScopeTypes != null && dataScopeTypes.contains(DataScopeTypeEnum.ALL)) {
needToDataScope = false;
}
@ -187,7 +187,7 @@ public class HrOrganizationServiceImpl extends ServiceImpl<HrOrganizationMapper,
// 如果需要数据范围过滤,则获取用户的数据范围,拼接查询条件
if (needToDataScope) {
Set<Long> dataScope = LoginContext.me().getLoginUser().getOrganizationIdDataScope();
Set<Long> dataScope = LoginContext.me().getLoginUser().getDataScopeOrganizationIds();
// 数据范围没有,直接返回空
if (ObjectUtil.isEmpty(dataScope)) {

View File

@ -25,10 +25,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import static cn.stylefeng.roses.kernel.system.constants.SystemConstants.DEFAULT_PARENT_ID;
@ -173,45 +170,20 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
}
@Override
public List<ResourceDefinition> getResourceListByIds(List<String> resourceCodes) {
ArrayList<ResourceDefinition> resourceDefinitions = new ArrayList<>();
public Set<String> getResourceUrlsListByCodes(List<String> resourceCodes) {
if (resourceCodes == null || resourceCodes.isEmpty()) {
return resourceDefinitions;
return new HashSet<>();
}
// 拼接in条件
LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysResource::getResourceCode, resourceCodes);
// 获取资源详情
List<SysResource> list = this.list(queryWrapper);
for (SysResource sysResource : list) {
ResourceDefinition resourceDefinition = ResourceFactory.createResourceDefinition(sysResource);
resourceDefinitions.add(resourceDefinition);
}
return resourceDefinitions;
}
@Override
public List<String> getResourceUrlsListByIds(List<String> resourceIds) {
ArrayList<String> resourceUrls = new ArrayList<>();
if (resourceIds == null || resourceIds.isEmpty()) {
return resourceUrls;
}
// 拼接in条件
LambdaQueryWrapper<SysResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysResource::getResourceId, resourceIds);
queryWrapper.select(SysResource::getUrl);
// 获取资源详情
List<SysResource> list = this.list(queryWrapper);
return list.stream().map(SysResource::getUrl).collect(Collectors.toList());
return list.stream().map(SysResource::getUrl).collect(Collectors.toSet());
}
/**

View File

@ -24,12 +24,12 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
*/
package cn.stylefeng.roses.kernel.role.modular.service;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRole;
import com.baomidou.mybatisplus.extension.service.IService;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.role.modular.entity.SysRole;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.system.pojo.role.request.SysRoleRequest;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
@ -116,16 +116,6 @@ public interface SysRoleService extends IService<SysRole> {
*/
List<Long> getRoleDataScope(SysRoleRequest sysRoleRequest);
/**
*
*
* @param userId id
* @return hashMap[{"id":456, "code":"zjl", "name":"总经理"}]
* @author majianguo
* @date 2020/11/5 11:12
*/
List<SimpleDict> getLoginRoles(Long userId);
/**
*
*

View File

@ -31,6 +31,7 @@ import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -194,10 +195,10 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
if (!LoginContext.me().getSuperAdminFlag()) {
// 查询自己拥有的
Set<SimpleDict> roles = LoginContext.me().getLoginUser().getRoles();
List<SimpleRoleInfo> roles = LoginContext.me().getLoginUser().getSimpleRoleInfoList();
// 取出所有角色id
Set<Long> loginUserRoleIds = roles.stream().map(SimpleDict::getId).collect(Collectors.toSet());
Set<Long> loginUserRoleIds = roles.stream().map(SimpleRoleInfo::getRoleId).collect(Collectors.toSet());
if (ObjectUtil.isEmpty(loginUserRoleIds)) {
return dictList;
}
@ -223,30 +224,6 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
return sysRoleDataScopeService.getRoleDataScopeIdList(CollectionUtil.newArrayList(sysRole.getRoleId()));
}
@Override
public List<SimpleDict> getLoginRoles(Long userId) {
List<SimpleDict> dictList = CollectionUtil.newArrayList();
// 获取用户角色id集合
List<Long> roleIdList = userServiceApi.getUserRoleIdList(userId);
if (ObjectUtil.isNotEmpty(roleIdList)) {
LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRole::getRoleId, roleIdList)
.eq(SysRole::getStatusFlag, StatusEnum.ENABLE.getCode())
.ne(SysRole::getDelFlag, YesOrNotEnum.N.getCode());
// 根据角色id集合查询并返回结果
this.list(queryWrapper).forEach(sysRole -> {
SimpleDict simpleDict = new SimpleDict();
simpleDict.setId(sysRole.getRoleId());
simpleDict.setCode(sysRole.getRoleCode());
simpleDict.setName(sysRole.getRoleName());
dictList.add(simpleDict);
});
}
return dictList;
}
@Override
public List<SimpleDict> list(SysRoleRequest sysRoleParam) {
List<SimpleDict> dictList = CollectionUtil.newArrayList();
@ -344,12 +321,11 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
}
@Override
public List<String> getRoleResourceList(List<Long> roleIdList) {
List<String> resourceList = CollectionUtil.newArrayList();
public List<String> getRoleResourceCodeList(List<Long> roleIdList) {
LambdaQueryWrapper<SysRoleResource> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SysRoleResource::getRoleId, roleIdList);
sysRoleResourceService.list(queryWrapper).forEach(sysRoleResource -> resourceList.add(sysRoleResource.getResourceCode()));
return resourceList;
List<SysRoleResource> sysRoleResources = sysRoleResourceService.list(queryWrapper);
return sysRoleResources.stream().map(SysRoleResource::getResourceCode).collect(Collectors.toList());
}
/**

View File

@ -6,8 +6,8 @@ import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserRoleService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
@ -31,7 +31,7 @@ public class SysUserController {
private SysUserService sysUserService;
@Resource
private UserServiceApi userServiceApi;
private SysUserRoleService sysUserRoleService;
/**
*
@ -183,7 +183,8 @@ public class SysUserController {
*/
@GetResource(name = "系统用户_获取用户的角色列表", path = "/sysUser/getUserRoles")
public ResponseData ownRole(@Validated(SysUserRequest.detail.class) SysUserRequest sysUserRequest) {
return new SuccessResponseData(sysUserService.getUserRoles(sysUserRequest));
Long userId = sysUserRequest.getUserId();
return new SuccessResponseData(sysUserRoleService.getUserRoles(userId));
}
/**
@ -194,7 +195,7 @@ public class SysUserController {
*/
@GetResource(name = "系统用户_获取用户数据范围列表", path = "/sysUser/getUserDataScope")
public ResponseData ownData(@Validated(SysUserRequest.detail.class) SysUserRequest sysUserRequest) {
List<Long> userBindDataScope = userServiceApi.getUserBindDataScope(sysUserRequest.getUserId());
List<Long> userBindDataScope = sysUserService.getUserBindDataScope(sysUserRequest.getUserId());
return new SuccessResponseData(userBindDataScope);
}

View File

@ -1,154 +0,0 @@
package cn.stylefeng.roses.kernel.system.modular.user.factory;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.file.FileInfoApi;
import cn.stylefeng.roses.kernel.file.FileOperatorApi;
import cn.stylefeng.roses.kernel.file.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.file.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.resource.api.pojo.resource.ResourceDefinition;
import cn.stylefeng.roses.kernel.rule.enums.SexEnum;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.system.AppServiceApi;
import cn.stylefeng.roses.kernel.system.DataScopeApi;
import cn.stylefeng.roses.kernel.system.ResourceServiceApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserOrgService;
import cn.stylefeng.roses.kernel.system.pojo.organization.DataScopeResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.SysUserOrgResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
*
* @author fengshuonan
* @date 2020/11/26 22:25
*/
public class LoginUserFactory {
/**
*
*
* @author fengshuonan
* @date 2020/11/26 22:32
*/
public static LoginUser createLoginUser(SysUser sysUser) {
// 一些需要调用的别的模块的接口
FileOperatorApi fileOperatorApi = SpringUtil.getBean(FileOperatorApi.class);
FileInfoApi fileInfoApi = SpringUtil.getBean(FileInfoApi.class);
RoleServiceApi roleServiceApi = SpringUtil.getBean(RoleServiceApi.class);
ResourceServiceApi resourceServiceApi = SpringUtil.getBean(ResourceServiceApi.class);
SysUserOrgService sysUserOrgService = SpringUtil.getBean(SysUserOrgService.class);
DataScopeApi dataScopeApi = SpringUtil.getBean(DataScopeApi.class);
AppServiceApi appServiceApi = SpringUtil.getBean(AppServiceApi.class);
// 填充基本信息
LoginUser loginUser = new LoginUser();
BeanUtil.copyProperties(sysUser, loginUser);
Long userId = sysUser.getUserId();
// 填充用户主组织机构id
SysUserOrgResponse userOrgInfo = sysUserOrgService.getUserOrgInfo(userId);
loginUser.setOrganizationId(userOrgInfo.getOrgId());
// 获取头像文件详细信息
SysFileInfoResponse fileInfoWithoutContent = fileInfoApi.getFileInfoWithoutContent(sysUser.getAvatar());
// 填充用户头像url
String fileAuthUrl = fileOperatorApi.getFileAuthUrl(
fileInfoWithoutContent.getFileBucket(),
fileInfoWithoutContent.getFileObjectName(),
FileConfigExpander.getDefaultFileTimeoutSeconds() * 1000);
loginUser.setAvatar(fileAuthUrl);
// 填充管理员转化
loginUser.setSuperAdmin(YesOrNotEnum.Y.getCode().equals(sysUser.getSuperAdminFlag()));
// 填充性别枚举转换
loginUser.setSex(SexEnum.codeToEnum(sysUser.getSex()).getMessage());
// 填充手机号
loginUser.setMobilePhone(sysUser.getPhone());
// 填充用户的数据范围
DataScopeResponse dataScopeResponse = dataScopeApi.getDataScope(userId);
if (loginUser.getSuperAdmin()) {
loginUser.setDataScopeTypes(getSuperAdminDataScopeTypeEnum());
} else {
loginUser.setDataScopeTypes(dataScopeResponse.getDataScopeTypeEnums());
}
loginUser.setOrganizationIdDataScope(dataScopeResponse.getOrganizationIds());
loginUser.setUserIdDataScope(dataScopeResponse.getUserIds());
// 填充用户角色信息
Set<SimpleDict> roleList = getRoleList(dataScopeResponse.getSysRoleResponses());
loginUser.setRoles(roleList);
// 通过用户角色获取角色所有的资源id
List<Long> roleIds = roleList.stream().map(SimpleDict::getId).collect(Collectors.toList());
List<String> resourceIdsList = roleServiceApi.getRoleResourceList(roleIds);
// 根据资源id获取资源的所有详情信息
List<ResourceDefinition> resourceDefinitions = resourceServiceApi.getResourceListByIds(resourceIdsList);
Set<String> resourceUrls = resourceDefinitions.stream().map(ResourceDefinition::getUrl).collect(Collectors.toSet());
// 填充用户的urls
loginUser.setResourceUrls(resourceUrls);
// 用户包含的app信息
Set<String> appCodes = resourceDefinitions.stream().map(ResourceDefinition::getAppCode).collect(Collectors.toSet());
Set<SimpleDict> appsByAppCodes = appServiceApi.getAppsByAppCodes(appCodes);
// 填充应用信息
loginUser.setApps(appsByAppCodes);
// 日志系统问题 默认初始化其他信息 dict
loginUser.setOtherInfos(new Dict());
return loginUser;
}
/**
*
*
* @author fengshuonan
* @date 2020/11/29 19:12
*/
private static Set<SimpleDict> getRoleList(List<SysRoleResponse> sysRoleResponses) {
if (sysRoleResponses == null || sysRoleResponses.isEmpty()) {
return new HashSet<>();
}
Set<SimpleDict> simpleRoles = new HashSet<>();
for (SysRoleResponse sysRoleResponse : sysRoleResponses) {
SimpleDict simpleRole = new SimpleDict();
simpleRole.setId(sysRoleResponse.getRoleId());
simpleRole.setName(sysRoleResponse.getRoleName());
simpleRole.setCode(sysRoleResponse.getRoleCode());
simpleRoles.add(simpleRole);
}
return simpleRoles;
}
/**
*
*
* @author fengshuonan
* @date 2020/12/14 23:12
*/
private static Set<DataScopeTypeEnum> getSuperAdminDataScopeTypeEnum() {
HashSet<DataScopeTypeEnum> dataScopeTypeEnums = new HashSet<>();
dataScopeTypeEnums.add(DataScopeTypeEnum.ALL);
return dataScopeTypeEnums;
}
}

View File

@ -16,7 +16,7 @@ import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest
* @author fengshuonan
* @date 2020/11/21 12:55
*/
public class SysUserFactory {
public class SysUserCreateFactory {
/**
*

View File

@ -0,0 +1,105 @@
package cn.stylefeng.roses.kernel.system.modular.user.factory;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import cn.stylefeng.roses.kernel.auth.api.prop.LoginUserPropExpander;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.pojo.organization.DataScopeResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.SysUserOrgResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.UserLoginInfoDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
*
* @author fengshuonan
* @date 2020/11/26 22:25
*/
public class UserLoginInfoFactory {
/**
*
*
* @param sysUser
* @param roleResponseList
* @param dataScopeResponse
* @param userOrgInfo
* @param resourceUrlsListByCodes url
* @author fengshuonan
* @date 2020/12/26 17:53
*/
public static UserLoginInfoDTO userLoginInfoDTO(SysUser sysUser,
List<SysRoleResponse> roleResponseList,
DataScopeResponse dataScopeResponse,
SysUserOrgResponse userOrgInfo,
Set<String> resourceUrlsListByCodes) {
UserLoginInfoDTO userLoginInfoDTO = new UserLoginInfoDTO();
// 设置用户加密的密码和状态
userLoginInfoDTO.setUserPasswordHexed(sysUser.getPassword());
userLoginInfoDTO.setUserStatus(sysUser.getStatusFlag());
// 创建登录用户对象
LoginUser loginUser = new LoginUser();
// 填充用户账号账号id管理员类型
loginUser.setAccount(sysUser.getAccount());
loginUser.setUserId(sysUser.getUserId());
loginUser.setSuperAdmin(YesOrNotEnum.Y.getCode().equals(sysUser.getSuperAdminFlag()));
// 填充用户基本信息
SimpleUserInfo simpleUserInfo = new SimpleUserInfo();
BeanUtil.copyProperties(sysUser, simpleUserInfo);
loginUser.setSimpleUserInfo(simpleUserInfo);
// 填充用户角色信息
if (!roleResponseList.isEmpty()) {
ArrayList<SimpleRoleInfo> simpleRoleInfos = new ArrayList<>();
for (SysRoleResponse sysRoleResponse : roleResponseList) {
SimpleRoleInfo simpleRoleInfo = new SimpleRoleInfo();
BeanUtil.copyProperties(sysRoleResponse, simpleRoleInfo);
simpleRoleInfos.add(simpleRoleInfo);
}
loginUser.setSimpleRoleInfoList(simpleRoleInfos);
}
// 填充用户公司和职务
if (userOrgInfo != null) {
loginUser.setOrganizationId(userOrgInfo.getOrgId());
loginUser.setPositionId(userOrgInfo.getPositionId());
}
// 设置用户数据范围
if (dataScopeResponse != null) {
loginUser.setDataScopeTypeEnums(dataScopeResponse.getDataScopeTypeEnums());
loginUser.setDataScopeOrganizationIds(dataScopeResponse.getOrganizationIds());
loginUser.setDataScopeUserIds(dataScopeResponse.getUserIds());
}
// 设置用户拥有的资源
loginUser.setResourceUrls(resourceUrlsListByCodes);
// 基于接口拓展用户登录信息
Map<String, LoginUserPropExpander> beansOfLoginUserExpander = SpringUtil.getBeansOfType(LoginUserPropExpander.class);
if (beansOfLoginUserExpander != null && beansOfLoginUserExpander.size() > 0) {
for (Map.Entry<String, LoginUserPropExpander> stringLoginUserPropExpanderEntry : beansOfLoginUserExpander.entrySet()) {
stringLoginUserPropExpanderEntry.getValue().expandAction(loginUser);
}
}
// 响应dto
userLoginInfoDTO.setLoginUser(loginUser);
return userLoginInfoDTO;
}
}

View File

@ -4,6 +4,8 @@ import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* service
*
@ -12,6 +14,15 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface SysUserRoleService extends IService<SysUserRole> {
/**
*
*
* @param userId id
* @author fengshuonan
* @date 2020/12/22 16:52
*/
List<SysUserRole> getUserRoles(Long userId);
/**
*
*
@ -29,4 +40,5 @@ public interface SysUserRoleService extends IService<SysUserRole> {
* @date 2020/11/6 15:03
*/
void deleteUserRoleListByUserId(Long userId);
}

View File

@ -2,6 +2,7 @@ package cn.stylefeng.roses.kernel.system.modular.user.service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.response.SysUserResponse;
@ -16,7 +17,7 @@ import java.util.List;
* @author luojie
* @date 2020/11/6 10:28
*/
public interface SysUserService extends IService<SysUser> {
public interface SysUserService extends IService<SysUser>, UserServiceApi {
/**
*
@ -122,25 +123,12 @@ public interface SysUserService extends IService<SysUser> {
*
*
* @param sysUserRequest
* searchValue
* organizationId id
* statusFlag
* @return
* @author fengshuonan
* @date 2020/11/21 15:24
*/
PageResult<SysUserResponse> page(SysUserRequest sysUserRequest);
/**
*
*
* @param sysUserRequest
* @return id
* @author luojie
* @date 2020/11/6 13:47
*/
List<Long> getUserRoles(SysUserRequest sysUserRequest);
/**
*
*
@ -168,6 +156,6 @@ public interface SysUserService extends IService<SysUser> {
* @author luojie
* @date 2020/11/6 15:09
*/
SysUser getUserByCount(String account);
SysUser getUserByAccount(String account);
}

View File

@ -1,7 +1,6 @@
package cn.stylefeng.roses.kernel.system.modular.user.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.kernel.system.UserOrgServiceApi;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserOrg;
import cn.stylefeng.roses.kernel.system.modular.user.mapper.SysUserOrgMapper;
@ -24,7 +23,7 @@ import static cn.stylefeng.roses.kernel.system.exception.enums.SysUserOrgExcepti
* @date 2020/12/19 22:17
*/
@Service
public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUserOrg> implements SysUserOrgService, UserOrgServiceApi {
public class SysUserOrgServiceServiceImpl extends ServiceImpl<SysUserOrgMapper, SysUserOrg> implements SysUserOrgService {
@Override
@Transactional(rollbackFor = Exception.class)

View File

@ -1,7 +1,7 @@
package cn.stylefeng.roses.kernel.system.modular.user.service.impl;
import cn.stylefeng.roses.kernel.system.DataScopeApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.exception.enums.SysUserExceptionEnum;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.system.modular.user.mapper.SysUserRoleMapper;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest;
@ -11,7 +11,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@ -24,11 +23,19 @@ import java.util.List;
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
@Resource
private RoleServiceApi roleServiceApi;
@Override
public List<SysUserRole> getUserRoles(Long userId) {
LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUserRole::getUserId, userId);
List<SysUserRole> list = this.list(wrapper);
@Resource
private DataScopeApi dataScopeApi;
// 账号下没有绑定角色
if (list.isEmpty()) {
throw new SystemModularException(SysUserExceptionEnum.USER_NOT_BIND_ROLE);
}
return list;
}
@Override
@Transactional(rollbackFor = Exception.class)

View File

@ -4,7 +4,6 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.stylefeng.roses.kernel.auth.api.expander.AuthConfigExpander;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
@ -12,15 +11,17 @@ import cn.stylefeng.roses.kernel.office.api.OfficeExcelApi;
import cn.stylefeng.roses.kernel.office.api.pojo.report.ExcelExportParam;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.pojo.dict.SimpleDict;
import cn.stylefeng.roses.kernel.system.UserServiceApi;
import cn.stylefeng.roses.kernel.system.DataScopeApi;
import cn.stylefeng.roses.kernel.system.ResourceServiceApi;
import cn.stylefeng.roses.kernel.system.RoleServiceApi;
import cn.stylefeng.roses.kernel.system.enums.UserStatusEnum;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.exception.enums.SysUserExceptionEnum;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserDataScope;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUserRole;
import cn.stylefeng.roses.kernel.system.modular.user.factory.LoginUserFactory;
import cn.stylefeng.roses.kernel.system.modular.user.factory.SysUserFactory;
import cn.stylefeng.roses.kernel.system.modular.user.factory.SysUserCreateFactory;
import cn.stylefeng.roses.kernel.system.modular.user.factory.UserLoginInfoFactory;
import cn.stylefeng.roses.kernel.system.modular.user.mapper.SysUserMapper;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.pojo.response.SysUserResponse;
@ -28,6 +29,8 @@ import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserDataScopeSer
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserOrgService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserRoleService;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import cn.stylefeng.roses.kernel.system.pojo.organization.DataScopeResponse;
import cn.stylefeng.roses.kernel.system.pojo.role.response.SysRoleResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.SysUserOrgResponse;
import cn.stylefeng.roses.kernel.system.pojo.user.UserLoginInfoDTO;
import cn.stylefeng.roses.kernel.system.util.DataScopeUtil;
@ -56,7 +59,7 @@ import java.util.stream.Collectors;
*/
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserServiceApi {
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
@Resource
private SysUserOrgService sysUserOrgService;
@ -70,6 +73,15 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
@Resource
private OfficeExcelApi officeExcelApi;
@Resource
private DataScopeApi dataScopeApi;
@Resource
private RoleServiceApi roleServiceApi;
@Resource
private ResourceServiceApi resourceServiceApi;
@Override
@Transactional(rollbackFor = Exception.class)
public void add(SysUserRequest sysUserRequest) {
@ -83,7 +95,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 请求bean转为实体填充一些基本属性
SysUser sysUser = new SysUser();
BeanUtil.copyProperties(sysUserRequest, sysUser);
SysUserFactory.fillAddSysUser(sysUser);
SysUserCreateFactory.fillAddSysUser(sysUser);
// 保存用户
this.save(sysUser);
@ -107,7 +119,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
BeanUtil.copyProperties(sysUserRequest, sysUser);
// 填充基础参数
SysUserFactory.fillEditSysUser(sysUser);
SysUserCreateFactory.fillEditSysUser(sysUser);
this.updateById(sysUser);
Long sysUserId = sysUser.getUserId();
@ -121,7 +133,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
SysUser sysUser = this.querySysUser(sysUserRequest);
// 填充更新用户的信息
SysUserFactory.fillUpdateInfo(sysUserRequest, sysUser);
SysUserCreateFactory.fillUpdateInfo(sysUserRequest, sysUser);
this.updateById(sysUser);
}
@ -280,11 +292,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return PageResultFactory.createPageResult(userPage);
}
@Override
public List<Long> getUserRoles(SysUserRequest sysUserRequest) {
return this.getUserRoleIdList(sysUserRequest.getUserId());
}
@Override
public List<SimpleDict> selector(SysUserRequest sysUserRequest) {
@ -323,31 +330,50 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
@Override
public SysUser getUserByCount(String account) {
public SysUser getUserByAccount(String account) {
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getAccount, account);
queryWrapper.ne(SysUser::getDelFlag, YesOrNotEnum.Y.getCode());
return this.getOne(queryWrapper);
List<SysUser> list = this.list(queryWrapper);
// 用户不存在
if (list.isEmpty()) {
throw new SystemModularException(SysUserExceptionEnum.USER_NOT_EXIST, account);
}
// 账号存在多个
if (list.size() > 1) {
throw new SystemModularException(SysUserExceptionEnum.ACCOUNT_HAVE_MANY, account);
}
return list.get(0);
}
@Override
public UserLoginInfoDTO getUserLoginInfo(String account) {
UserLoginInfoDTO userLoginInfoDTO = new UserLoginInfoDTO();
// 根据账号获取系统用户表中的信息
SysUser sysUser = getUserByCount(account);
LoginUser loginUser = LoginUserFactory.createLoginUser(sysUser);
// 1. 获取用户和账号信息
SysUser sysUser = this.getUserByAccount(account);
Long userId = sysUser.getUserId();
// 获取用户加密的密码,用于登录校验
userLoginInfoDTO.setUserPasswordHexed(sysUser.getPassword());
// 2. 获取用户角色信息
List<SysUserRole> userRoles = sysUserRoleService.getUserRoles(userId);
List<Long> roleIds = userRoles.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
List<SysRoleResponse> roleResponseList = roleServiceApi.getRolesByIds(roleIds);
// 填充用户状态
userLoginInfoDTO.setUserStatus(sysUser.getStatusFlag());
// 3. 获取用户的数据范围
DataScopeResponse dataScopeResponse = dataScopeApi.getDataScope(userId, roleResponseList);
// 设置用户登录详情信息
userLoginInfoDTO.setLoginUser(loginUser);
// 4. 获取用户的组织机构和职位信息
SysUserOrgResponse userOrgInfo = sysUserOrgService.getUserOrgInfo(userId);
return userLoginInfoDTO;
// 5. 获取用户的所有资源url
List<String> resourceCodeList = roleServiceApi.getRoleResourceCodeList(roleIds);
Set<String> resourceUrlsListByCodes = resourceServiceApi.getResourceUrlsListByCodes(resourceCodeList);
// 5. 组装响应结果
return UserLoginInfoFactory.userLoginInfoDTO(sysUser, roleResponseList, dataScopeResponse, userOrgInfo, resourceUrlsListByCodes);
}
@Transactional(rollbackFor = Exception.class)
@ -413,7 +439,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
private SysUser querySysUser(SysUserRequest sysUserRequest) {
SysUser sysUser = this.getById(sysUserRequest.getUserId());
if (ObjectUtil.isNull(sysUser)) {
throw new SystemModularException(SysUserExceptionEnum.USER_NOT_EXIST);
throw new SystemModularException(SysUserExceptionEnum.USER_NOT_EXIST, sysUserRequest.getUserId());
}
return sysUser;
}