Search in sources :

Example 1 with Account

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

the class FfeeAccountManageServiceImpl method regist.

/**
 * 使用用户名、密码方式注册账户。
 *
 * @see FfeeAccountManageService#regist(String, String, String)
 */
@Override
@Transactional()
public FfeeAccount regist(String code, String name, String password) {
    if (StringUtils.isBlank(code) || StringUtils.isBlank(name) || StringUtils.isBlank(password)) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    Account account = accessor.getByCode(code, Account.class);
    if (account != null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST);
    }
    Role role = accessor.getByCode("user", Role.class);
    if (role == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
    }
    account = EntityFactory.createEntity(Account.class);
    account.setCode(code);
    account.setName(name);
    try {
        account.setPassword(DigestUtils.md5(password));
    } catch (NoSuchAlgorithmException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Digest the password fail.", ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
    }
    account = accessor.save(account, false);
    FfeeAccount ffeeAccount = EntityFactory.createEntity(FfeeAccount.class);
    ffeeAccount.setAccount(account);
    ffeeAccount.setSourceType(FfeeAccount.AccountSourceType.NORMAL);
    ffeeAccount = accessor.save(ffeeAccount, false);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("常规账户[%s]注册成功。", name));
    }
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Create a normal registry FFEE account[%s - %s] successfully.", code, name));
    }
    return ffeeAccount;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) FfeeAccount(org.mx.tools.ffee.dal.entity.FfeeAccount) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Account

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

the class TestAccount method testChangePassword.

@Test
public void testChangePassword() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    AccountManageService accountService = context.getBean(AccountManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        testInsertAccount(service, accountService);
        testEditAccount(service, accountService);
        assertEquals(3, service.count(Account.class));
        Account account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertEquals(DigestUtils.md5("password"), account1.getPassword());
        accountService.changePassword(account1.getId(), "password", "new password");
        account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertEquals(DigestUtils.md5("new password"), account1.getPassword());
        Account account3 = service.getById(account3Id, Account.class);
        assertNotNull(account3);
        assertEquals(DigestUtils.md5("ds110119"), account3.getPassword());
        accountService.changePassword(account3.getId(), "ds110119", "new password");
        account3 = service.getById(account3Id, Account.class);
        assertNotNull(account3);
        assertEquals(DigestUtils.md5("new password"), account3.getPassword());
    } 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) 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) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 3 with Account

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

the class TestAccount method testEditAccount.

public static void testEditAccount(GeneralDictAccessor service, AccountManageService accountService) {
    User john = service.getById(TestUser.johnId, User.class);
    assertNotNull(john);
    AccountManageService.AccountInfo accountInfo = AccountManageService.AccountInfo.valueOf("account3", "", "description account.", "", TestUser.johnId, Arrays.asList(), true);
    Account account3 = accountService.saveAccount(accountInfo);
    assertEquals(3, service.count(Account.class));
    assertNotNull(account3);
    assertNotNull(account3.getId());
    account3Id = account3.getId();
    assertEquals("description account.", account3.getDesc());
    assertEquals(john.getFullName(), account3.getName());
    assertEquals(john, account3.getOwner());
    accountInfo = AccountManageService.AccountInfo.valueOf(account3.getCode(), "", "new desc.", account3.getId(), TestUser.johnId, Arrays.asList(), false);
    account3 = accountService.saveAccount(accountInfo);
    assertEquals(2, service.count(Account.class));
    assertEquals(2, service.count(Account.class, true));
    assertEquals(3, service.count(Account.class, false));
    assertNotNull(account3);
    assertEquals("new desc.", account3.getDesc());
    assertFalse(account3.isValid());
    account3 = service.getByCode("account3", Account.class);
    assertNotNull(account3);
    assertEquals("new desc.", account3.getDesc());
    assertFalse(account3.isValid());
    accountInfo = AccountManageService.AccountInfo.valueOf(account3.getCode(), "", account3.getDesc(), account3.getId(), TestUser.johnId, Arrays.asList(), true);
    accountService.saveAccount(accountInfo);
    assertEquals(3, service.count(Account.class));
    assertEquals(3, service.count(Account.class, true));
    assertEquals(3, service.count(Account.class, false));
    account3 = service.getById(account3Id, Account.class);
    assertNotNull(account3);
    assertTrue(account3.isValid());
}
Also used : Account(org.mx.comps.rbac.dal.entity.Account) User(org.mx.comps.rbac.dal.entity.User) AccountManageService(org.mx.comps.rbac.service.AccountManageService)

Example 4 with Account

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

the class TestAccount method testLoginAndLogout.

@Test
public void testLoginAndLogout() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    AccountManageService accountService = context.getBean(AccountManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        testInsertAccount(service, accountService);
        testEditAccount(service, accountService);
        assertEquals(3, service.count(Account.class));
        // 测试正常流程
        LoginHistory login = accountService.login("account1", "password", false);
        assertNotNull(login);
        Account account1 = service.getById(account1Id, Account.class);
        assertNotNull(account1);
        assertEquals(account1, login.getAccount());
        // 测试用户重复登录
        login = accountService.login("account1", "password", true);
        assertNotNull(login);
        assertEquals(account1, login.getAccount());
        try {
            accountService.login("account1", "password", false);
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_ALREADY_LOGINED.getErrorCode(), ex.getErrorCode());
        }
        // 测试正常登出
        login = accountService.logout(account1.getId());
        assertNotNull(login);
        assertEquals(account1, login.getAccount());
        // 测试用户不存在
        try {
            accountService.login("abc", "adasd", false);
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND.getErrorMessage(), ex.getErrorMessage());
        }
        // 测试密码不正确
        try {
            accountService.login("account1", "adfasd", false);
        } catch (UserInterfaceRbacErrorException ex) {
            assertEquals(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED.getErrorMessage(), ex.getErrorMessage());
        }
    } 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) LoginHistory(org.mx.comps.rbac.dal.entity.LoginHistory) 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) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 5 with Account

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

the class TestRole method testRoleAccounts.

@Test
public void testRoleAccounts() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    RoleManageService roleService = context.getBean(RoleManageService.class);
    assertNotNull(service);
    UserManageService userManageService = context.getBean(UserManageService.class);
    assertNotNull(userManageService);
    AccountManageService accountManageService = context.getBean(AccountManageService.class);
    assertNotNull(accountManageService);
    try {
        TestUser.testInsertUser(service, userManageService);
        TestUser.testEditUser(service, userManageService);
        assertEquals(3, service.count(User.class));
        TestAccount.testInsertAccount(service, accountManageService);
        TestAccount.testEditAccount(service, accountManageService);
        testInsertRole(service, roleService);
        testEditRole(service, roleService);
        assertEquals(3, service.count(Account.class));
        assertEquals(3, service.count(Role.class));
        Role role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(0, role1.getAccounts().size());
        Account account1 = service.getById(TestAccount.account1Id, Account.class);
        Account account2 = service.getById(TestAccount.account2Id, Account.class);
        Account account3 = service.getById(TestAccount.account3Id, Account.class);
        assertNotNull(account1);
        assertNotNull(account2);
        assertNotNull(account3);
        RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(account1.getId(), account2.getId(), account3.getId()), Arrays.asList(), role1.isValid());
        roleService.saveRole(roleInfo);
        assertEquals(3, service.count(Role.class));
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertNotNull(role1.getAccounts());
        assertEquals(3, role1.getAccounts().size());
        assertEquals(new HashSet<>(Arrays.asList(account1, account2, account3)), role1.getAccounts());
        roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(account1.getId(), account3.getId()), Arrays.asList(), role1.isValid());
        roleService.saveRole(roleInfo);
        assertEquals(3, service.count(Role.class));
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertNotNull(role1.getAccounts());
        assertEquals(2, role1.getAccounts().size());
        assertEquals(new HashSet<>(Arrays.asList(account1, account3)), role1.getAccounts());
        roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(), role1.isValid());
        roleService.saveRole(roleInfo);
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(0, role1.getAccounts().size());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) User(org.mx.comps.rbac.dal.entity.User) AccountManageService(org.mx.comps.rbac.service.AccountManageService) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) UserManageService(org.mx.comps.rbac.service.UserManageService) RoleManageService(org.mx.comps.rbac.service.RoleManageService) Test(org.junit.Test)

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