Search in sources :

Example 21 with Account

use of org.mx.comps.rbac.dal.entity.Account 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 22 with Account

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

the class TestUser method testAllocateAccount.

@Test
public void testAllocateAccount() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    UserManageService userService = context.getBean(UserManageService.class);
    assertNotNull(service);
    AccountManageService accountManageService = context.getBean(AccountManageService.class);
    assertNotNull(accountManageService);
    try {
        testInsertUser(service, userService);
        testEditUser(service, userService);
        assertEquals(3, service.count(User.class));
        User john = service.getById(joshId, User.class);
        assertNotNull(john);
        assertEquals(0, service.count(Account.class));
        // 用户不存在
        AccountManageService.AccountInfo accountInfo = AccountManageService.AccountInfo.valueOf("john---", "password", "desc", "", "asdfasd", Arrays.asList(), true);
        try {
            userService.allocateAccount(accountInfo);
            fail("here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND.getErrorCode(), ex.getErrorCode());
        }
        // 正常创建
        accountInfo = AccountManageService.AccountInfo.valueOf("John.Peng", "edmund!@#123", "desc", "", john.getId(), Arrays.asList(), true);
        Account account = userService.allocateAccount(accountInfo);
        assertNotNull(account);
        assertEquals(3, service.count(User.class));
        assertEquals(1, service.count(Account.class));
        account = service.getByCode("John.Peng", Account.class);
        assertNotNull(account);
        assertNotNull(account.getOwner());
        assertEquals(john, account.getOwner());
        assertEquals(DigestUtils.md5("edmund!@#123"), account.getPassword());
        assertEquals(john.getFullName(), account.getName());
        assertEquals("desc", account.getDesc());
        assertEquals(0, account.getRoles().size());
        // 账户已存在
        try {
            userService.allocateAccount(accountInfo);
            fail("here need a exception");
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST.getErrorCode(), ex.getErrorCode());
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 23 with Account

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

the class AccountManageServiceCommonImpl method changePassword.

/**
 * {@inheritDoc}
 *
 * @see AccountManageService#changePassword(String, String, String)
 */
@Override
public Account changePassword(String accountId, String oldPassword, String newPassword) {
    Account account = accessor.getById(accountId, Account.class);
    if (account == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
    }
    try {
        if (account.getPassword().equals(DigestUtils.md5(oldPassword))) {
            // the old password is matched.
            account.setPassword(DigestUtils.md5(newPassword));
            account = this.save(account);
            if (operateLogService != null) {
                operateLogService.writeLog(String.format("修改账户[code=%s, name=%s]的密码成功。", account.getCode(), account.getName()));
            }
            return account;
        } else {
            throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED);
        }
    } catch (NoSuchAlgorithmException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
    }
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 24 with Account

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

the class RoleManageServiceCommonImpl method saveRole.

/**
 * {@inheritDoc}
 *
 * @see RoleManageService#saveRole(RoleInfo)
 */
@Override
public Role saveRole(RoleInfo roleInfo) {
    if (roleInfo == null) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    String id = roleInfo.getRoleId();
    Role role;
    if (!StringUtils.isBlank(id)) {
        role = accessor.getById(id, Role.class);
        if (role == null) {
            throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
        }
    } else {
        role = EntityFactory.createEntity(Role.class);
    }
    role.setCode(roleInfo.getCode());
    role.setName(roleInfo.getName());
    role.setDesc(roleInfo.getDesc());
    if (role.getAccounts() != null && !role.getAccounts().isEmpty()) {
        role.getAccounts().clear();
    }
    if (roleInfo.getAccountIds() != null && !roleInfo.getAccountIds().isEmpty()) {
        for (String accountId : roleInfo.getAccountIds()) {
            Account account = accessor.getById(accountId, Account.class);
            if (account == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
            }
            role.getAccounts().add(account);
        }
    }
    if (role.getPrivileges() != null && !role.getPrivileges().isEmpty()) {
        role.getPrivileges().clear();
        for (String privilegeId : roleInfo.getPrivilegeIds()) {
            Privilege privilege = accessor.getById(privilegeId, Privilege.class);
            if (privilege == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.PRIVILEGE_NOT_FOUND);
            }
            role.getPrivileges().add(privilege);
        }
    }
    role.setValid(roleInfo.isValid());
    role = this.save(role);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("保存角色[code=%s, name=%s]信息成功。", roleInfo.getCode(), roleInfo.getName()));
    }
    return role;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Privilege(org.mx.comps.rbac.dal.entity.Privilege)

Example 25 with Account

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

the class RoleManageServiceImpl method save.

/**
 * {@inheritDoc}
 *
 * @see RoleManageServiceCommonImpl#save(Role)
 */
@Override
public Role save(Role role) {
    Set<Privilege> oldPrivileges = new HashSet<>();
    Set<Account> oldAccounts = new HashSet<>();
    if (!StringUtils.isBlank(role.getId())) {
        Role checked = accessor.getById(role.getId(), Role.class);
        oldPrivileges.addAll(checked.getPrivileges());
        oldAccounts.addAll(checked.getAccounts());
    }
    accessor.save(role, false);
    Set<Privilege> privileges = role.getPrivileges();
    Set<Account> accounts = role.getAccounts();
    for (Privilege privilege : privileges) {
        if (oldPrivileges.contains(privilege)) {
            oldPrivileges.remove(privilege);
            continue;
        } else {
            privilege.getRoles().add(role);
            accessor.save(privilege, false);
        }
    }
    for (Privilege privilege : oldPrivileges) {
        privilege.getRoles().remove(role);
        accessor.save(privilege, false);
    }
    for (Account account : accounts) {
        if (oldAccounts.contains(account)) {
            oldAccounts.remove(account);
            continue;
        } else {
            account.getRoles().add(role);
            accessor.save(account, false);
        }
    }
    for (Account account : oldAccounts) {
        account.getRoles().remove(role);
        accessor.save(account, false);
    }
    return role;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) Privilege(org.mx.comps.rbac.dal.entity.Privilege) HashSet(java.util.HashSet)

Aggregations

Account (org.mx.comps.rbac.dal.entity.Account)26 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)14 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)12 User (org.mx.comps.rbac.dal.entity.User)10 Role (org.mx.comps.rbac.dal.entity.Role)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 AccountManageService (org.mx.comps.rbac.service.AccountManageService)8 Test (org.junit.Test)6 UserManageService (org.mx.comps.rbac.service.UserManageService)6 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)6 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)5 UserInterfaceException (org.mx.error.UserInterfaceException)5 DataVO (org.mx.service.rest.vo.DataVO)5 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)5 Date (java.util.Date)4 HashSet (java.util.HashSet)3 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)3 RoleManageService (org.mx.comps.rbac.service.RoleManageService)3 Accredit (org.mx.comps.rbac.dal.entity.Accredit)2 Privilege (org.mx.comps.rbac.dal.entity.Privilege)2