Search in sources :

Example 11 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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 12 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.

the class JwtService method init.

/**
 * 初始化
 */
private void init() {
    if (hasInitialized) {
        return;
    }
    String algorithmName = env.getProperty("auth.algorithm", "HS256");
    issue = env.getProperty("auth.issue", "mx institute");
    subject = env.getProperty("auth.subject", "everyone");
    String expired = env.getProperty("auth.expired", "2 Sec");
    expiredClock = env.getProperty("auth.expiredClock", Integer.class, -1);
    timePeriod = StringUtils.stirng2TimePeriod(expired, StringUtils.DAY);
    try {
        String secret = "john_73_9@hotmail.com";
        switch(algorithmName) {
            case "HS256":
                algorithm = Algorithm.HMAC256(secret);
                break;
            case "HS384":
                algorithm = Algorithm.HMAC384(secret);
                break;
            case "HS512":
                algorithm = Algorithm.HMAC512(secret);
                break;
            default:
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.UNSUPPORTED_ALGORITHM);
        }
        verifier = JWT.require(algorithm).withIssuer(issue).withSubject(subject).acceptLeeway(1).acceptExpiresAt(1).build();
        hasInitialized = true;
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Initialize a JWT instance, algorithm: %s, issue: %s, subject: %s.", algorithmName, issue, subject));
        }
    } catch (UnsupportedEncodingException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Initialize JWT fail.", ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.UNSUPPORTED_ALGORITHM);
    }
}
Also used : UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 13 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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 14 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException 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 15 with UserInterfaceRbacErrorException

use of org.mx.comps.rbac.error.UserInterfaceRbacErrorException in project main by JohnPeng739.

the class DepartmentManageServiceCommonImpl method saveDepartment.

/**
 * {@inheritDoc}
 *
 * @see DepartmentManageService#saveDepartment(DepartInfo)
 */
@Override
public Department saveDepartment(DepartInfo departInfo) {
    if (departInfo == null) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    String id = departInfo.getId();
    Department department;
    if (!StringUtils.isBlank(id)) {
        department = accessor.getById(id, Department.class);
        if (department == null) {
            throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.DEPARTMENT_NOT_FOUND);
        }
    } else {
        department = EntityFactory.createEntity(Department.class);
    }
    department.setCode(departInfo.getCode());
    department.setName(departInfo.getName());
    department.setDesc(departInfo.getDesc());
    if (StringUtils.isBlank(departInfo.getManagerId())) {
        department.setManager(null);
    } else {
        User manager = accessor.getById(departInfo.getManagerId(), User.class);
        if (manager == null) {
            throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
        }
        department.setManager(manager);
    }
    if (department.getEmployees() != null && !department.getEmployees().isEmpty()) {
        department.getEmployees().clear();
    }
    if (departInfo.getEmployeeIds() != null) {
        for (String employeeId : departInfo.getEmployeeIds()) {
            User employee = accessor.getById(employeeId, User.class);
            if (employee == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
            }
            department.getEmployees().add(employee);
        }
    }
    department.setValid(departInfo.isValid());
    department = this.save(department);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("保存部门[code=%s, name=%s]信息成功。", departInfo.getCode(), departInfo.getName()));
    }
    return department;
}
Also used : Department(org.mx.comps.rbac.dal.entity.Department) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException)

Aggregations

UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)16 Account (org.mx.comps.rbac.dal.entity.Account)12 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)10 User (org.mx.comps.rbac.dal.entity.User)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 Date (java.util.Date)5 Role (org.mx.comps.rbac.dal.entity.Role)5 Test (org.junit.Test)3 Accredit (org.mx.comps.rbac.dal.entity.Accredit)3 LoginHistory (org.mx.comps.rbac.dal.entity.LoginHistory)3 AccountManageService (org.mx.comps.rbac.service.AccountManageService)3 UserManageService (org.mx.comps.rbac.service.UserManageService)3 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)3 Department (org.mx.comps.rbac.dal.entity.Department)2 GeneralAccessor (org.mx.dal.service.GeneralAccessor)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ParseException (java.text.ParseException)1 HashSet (java.util.HashSet)1 Privilege (org.mx.comps.rbac.dal.entity.Privilege)1 AccreditManageService (org.mx.comps.rbac.service.AccreditManageService)1