统一变量命名,逻辑功能完善

This commit is contained in:
myh 2023-11-12 16:47:52 +08:00
parent 9a0f860092
commit 587ac7bb45
21 changed files with 304 additions and 188 deletions

View File

@ -6,6 +6,8 @@ import com.example.springdemo.service.MerchantsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
@RequestMapping("/merchants")
public class MerchantsController {
@ -13,19 +15,19 @@ public class MerchantsController {
private MerchantsService merchantsService;
@PostMapping("/add")
public Merchants addMerchants(@RequestBody Merchants merchants,
@RequestBody MerchantsPassword merchantsPassword) {
return merchantsService.addMerchants(merchants, merchantsPassword);
public Merchants addMerchants(@RequestBody Merchants merchant,
@RequestBody MerchantsPassword merchantPassword) {
return merchantsService.addMerchant(merchant, merchantPassword);
}
@DeleteMapping("/delete/{id}")
public void deleteMerchants(@PathVariable("id") Long merchantID) {
merchantsService.deleteMerchantsById(merchantID);
public void deleteMerchants(@PathVariable("id") Long merchantId) {
merchantsService.deleteMerchantById(merchantId);
}
@PutMapping("/update/info")
public Merchants updateMerchants(@RequestBody Merchants merchants) {
return merchantsService.updateMerchants(merchants);
public Merchants updateMerchants(@RequestBody Merchants merchant) {
return merchantsService.updateMerchant(merchant);
}
@GetMapping("/find")
@ -34,17 +36,17 @@ public class MerchantsController {
}
@GetMapping("/find/{name}")
public Merchants getMerchantsByName(@PathVariable("name") String name) {
public Optional<Merchants> getMerchantsByName(@PathVariable("name") String name) {
return merchantsService.findByName(name);
}
@GetMapping("/find/{id}")
public Merchants getMerchantsById(@PathVariable("id") Long merchantID) {
return merchantsService.findById(merchantID);
public Optional<Merchants> getMerchantsById(@PathVariable("id") Long merchantId) {
return merchantsService.findById(merchantId);
}
@PutMapping("/update/password")
public void updatePassword(@RequestParam String password, @RequestParam Long merchantID) {
merchantsService.updatePassword(password, merchantID);
public int updatePassword(@RequestParam String password, @RequestParam Long merchantId) {
return merchantsService.updateMerchantPasswordById(password, merchantId);
}
}

View File

@ -6,36 +6,38 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/permissions")
@RoleVerificationAnnotation(UserIDList = {1})
//FIXME: Ambiguous mapping. Cannot map 'permissionsController' method
public class PermissionsController {
@Resource
private PermissionsService permissionsService;
@PostMapping("/add")
public Permissions addPermissions(@RequestBody Permissions permissions) {
return permissionsService.createPermissions(permissions);
public Permissions addPermissions(@RequestBody Permissions permission) {
return permissionsService.addPermission(permission);
}
@DeleteMapping("/delete")
public void deletePermissions(@RequestParam("id") Long permissionsID) {
permissionsService.deletePermissions(permissionsID);
@DeleteMapping("/delete/{id}")
public void deletePermissions(@PathVariable("id") Long permissionId) {
permissionsService.deletePermissionById(permissionId);
}
@PutMapping("/update")
public Permissions updatePermissions(@RequestBody Permissions permissions) {
return permissionsService.updatePermissions(permissions);
public Permissions updatePermissions(@RequestBody Permissions permission) {
return permissionsService.updatePermission(permission);
}
@GetMapping("/get")
public Iterable<Permissions> getAllPermissions() {
return permissionsService.getAllPermissions();
@GetMapping("/find")
public List<Permissions> getPermissions() {
return permissionsService.findAllPermissions();
}
@GetMapping("/get")
public Permissions getPermissions(@RequestParam Long permissionsID) {
return permissionsService.getPermissions(permissionsID);
@GetMapping("/find/{permissionsId}")
public Optional<Permissions> getPermissionsById(@PathVariable Long permissionsId) {
return permissionsService.findById(permissionsId);
}
}

View File

@ -6,41 +6,43 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RoleVerificationAnnotation(UserIDList = {1})
@RestController
@RequestMapping("/roles")
//FIXME: Ambiguous mapping. Cannot map 'RolesController' method
public class RolesController {
@Resource
private RolesService rolesService;
@PostMapping("/add")
public Roles addRoles(@RequestBody Roles roles) {
return rolesService.createRoles(roles);
public Roles addRole(@RequestBody Roles role) {
return rolesService.createRole(role);
}
@DeleteMapping("/delete")
public void deleteRoles(@RequestParam("id") Long rolesID) {
rolesService.deleteRoles(rolesID);
@DeleteMapping("/delete/{id}")
public void deleteRole(@PathVariable("id") Long roleId) {
rolesService.deleteRoleById(roleId);
}
@PutMapping("/update")
public Roles updateRoles(@RequestBody Roles roles) {
return rolesService.updateRoles(roles);
public Roles updateRole(@RequestBody Roles role) {
return rolesService.updateRole(role);
}
@GetMapping("/get")
public Iterable<Roles> getAllRoles() {
return rolesService.getAllRoles();
@GetMapping("/find")
public List<Roles> getAllRoles() {
return rolesService.findAllRoles();
}
@GetMapping("/get")
public Roles getRoles(@RequestParam Long rolesID) {
return rolesService.getRolesById(rolesID);
@GetMapping("/find/{roleId}")
public Optional<Roles> getRolesById(@PathVariable Long roleId) {
return rolesService.findById(roleId);
}
@GetMapping("/get")
public Roles getRoles(@RequestParam String rolesName) {
return rolesService.getRolesByName(rolesName);
@GetMapping("/find/{roleName}")
public Optional<Roles> getRolesByName(@PathVariable String roleName) {
return rolesService.findByName(roleName);
}
}

View File

@ -6,6 +6,9 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RoleVerificationAnnotation(UserIDList = {1})
@RestController
@RequestMapping("/rolesPermissions")
@ -19,8 +22,9 @@ public class RolesPermissionsController {
}
@DeleteMapping("/remove")
public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId)) {
public String removePermissionsFromRole(@RequestParam(name = "roleId") Long roleId,
@RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.RemovePermissionsFromRole(roleId, permissionId) != 0) {
return "remove success";
} else {
return "remove failed, you can't update or delete admin role";
@ -28,8 +32,9 @@ public class RolesPermissionsController {
}
@PutMapping("/update")
public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId, @RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.updateRolePermissions(roleId, permissionId)) {
public String updateRolePermissions(@RequestParam(name = "roleId") Long roleId,
@RequestParam(name = "permissionId") Long permissionId) {
if (rolesPermissionsService.updateRolePermissions(roleId, permissionId) != 0) {
return "update success";
} else {
return "update failed, you can't update or delete admin role";
@ -37,8 +42,13 @@ public class RolesPermissionsController {
}
@GetMapping("/find")
public RolesPermissions getRolePermissionById(@RequestParam(name = "rolesPermissionsId") Long rolesPermissionsId) {
return rolesPermissionsService.getRolePermissionById(rolesPermissionsId);
public List<RolesPermissions> findAllRolesPermissions() {
return rolesPermissionsService.findAllRolesPermissions();
}
@GetMapping("/find/{rolesPermissionsId}")
public Optional<RolesPermissions> getRolePermissionById(@PathVariable(name = "rolesPermissionsId") Long rolesPermissionsId) {
return rolesPermissionsService.findById(rolesPermissionsId);
}
}

View File

@ -7,6 +7,7 @@ import com.example.springdemo.utils.RoleVerificationAnnotation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@ -19,59 +20,58 @@ public class UsersController {
//添加新用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@PostMapping("/add")
public Users addUsers(@RequestBody UsersPassword usersPassword,
@RequestBody Users users) {
return usersService.addUsers(users, usersPassword);
public Users addUsers(@RequestBody UsersPassword userPassword,
@RequestBody Users user) {
return usersService.addUser(user, userPassword);
}
//通过ID删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete/{userID}")
public void deleteUsers(@PathVariable(name = "userID") Long userID) {
usersService.deleteUsersById(userID);
@DeleteMapping("/delete/{id}")
public void deleteUserById(@PathVariable(name = "id") Long userId) {
usersService.deleteUserById(userId);
}
//通过姓名删除用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@DeleteMapping("/delete/{name}")
public void deleteUsers(@PathVariable(name = "name") String name) {
usersService.deleteUsersByName(name);
public void deleteUserByName(@PathVariable(name = "name") String name) {
usersService.deleteUserByName(name);
}
//更新用户信息
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/info")
public Users updateUsers(@RequestBody Users users) {
return usersService.updateUsers(users);
public Users updateUser(@RequestBody Users user) {
return usersService.updateUser(user);
}
//查找全部用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find")
public Iterable<Users> getUsers() {
public List<Users> getUsers() {
return usersService.findAllUsers();
}
//根据姓名查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find/{name}")
public Users getUsersByName(@PathVariable(name = "name") String name) {
public Optional<Users> getUsersByName(@PathVariable(name = "name") String name) {
return usersService.findByName(name);
}
//根据ID查找用户
@RoleVerificationAnnotation(RoleIDList = {1}, UserIDList = {1})
@GetMapping("/find/{id}")
public Users getUsersById(@PathVariable(name = "id") Long userID) {
return usersService.findById(userID);
public Optional<Users> getUsersById(@PathVariable(name = "id") Long userId) {
return usersService.findById(userId);
}
//更新用户密码
@RoleVerificationAnnotation(RoleIDList = {1, 3}, UserIDList = {1})
@PutMapping("/update/password")
public void updatePassword(@RequestParam(name = "password") String password,
@RequestParam(name = "userID") Long userID) {
usersService.updatePassword(password, userID);
public int updatePassword(@RequestParam(name = "password") String password,
@RequestParam(name = "userId") Long userId) {
return usersService.updateUserPasswordById(password, userId);
}
}

View File

@ -3,16 +3,17 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.password.MerchantsPassword;
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;
@Repository
public interface MerchantsPasswordRepository
extends JpaRepository<MerchantsPassword, Long> {
@Modifying
@Query("update MerchantsPassword MerchantsPwd set MerchantsPwd.password = ?1 where MerchantsPwd.merchants.id = ?2")
void updatePassword(String password, Long merchantID);
int updatePassword(String password, Long merchantID);
//删除账户时删除密码
@Query("delete from MerchantsPassword MerchantsPwd where MerchantsPwd.merchants.id = ?1")
void deletePasswordById(Long merchantID);
void deleteByMerchantsId(Long merchantID);
}

View File

@ -4,9 +4,11 @@ import com.example.springdemo.entities.Merchants;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface MerchantsRepository
extends JpaRepository<Merchants, Long> {
Merchants findByName(String name);
Optional<Merchants> findByName(String name);
}

View File

@ -7,19 +7,26 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
@Repository
public interface RolesPermissionsRepository
extends JpaRepository<RolesPermissions, Long> {
@Modifying
@Query(value = "delete from roles_permissions " +
"where rolesId = ?1 and permissionsId = ?2", nativeQuery = true)
int RemovePermissionsFromRole(Long roleId, Long permissionId);
@Modifying
@Transactional
@Query(value = "delete from RolesPermissions " +
"where RolesID = ?1 and PermissionsID = ?2", nativeQuery = true)
void RemovePermissionsFromRole(Long roleId, Long permissionId);
@Query(value = "update roles_permissions set rolesId = ?1, permissionsId = ?2 " +
"where rolesId = ?1 and permissionsId = ?2", nativeQuery = true)
int updateRolePermissions(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);
Optional<RolesPermissions> findByRolesId(Long roleId);
Optional<RolesPermissions> findByPermissionsId(Long permissionId);
void deleteByRolesId(Long roleId);
void deleteByPermissionsId(Long permissionId);
}

View File

@ -4,9 +4,11 @@ import com.example.springdemo.entities.RBAC.Roles;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface RolesRepository
extends JpaRepository<Roles, Long> {
Roles findByName(String name);
Optional<Roles> findByName(String name);
}

View File

@ -2,16 +2,17 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.password.UsersPassword;
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;
@Repository
public interface UsersPasswordRepository
extends JpaRepository<UsersPassword, Long> {
@Modifying
@Query("update UsersPassword UsersPwd set UsersPwd.password = ?1 where UsersPwd.users.id = ?2")
void updatePassword(String password, Long userID);
int updatePassword(String password, Long userID);
//删除账户时删除密码
@Query("delete from UsersPassword UsersPwd where UsersPwd.users.id = ?1")
void deletePasswordById(Long userID);
void deleteByUsersId(Long userID);
}

View File

@ -2,12 +2,16 @@ package com.example.springdemo.dao;
import com.example.springdemo.entities.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface UsersRepository
extends JpaRepository<Users, Long> {
Users findByName(String name);
Optional<Users> findByName(String name);
void deleteByName(String name);
}

View File

@ -1,22 +1,28 @@
package com.example.springdemo.service;
import com.example.springdemo.entities.Dishes;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword;
import java.util.List;
import java.util.Optional;
public interface MerchantsService {
Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword);
// CRUD operations for Merchants entity
Merchants addMerchant(Merchants merchant, MerchantsPassword merchantPassword);
void deleteMerchantsById(Long id);
void deleteMerchantById(Long merchantId);
Merchants updateMerchants(Merchants merchant);
Merchants updateMerchant(Merchants merchant);
Iterable<Merchants> findAllMerchants();
List<Merchants> findAllMerchants();
Merchants findById(Long id);
Optional<Merchants> findById(Long merchantId);
Merchants findByName(String name);
Optional<Merchants> findByName(String name);
void updatePassword(String password, Long id);
int updateMerchantPasswordById(String password, Long merchantId);
// CRUD operations for MerchantsDishes entity
Dishes addDish(Dishes dish, Long merchantId);
}

View File

@ -1,7 +1,9 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.DishesRepository;
import com.example.springdemo.dao.MerchantsPasswordRepository;
import com.example.springdemo.dao.MerchantsRepository;
import com.example.springdemo.entities.Dishes;
import com.example.springdemo.entities.Merchants;
import com.example.springdemo.entities.password.MerchantsPassword;
import jakarta.annotation.Resource;
@ -10,6 +12,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class MerchantsServiceImpl implements MerchantsService {
@ -18,29 +21,30 @@ public class MerchantsServiceImpl implements MerchantsService {
@Resource
private MerchantsPasswordRepository merchantsPasswordRepository;
@Resource
private DishesRepository dishesRepository;
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//添加商户时添加密码
public Merchants addMerchants(Merchants merchant, MerchantsPassword merchantsPassword) {
public Merchants addMerchant(Merchants merchant, MerchantsPassword merchantPassword) {
Merchants m = merchantsRepository.save(merchant);
merchantsPasswordRepository.save(merchantsPassword);
merchantsPasswordRepository.save(merchantPassword);
return m;
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//删除商户时删除密码
public void deleteMerchantsById(Long merchantID) {
merchantsRepository.deleteById(merchantID);
merchantsPasswordRepository.deletePasswordById(merchantID);
public void deleteMerchantById(Long merchantId) {
merchantsRepository.deleteById(merchantId);
merchantsPasswordRepository.deleteByMerchantsId(merchantId);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
public Merchants updateMerchants(Merchants merchant) {
//修改商户信息
public Merchants updateMerchant(Merchants merchant) {
return merchantsRepository.save(merchant);
}
@ -50,20 +54,25 @@ public class MerchantsServiceImpl implements MerchantsService {
}
@Override
public Merchants findById(Long id) {
return merchantsRepository.findById(id).orElse(null);
public Optional<Merchants> findById(Long merchantId) {
return merchantsRepository.findById(merchantId);
}
@Override
public Merchants findByName(String name) {
public Optional<Merchants> findByName(String name) {
return merchantsRepository.findByName(name);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//修改密码
public void updatePassword(String password, Long merchantID) {
merchantsPasswordRepository.updatePassword(password, merchantID);
public int updateMerchantPasswordById(String password, Long merchantId) {
return merchantsPasswordRepository.updatePassword(password, merchantId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Dishes addDish(Dishes dish, Long merchantId) {
return dishesRepository.save(dish);
}
}

View File

@ -2,15 +2,18 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Permissions;
import java.util.List;
import java.util.Optional;
public interface PermissionsService {
Permissions createPermissions(Permissions permissions);
Permissions addPermission(Permissions permission);
Permissions updatePermissions(Permissions permissions);
Permissions updatePermission(Permissions permission);
void deletePermissions(Long id);
void deletePermissionById(Long permissionId);
Permissions getPermissions(Long id);
Optional<Permissions> findById(Long permissionId);
Iterable<Permissions> getAllPermissions();
List<Permissions> findAllPermissions();
}

View File

@ -1,38 +1,58 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.PermissionsRepository;
import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.entities.RBAC.Permissions;
import jakarta.annotation.Resource;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class PermissionsServiceImpl implements PermissionsService {
@Resource
private PermissionsRepository permissionsRepository;
@Override
public Permissions createPermissions(Permissions permissions) {
@Resource
private RolesPermissionsRepository rolesPermissionsRepository;
return permissionsRepository.save(permissions);
@Override
@Transactional(rollbackFor = Exception.class)
public Permissions addPermission(Permissions permission) {
return permissionsRepository.save(permission);
}
@Override
public Permissions updatePermissions(Permissions permissions) {
return permissionsRepository.save(permissions);
@Transactional(rollbackFor = Exception.class)
public Permissions updatePermission(Permissions permission) {
return permissionsRepository.save(permission);
}
@Override
public void deletePermissions(Long permissionsID) {
permissionsRepository.deleteById(permissionsID);
@Transactional(rollbackFor = Exception.class)
public void deletePermissionById(Long permissionId) {
if (this.checkPermissionsRolesIfExist(permissionId)) {
//删除权限与角色的关联关系
rolesPermissionsRepository.deleteByPermissionsId(permissionId);
}
//删除权限
permissionsRepository.deleteById(permissionId);
}
private boolean checkPermissionsRolesIfExist(Long permissionId) {
return rolesPermissionsRepository.findByPermissionsId(permissionId).isPresent();
}
@Override
public Permissions getPermissions(Long permissionsID) {
return permissionsRepository.findById(permissionsID).orElse(null);
public Optional<Permissions> findById(Long permissionId) {
return permissionsRepository.findById(permissionId);
}
@Override
public Iterable<Permissions> getAllPermissions() {
public List<Permissions> findAllPermissions() {
return permissionsRepository.findAll();
}
}

View File

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

View File

@ -4,6 +4,10 @@ import com.example.springdemo.dao.RolesPermissionsRepository;
import com.example.springdemo.entities.RBAC.RolesPermissions;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class RolesPermissionsServiceImpl implements RolesPermissionsService {
@ -11,43 +15,48 @@ public class RolesPermissionsServiceImpl implements RolesPermissionsService {
private RolesPermissionsRepository rolesPermissionsRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public RolesPermissions AssignPermissionsToRole(RolesPermissions rolesPermissions) {
return rolesPermissionsRepository.save(rolesPermissions);
}
@Override
public Boolean RemovePermissionsFromRole(Long roleId, Long permissionId) {
@Transactional(rollbackFor = Exception.class)
public int RemovePermissionsFromRole(Long roleId, Long permissionId) {
int count;
if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId);
return true;
count = rolesPermissionsRepository.RemovePermissionsFromRole(roleId, permissionId);
} else {
return false;
count = 0;
}
return count;
}
@Override
public Boolean updateRolePermissions(Long roleId, Long permissionId) {
@Transactional(rollbackFor = Exception.class)
public int updateRolePermissions(Long roleId, Long permissionId) {
int count;
if (this.CheckIfDeleteOrUpdateAdmin(roleId)) {
rolesPermissionsRepository.updateRolePermissions(roleId, permissionId);
return true;
count = rolesPermissionsRepository.updateRolePermissions(roleId, permissionId);
} else {
return false;
count = 0;
}
return count;
}
@Override
public RolesPermissions getRolePermissionById(Long rolesPermissionsId) {
return rolesPermissionsRepository.findById(rolesPermissionsId).orElse(null);
public Optional<RolesPermissions> findById(Long rolesPermissionsId) {
return rolesPermissionsRepository.findById(rolesPermissionsId);
}
//TODO: 询全部权限和角色的对应关系
//TODO: 找全部时返回名字而不是id
@Override
public Iterable<RolesPermissions> getAllRolesPermissions() {
return null;
public List<RolesPermissions> findAllRolesPermissions() {
return rolesPermissionsRepository.findAll();
}
@Override
//检查是否删除或更新管理员
public Boolean CheckIfDeleteOrUpdateAdmin(Long roleId) {
return roleId != 1;
}

View File

@ -3,18 +3,21 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.RBAC.Roles;
import com.example.springdemo.entities.Users;
import java.util.List;
import java.util.Optional;
public interface RolesService {
Roles createRoles(Roles roles);
Roles createRole(Roles role);
Roles updateRoles(Roles roles);
Roles updateRole(Roles role);
void deleteRoles(Long id);
void deleteRoleById(Long roleId);
Iterable<Roles> getAllRoles();
List<Roles> findAllRoles();
Roles getRolesByName(String name);
Optional<Roles> findByName(String name);
Roles getRolesById(Long id);
Optional<Roles> findById(Long roleId);
//Users角色管理
public void RemoveRoleFromUser(Long userId, Long roleId);

View File

@ -1,10 +1,15 @@
package com.example.springdemo.service;
import com.example.springdemo.dao.RolesPermissionsRepository;
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;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class RolesServiceImpl implements RolesService {
@ -12,40 +17,59 @@ public class RolesServiceImpl implements RolesService {
@Resource
private RolesRepository rolesRepository;
@Resource
private RolesPermissionsRepository rolesPermissionsRepository;
@Override
public Roles createRoles(Roles roles) {
return rolesRepository.save(roles);
@Transactional(rollbackFor = Exception.class)
//创建角色
public Roles createRole(Roles role) {
return rolesRepository.save(role);
}
@Override
public Roles updateRoles(Roles roles) {
return rolesRepository.save(roles);
@Transactional(rollbackFor = Exception.class)
//更新角色信息
public Roles updateRole(Roles role) {
return rolesRepository.save(role);
}
@Override
public void deleteRoles(Long id) {
rolesRepository.deleteById(id);
@Transactional(rollbackFor = Exception.class)
//删除角色时需要删除角色与权限的关联关系
public void deleteRoleById(Long roleId) {
if (this.checkRolesPermissionsIfExist(roleId)) {
//删除角色与权限的关联关系
rolesPermissionsRepository.deleteByRolesId(roleId);
}
//删除角色
rolesRepository.deleteById(roleId);
}
//检查角色与权限的关联关系是否存在
private boolean checkRolesPermissionsIfExist(Long roleId) {
return rolesPermissionsRepository.findByRolesId(roleId).isPresent();
}
@Override
public Iterable<Roles> getAllRoles() {
public List<Roles> findAllRoles() {
return rolesRepository.findAll();
}
@Override
public Roles getRolesByName(String name) {
public Optional<Roles> findByName(String name) {
return rolesRepository.findByName(name);
}
@Override
public Roles getRolesById(Long id) {
return rolesRepository.findById(id).orElse(null);
public Optional<Roles> findById(Long roleId) {
return rolesRepository.findById(roleId);
}
//TODO: Users角色管理
@Override
public void RemoveRoleFromUser(Long userId, Long roleId) {
}
@Override

View File

@ -3,21 +3,24 @@ package com.example.springdemo.service;
import com.example.springdemo.entities.Users;
import com.example.springdemo.entities.password.UsersPassword;
import java.util.List;
import java.util.Optional;
public interface UsersService {
Users addUsers(Users user, UsersPassword usersPassword);
Users addUser(Users user, UsersPassword userPassword);
void deleteUsersById(Long userID);
void deleteUserById(Long userId);
void deleteUsersByName(String name);
void deleteUserByName(String name);
Users updateUsers(Users user);
Users updateUser(Users user);
Iterable<Users> findAllUsers();
List<Users> findAllUsers();
Users findById(Long userID);
Optional<Users> findById(Long userId);
Users findByName(String name);
Optional<Users> findByName(String name);
void updatePassword(String password, Long userID);
int updateUserPasswordById(String password, Long userId);
}

View File

@ -9,6 +9,9 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
public class UsersServiceImpl implements UsersService {
@Resource
@ -18,62 +21,63 @@ public class UsersServiceImpl implements UsersService {
private UsersPasswordRepository usersPasswordRepository;
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//添加用户时同步添加密码
public Users addUsers(Users user, UsersPassword usersPassword) {
public Users addUser(Users user, UsersPassword userPassword) {
Users u = usersRepository.save(user);
usersPasswordRepository.save(usersPassword);
usersPasswordRepository.save(userPassword);
return u;
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码
public void deleteUsersById(Long userID) {
usersRepository.deleteById(userID);
usersPasswordRepository.deletePasswordById(userID);
public void deleteUserById(Long userId) {
usersRepository.deleteById(userId);
usersPasswordRepository.deleteByUsersId(userId);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//删除用户时同步删除密码
public void deleteUsersByName(String name) {
usersRepository.deleteByName(name);
//获取用户ID
Long userID = usersRepository.findByName(name).getId();
usersPasswordRepository.deletePasswordById(userID);
public void deleteUserByName(String name) {
Long userId;
//如果用户存在
if (usersRepository.findByName(name).isPresent()) {
usersRepository.deleteByName(name);
//获取用户ID
userId = usersRepository.findByName(name).get().getId();
//删除密码
usersPasswordRepository.deleteByUsersId(userId);
}
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
public Users updateUsers(Users user) {
//更新用户信息
public Users updateUser(Users user) {
return usersRepository.save(user);
}
@Override
public Iterable<Users> findAllUsers() {
public List<Users> findAllUsers() {
return usersRepository.findAll();
}
@Override
public Users findById(Long userID) {
return usersRepository.findById(userID).orElse(null);
public Optional<Users> findById(Long userId) {
return usersRepository.findById(userId);
}
@Override
public Users findByName(String name) {
public Optional<Users> findByName(String name) {
return usersRepository.findByName(name);
}
@Override
@Modifying
@Transactional(rollbackFor = Exception.class)
//更新用户密码
public void updatePassword(String password, Long userID) {
usersPasswordRepository.updatePassword(password, userID);
public int updateUserPasswordById(String password, Long userId) {
return usersPasswordRepository.updatePassword(password, userId);
}
}