Browse Source

权限管理类

tags/v1.0.0^2
chengwang 1 year ago
parent
commit
9e7949fa27
7 changed files with 121 additions and 5 deletions
  1. +52
    -3
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/PermissionController.java
  2. +17
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/GetPermissionRoleIdListByApiUrlDto.java
  3. +2
    -2
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/OpRolePermission.java
  4. +2
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/OpPermissionsMapper.java
  5. +5
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/permissions/OpPermissionsService.java
  6. +36
    -0
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/permissions/OpPermissionsServiceImpl.java
  7. +7
    -0
      tuoheng-service/tuoheng-admin/src/main/resources/mapper/OpPermissionsMapper.xml

+ 52
- 3
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/controller/PermissionController.java View File

@@ -1,13 +1,13 @@
package com.tuoheng.admin.controller;

import com.tuoheng.admin.dto.GetPermissionRoleIdListByApiUrlDto;
import com.tuoheng.admin.entity.OpPermissions;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.service.permissions.OpPermissionsService;
import com.tuoheng.common.core.common.BaseController;
import com.tuoheng.common.core.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* @Author ChengWang
@@ -21,6 +21,8 @@ public class PermissionController{
@Autowired
private OpPermissionsService permissionService;



/**
* 根据 RoleId 获取用户权限
*
@@ -30,4 +32,51 @@ public class PermissionController{
public JsonResult getMenuAndPermissionByRoleId(RoleClientQuery query) {
return permissionService.getRolePermissionByRoleId(query);
}

/**
* 根据ApiUrl获取角色集合
*
* @return
*/
@PostMapping("/getRoleIdList")
public JsonResult getRoleIdListByApiUrl(@RequestBody GetPermissionRoleIdListByApiUrlDto getPermissionRoleIdListByApiUrlDto) {
return permissionService.getRoleIdListByApiUrl(getPermissionRoleIdListByApiUrlDto.getApiUrl());
}

/**
* 添加权限(按钮)
*
* @param opPermissions
* @return
*/
@PostMapping("/add")
public JsonResult add(@RequestBody OpPermissions opPermissions) {
return permissionService.edit(opPermissions);
}

/**
* 编辑权限(按钮)
*
* @param opPermissions
* @return
*/
@PutMapping("/edit")
public JsonResult edit(@RequestBody OpPermissions opPermissions) {
return permissionService.edit(opPermissions);
}

/**
* 删除权限按钮(更新)
* @param id
* @return
*/
@DeleteMapping("/delete/{id}")
public JsonResult delete(@PathVariable("id") Integer id) {
boolean flag = permissionService.update().eq("id",id).set("mark", 0).update();
if (flag) {
return JsonResult.success();
} else {
return JsonResult.error();
}
}
}

+ 17
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/dto/GetPermissionRoleIdListByApiUrlDto.java View File

@@ -0,0 +1,17 @@
package com.tuoheng.admin.dto;

import lombok.Data;

/**
* @author chenjiandong
* @description: TODO
* @date 2022/11/17 13:21
*/
@Data
public class GetPermissionRoleIdListByApiUrlDto {

private String apiUrl;

private String method;

}

+ 2
- 2
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/entity/OpRolePermission.java View File

@@ -28,11 +28,11 @@ public class OpRolePermission{
/**
* 角色id
*/
private String roleId;
private Integer roleId;
/**
* 权限id
*/
private String permissionId;
private Integer permissionId;

@TableField(exist = false)
private static final long serialVersionUID = 1L;

+ 2
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/mapper/OpPermissionsMapper.java View File

@@ -12,4 +12,6 @@ import java.util.List;
*/
public interface OpPermissionsMapper extends BaseMapper<OpPermissions> {
List<OpPermissions> getOpPermissionsByRoleId(@Param("roleId") Integer roleId,@Param("clientId") String clientId);

List<Integer> getRoleIdsByApiUrl(@Param("apiUrl") String apiUrl);
}

+ 5
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/permissions/OpPermissionsService.java View File

@@ -11,4 +11,9 @@ import com.tuoheng.common.core.utils.JsonResult;
*/
public interface OpPermissionsService extends IService<OpPermissions> {
JsonResult getRolePermissionByRoleId(RoleClientQuery query);

JsonResult getRoleIdListByApiUrl(String apiUrl);

JsonResult edit(OpPermissions opPermissions);

}

+ 36
- 0
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/service/permissions/OpPermissionsServiceImpl.java View File

@@ -5,16 +5,21 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuoheng.admin.dto.RolePermissionDto;
import com.tuoheng.admin.entity.Menu;
import com.tuoheng.admin.entity.OpPermissions;
import com.tuoheng.admin.entity.OpRolePermission;
import com.tuoheng.admin.entity.RoleMenu;
import com.tuoheng.admin.mapper.MenuMapper;
import com.tuoheng.admin.mapper.OpPermissionsMapper;
import com.tuoheng.admin.mapper.OpRolePermissionMapper;
import com.tuoheng.admin.mapper.RoleMenuMapper;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
@@ -37,6 +42,9 @@ public class OpPermissionsServiceImpl extends ServiceImpl<OpPermissionsMapper, O
@Autowired
private RoleMenuMapper roleMenuMapper;

@Autowired
private OpRolePermissionMapper opRolePermissionMapper;


@Override
public JsonResult getRolePermissionByRoleId(RoleClientQuery query) {
@@ -54,7 +62,35 @@ public class OpPermissionsServiceImpl extends ServiceImpl<OpPermissionsMapper, O
return JsonResult.success(rolePermissionDto);
}

@Override
@Transactional(readOnly = true)
public JsonResult getRoleIdListByApiUrl(String apiUrl) {
//根据接口url查询出所属的角色
List<Integer> roleIdsByApiUrl = opPermissionsMapper.getRoleIdsByApiUrl(apiUrl);
return JsonResult.success(roleIdsByApiUrl);
}

@Override
public JsonResult edit(OpPermissions opPermissions) {
Integer opPermissionsId = opPermissions.getId();
//添加
if (null == opPermissionsId) {
//默认
opPermissions.setCreateUser(CurrentUserUtil.getUserId());
opPermissions.setCreateTime(DateUtils.now());
opPermissionsMapper.insert(opPermissions);
//添加对应创建权限时的关联关系
OpRolePermission opRolePermission = new OpRolePermission();
opRolePermission.setRoleId(CurrentUserUtil.getUserInfo().getRoleId());
opRolePermission.setPermissionId(opPermissions.getId());
opRolePermissionMapper.insert(opRolePermission);
} else {
opPermissions.setUpdateUser(CurrentUserUtil.getUserId());
opPermissions.setUpdateTime(DateUtils.now());
opPermissionsMapper.updateById(opPermissions);
}
return JsonResult.success();
}


/**

+ 7
- 0
tuoheng-service/tuoheng-admin/src/main/resources/mapper/OpPermissionsMapper.xml View File

@@ -39,4 +39,11 @@
</if>
and id in (select permission_id from op_role_permission where role_id = #{roleId} and mark = 1)
</select>
<!--根据接口url获取所属角色-->
<select id="getRoleIdsByApiUrl" parameterType="java.lang.String" resultType="java.lang.Integer">
select role_id
from op_role_permission
where mark = 1
and permission_id in (select id from op_permissions where api_url = #{apiUrl} and mark = 1)
</select>
</mapper>

Loading…
Cancel
Save