RBAC权限CRUD和Controller API

This commit is contained in:
myh 2023-11-07 23:44:33 +08:00
parent 74976ade9c
commit 8f1ee8896b
12 changed files with 387 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.RBAC.Permissions;
import com.example.springdemo.service.PermissionsService;
import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/permissions")
@RoleVerificationAnnotation(UserIDList = {1})
public class PermissionsController {
@Resource
private PermissionsService permissionsService;
@PostMapping("/add")
public Permissions addPermissions(@RequestBody Permissions permissions) {
return permissionsService.createPermissions(permissions);
}
@DeleteMapping("/delete")
public void deletePermissions(@RequestParam("id") Long permissionsID) {
permissionsService.deletePermissions(permissionsID);
}
@PutMapping("/update")
public Permissions updatePermissions(@RequestBody Permissions permissions) {
return permissionsService.updatePermissions(permissions);
}
@GetMapping("/get")
public Iterable<Permissions> getAllPermissions() {
return permissionsService.getAllPermissions();
}
@GetMapping("/get")
public Permissions getPermissions(@RequestParam Long permissionsID) {
return permissionsService.getPermissions(permissionsID);
}
}

View File

@ -0,0 +1,45 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.service.RolesService;
import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@RoleVerificationAnnotation(UserIDList = {1})
@RestController
@RequestMapping("/roles")
public class RolesController {
@Resource
private RolesService rolesService;
@PostMapping("/add")
public Roles addRoles(@RequestBody Roles roles) {
return rolesService.createRoles(roles);
}
@DeleteMapping("/delete")
public void deleteRoles(@RequestParam("id") Long rolesID) {
rolesService.deleteRoles(rolesID);
}
@PutMapping("/update")
public Roles updateRoles(@RequestBody Roles roles) {
return rolesService.updateRoles(roles);
}
@GetMapping("/get")
public Iterable<Roles> getAllRoles() {
return rolesService.getAllRoles();
}
@GetMapping("/get")
public Roles getRoles(@RequestParam Long rolesID) {
return rolesService.getRolesById(rolesID);
}
@GetMapping("/get")
public Roles getRoles(@RequestParam String rolesName) {
return rolesService.getRolesByName(rolesName);
}
}

View File

@ -0,0 +1,44 @@
package com.example.springdemo.controller;
import com.example.springdemo.entities.RBAC.RolesPermissions;
import com.example.springdemo.service.RolesPermissionsService;
import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
@RoleVerificationAnnotation(UserIDList = {1})
@RestController
@RequestMapping("/rolesPermissions")
public class RolesPermissionsController {
@Resource
private RolesPermissionsService rolesPermissionsService;
@PostMapping("/assign")
public RolesPermissions assignPermissionsToRole(@RequestBody RolesPermissions rolesPermissions) {
return rolesPermissionsService.AssignPermissionsToRole(rolesPermissions);
}
@DeleteMapping("/remove")
public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId)) {
return "remove success";
} else {
return "remove failed, you can't update or delete admin role";
}
}
@PutMapping("/update")
public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.updateRolePermissions(roleId, permissionId)) {
return "update success";
} else {
return "update failed, you can't update or delete admin role";
}
}
@GetMapping("/find")
public RolesPermissions getRolePermissionById(@RequestParam(name = "rolesPermissionsId") Long rolesPermissionsId) {
return rolesPermissionsService.getRolePermissionById(rolesPermissionsId);
}
}

View File

@ -0,0 +1,10 @@
package com.example.springdemo.dao;
import com.example.springdemo.entities.RBAC.Permissions;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PermissionsRepository
extends JpaRepository<Permissions, Long> {
}

View File

@ -0,0 +1,25 @@
package com.example.springdemo.dao;
import com.example.springdemo.entities.RBAC.RolesPermissions;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public interface RolesPermissionsRepository
extends JpaRepository<RolesPermissions, Long> {
@Modifying
@Transactional
@Query(value = "delete from RolesPermissions " +
"where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true)
void RemovePermissionsFromRole(Long roleId, Long permissionId);
@Modifying
@Transactional
@Query(value = "update RolesPermissions set RolesID = ?1, PermissionsID = ?2 " +
"where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true)
void updateRolePermissions(Long roleId, Long permissionId);
}

View File

@ -0,0 +1,12 @@
package com.example.springdemo.dao;
import com.example.springdemo.entities.RBAC.Roles;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RolesRepository
extends JpaRepository<Roles, Long> {
Roles findByName(String name);
}

View File

@ -0,0 +1,16 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Permissions;
public interface PermissionsService {
Permissions createPermissions(Permissions permissions);
Permissions updatePermissions(Permissions permissions);
void deletePermissions(Long id);
Permissions getPermissions(Long id);
Iterable<Permissions> getAllPermissions();
}

View File

@ -0,0 +1,38 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.PermissionsRepository;
import com.example.springdemo.entities.RBAC.Permissions;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class PermissionsServiceImpl implements PermissionsService {
@Resource
private PermissionsRepository permissionsRepository;
@Override
public Permissions createPermissions(Permissions permissions) {
return permissionsRepository.save(permissions);
}
@Override
public Permissions updatePermissions(Permissions permissions) {
return permissionsRepository.save(permissions);
}
@Override
public void deletePermissions(Long permissionsID) {
permissionsRepository.deleteById(permissionsID);
}
@Override
public Permissions getPermissions(Long permissionsID) {
return permissionsRepository.findById(permissionsID).orElse(null);
}
@Override
public Iterable<Permissions> getAllPermissions() {
return permissionsRepository.findAll();
}
}

View File

@ -0,0 +1,18 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.RolesPermissions;
import com.example.springdemo.entities.Users;
public interface RolesPermissionsService {
RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions);
Boolean RemovePermissionsFromRole(Long roleId, Long permissionId);
Boolean updateRolePermissions(Long roleId, Long permissionId);
RolesPermissions getRolePermissionById(Long rolesPermissionsId);
Iterable<RolesPermissions> getAllRolesPermissions();
Boolean CheckIfDeleteOrUpdateAdmin(Long roleId);
}

View File

@ -0,0 +1,54 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.entities.RBAC.RolesPermissions;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class RolesPermissionsServiceImpl implements RolesPermissionsService {
@Resource
private RolesPermissionsRepository rolesPermissionsRepository;
@Override
public RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions) {
return rolesPermissionsRepository.save(rolesPermissions);
}
@Override
public Boolean RemovePermissionsFromRole(Long roleId, Long permissionId) {
if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId);
return true;
} else {
return false;
}
}
@Override
public Boolean updateRolePermissions(Long roleId, Long permissionId) {
if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.updateRolePermissions(roleId, permissionId);
return true;
} else {
return false;
}
}
@Override
public RolesPermissions getRolePermissionById(Long rolesPermissionsId) {
return rolesPermissionsRepository.findById(rolesPermissionsId).orElse(null);
}
//TODO: 查询全部权限和角色的对应关系
@Override
public Iterable<RolesPermissions> getAllRolesPermissions() {
return null;
}
@Override
public Boolean CheckIfDeleteOrUpdateAdmin(Long roleId) {
return roleId != 1;
}
}

View File

@ -0,0 +1,25 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.entities.Users;
public interface RolesService {
Roles createRoles(Roles roles);
Roles updateRoles(Roles roles);
void deleteRoles(Long id);
Iterable<Roles> getAllRoles();
Roles getRolesByName(String name);
Roles getRolesById(Long id);
//Users角色管理
public void RemoveRoleFromUser(Long userId, Long roleId);
public Users AssignRoleToUser(Long userId, Long roleId);
public Users getUserRoleById(Long userId);
}

View File

@ -0,0 +1,60 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.RolesRepository;
import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.entities.Users;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class RolesServiceImpl implements RolesService {
@Resource
private RolesRepository rolesRepository;
@Override
public Roles createRoles(Roles roles) {
return rolesRepository.save(roles);
}
@Override
public Roles updateRoles(Roles roles) {
return rolesRepository.save(roles);
}
@Override
public void deleteRoles(Long id) {
rolesRepository.deleteById(id);
}
@Override
public Iterable<Roles> getAllRoles() {
return rolesRepository.findAll();
}
@Override
public Roles getRolesByName(String name) {
return rolesRepository.findByName(name);
}
@Override
public Roles getRolesById(Long id) {
return rolesRepository.findById(id).orElse(null);
}
//TODO: Users角色管理
@Override
public void RemoveRoleFromUser(Long userId, Long roleId) {
}
@Override
public Users AssignRoleToUser(Long userId, Long roleId) {
return null;
}
@Override
public Users getUserRoleById(Long userId) {
return null;
}
}