Search in sources :

Example 6 with Role

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

the class TestRole method testEditRole.

public static void testEditRole(GeneralDictAccessor service, RoleManageService roleService) {
    RoleManageService.RoleInfo roleInfo = RoleManageService.RoleInfo.valueOf("role3", "role3", "description");
    Role role3 = roleService.saveRole(roleInfo);
    assertEquals(3, service.count(Role.class));
    role3 = service.getById(role3.getId(), Role.class);
    assertNotNull(role3);
    assertNotNull(role3.getId());
    role3Id = role3.getId();
    assertEquals("role3", role3.getName());
    assertEquals("description", role3.getDesc());
    assertTrue(role3.isValid());
    roleInfo = RoleManageService.RoleInfo.valueOf("role3", "new name", "new desc", role3.getId(), Arrays.asList(), Arrays.asList(), false);
    roleService.saveRole(roleInfo);
    assertEquals(2, service.count(Role.class));
    assertEquals(3, service.count(Role.class, false));
    role3 = service.getById(role3.getId(), Role.class);
    assertNotNull(role3);
    assertNotNull(role3.getId());
    role3Id = role3.getId();
    assertEquals("new name", role3.getName());
    assertEquals("new desc", role3.getDesc());
    assertFalse(role3.isValid());
    roleInfo = RoleManageService.RoleInfo.valueOf("role3", "role3", "new desc", role3.getId(), Arrays.asList(), Arrays.asList(), true);
    roleService.saveRole(roleInfo);
    assertEquals(3, service.count(Role.class));
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) RoleManageService(org.mx.comps.rbac.service.RoleManageService)

Example 7 with Role

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

the class AccountManageServiceCommonImpl method saveAccount.

/**
 * {@inheritDoc}
 *
 * @see AccountManageService#saveAccount(AccountInfo)
 */
@Override
public Account saveAccount(AccountInfo accountInfo) {
    if (accountInfo == null) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    try {
        String accountId = accountInfo.getAccountId();
        Account account;
        if (!StringUtils.isBlank(accountId)) {
            account = accessor.getById(accountId, Account.class);
            if (account == null) {
                if (logger.isErrorEnabled()) {
                    logger.error(String.format("The Account entity[%s] not found.", accountId));
                }
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
            }
        // 这里不允许修改密码,密码必须通过另外途径进行修改
        } else {
            String password = accountInfo.getPassword();
            if (StringUtils.isBlank(password)) {
                password = "ds110119";
            }
            account = EntityFactory.createEntity(Account.class);
            account.setPassword(DigestUtils.md5(password));
        }
        account.setCode(accountInfo.getCode());
        if (StringUtils.isBlank(accountInfo.getOwnerId())) {
            if (!"admin".equals(accountInfo.getCode())) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOALLOCATE_USER);
            }
        } else {
            User owner = accessor.getById(accountInfo.getOwnerId(), User.class);
            if (owner == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
            }
            account.setOwner(owner);
            account.setName(owner.getFullName());
        }
        account.setDesc(accountInfo.getDesc());
        if (account.getRoles() != null && !account.getRoles().isEmpty()) {
            account.getRoles().clear();
        }
        for (String roleId : accountInfo.getRoleIds()) {
            Role role = accessor.getById(roleId, Role.class);
            if (role == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
            }
            account.getRoles().add(role);
        }
        account.setValid(accountInfo.isValid());
        account = this.save(account);
        if (operateLogService != null) {
            operateLogService.writeLog(String.format("保存账户[code=%s, name=%s]成功。", account.getCode(), account.getName()));
        }
        return account;
    } catch (UserInterfaceDalErrorException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    } catch (NoSuchAlgorithmException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
    }
}
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) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 8 with Role

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

the class AccreditManageServiceCommonImpl method accredit.

/**
 * {@inheritDoc}
 *
 * @see AccreditManageService#accredit(AccreditInfo)
 */
@Override
public Accredit accredit(AccreditInfo accreditInfo) {
    if (accreditInfo == null || StringUtils.isBlank(accreditInfo.getSrcAccountId()) || StringUtils.isBlank(accreditInfo.getTarAccountId()) || accreditInfo.getRoleIds() == null || accreditInfo.getRoleIds().isEmpty()) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    // 判断是否存在相同的有效授权
    if (hasSameAccredit(accreditInfo)) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCREDIT_SAME_FOUND);
    }
    Account src = accessor.getById(accreditInfo.getSrcAccountId(), Account.class);
    if (src == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
    }
    Account tar = accessor.getById(accreditInfo.getTarAccountId(), Account.class);
    if (tar == null) {
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
    }
    Set<Role> roles = new HashSet<>();
    for (String roleId : accreditInfo.getRoleIds()) {
        Role role = accessor.getById(roleId, Role.class);
        if (role == null) {
            throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
        }
        roles.add(role);
    }
    Accredit accredit = EntityFactory.createEntity(Accredit.class);
    accredit.setSrc(src);
    accredit.setTar(tar);
    accredit.setRoles(roles);
    accredit.setStartTime(new Date(accreditInfo.getStartTime()));
    if (accreditInfo.getEndTime() > 0 && accreditInfo.getEndTime() > accreditInfo.getStartTime()) {
        accredit.setEndTime(new Date(accreditInfo.getEndTime()));
    }
    accredit.setValid(true);
    accredit.setDesc(accreditInfo.getDesc());
    accredit = this.save(accredit);
    if (operateLogService != null) {
        operateLogService.writeLog(String.format("新增授权[%s=>%s]成功。", accredit.getSrc().getName(), accredit.getTar().getName()));
    }
    return accredit;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) Accredit(org.mx.comps.rbac.dal.entity.Accredit) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) Date(java.util.Date) HashSet(java.util.HashSet)

Example 9 with Role

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

the class AccountManageServiceImpl method save.

/**
 * {@inheritDoc}
 * @see AccountManageServiceCommonImpl#save(Account)
 */
@Override
protected Account save(Account account) {
    Set<Role> oldRoles = new HashSet<>();
    if (!StringUtils.isBlank(account.getId())) {
        oldRoles.addAll(accessor.getById(account.getId(), Account.class).getRoles());
    }
    account = accessor.save(account, false);
    Set<Role> roles = account.getRoles();
    for (Role role : roles) {
        if (oldRoles.contains(role)) {
            oldRoles.remove(role);
            continue;
        } else {
            role.getAccounts().add(account);
            accessor.save(role, false);
        }
    }
    for (Role role : oldRoles) {
        role.getAccounts().remove(role);
        accessor.save(role, false);
    }
    return account;
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) HashSet(java.util.HashSet)

Example 10 with Role

use of org.mx.comps.rbac.dal.entity.Role 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", accessor.getById(accreditInfo.getSrcAccountId(), Account.class)));
    conditions.add(new GeneralAccessor.ConditionTuple("tar", accessor.getById(accreditInfo.getTarAccountId(), Account.class)));
    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

Role (org.mx.comps.rbac.dal.entity.Role)22 Account (org.mx.comps.rbac.dal.entity.Account)9 UserInterfaceSystemErrorException (org.mx.error.UserInterfaceSystemErrorException)9 RoleManageService (org.mx.comps.rbac.service.RoleManageService)7 UserInterfaceRbacErrorException (org.mx.comps.rbac.error.UserInterfaceRbacErrorException)6 Test (org.junit.Test)5 GeneralDictAccessor (org.mx.dal.service.GeneralDictAccessor)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 HashSet (java.util.HashSet)4 AuthenticateAround (org.mx.comps.jwt.AuthenticateAround)4 Accredit (org.mx.comps.rbac.dal.entity.Accredit)4 Privilege (org.mx.comps.rbac.dal.entity.Privilege)4 User (org.mx.comps.rbac.dal.entity.User)4 RoleVO (org.mx.comps.rbac.rest.vo.RoleVO)4 UserInterfaceException (org.mx.error.UserInterfaceException)4 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)4 AccountManageService (org.mx.comps.rbac.service.AccountManageService)3 UserManageService (org.mx.comps.rbac.service.UserManageService)3 DataVO (org.mx.service.rest.vo.DataVO)3 ArrayList (java.util.ArrayList)2