Search in sources :

Example 1 with Permission

use of com.webank.wedatasphere.qualitis.entity.Permission in project Qualitis by WeBankFinTech.

the class UserSpecPermissionDaoTest method savePermission.

private Permission savePermission() {
    // 保存是否成功
    Permission entity = new Permission();
    entity.setUrl("/junit/**");
    entity.setMethod("GET");
    Permission saveEntity = permissionDao.savePermission(entity);
    assertTrue(saveEntity.getId() != 0);
    return saveEntity;
}
Also used : UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission)

Example 2 with Permission

use of com.webank.wedatasphere.qualitis.entity.Permission in project Qualitis by WeBankFinTech.

the class UserSpecPermissionDaoTest method test.

@Test
@Transactional
public void test() {
    Permission savePermission = savePermission();
    User saveUser = saveUser();
    // 保存是否成功
    UserSpecPermission entity = new UserSpecPermission();
    entity.setId(UuidGenerator.generate());
    entity.setPermission(savePermission);
    entity.setUser(saveUser);
    UserSpecPermission saveEntity = dao.saveUserSpecPermission(entity);
    assertNotNull(saveEntity.getId());
    // 总数量大于0
    long size = dao.countAll();
    assertTrue(size > 0);
    // 分页查询有结果
    List<UserSpecPermission> datas = dao.findAllUserSpecPermission(0, 5);
    assertTrue(datas.size() > 0);
    // 保存到数据库的对象是否和保存的值一致
    UserSpecPermission findByIdEntity = dao.findByUuid(saveEntity.getId());
    assertNotNull(findByIdEntity);
    assertEquals(findByIdEntity.getPermission().getUrl(), saveEntity.getPermission().getUrl());
    assertEquals(findByIdEntity.getUser().getUserName(), saveEntity.getUser().getUserName());
    UserSpecPermission findByRoleAndPermissionEntity = dao.findByUserAndPermission(saveUser, savePermission);
    assertNotNull(findByRoleAndPermissionEntity);
    assertEquals(findByRoleAndPermissionEntity.getPermission().getUrl(), saveEntity.getPermission().getUrl());
    assertEquals(findByRoleAndPermissionEntity.getUser().getUserName(), saveEntity.getUser().getUserName());
    // 根据username查询的数据库对象是否和保存的值一致
    List<UserSpecPermission> findByRoleEntity = dao.findByUser(saveUser);
    assertTrue(findByRoleEntity.size() > 0);
    assertEquals(findByRoleEntity.get(0).getPermission().getUrl(), saveEntity.getPermission().getUrl());
    // 根据username查询的数据库对象是否和保存的值一致
    List<UserSpecPermission> findByPermissionEntity = dao.findByPermission(savePermission);
    assertTrue(findByPermissionEntity.size() > 0);
    assertEquals(findByPermissionEntity.get(0).getUser().getUserName(), saveEntity.getUser().getUserName());
    // 删除后,是否还能找到对象
    dao.deleteUserSpecPermission(saveEntity);
    UserSpecPermission deleteEntity = dao.findByUuid(saveEntity.getId());
    assertNull(deleteEntity);
}
Also used : User(com.webank.wedatasphere.qualitis.entity.User) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Permission

use of com.webank.wedatasphere.qualitis.entity.Permission in project Qualitis by WeBankFinTech.

the class PermissionServiceImpl method addPermission.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<PermissionResponse> addPermission(AddPermissionRequest request) throws UnExpectedRequestException {
    // Check Arguments
    AddPermissionRequest.checkRequest(request);
    String method = request.getMethod().trim();
    String url = request.getUrl().trim();
    // Find permission by method and url
    Permission permissionInDb = permissionDao.findByMethodAndUrl(method, url);
    if (permissionInDb != null) {
        throw new UnExpectedRequestException("{&METHOD_AND_URL_ALREADY_EXIST}, request: " + request);
    }
    Permission newPermission = new Permission();
    newPermission.setMethod(method);
    newPermission.setUrl(url);
    Permission savedPermission = permissionDao.savePermission(newPermission);
    LOGGER.info("Succeed to add permission, id: {}, method: {}, url: {}, current_user: {}", savedPermission.getId(), method, url, HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&ADD_PERMISSION_SUCCESSFULLY}", new PermissionResponse(savedPermission));
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) PermissionResponse(com.webank.wedatasphere.qualitis.response.PermissionResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Permission

use of com.webank.wedatasphere.qualitis.entity.Permission in project Qualitis by WeBankFinTech.

the class PermissionServiceImpl method deletePermission.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deletePermission(DeletePermissionRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    Long permissionId = request.getPermissionId();
    // Find permissions by permission id
    Permission permissionInDb = permissionDao.findById(permissionId);
    if (permissionInDb == null) {
        throw new UnExpectedRequestException("{&PERMISSION_ID_NOT_EXIST}, request: " + request);
    }
    List<RolePermission> rolePermissionsInDb = rolePermissionDao.findByPermission(permissionInDb);
    if (null != rolePermissionsInDb && !rolePermissionsInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_ROLE_PERMISSION_HAS_FOREIGN_KEY}");
    }
    List<UserSpecPermission> userSpecPermissionsInDb = userSpecPermissionDao.findByPermission(permissionInDb);
    if (null != userSpecPermissionsInDb && !userSpecPermissionsInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_USER_SPEC_PERMISSION_HAS_FOREIGN_KEY}");
    }
    permissionDao.deletePermission(permissionInDb);
    LOGGER.info("Succeed to delete permission, permissionId: {}, method: {}, url: {}, current_user: {}", permissionId, permissionInDb.getMethod(), permissionInDb.getUrl(), HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&DELETE_PERMISSION_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Permission

use of com.webank.wedatasphere.qualitis.entity.Permission in project Qualitis by WeBankFinTech.

the class RolePermissionServiceImpl method modifyRolePermission.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> modifyRolePermission(ModifyRolePermissionRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    // Check existence of role permission
    String uuid = request.getUuid();
    RolePermission rolePermissionInDb = rolePermissionDao.findByUuid(uuid);
    if (rolePermissionInDb == null) {
        throw new UnExpectedRequestException("role permission id {&DOES_NOT_EXIST}, request: " + request);
    }
    LOGGER.info("Succeed to find role_permission, uuid: {}, role_id: {}, permission_id: {}, current_user: {}", uuid, rolePermissionInDb.getRole().getId(), rolePermissionInDb.getPermission().getId(), HttpUtils.getUserName(httpServletRequest));
    long roleId = request.getRoleId();
    long permissionId = request.getPermissionId();
    Role roleInDb = roleDao.findById(roleId);
    if (roleInDb == null) {
        throw new UnExpectedRequestException("role id {&DOES_NOT_EXIST}, request: " + request);
    }
    Permission permissionInDb = permissionDao.findById(permissionId);
    if (permissionInDb == null) {
        throw new UnExpectedRequestException("permission id {&DOES_NOT_EXIST}, request: " + request);
    }
    RolePermission roleIdAndPermissionIdInDb = rolePermissionDao.findByRoleAndPermission(roleInDb, permissionInDb);
    if (roleIdAndPermissionIdInDb != null) {
        throw new UnExpectedRequestException("role and permission {&ALREADY_EXIST}, request: " + request);
    }
    // Save role permission
    rolePermissionInDb.setRole(roleInDb);
    rolePermissionInDb.setPermission(permissionInDb);
    RolePermission savedRolePermission = rolePermissionDao.saveRolePermission(rolePermissionInDb);
    LOGGER.info("Succeed to modify role_permission, uuid: {}, role_id: {}, permission_id: {}, current_user: {}", uuid, savedRolePermission.getRole().getId(), savedRolePermission.getPermission().getId(), HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&MODIFY_ROLE_PERMISSION_SUCCESSFULLY}", null);
}
Also used : Role(com.webank.wedatasphere.qualitis.entity.Role) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Permission (com.webank.wedatasphere.qualitis.entity.Permission)16 Transactional (org.springframework.transaction.annotation.Transactional)10 RolePermission (com.webank.wedatasphere.qualitis.entity.RolePermission)8 UserSpecPermission (com.webank.wedatasphere.qualitis.entity.UserSpecPermission)8 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)8 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)7 Role (com.webank.wedatasphere.qualitis.entity.Role)4 User (com.webank.wedatasphere.qualitis.entity.User)4 Test (org.junit.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 PermissionResponse (com.webank.wedatasphere.qualitis.response.PermissionResponse)2 ArrayList (java.util.ArrayList)2 HttpSession (javax.servlet.http.HttpSession)2 AntPathMatcher (org.springframework.util.AntPathMatcher)2 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)1 RolePermissionResponse (com.webank.wedatasphere.qualitis.response.RolePermissionResponse)1 UserSpecPermissionResponse (com.webank.wedatasphere.qualitis.response.UserSpecPermissionResponse)1 List (java.util.List)1 RoleNotFoundException (javax.management.relation.RoleNotFoundException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1