作者 唐俊升

组织机构token持久化

... ... @@ -242,6 +242,11 @@
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.14</version>
</dependency>
</dependencies>
<dependencyManagement>
... ...
... ... @@ -7,6 +7,7 @@ import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.model.RoleDataPermission;
import com.tianbo.warehouse.model.RolePermission;
import com.tianbo.warehouse.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
... ... @@ -121,4 +122,19 @@ public class RoleController {
return resultJson;
}
// @LogAnnotation(moduleName = "岗位/角色管理", operate = "添加组织机构token")
@PostMapping("/createRoleToken")
public ResultJson createRoleToken(@RequestParam(value = "roleId") Integer roleId){
String roleToken = roleService.createRoleToken(roleId);
if (StringUtils.isNotBlank(roleToken)){
return new ResultJson("200", "success", roleToken);
}
return new ResultJson("201","error");
}
@PostMapping("/permRoleToken")
public ResultJson permRoleToken(@RequestBody RolePermission rolePermission){
return roleService.permRoleToken(rolePermission);
}
}
... ...
... ... @@ -44,4 +44,6 @@ public interface PERMISSIONMapper {
List<PERMISSION> getUserMenuTreeByUserId(@Param("userId") Integer userId);
List<PERMISSION> getChildMenu(Integer parentId);
List<PERMISSION> selectByIds(List<Integer> permissionIds);
}
... ...
... ... @@ -46,4 +46,7 @@ public interface ROLEMapper {
int updateByPrimaryKeySelective(ROLE record);
int updateByPrimaryKey(ROLE record);
int updateTokenById(@Param("roleId") Integer roleId,
@Param("token") String token);
}
... ...
... ... @@ -36,6 +36,9 @@ public class ROLE implements GrantedAuthority {
private String orgtype;
// 组织机构持久token
private String ext;
private List<ROLE> children;
public Integer getRoleId() {
... ...
... ... @@ -56,4 +56,6 @@ public interface PermissionService {
* @return 有为true 没有为false
*/
Boolean getPermission(String token,String url,String name);
List<PERMISSION> selectByIds(List<Integer> permissionIds);
}
... ...
package com.tianbo.warehouse.service;
import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.model.RoleDataPermission;
import com.tianbo.warehouse.model.RolePermission;
... ... @@ -25,4 +26,8 @@ public interface RoleService {
int updateByPrimaryKeySelective(ROLE role);
int deleteByPrimaryKey(Integer departmentId);
String createRoleToken(Integer roleId);
ResultJson permRoleToken(RolePermission rolePermission);
}
... ...
package com.tianbo.warehouse.service.imp;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
... ... @@ -268,4 +269,12 @@ public class PermissionServiceImp implements PermissionService {
return false;
}
@Override
public List<PERMISSION> selectByIds(List<Integer> permissionIds) {
List<PERMISSION> permissionList = new ArrayList<>();
if (ArrayUtil.isNotEmpty(permissionIds)){
permissionList = permissionMapper.selectByIds(permissionIds);
}
return permissionList;
}
}
... ...
package com.tianbo.warehouse.service.imp;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheDelTarget;
import com.tianbo.warehouse.controller.response.ResultJson;
import com.tianbo.warehouse.dao.DepartmentMapper;
import com.tianbo.warehouse.dao.ROLEMapper;
import com.tianbo.warehouse.dao.RoleDataPermissionDao;
import com.tianbo.warehouse.dao.RolePermissionMapper;
import com.tianbo.warehouse.model.Department;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.model.RoleDataPermission;
import com.tianbo.warehouse.model.RolePermission;
import com.tianbo.warehouse.model.*;
import com.tianbo.warehouse.service.PermissionService;
import com.tianbo.warehouse.service.RoleService;
import com.tianbo.warehouse.util.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service(value = "roleService")
... ... @@ -41,6 +46,11 @@ public class RoleServiceImp implements RoleService{
@Autowired
PermissionService permissionService;
@Autowired
RedisUtils redisUtils;
private final String ROLE_TOKEN_HEAD = "TOKEN:ROLE:";
/**
*
* @param pageNum
... ... @@ -182,6 +192,41 @@ public class RoleServiceImp implements RoleService{
}
}
@Override
public String createRoleToken(Integer roleId) {
ROLE role = roleMapper.selectByPrimaryKey(roleId);
String oldTokenId = role.getExt();
if (StringUtils.isNotBlank(oldTokenId)) {
String oldToken = ROLE_TOKEN_HEAD + oldTokenId;
redisUtils.del(oldToken);
}
String tokenId = IdUtil.simpleUUID();
String token = ROLE_TOKEN_HEAD + tokenId;
int i = roleMapper.updateTokenById(roleId, token);
if (i > 0){
return token;
}
return null;
}
@Override
public ResultJson permRoleToken(RolePermission rolePermission) {
ROLE role = roleMapper.selectByPrimaryKey(rolePermission.getRoleId());
String token = role.getExt();
if (StringUtils.isBlank(token)) {
return new ResultJson("201", "请先申请组织机构令牌");
}
List<PERMISSION> permissionList = permissionService.selectByIds(rolePermission.getPermissionIds());
USERS users = new USERS();
users.setCompanyInfo(role);
users.setPermissions(permissionList);
String tokenValue = JSON.toJSONString(users);
boolean redisSava = redisUtils.set(token, tokenValue, -1);
if (redisSava){
return new ResultJson("200", "权限设置成功");
}
return new ResultJson("202", "系统错误,请稍后重试");
}
//递归查找所有菜单的子菜单
private List<ROLE> getChildrens(ROLE root, List<ROLE> listAll) {
... ...
... ... @@ -380,4 +380,16 @@ where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order
ORDER BY
permission_order
</select>
<select id="selectByIds" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM
permission
<where>
permission_id IN
<foreach collection="permissionIds" item="id" open="(" close=")" separator=",">
#{id,jdbcType=INTEGER}
</foreach>
</where>
</select>
</mapper>
... ...
... ... @@ -14,6 +14,7 @@
<result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
<result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
<result column="org_type" jdbcType="VARCHAR" property="orgtype" />
<result column="ext" jdbcType="VARCHAR" property="ext"/>
</resultMap>
<resultMap id="TreeSqlWithPermResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap">
<collection column="role_id" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions" select="com.tianbo.warehouse.dao.PERMISSIONMapper.getRolePermisson" />
... ... @@ -41,7 +42,7 @@
</resultMap>
<sql id="Base_Column_List">
role_id, role_name, role_sign, description, `type`, parentId, rsort, customs_reg_code,
business_license, departmentId, mq_code, org_type
business_license, departmentId, mq_code, org_type, ext
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
... ... @@ -231,6 +232,11 @@ WHERE
org_type = #{orgtype,jdbcType=VARCHAR}
where role_id = #{roleId,jdbcType=INTEGER}
</update>
<update id="updateTokenById">
update role
set ext = #{token,jdbcType=VARCHAR}
where role_id = #{roleId,jdbcType=INTEGER}
</update>
<select id="findAll" parameterType="java.lang.String" resultMap="TreeSqlWithPermResultMap">
SELECT
... ...