use of org.mx.comps.rbac.dal.entity.Account in project main by JohnPeng739.
the class AccountManageServiceCommonImpl method logout.
/**
* {@inheritDoc}
*
* @see AccountManageService#logout(String)
*/
@Override
public LoginHistory logout(String accountId) {
Account account = accessor.getById(accountId, Account.class);
if (account == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
List<GeneralAccessor.ConditionTuple> tuples = Arrays.asList(new GeneralAccessor.ConditionTuple("account", account), new GeneralAccessor.ConditionTuple("online", true));
List<LoginHistory> loginHistories = accessor.find(tuples, LoginHistory.class);
if (loginHistories == null || loginHistories.isEmpty()) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_LOGIN);
} else {
if (loginHistories.size() > 1) {
// 根据登录时间排序
Collections.sort(loginHistories);
}
LoginHistory loginHistory = loginHistories.get(0);
loginHistory.setLogoutTime(new Date().getTime());
loginHistory.setOnline(false);
loginHistory = accessor.save(loginHistory, false);
if (operateLogService != null) {
operateLogService.writeLog(String.format("账户[code=%s, name=%s]登出系统成功。", account.getCode(), account.getName()));
}
return loginHistory;
}
}
use of org.mx.comps.rbac.dal.entity.Account in project main by JohnPeng739.
the class AccountManageServiceCommonImpl method login.
/**
* {@inheritDoc}
*
* @see AccountManageService#login(String, String, boolean)
*/
@Override
public LoginHistory login(String accountCode, String password, boolean forced) {
Account account = accessor.getByCode(accountCode, Account.class);
if (account == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
}
try {
if (!DigestUtils.md5(password).equals(account.getPassword())) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_PASSWORD_NOT_MATCHED);
}
} catch (NoSuchAlgorithmException ex) {
if (logger.isErrorEnabled()) {
logger.error(ex);
}
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_UNSUPPORTED_OPERATE);
}
List<GeneralAccessor.ConditionTuple> tuples = Arrays.asList(new GeneralAccessor.ConditionTuple("account", account), new GeneralAccessor.ConditionTuple("online", true));
List<LoginHistory> loginHistories = accessor.find(tuples, LoginHistory.class);
LoginHistory loginHistory;
if (loginHistories != null && !loginHistories.isEmpty()) {
// 已经登录
if (forced) {
if (logger.isWarnEnabled()) {
logger.warn(String.format("The account[%s] has login, now login again.", accountCode));
}
// 强制重新登录
if (loginHistories.size() > 1) {
// 根据登录时间排序
Collections.sort(loginHistories);
}
loginHistory = loginHistories.get(0);
} else {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_ALREADY_LOGINED);
}
} else {
// 新登录
loginHistory = EntityFactory.createEntity(LoginHistory.class);
loginHistory.setAccount(account);
}
loginHistory.setLoginTime(new Date().getTime());
loginHistory.setOnline(true);
// 设置令牌
loginHistory.setToken(jwtService.sign(account.getCode()));
loginHistory = accessor.save(loginHistory, false);
if (operateLogService != null) {
operateLogService.writeLog(String.format("账户[code=%s, name=%s]登录系统成功。", account.getCode(), account.getName()));
}
return loginHistory;
}
use of org.mx.comps.rbac.dal.entity.Account 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;
}
use of org.mx.comps.rbac.dal.entity.Account in project main by JohnPeng739.
the class UserManageServiceCommonImpl method allocateAccount.
/**
* {@inheritDoc}
*
* @see UserManageService#allocateAccount(AccountManageService.AccountInfo)
*/
@Override
public Account allocateAccount(AccountManageService.AccountInfo accountInfo) {
if (accountInfo == null || StringUtils.isBlank(accountInfo.getOwnerId()) || StringUtils.isBlank(accountInfo.getCode())) {
throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
}
User user = accessor.getById(accountInfo.getOwnerId(), User.class);
if (user == null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
}
Account account = accessor.getByCode(accountInfo.getCode(), Account.class);
if (account != null) {
throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_HAS_EXIST);
}
account = accountManageService.saveAccount(accountInfo);
if (operateLogService != null) {
operateLogService.writeLog(String.format("为用户[%s]分配账户[%s]成功。", user.getFullName(), account.getCode()));
}
return account;
}
use of org.mx.comps.rbac.dal.entity.Account in project main by JohnPeng739.
the class InitializeAdminAccountTask method createAccount.
/**
* 初始化指定的账户
*
* @param accessor 实体访问器
* @param code 代码
* @param name 名称
* @param password 密码
* @param desc 描述
* @param roleCode 角色代码
*/
private void createAccount(GeneralDictAccessor accessor, String code, String name, String password, String desc, String... roleCode) {
Set<Role> roles = new HashSet<>();
if (roleCode != null && roleCode.length > 0) {
for (int index = 0; index < roleCode.length; index++) {
Role role = accessor.getByCode(roleCode[index], Role.class);
if (role == null) {
if (logger.isErrorEnabled())
logger.error(String.format("The role for %s is not existed.", roleCode));
return;
}
roles.add(role);
}
}
Account admin = accessor.getByCode(code, Account.class);
if (admin == null) {
if (logger.isInfoEnabled()) {
logger.info(String.format("The account for %s not exist, will create it.", code));
}
try {
admin = EntityFactory.createEntity(Account.class);
admin.setCode(code);
admin.setName(name);
admin.setPassword(DigestUtils.md5(password));
admin.setRoles(roles);
admin.setDesc(desc);
admin.setValid(true);
accessor.save(admin);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Create the %s account successfully.", code));
}
} catch (NoSuchAlgorithmException ex) {
if (logger.isErrorEnabled()) {
logger.error(String.format("Create the %s account fail.", code), ex);
}
}
} else {
if (logger.isInfoEnabled()) {
logger.info(String.format("The account for %s has existed, this task will ignored.", code));
}
}
}
Aggregations