Search in sources :

Example 6 with Accredit

use of org.mx.comps.rbac.dal.entity.Accredit in project main by JohnPeng739.

the class AccreditManageResource method getAccredit.

@Path("accredits/{id}")
@GET
@AuthenticateAround(returnValueClass = DataVO.class)
public DataVO<AccreditVO> getAccredit(@QueryParam("id") String id) {
    if (StringUtils.isBlank(id)) {
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM));
    }
    try {
        Accredit accredit = accessor.getById(id, Accredit.class);
        AccreditVO vo = AccreditVO.transform(accredit, true);
        return new DataVO<>(vo);
    } catch (UserInterfaceException ex) {
        return new DataVO<>(ex);
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Get accredit fail.", ex);
        }
        return new DataVO<>(new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_OTHER_FAIL));
    }
}
Also used : Accredit(org.mx.comps.rbac.dal.entity.Accredit) DataVO(org.mx.service.rest.vo.DataVO) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) AccreditVO(org.mx.comps.rbac.rest.vo.AccreditVO) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceException(org.mx.error.UserInterfaceException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) AuthenticateAround(org.mx.comps.jwt.AuthenticateAround)

Example 7 with Accredit

use of org.mx.comps.rbac.dal.entity.Accredit in project main by JohnPeng739.

the class TestAccredit method testAccredit.

@Test
public void testAccredit() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    AccreditManageService accreditService = context.getBean(AccreditManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    AccountManageService accountManageService = context.getBean(AccountManageService.class);
    assertNotNull(accountManageService);
    RoleManageService roleManageService = context.getBean(RoleManageService.class);
    assertNotNull(roleManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        TestAccount.testInsertAccount(service, accountManageService);
        TestAccount.testEditAccount(service, accountManageService);
        TestRole.testInsertRole(service, roleManageService);
        TestRole.testEditRole(service, roleManageService);
        assertEquals(3, service.count(Account.class));
        assertEquals(3, service.count(Role.class));
        assertEquals(0, service.count(Accredit.class));
        Account account1 = service.getById(TestAccount.account1Id, Account.class);
        assertNotNull(account1);
        Account account2 = service.getById(TestAccount.account2Id, Account.class);
        assertNotNull(account2);
        Role role1 = service.getById(TestRole.role1Id, Role.class);
        assertNotNull(role1);
        Role role2 = service.getById(TestRole.role2Id, Role.class);
        assertNotNull(role2);
        Role role3 = service.getById(TestRole.role3Id, Role.class);
        assertNotNull(role3);
        long startTime = new Date().getTime();
        AccreditManageService.AccreditInfo accreditInfo;
        // 测试没有设置源
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf("", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, null, startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceSystemErrorException ex) {
            assertEquals(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf("abcde", TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, "abcde", Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, "abcdef", TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        // 测试正常授权
        long endTime = new Date().getTime() + 500;
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role2Id, TestRole.role3Id), startTime, endTime, "desc");
        Accredit accredit = accreditService.accredit(accreditInfo);
        assertEquals(1, service.count(Accredit.class));
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertEquals(endTime, accredit.getEndTime().getTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role2, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertEquals(endTime, accredit.getEndTime().getTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        // 测试重复授权
        try {
            accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
            accreditService.accredit(accreditInfo);
            fail("Here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_SAME_FOUND.getErrorCode(), ex.getErrorCode());
        }
        // 测试自动时间到达后关闭
        Thread.sleep(600);
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
        accredit = accreditService.accredit(accreditInfo);
        assertEquals(2, service.count(Accredit.class));
        assertEquals(2, service.count(Accredit.class, false));
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertNull(accredit.getEndTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
        // 测试关闭
        accreditService.closeAccredit(accredit.getId());
        assertEquals(1, service.count(Accredit.class));
        assertEquals(2, service.count(Accredit.class, false));
        // 再次授权
        accreditInfo = AccreditManageService.AccreditInfo.valueOf(TestAccount.account1Id, TestAccount.account2Id, Arrays.asList(TestRole.role1Id, TestRole.role3Id), startTime, -1, "desc");
        accredit = accreditService.accredit(accreditInfo);
        assertEquals(2, service.count(Accredit.class));
        assertEquals(3, service.count(Accredit.class, false));
        assertNotNull(accredit);
        accredit = service.getById(accredit.getId(), Accredit.class);
        assertNotNull(accredit);
        assertEquals(account1, accredit.getSrc());
        assertEquals(account2, accredit.getTar());
        assertEquals(new HashSet<>(Arrays.asList(role1, role3)), accredit.getRoles());
        assertEquals(startTime, accredit.getStartTime().getTime());
        assertNull(accredit.getEndTime());
        assertTrue(accredit.isValid());
        assertEquals("desc", accredit.getDesc());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) Accredit(org.mx.comps.rbac.dal.entity.Accredit) User(org.mx.comps.rbac.dal.entity.User) AccreditManageService(org.mx.comps.rbac.service.AccreditManageService) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Date(java.util.Date) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Role(org.mx.comps.rbac.dal.entity.Role) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) RoleManageService(org.mx.comps.rbac.service.RoleManageService) Test(org.junit.Test)

Example 8 with Accredit

use of org.mx.comps.rbac.dal.entity.Accredit in project main by JohnPeng739.

the class AccreditManageServiceCommonImpl method closeAccredit.

/**
 * {@inheritDoc}
 *
 * @see AccreditManageService#closeAccredit(String)
 */
@Override
public Accredit closeAccredit(String accreditId) {
    if (StringUtils.isBlank(accreditId)) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    Accredit accredit = accessor.getById(accreditId, Accredit.class);
    if (accredit == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_NOT_FOUND);
    }
    if (accredit.isClosed()) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_HAS_CLOSED);
    }
    accredit.setValid(false);
    accredit = this.save(accredit);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("关闭授权[%s=>%s]成功。", accredit.getSrc().getName(), accredit.getTar().getName()));
    }
    return accredit;
}
Also used : Accredit(org.mx.comps.rbac.dal.entity.Accredit) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException)

Example 9 with Accredit

use of org.mx.comps.rbac.dal.entity.Accredit in project main by JohnPeng739.

the class AccreditManageServiceImpl method hasSameAccredit.

/**
 * {@inheritDoc}
 *
 * @see AccreditManageServiceCommonImpl#hasSameAccredit(AccreditInfo)
 */
@Override
protected boolean hasSameAccredit(AccreditInfo accreditInfo) {
    List<GeneralAccessor.ConditionTuple> conditions = new ArrayList<>();
    conditions.add(new GeneralAccessor.ConditionTuple("src.id", accreditInfo.getSrcAccountId()));
    conditions.add(new GeneralAccessor.ConditionTuple("tar.id", accreditInfo.getTarAccountId()));
    conditions.add(new GeneralAccessor.ConditionTuple("valid", true));
    List<Accredit> list = accessor.find(conditions, Accredit.class);
    List<Accredit> accredits = new ArrayList<>();
    if (list != null && !list.isEmpty()) {
        list.forEach(accredit -> {
            if (!accredit.isClosed()) {
                accredits.add(accredit);
            }
        });
    }
    if (accredits.isEmpty()) {
        return false;
    }
    for (Accredit accredit : accredits) {
        if (!accredit.isClosed()) {
            for (String roleId : accreditInfo.getRoleIds()) {
                boolean found = false;
                for (Role role : accredit.getRoles()) {
                    if (roleId.equals(role.getId())) {
                        found = true;
                    }
                }
                if (!found) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Accredit(org.mx.comps.rbac.dal.entity.Accredit) ArrayList(java.util.ArrayList) GeneralAccessor(org.mx.dal.service.GeneralAccessor)

Aggregations

Accredit (org.mx.comps.rbac.dal.entity.Accredit)9 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)7 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)4 Role (org.mx.comps.rbac.dal.entity.Role)4 AccreditVO (org.mx.comps.rbac.rest.vo.AccreditVO)4 UserInterfaceException (org.mx.error.UserInterfaceException)4 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)4 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)3 DataVO (org.mx.service.rest.vo.DataVO)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Account (org.mx.comps.rbac.dal.entity.Account)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 User (org.mx.comps.rbac.dal.entity.User)1 AccountManageService (org.mx.comps.rbac.service.AccountManageService)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1 RoleManageService (org.mx.comps.rbac.service.RoleManageService)1 UserManageService (org.mx.comps.rbac.service.UserManageService)1