|
@@ -3,19 +3,17 @@ package com.bosshand.virgo.core.service;
|
|
|
import com.alibaba.druid.util.StringUtils;
|
|
|
import com.bosshand.virgo.core.dao.*;
|
|
|
import com.bosshand.virgo.core.model.*;
|
|
|
+import com.bosshand.virgo.core.utils.Utils;
|
|
|
import com.bosshand.virgo.exception.Constant;
|
|
|
import com.bosshand.virgo.exception.ServiceException;
|
|
|
import org.apache.shiro.subject.Subject;
|
|
|
-import org.crazycake.shiro.IRedisManager;
|
|
|
-import org.crazycake.shiro.RedisSessionDAO;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -42,46 +40,58 @@ public class MgrUserService {
|
|
|
|
|
|
@Autowired
|
|
|
private MgrOrganizationTypeRoleDao mgrOrganizationTypeRoleDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private OperationRecordDao operationRecordDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private OperationRecordHistoryDao operationRecordHistoryDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RedisSessionDAO redisSessionDAO;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AppUserTokenDao appUserTokenDao;
|
|
|
|
|
|
static Logger log = LoggerFactory.getLogger(MgrUserService.class);
|
|
|
-
|
|
|
- public MgrUser getById(long id) {
|
|
|
-
|
|
|
- return mgrUserDao.getById(id);
|
|
|
+
|
|
|
+ public void delete(int userId) {
|
|
|
+ mgrUserDao.delete(userId);
|
|
|
}
|
|
|
- public MgrUser getByUserNameOrPhone(String userName) {
|
|
|
- MgrUser user = mgrUserDao.getByUserName(userName);
|
|
|
- if(user == null) {
|
|
|
- user = mgrUserDao.getByPhone(userName);
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void register(MgrUser user, long typeId) {
|
|
|
+ MgrOrganizationType type = mgrOrganizationTypeDao.get(typeId);
|
|
|
+ if (type == null) {
|
|
|
+ throw new ServiceException("Organization Type not found", Constant.RET_BAD_REQUEST);
|
|
|
}
|
|
|
- return user;
|
|
|
-
|
|
|
+ MgrOrganization mgrOrganization = new MgrOrganization();
|
|
|
+ mgrOrganization.setMgrOrganizationTypeId(typeId);
|
|
|
+ mgrOrganization.setMgrOrganizationTypeName(type.getName());
|
|
|
+ mgrOrganization.setName(null);
|
|
|
+ mgrOrganization.setRegisteDate(new Date());
|
|
|
+ mgrOrganization.setContact(user.getName());
|
|
|
+ mgrOrganization.setContactTel(user.getPhone());
|
|
|
+ mgrOrganization.setOrganizationCode(Utils.getPinYinHeadChar(type.getName() + Utils.random(8)));
|
|
|
+ mgrOrganizationDao.insert(mgrOrganization);
|
|
|
+
|
|
|
+ user.setLastOrganizationId(mgrOrganization.getId());
|
|
|
+
|
|
|
+ long userId = 0;
|
|
|
+
|
|
|
+ MgrUser us = mgrUserDao.getByPhone(user.getPhone());
|
|
|
+ if (us != null) {
|
|
|
+ us.setLastOrganizationId(mgrOrganization.getId());
|
|
|
+ mgrUserDao.updateLastOrganizationId(us);
|
|
|
+ userId = us.getId();
|
|
|
+ } else {
|
|
|
+ mgrUserDao.insert(user);
|
|
|
+ userId = user.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ MgrUserRole userRole = new MgrUserRole();
|
|
|
+ userRole.setAdmin(true);
|
|
|
+ userRole.setOrganizationId(mgrOrganization.getId());
|
|
|
+ userRole.setUserId(userId);
|
|
|
+ mgrUserRoleDao.insert(userRole);
|
|
|
}
|
|
|
|
|
|
- public MgrUser getByUserName(String userName) {
|
|
|
- return mgrUserDao.getByUserName(userName);
|
|
|
+ public MgrUser getById(long id) {
|
|
|
+ return mgrUserDao.getById(id);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public MgrUser getByPhone(String phone) {
|
|
|
return mgrUserDao.getByPhone(phone);
|
|
|
}
|
|
|
-
|
|
|
- public MgrOrganization getOrganization(long organizationId) {
|
|
|
- return mgrOrganizationDao.getById(organizationId);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public MgrOrganization getLastOrganization(MgrUser user) {
|
|
|
if(user == null || user.getLastOrganizationId() == -1) {
|
|
|
return null;
|
|
@@ -90,7 +100,6 @@ public class MgrUserService {
|
|
|
}
|
|
|
|
|
|
public List<MgrOrganization> getOrganizationList(MgrUser user){
|
|
|
-
|
|
|
if(this.isSupperAdmin(user)) {
|
|
|
return mgrOrganizationDao.listAll();
|
|
|
}
|
|
@@ -98,19 +107,14 @@ public class MgrUserService {
|
|
|
List<MgrUserRole> userRoleList = getUserRoles(user);
|
|
|
List<Long> ids = new LinkedList<Long>();
|
|
|
userRoleList.forEach(r -> ids.add(r.getOrganizationId()));
|
|
|
-
|
|
|
return mgrOrganizationDao.listByIds(ids);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public List<MgrUserRole> getUserRoles(MgrUser user){
|
|
|
-
|
|
|
List<MgrUserRole> roleList = new LinkedList<MgrUserRole>();
|
|
|
-
|
|
|
if(user != null ) {
|
|
|
return mgrUserRoleDao.getUserRoles(user.getId());
|
|
|
}
|
|
|
-
|
|
|
return roleList;
|
|
|
}
|
|
|
|
|
@@ -120,39 +124,31 @@ public class MgrUserService {
|
|
|
}
|
|
|
|
|
|
public boolean isAdmin(MgrUser user, long organizationId) {
|
|
|
-
|
|
|
MgrUserRole userRole = getUserRole(user, organizationId);
|
|
|
if(userRole == null) {
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
return userRole.isAdmin();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public MgrUserRole getUserRole(MgrUser user, long organizationId) {
|
|
|
-
|
|
|
List<MgrUserRole> userRoles = this.getUserRoles(user);
|
|
|
- if(userRoles == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- for(MgrUserRole userRole: userRoles) {
|
|
|
- if(userRole.getOrganizationId() == organizationId) {
|
|
|
- return userRole;
|
|
|
+ if (userRoles != null) {
|
|
|
+ for (MgrUserRole userRole : userRoles) {
|
|
|
+ if (userRole.getOrganizationId() == organizationId) {
|
|
|
+ return userRole;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
- public List<MgrRoleCode> getRoleCodeList(MgrUserRole userRole){
|
|
|
-
|
|
|
- List<MgrRoleCode> roleCodeList = null;
|
|
|
- if(userRole!=null && !StringUtils.isEmpty(userRole.getRoles())) {
|
|
|
+
|
|
|
+ public List<MgrRoleCode> getRoleCodeList(MgrUserRole userRole) {
|
|
|
+ if (userRole != null && !StringUtils.isEmpty(userRole.getRoles())) {
|
|
|
String[] roleIds = userRole.getRoles().split(",");
|
|
|
return mgrRoleCodeDao.getListByIds(roleIds);
|
|
|
}
|
|
|
- return roleCodeList;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
public List<MgrOrganizationTypeRole> getOrganizationTypeRoleList(long organizationTypeId){
|
|
@@ -177,63 +173,16 @@ public class MgrUserService {
|
|
|
return mgrOrganizationTypeDao.getById(id);
|
|
|
}
|
|
|
|
|
|
- @Transactional
|
|
|
- public void forcedOffline(int id) {
|
|
|
- OperationRecord operationRecord = operationRecordDao.getId(id);
|
|
|
- StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
- IRedisManager redisManager = redisSessionDAO.getRedisManager();
|
|
|
- redisManager.del(stringRedisSerializer.serialize(redisSessionDAO.getKeyPrefix() + operationRecord.getToken()));
|
|
|
- operationRecordDao.delete(id);
|
|
|
- operationRecordHistoryDao.deleteByOperationRecordId(id);
|
|
|
- }
|
|
|
-
|
|
|
- public void saveOperationRecordHistory(OperationRecordHistory operationRecordHistory, String token) {
|
|
|
- OperationRecord operationRecord = operationRecordDao.getByToken(token);
|
|
|
- if(operationRecord == null) {
|
|
|
- throw new ServiceException("fail to get token", Constant.RET_UNKNOWN);
|
|
|
- }
|
|
|
- operationRecordHistory.setOperationRecordId(operationRecord.getId());
|
|
|
- operationRecordHistoryDao.insert(operationRecordHistory);
|
|
|
- }
|
|
|
-
|
|
|
- public List<OperationRecord> operationRecordList(int currentPage, int pageSize) {
|
|
|
- List<OperationRecord> dataList = operationRecordDao.getList();
|
|
|
- List<OperationRecord> currentPageList = new ArrayList<>();
|
|
|
- if (dataList != null && dataList.size() > 0) {
|
|
|
- int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
|
|
|
- for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
|
|
|
- OperationRecord data = dataList.get(currIdx + i);
|
|
|
- currentPageList.add(data);
|
|
|
- }
|
|
|
- }
|
|
|
- return currentPageList;
|
|
|
- }
|
|
|
-
|
|
|
- public List<OperationRecord> operationRecordList() {
|
|
|
- return operationRecordDao.getList();
|
|
|
- }
|
|
|
-
|
|
|
- public void saveOperationRecord(String equipment, String ip, Subject subject) {
|
|
|
- OperationRecord operationRecord = new OperationRecord();
|
|
|
- operationRecord.setEquipment(equipment);
|
|
|
- operationRecord.setIp(ip);
|
|
|
- operationRecord.setToken(subject.getSession().getId().toString());
|
|
|
- operationRecord.setName(this.getByUserNameOrPhone(subject.getPrincipal().toString()).getPhone());
|
|
|
- operationRecordDao.insert(operationRecord);
|
|
|
- }
|
|
|
-
|
|
|
//store user context into session. This is not good place to put those code.
|
|
|
public void loadContext(Subject subject) {
|
|
|
-
|
|
|
if(subject == null) {
|
|
|
throw new ServiceException("fail to get subject", Constant.RET_NO_SPECIFIC_USER);
|
|
|
}
|
|
|
-
|
|
|
- MgrUser user = this.getByUserNameOrPhone(subject.getPrincipal().toString());
|
|
|
+ MgrUser user = this.getByPhone(subject.getPrincipal().toString());
|
|
|
if(user == null) {
|
|
|
throw new ServiceException("fail to get user", Constant.RET_NO_SPECIFIC_USER);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//process organization
|
|
|
List<MgrOrganization> organizationList = getOrganizationList(user);
|
|
|
user.setOrganizationList(organizationList);
|
|
@@ -245,7 +194,7 @@ public class MgrUserService {
|
|
|
currentOrganization = null;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//load first organization in list if no organization loaded.
|
|
|
if(currentOrganization == null) {
|
|
|
//Load the first organization default.
|
|
@@ -296,18 +245,9 @@ public class MgrUserService {
|
|
|
subject.getSession().setTimeout(25920000000L);
|
|
|
subject.getSession().setAttribute("USER",user);
|
|
|
|
|
|
- UserContext context = UserContext.getUserContext(user, organizationList, currentOrganization, currentUserRole, roleCodeList, organizationTypeRoles);
|
|
|
+ UserContext context = UserContext.getUserContext(user, organizationList, currentOrganization, currentUserRole, roleCodeList, organizationTypeRoles);
|
|
|
subject.getSession().setAttribute("USERCONTEXT", context);
|
|
|
|
|
|
}
|
|
|
|
|
|
- //save login equip information
|
|
|
- public void saveAppUserToken(AppUserToken appUserToken) {
|
|
|
- appUserTokenDao.insert(appUserToken);
|
|
|
- }
|
|
|
-
|
|
|
- public void cancellationUser(int userId) {
|
|
|
- mgrUserDao.delete(userId);
|
|
|
- }
|
|
|
-
|
|
|
}
|