Search in sources :

Example 6 with GeneralDictAccessor

use of org.mx.dal.service.GeneralDictAccessor 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)

Example 7 with GeneralDictAccessor

use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.

the class TestRole method testRolePrivileges.

@Test
public void testRolePrivileges() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    RoleManageService roleService = context.getBean(RoleManageService.class);
    assertNotNull(service);
    try {
        TestPrivilege.testInsertPrivilege(service);
        TestPrivilege.testEditPrivilege(service);
        testInsertRole(service, roleService);
        testEditRole(service, roleService);
        assertEquals(3, service.count(Role.class));
        assertEquals(3, service.count(Privilege.class));
        Role role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertTrue(role1.getPrivileges().isEmpty());
        Privilege p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
        assertNotNull(p1);
        assertTrue(p1.getRoles().isEmpty());
        Privilege p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
        assertNotNull(p2);
        assertTrue(p2.getRoles().isEmpty());
        Privilege p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
        assertNotNull(p3);
        assertTrue(p3.getRoles().isEmpty());
        RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(p1.getId(), p2.getId(), p3.getId()), role1.isValid());
        roleService.saveRole(roleInfo);
        assertEquals(3, service.count(Role.class));
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(3, role1.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1, p2, p3)), role1.getPrivileges());
        p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(1, p1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1)), p1.getRoles());
        p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
        assertNotNull(p2);
        assertEquals(1, p2.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1)), p2.getRoles());
        p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
        assertNotNull(p3);
        assertEquals(1, p3.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1)), p3.getRoles());
        roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(p1.getId(), p3.getId()), role1.isValid());
        roleService.saveRole(roleInfo);
        assertEquals(3, service.count(Role.class));
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(2, role1.getPrivileges().size());
        assertEquals(new HashSet<>(Arrays.asList(p1, p3)), role1.getPrivileges());
        p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(1, p1.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1)), p1.getRoles());
        p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
        assertNotNull(p2);
        assertEquals(0, p2.getRoles().size());
        p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
        assertNotNull(p3);
        assertEquals(1, p3.getRoles().size());
        assertEquals(new HashSet<>(Arrays.asList(role1)), p3.getRoles());
        roleInfo = RoleManageService.RoleInfo.valueOf(role1.getCode(), role1.getName(), role1.getDesc(), role1.getId(), Arrays.asList(), Arrays.asList(), role1.isValid());
        roleService.saveRole(roleInfo);
        assertEquals(3, service.count(Role.class));
        role1 = service.getById(role1Id, Role.class);
        assertNotNull(role1);
        assertEquals(0, role1.getPrivileges().size());
        p1 = service.getById(TestPrivilege.p1Id, Privilege.class);
        assertNotNull(p1);
        assertEquals(0, p1.getRoles().size());
        p2 = service.getById(TestPrivilege.p2Id, Privilege.class);
        assertNotNull(p2);
        assertEquals(0, p2.getRoles().size());
        p3 = service.getById(TestPrivilege.p3Id, Privilege.class);
        assertNotNull(p3);
        assertEquals(0, p3.getRoles().size());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) RoleManageService(org.mx.comps.rbac.service.RoleManageService) Privilege(org.mx.comps.rbac.dal.entity.Privilege) Test(org.junit.Test)

Example 8 with GeneralDictAccessor

use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.

the class TestUser method testUserCrud.

@Test
public void testUserCrud() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    UserManageService userService = context.getBean(UserManageService.class);
    assertNotNull(service);
    try {
        assertEquals(service.count(User.class), 0);
        // insert
        testInsertUser(service, userService);
        // edit
        testEditUser(service, userService);
        // delete
        testDeleteUser(service);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : User(org.mx.comps.rbac.dal.entity.User) 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 9 with GeneralDictAccessor

use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.

the class TestUser method testUserDepartment.

@Test
public void testUserDepartment() {
    GeneralDictAccessor service = context.getBean("generalDictAccessor", GeneralDictAccessor.class);
    assertNotNull(service);
    UserManageService userService = context.getBean(UserManageService.class);
    assertNotNull(service);
    DepartmentManageService departManageService = context.getBean(DepartmentManageService.class);
    assertNotNull(departManageService);
    try {
        TestDepartment.testInsertDepartment(service, departManageService);
        TestDepartment.testEditDepartment(service, departManageService);
        testInsertUser(service, userService);
        testEditUser(service, userService);
        assertEquals(3, service.count(Department.class));
        assertEquals(3, service.count(User.class));
        User joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNull(joy.getDepartment());
        User josh = service.getById(joshId, User.class);
        assertNotNull(josh);
        assertNull(josh.getDepartment());
        Department depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        UserManageService.UserInfo userInfo = UserManageService.UserInfo.valueOf(joy.getFirstName(), joy.getMiddleName(), joy.getLastName(), joy.getSex(), joy.getId(), 0, depart1.getId(), joy.getStation(), joy.isValid(), joy.getDesc());
        userService.saveUser(userInfo);
        joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNotNull(joy.getDepartment());
        assertEquals(depart1, joy.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(1, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(joy)), depart1.getEmployees());
        userInfo = UserManageService.UserInfo.valueOf(josh.getFirstName(), josh.getMiddleName(), josh.getLastName(), josh.getSex(), josh.getId(), 0, depart1.getId(), josh.getStation(), josh.isValid(), josh.getDesc());
        userService.saveUser(userInfo);
        josh = service.getById(joshId, User.class);
        assertNotNull(josh);
        assertNotNull(josh.getDepartment());
        assertEquals(depart1, josh.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(2, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(joy, josh)), depart1.getEmployees());
        userInfo = UserManageService.UserInfo.valueOf(joy.getFirstName(), joy.getMiddleName(), joy.getLastName(), joy.getSex(), joy.getId(), 0, null, joy.getStation(), joy.isValid(), joy.getDesc());
        userService.saveUser(userInfo);
        joy = service.getById(joyId, User.class);
        assertNotNull(joy);
        assertNull(joy.getDepartment());
        depart1 = service.getById(TestDepartment.depart1Id, Department.class);
        assertNotNull(depart1);
        assertEquals(1, depart1.getEmployees().size());
        assertEquals(new HashSet<>(Arrays.asList(josh)), depart1.getEmployees());
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : Department(org.mx.comps.rbac.dal.entity.Department) User(org.mx.comps.rbac.dal.entity.User) DepartmentManageService(org.mx.comps.rbac.service.DepartmentManageService) 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 10 with GeneralDictAccessor

use of org.mx.dal.service.GeneralDictAccessor in project main by JohnPeng739.

the class InitializeAdminAccountTask method invokeTask.

/**
 * {@inheritDoc}
 * <p>
 * 初始化系统数据的任务,包括:admin、user、guest角色,以及admin、guest账户。
 *
 * @see InitializeTask#invokeTask()
 */
@Override
public void invokeTask() {
    GeneralDictAccessor accessor = SpringContextHolder.getBean("generalDictAccessor", GeneralDictAccessor.class);
    SessionDataStore sessionDataStore = SpringContextHolder.getBean(SessionDataStore.class);
    sessionDataStore.setCurrentUserCode("system");
    // 创建相关的角色
    createRole(accessor, "admin", "系统管理员", "系统管理员角色");
    createRole(accessor, "user", "用户", "一般业务操作用户角色");
    createRole(accessor, "guest", "客人", "客人访问角色,只能不需要权限认证的功能");
    // 创建相关的账户
    createAccount(accessor, "admin", "系统管理员", "ds110119", "系统管理员账户", "admin");
    createAccount(accessor, "guest", "客人", "guest", "客人账户", "guest");
    sessionDataStore.removeCurrentUserCode();
}
Also used : GeneralDictAccessor(org.mx.dal.service.GeneralDictAccessor) SessionDataStore(org.mx.dal.session.SessionDataStore)

Aggregations

GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)17 Test (org.junit.Test)16 User (org.mx.comps.rbac.dal.entity.User)10 UserManageService (org.mx.comps.rbac.service.UserManageService)10 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)8 Account (org.mx.comps.rbac.dal.entity.Account)7 AccountManageService (org.mx.comps.rbac.service.AccountManageService)7 Role (org.mx.comps.rbac.dal.entity.Role)6 RoleManageService (org.mx.comps.rbac.service.RoleManageService)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 ParseException (java.text.ParseException)3 Privilege (org.mx.comps.rbac.dal.entity.Privilege)3 DepartmentManageService (org.mx.comps.rbac.service.DepartmentManageService)3 Department (org.mx.comps.rbac.dal.entity.Department)2 Date (java.util.Date)1 Accredit (org.mx.comps.rbac.dal.entity.Accredit)1 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1 GeneralAccessor (org.mx.dal.service.GeneralAccessor)1 SessionDataStore (org.mx.dal.session.SessionDataStore)1