SystemService.java 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. /**
  2. * Copyright &copy; 2013-2017 <a href="http://www.rhcncpa.com/">瑞华会计师事务所</a> All rights reserved.
  3. */
  4. package com.jeeplus.modules.sys.service;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import com.jeeplus.common.bos.BOSClientUtil;
  8. import com.jeeplus.common.config.Global;
  9. import com.jeeplus.common.json.AjaxJson;
  10. import com.jeeplus.common.oss.OSSClientUtil;
  11. import com.jeeplus.common.persistence.Page;
  12. import com.jeeplus.common.security.Digests;
  13. import com.jeeplus.common.security.shiro.session.JedisSessionDAO;
  14. import com.jeeplus.common.security.shiro.session.SessionDAO;
  15. import com.jeeplus.common.service.BaseService;
  16. import com.jeeplus.common.utils.*;
  17. import com.jeeplus.modules.modules.dao.ModuleChooseDao;
  18. import com.jeeplus.modules.modules.entity.ModuleChoose;
  19. import com.jeeplus.modules.sys.dao.*;
  20. import com.jeeplus.modules.sys.entity.*;
  21. import com.jeeplus.modules.sys.security.SystemAuthorizingRealm;
  22. import com.jeeplus.modules.sys.utils.DictUtils;
  23. import com.jeeplus.modules.sys.utils.LogUtils;
  24. import com.jeeplus.modules.sys.utils.UserUtils;
  25. import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
  26. import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
  27. import com.jeeplus.modules.tools.utils.TwoDimensionCode;
  28. import com.jeeplus.modules.utils.ErrorCode;
  29. import org.apache.shiro.SecurityUtils;
  30. import org.apache.shiro.realm.Realm;
  31. import org.apache.shiro.session.Session;
  32. import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
  33. import org.springframework.beans.factory.InitializingBean;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Service;
  36. import org.springframework.transaction.annotation.Transactional;
  37. import redis.clients.jedis.Jedis;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import java.net.HttpURLConnection;
  41. import java.net.MalformedURLException;
  42. import java.net.URL;
  43. import java.util.*;
  44. /**
  45. * 系统管理,安全相关实体的管理类,包括用户、岗位、菜单.
  46. *
  47. * @author jeeplus
  48. * @version 2013-12-05
  49. */
  50. @Service
  51. @Transactional(readOnly = true)
  52. public class SystemService extends BaseService implements InitializingBean {
  53. public static final String HASH_ALGORITHM = "SHA-1";
  54. public static final int HASH_INTERATIONS = 1024;
  55. public static final int SALT_SIZE = 8;
  56. private String sessionKeyPrefix = Global.getConfig("redis.keyPrefix") + "_session_";
  57. @Autowired
  58. private UserDao userDao;
  59. @Autowired
  60. private RoleDao roleDao;
  61. @Autowired
  62. private SysRoleActivityDao roleActivityDao;
  63. @Autowired
  64. private MenuDao menuDao;
  65. @Autowired
  66. private OfficeDao officeDao;
  67. @Autowired
  68. private SessionDAO sessionDao;
  69. @Autowired
  70. private ModuleChooseDao moduleChooseDao;
  71. @Autowired
  72. private UserofficeService userofficeService;
  73. @Autowired
  74. private OfficeService officeService;
  75. @Autowired
  76. private DictService dictService;
  77. public SessionDAO getSessionDao() {
  78. return sessionDao;
  79. }
  80. // -- User Service --//
  81. /**
  82. * 获取岗位
  83. *
  84. * @param user
  85. * @return
  86. */
  87. public List<Role> getRoleListByUser(User user,Office company) {
  88. Role role = new Role();
  89. role.setCompany(company);
  90. role.setUser(user);
  91. List<Role> roleList = roleDao.findList(role);
  92. return roleList;
  93. }
  94. /**
  95. * 获取用户
  96. *
  97. * @param id
  98. * @return
  99. */
  100. public User getUser(String id) {
  101. return UserUtils.get(id);
  102. }
  103. /**
  104. * 根据登录名获取用户
  105. *
  106. * @param loginName
  107. * @return
  108. */
  109. public User getUserByLoginName(String loginName) {
  110. return UserUtils.getByLoginName(loginName);
  111. }
  112. public Page<User> findUser(Page<User> page, User user) {
  113. // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL)
  114. user.getSqlMap().put("dsf",
  115. dataScopeFilter(user.getCurrentUser(), "o", "a","s",MenuStatusEnum.USER.getValue()));
  116. // 设置分页参数
  117. user.setPage(page);
  118. if (user.getOffice()!=null&&StringUtils.isNotBlank(user.getOffice().getId())) {
  119. Office office = officeService.get(user.getOffice().getId());
  120. if (office!=null&&"1".equals(office.getType())) {
  121. user.setCompany(office);
  122. user.setOffice(null);
  123. }
  124. }
  125. // 执行分页查询
  126. if (user.getCompany() == null || StringUtils.isBlank(user.getCompany().getId())){
  127. user.setCompany(UserUtils.getSelectCompany());
  128. }
  129. List<User> users = userDao.findList(user);
  130. /* if (users!=null && users.size()!=0){
  131. for (User u :users){
  132. if (u.getBranchOffice()!=null && StringUtils.isNotBlank(u.getBranchOffice().getId())){
  133. Office branchOffice = officeService.get(u.getBranchOffice().getId());
  134. u.setBranchOffice(branchOffice);
  135. }
  136. }
  137. }*/
  138. page.setList(users);
  139. return page;
  140. }
  141. public Page<User> findUserByCompany(Page<User> page, User user) {
  142. // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL)
  143. user.getSqlMap().put("dsf",
  144. dataScopeFilter(user.getCurrentUser(), "o", "a","s", MenuStatusEnum.USER.getValue()));
  145. // 设置分页参数
  146. user.setPage(page);
  147. if (user.getOffice()!=null&&StringUtils.isNotBlank(user.getOffice().getId())) {
  148. Office office = officeService.get(user.getOffice().getId());
  149. if (office!=null&&"1".equals(office.getType())) {
  150. user.setCompany(office);
  151. user.setOffice(null);
  152. }
  153. }
  154. // 执行分页查询
  155. if (user.getCompany() == null || StringUtils.isBlank(user.getCompany().getId())){
  156. user.setCompany(UserUtils.getSelectCompany());
  157. }
  158. List<User> users = userDao.findListByComany(user);
  159. /* if (users!=null && users.size()!=0){
  160. for (User u :users){
  161. if (u.getBranchOffice()!=null && StringUtils.isNotBlank(u.getBranchOffice().getId())){
  162. Office branchOffice = officeService.get(u.getBranchOffice().getId());
  163. u.setBranchOffice(branchOffice);
  164. }
  165. }
  166. }*/
  167. page.setList(users);
  168. return page;
  169. }
  170. public Page<User> findUserByRoleOffice(Page<User> page, User user) {
  171. user.setPage(page);
  172. // 执行分页查询
  173. if(!UserUtils.getUser().isAdmin()){
  174. // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL)
  175. user.getSqlMap().put("dsf",
  176. dataScopeFilter(user.getCurrentUser(), "o", "a","s", MenuStatusEnum.USER.getValue()));
  177. // 设置分页参数
  178. if (user.getCompany() == null || StringUtils.isBlank(user.getCompany().getId())){
  179. user.setCompany(UserUtils.getSelectCompany());
  180. }
  181. }
  182. page.setList(userDao.findList(user));
  183. return page;
  184. }
  185. public Page<User> findUser2(Page<User> page, User user) {
  186. // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL)
  187. user.getSqlMap().put("dsf",
  188. dataScopeFilter(user.getCurrentUser(), "o", "a","s", MenuStatusEnum.USER.getValue()));
  189. // 设置分页参数
  190. user.setPage(page);
  191. // 执行分页查询
  192. if (user.getCompany() == null || StringUtils.isBlank(user.getCompany().getId())){
  193. user.setCompany(UserUtils.getSelectCompany());
  194. }
  195. List<User> ulist= userDao.findList(user);
  196. List<User> ulist1 = new ArrayList<User>();
  197. for (User u:
  198. ulist) {
  199. if (u.isAdmin()){
  200. List<Role> roleList = roleDao.findAllList(new Role());
  201. u.setRoleList(roleList);
  202. }else{
  203. Role role = new Role();
  204. role.setUser(u);
  205. List<Role> roleList = roleDao.findList(role);
  206. u.setRoleList(roleList);
  207. }
  208. ulist1.add(u);
  209. }
  210. page.setList(ulist1);
  211. return page;
  212. }
  213. /**
  214. * 无分页查询人员列表
  215. *
  216. * @param user
  217. * @return
  218. */
  219. public List<User> findUser(User user) {
  220. // 生成数据权限过滤条件(dsf为dataScopeFilter的简写,在xml中使用 ${sqlMap.dsf}调用权限SQL)
  221. if(!UserUtils.getUser().isAdmin()){
  222. user.getSqlMap().put("dsf",
  223. dataScopeFilter(user.getCurrentUser(), "o", "a","s", MenuStatusEnum.USER.getValue()));
  224. if (user.getCompany() == null || StringUtils.isBlank(user.getCompany().getId())){
  225. user.setCompany(UserUtils.getSelectCompany());
  226. }
  227. }
  228. List<User> list = userDao.findList(user);
  229. return list;
  230. }
  231. /**
  232. * 根据别名查询岗位
  233. *
  234. * @param role
  235. * @return
  236. */
  237. /*public Role getByEnname(Role role) {
  238. try {
  239. List<Role> roleList = roleDao.getByOffice(role);
  240. if (role.getOffice()!=null && StringUtils.isNotBlank(role.getOffice().getId())){
  241. return roleList.get(0);
  242. }else {
  243. }
  244. return role;
  245. }catch (Exception e){
  246. return null;
  247. }
  248. }*/
  249. /**
  250. * 通过部门ID获取用户列表,仅返回用户id和name(树查询用户时用)
  251. *
  252. * @return
  253. */
  254. public List<User> findUserByOfficeId(String officeId) {
  255. User user = new User();
  256. user.setOffice(new Office(officeId));
  257. if(!UserUtils.getSelectCompany().getId().equals("1")) {
  258. user.setCompany(UserUtils.getSelectCompany());
  259. }
  260. List<User> list = userDao.findUserByOfficeId(user);
  261. CacheUtils.put(UserUtils.USER_CACHE,
  262. UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_ + officeId, list);
  263. return list;
  264. }
  265. /**
  266. * 通过部门ID获取用户列表,仅返回用户id和name(树查询用户时用)
  267. *
  268. * @return
  269. */
  270. public List<User> findUserByOffices(Office office,Office company) {
  271. User user = new User();
  272. user.setOffice(office);
  273. user.setCompany(company);
  274. List<User> list = userDao.findUserByOffices(user);
  275. return list;
  276. }
  277. /**
  278. * 通过部门ID获取用户列表,仅返回用户id和name(树查询用户时用)
  279. * @return
  280. */
  281. public List<User> findUserByCompanyId(String companyId) {
  282. User user = new User();
  283. user.setCompany(new Office(companyId));
  284. List<User> userLists = new ArrayList<>();
  285. Set<String> set = new HashSet<>();
  286. user.setCompany(new Office(companyId));
  287. List<User> list = userDao.findUserByOfficeId(user);
  288. for (User users : list){
  289. set.add(users.getId());
  290. }
  291. List<Office> officeList = officeDao.findAllListByParentIds(new Office(companyId));
  292. for (Office office : officeList){
  293. User u = new User();
  294. u.setCompany(office);
  295. List<User> userList = userDao.findUserByOfficeId(u);
  296. for (User users : userList){
  297. set.add(users.getId());
  298. }
  299. }
  300. for (String id:set){
  301. userLists.add(UserUtils.get(id));
  302. }
  303. return userLists;
  304. }
  305. @Transactional(readOnly = false)
  306. public User saveUserInfo(User user) {
  307. if (StringUtils.isBlank(user.getId())) {
  308. user.preInsert();
  309. userDao.insert(user);
  310. } else {
  311. // 清除原用户机构用户缓存
  312. User oldUser = userDao.get(user.getId());
  313. if (oldUser.getOffice() != null
  314. && oldUser.getOffice().getId() != null) {
  315. CacheUtils.remove(UserUtils.USER_CACHE,
  316. UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_
  317. + oldUser.getOffice().getId());
  318. CacheUtils.remove(UserUtils.USER_CACHE,
  319. UserUtils.CACHE_COMPANY
  320. + oldUser.getComId());
  321. }
  322. // 更新用户数据
  323. user.preUpdate();
  324. userDao.update(user);
  325. }
  326. ModuleChoose mc=new ModuleChoose();
  327. mc.setUser(user);
  328. StringBuffer moduleIds =new StringBuffer();
  329. Dict dict = new Dict();
  330. dict.setType("module_choose");
  331. List<Dict> list = dictService.findList(dict);
  332. if(list.size()>0){
  333. for (Dict dict2 : list) {
  334. moduleIds.append(dict2.getValue()+",");
  335. }
  336. }
  337. mc.setModuleId(moduleIds.toString());
  338. mc.preInsert();
  339. moduleChooseDao.insert(mc);
  340. if(StringUtils.isNotBlank(user.getId())){
  341. UserUtils.clearCache(user);
  342. removeSession(user);
  343. }
  344. return user;
  345. }
  346. @Transactional(readOnly = false)
  347. public AjaxJson registerUser(String mobile,String randomCode,String name,String password,String contextPath,String urls) {
  348. AjaxJson j = new AjaxJson();
  349. long time = System.currentTimeMillis();
  350. User user = new User();
  351. try {
  352. // 验证手机号是否已经注册
  353. if (userDao.findUniqueByProperty("mobile", mobile) != null) {
  354. // 如果是手机登录,则返回JSON字符串
  355. j.setSuccess(false);
  356. j.setErrorCode(ErrorCode.code_1007);
  357. j.setMsg("手机号已经被使用!");
  358. return j;
  359. }
  360. // // 验证短信内容
  361. // if (!randomCode.equals(request.getSession().getServletContext().getAttribute(mobile))) {
  362. // // 如果是手机登录,则返回JSON字符串
  363. // j.setSuccess(false);
  364. // j.setErrorCode("3");
  365. // j.setMsg("手机验证码不正确!");
  366. // return j;
  367. // }
  368. // 修正引用赋值问题,不知道为何,Company和Office引用的一个实例地址,修改了一个,另外一个跟着修改。
  369. List<Dict> dictList = DictUtils.getDictList("defalut_role");
  370. Dict dict = dictList.get(0);
  371. Role role = this.getRole(dict.getValue());
  372. // 密码MD5加密
  373. user.setPasswordRemake(password);
  374. user.setPassword(SystemService.entryptPassword(password));
  375. user.setLoginName(mobile);
  376. // 岗位数据有效性验证,过滤不在授权内的岗位
  377. /*
  378. * //保存机构 user.setCompany(office); user.setOffice(office);
  379. */
  380. // 生成用户二维码,使用登录名
  381. String realPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + time + "/qrcode/";
  382. FileUtils.createDirectory(realPath);
  383. String picName = time + ".png"; // encoderImgId此处二维码的图片名
  384. String filePath = realPath + picName; // 存放路径
  385. TwoDimensionCode.encoderQRCode(user.getLoginName(), filePath, "png");// 执行生成二维码
  386. user.setQrCode(contextPath + Global.USERFILES_BASE_URL + time + "/qrcode/" + picName);
  387. if (user.getQrCode() != null && !user.getQrCode().equals("")) {
  388. InputStream inputStream = null;
  389. HttpURLConnection httpURLConnection;
  390. try {
  391. URL url = new URL(urls + user.getQrCode());
  392. httpURLConnection = (HttpURLConnection) url.openConnection();
  393. // 设置网络连接超时时间
  394. httpURLConnection.setConnectTimeout(3000);
  395. // 设置应用程序要从网络连接读取数据
  396. httpURLConnection.setDoInput(true);
  397. httpURLConnection.setRequestMethod("GET");
  398. int responseCode = httpURLConnection.getResponseCode();
  399. if (responseCode == 200) {
  400. // 从服务器返回一个输入流
  401. inputStream = httpURLConnection.getInputStream();
  402. }
  403. String path = new StringBuilder("/qrcode/")
  404. .append(user.getName()+System.currentTimeMillis()).append(".png").toString();
  405. BOSClientUtil bosClientUtil = new BOSClientUtil();
  406. String uploadPath = bosClientUtil.upload(path, inputStream);
  407. user.setQrCode(uploadPath);
  408. } catch (MalformedURLException e) {
  409. e.printStackTrace();
  410. } catch (IOException e) {
  411. e.printStackTrace();
  412. } finally {
  413. inputStream.close();
  414. }
  415. }
  416. user.setName(name);
  417. String realPathPhoto = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + time + "/img/";
  418. FileUtils.createDirectory(realPathPhoto);
  419. String picNamePhoto = time + ".png"; // encoderImgId此处二维码的图片名
  420. String filePathPhoto = realPathPhoto + picNamePhoto; // 存放路径
  421. //FontImageUtil.createImage(name.substring(0, 1), new Font("宋体", Font.BOLD, 100), new File(filePathPhoto), 200, 200);
  422. WordToPic.TextToPic(name.substring(name.length() - 2 > 0 ? name.length() - 2 : 0, name.length()), 200, 200, 55, filePathPhoto);
  423. user.setPhoto(contextPath + Global.USERFILES_BASE_URL + time + "/img/" + picNamePhoto);
  424. if (user.getPhoto() != null && !user.getPhoto().equals("")) {
  425. InputStream inputStream = null;
  426. HttpURLConnection httpURLConnection;
  427. try {
  428. URL url = new URL(urls + user.getPhoto());
  429. httpURLConnection = (HttpURLConnection) url.openConnection();
  430. // 设置网络连接超时时间
  431. httpURLConnection.setConnectTimeout(3000);
  432. // 设置应用程序要从网络连接读取数据
  433. httpURLConnection.setDoInput(true);
  434. httpURLConnection.setRequestMethod("GET");
  435. int responseCode = httpURLConnection.getResponseCode();
  436. if (responseCode == 200) {
  437. // 从服务器返回一个输入流
  438. inputStream = httpURLConnection.getInputStream();
  439. }
  440. OSSClientUtil ossUtil = new OSSClientUtil();
  441. String[] arr = user.getPhoto().split("/");
  442. String photoName = time + arr[arr.length - 1];
  443. ossUtil.uploadFile2OSS(inputStream, Global.getPhoto(), photoName);
  444. user.setPhoto(Global.getAliyunUrl() + "/" + Global.getPhoto() + photoName);
  445. } catch (MalformedURLException e) {
  446. e.printStackTrace();
  447. } catch (IOException e) {
  448. e.printStackTrace();
  449. } finally {
  450. inputStream.close();
  451. }
  452. }
  453. user.setMobile(mobile);
  454. // 保存用户信息
  455. user.setDefaultPhoto("0");
  456. user.setComId("1");
  457. user.setCompany(new Office("1"));
  458. user.setOffice(new Office("5"));
  459. role.setOffice(new Office("5"));
  460. this.saveUserInfo(user);
  461. this.assignUserToRole(role, user,0);
  462. //屏蔽掉环信注册,加个说明
  463. user.setRemarks("");
  464. // 清除当前用户缓存
  465. if (user.getLoginName().equals(UserUtils.getUser().getLoginName())) {
  466. UserUtils.clearCache();
  467. // UserUtils.getCacheMap().clear();
  468. }
  469. //request.getSession().getServletContext().removeAttribute(user.getMobile());// 清除验证码
  470. //环信注册
  471. //屏蔽掉环信注册
  472. // RegisterUsers users = new RegisterUsers();
  473. // io.swagger.client.model.User user1 = new io.swagger.client.model.User().username(user.getId()).password(password);
  474. // users.add(user1);
  475. // Object result = easemobIMUsers.createNewIMUserSingle(users);
  476. // Nickname nickname = new Nickname();
  477. // nickname.setNickname(user.getName());
  478. // easemobIMUsers.modifyIMUserNickNameWithAdminToken(user.getId(), nickname);
  479. // logger.info(result.toString());
  480. // JSONObject json = JSONObject.fromObject(result);
  481. // j.put("IMUser", json);
  482. j.setSuccess(true);
  483. j.setMsg("注册用户'" + user.getLoginName() + "'成功!");
  484. j.put("userId", user.getId());
  485. j.setErrorCode(ErrorCode.code_1004);
  486. } catch (Exception e) {
  487. j.setSuccess(false);
  488. j.setMsg("注册用户'" + user.getLoginName() + "'失败");
  489. j.put("userId", user.getId());
  490. j.setErrorCode(ErrorCode.code_2004);
  491. logger.info("Exception e:"+e);
  492. userDao.delete(user);
  493. }
  494. return j;
  495. }
  496. @Transactional(readOnly = false)
  497. public void updateUserInfo(User user) {
  498. user.preUpdate();
  499. userDao.updateUserInfo(user);
  500. // 清除用户缓存
  501. UserUtils.clearCache(user);
  502. // // 清除权限缓存
  503. // systemRealm.clearAllCachedAuthorizationInfo();
  504. }
  505. @Transactional(readOnly = false)
  506. public void updateMo(User user) {
  507. user.preUpdate();
  508. userDao.updateMo(user);
  509. // 清除用户缓存
  510. UserUtils.clearCache(user);
  511. // // 清除权限缓存
  512. // systemRealm.clearAllCachedAuthorizationInfo();
  513. }
  514. @Transactional(readOnly = false)
  515. public void deleteUser(User user) {
  516. //清除环信帐号
  517. userDao.delete(user);
  518. // 清除用户缓存
  519. UserUtils.clearCache(user);
  520. // // 清除权限缓存
  521. // systemRealm.clearAllCachedAuthorizationInfo();
  522. }
  523. @Transactional(readOnly = false)
  524. public void updatePasswordById(String id, String loginName,
  525. String newPassword) {
  526. User user = new User(id);
  527. user.setPassword(entryptPassword(newPassword));
  528. user.setPasswordRemake(newPassword);
  529. user.setFirstFlag("0");
  530. userDao.updatePasswordById(user);
  531. // 清除用户缓存
  532. user.setLoginName(loginName);
  533. UserUtils.clearCache(user);
  534. // // 清除权限缓存
  535. // systemRealm.clearAllCachedAuthorizationInfo();
  536. }
  537. @Transactional(readOnly = false)
  538. public void updateUserLoginInfo(User user) {
  539. // 保存上次登录信息
  540. user.setOldLoginIp(user.getLoginIp());
  541. user.setOldLoginDate(user.getLoginDate());
  542. // 更新本次登录信息
  543. user.setLoginIp(UserUtils.getSession().getHost());
  544. user.setLoginDate(new Date());
  545. userDao.updateLoginInfo(user);
  546. }
  547. /**
  548. * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash
  549. */
  550. public static String entryptPassword(String plainPassword) {
  551. byte[] salt = Digests.generateSalt(SALT_SIZE);
  552. byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt,
  553. HASH_INTERATIONS);
  554. return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
  555. }
  556. /**
  557. * 生成MD5加密
  558. */
  559. public static String encrypeString(String plainPassword) {
  560. String password="";
  561. try {
  562. password = EncrypeUtil.encrypeString(plainPassword, "UTF-8");
  563. } catch (Exception e) {
  564. e.printStackTrace();
  565. }
  566. return password;
  567. }
  568. /**
  569. * 验证密码
  570. *
  571. * @param plainPassword
  572. * 明文密码
  573. * @param password
  574. * 密文密码
  575. * @return 验证成功返回true
  576. */
  577. public static boolean validatePassword(String plainPassword, String password) {
  578. byte[] salt = Encodes.decodeHex(password.substring(0, 16));
  579. byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt,
  580. HASH_INTERATIONS);
  581. return password.equals(Encodes.encodeHex(salt)
  582. + Encodes.encodeHex(hashPassword));
  583. }
  584. /**
  585. * 验证密码
  586. *
  587. * 密文密码
  588. * @return 验证成功返回true
  589. */
  590. public List<User> getUserListByEnname(String userId, String companyId,String enname) {
  591. //该公司下所有的岗位
  592. List<Role> roleList = getRoleList("",companyId);
  593. Role role = new Role();
  594. if (roleList!=null && roleList.size()!=0){
  595. for (Role r :roleList){
  596. //要的是管理员
  597. if (r.getEnname().endsWith(enname)){
  598. role = r;
  599. break;
  600. }
  601. }
  602. }
  603. //该公司下拥有管理员岗位的所有员工
  604. role.setCompany(new Office(companyId));
  605. List<User> userList = userDao.findUserByRole(role);
  606. return userList;
  607. }
  608. /**
  609. * 获得活动会话
  610. *
  611. * @return
  612. */
  613. public Collection<Session> getActiveSessions() {
  614. return sessionDao.getActiveSessions(false);
  615. }
  616. // -- Role Service --//
  617. public Role getRole(String id) {
  618. return roleDao.get(id);
  619. }
  620. public Role getRoleMenu(String id) {
  621. return roleDao.getMenuList(id);
  622. }
  623. public Role getRoleByName(String name,String companyId) {
  624. Role r = new Role();
  625. r.setName(name);
  626. r.setCompany(new Office(companyId));
  627. return roleDao.getByName(r);
  628. }
  629. public Role getRoleByEnname(String companyId,Office office,String enname) {
  630. Role r = new Role();
  631. r.setEnname(enname);
  632. r.setCompany(new Office(companyId));
  633. if (office==null || StringUtils.isBlank(office.getId())){
  634. r.setOffice(officeDao.getByOfficeInfo(new Office(companyId)));
  635. }else {
  636. r.setOffice(office);
  637. }
  638. Role role = roleDao.getByEnname(r);
  639. return role;
  640. }
  641. public SysRoleActivity getRoleActivityByEnname(String enname) {
  642. return roleActivityDao.getByEnname(enname);
  643. }
  644. public List<Role> findRole(Role role) {
  645. return roleDao.findList(role);
  646. }
  647. public List<Role> findListRole(Role role) {
  648. return roleDao.findListRole(role);
  649. }
  650. public Page<Role> findPageRole(Page<Role> page,Role role) {
  651. role.setPage(page);
  652. List<Role> roles = findListRole(role);
  653. page.setList(roles);
  654. return page;
  655. }
  656. public Page<Role> findPageRoleByUser(Page<Role> page,Role role) {
  657. role.setPage(page);
  658. List<Role> roles = roleDao.findRoleList(role);
  659. page.setList(roles);
  660. return page;
  661. }
  662. public List<Role> findListRoleByUser(Role role) {
  663. List<Role> roles = roleDao.findRoleList(role);
  664. return roles;
  665. }
  666. public List<Role> findAllRole() {
  667. return UserUtils.getRoleList();
  668. }
  669. public List<Role> findAllRoleByCompany() {
  670. return UserUtils.getRoleListByCompany("");
  671. }
  672. @Transactional(readOnly = false)
  673. public String saveRole(Role role,Boolean b,int type) {
  674. Office office = officeDao.get(role.getOffice());
  675. if (StringUtils.isNotBlank(office.getBranchOffice())){
  676. role.setBranchCompany(new Office(office.getBranchOffice()));
  677. }
  678. if (role.getCompany().getId().equals("1")){
  679. String parentIds = office.getParentIds();
  680. for (String parentId:parentIds.split(",")){
  681. if (!parentId.equals("0")) {
  682. Office o = officeDao.get(parentId);
  683. if (o.getType().equals("1")) {
  684. role.setCompany(o);
  685. break;
  686. }
  687. }
  688. }
  689. }
  690. String str = "";
  691. if (StringUtils.isBlank(role.getEnname())){
  692. String enname = StringUtils.getPinYinHeadChar(role.getName(), 1);
  693. role.setEnname(enname);
  694. }
  695. if (StringUtils.isBlank(role.getId())) {
  696. role.preInsert();
  697. roleDao.insert(role);
  698. } else {
  699. role.preUpdate();
  700. //修改时间
  701. roleDao.update(role);
  702. }
  703. if (b){
  704. // 更新岗位与菜单关联
  705. Role role1 = new Role(role.getId());
  706. roleDao.deleteRoleMenu(role1);
  707. //roleDao.deleteRoleMenu(role);//删除岗位id 关联表
  708. logger.info("角色菜单id:------------------------------");
  709. if (role.getMenuList().size() > 0) {
  710. for (Menu menu:role.getMenuList()){
  711. System.out.print(menu.getId()+",");
  712. }
  713. //插入岗位与权限
  714. try{
  715. role1.setMenuList(role.getMenuList());
  716. roleDao.insertRoleMenu(role1);
  717. }catch (Exception e){
  718. e.printStackTrace();
  719. }
  720. }
  721. logger.info("------------------------------");
  722. }
  723. // 删除岗位与部门关联
  724. /*roleDao.deleteRoleOffice(role);
  725. if (role.getOfficeList().size() > 0) {
  726. try{
  727. roleDao.insertRoleOffice(role);
  728. }catch (Exception e){
  729. e.printStackTrace();
  730. }
  731. }*/
  732. // 清除用户岗位缓存
  733. UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
  734. // // 清除权限缓存
  735. // systemRealm.clearAllCachedAuthorizationInfo();
  736. List<User> users = findUserByRoleId(role);
  737. Jedis jedis = null;
  738. try {
  739. jedis = JedisUtils.getResource();
  740. if (users!=null && users.size()!=0){
  741. for (User user :users){
  742. UserUtils.clearCache(user);
  743. jedis.del("menu1_"+user.getComId()+"_" + user.getId());
  744. jedis.del("menu2_"+user.getComId()+"_" + user.getId());
  745. UserUtils.clearCache(user);
  746. }
  747. removeSession(users);
  748. }
  749. //String sql = jedis.hget("menu2_user_" + user.getId(), menuId);
  750. }catch (Exception e){
  751. System.out.println("------------------dataScopeFilterOR Exception e:"+e);
  752. }finally {
  753. JedisUtils.returnResource(jedis);
  754. }
  755. if (type == 0){
  756. relationRoleActivitys(role);
  757. }
  758. str = "成功!";
  759. return str;
  760. }
  761. @Transactional(readOnly = false)
  762. public List<User> findUserByRoleId(Role role){
  763. List<User> users = userDao.findUserByRoleId(role.getId(),role.getCompany().getId());
  764. return users;
  765. }
  766. @Transactional(readOnly = false)
  767. public Boolean relationRoleActivity(Role role) {
  768. String[] ids = role.getRoleActivityIds().split(",");
  769. List<SysRoleActivity> roleActivities = new ArrayList<>();
  770. for (String roleActivityId:ids){
  771. roleActivities.add(new SysRoleActivity(roleActivityId));
  772. }
  773. role.setRoleActivityList(roleActivities);
  774. //roleDao.deleteRoleActivity(role);
  775. int count = roleDao.insertRoleActivity(role);
  776. if (count!=0){
  777. return true;
  778. }else {
  779. return false;
  780. }
  781. }
  782. @Transactional(readOnly = false)
  783. public Boolean relationRoleActivitys(Role role) {
  784. List<SysRoleActivity> sysRoleActivities = role.getRoleActivityList();
  785. List<SysRoleActivity> roleActivities = new ArrayList<>();
  786. for (SysRoleActivity sysRoleActivity:sysRoleActivities){
  787. if (sysRoleActivity.getDelFlag().equals("0")){
  788. roleActivities.add(new SysRoleActivity(sysRoleActivity.getId()));
  789. }
  790. }
  791. role.setRoleActivityList(roleActivities);
  792. roleDao.deleteRoleActivity(role);
  793. if (role.getRoleActivityList()!=null && role.getRoleActivityList().size()!=0) {
  794. int count = roleDao.insertRoleActivity(role);
  795. if (count!=0){
  796. return true;
  797. }else {
  798. return false;
  799. }
  800. }else {
  801. return true;
  802. }
  803. }
  804. @Transactional(readOnly = false)
  805. public Boolean deleteRoleActivity(Role role) {
  806. String[] ids = role.getRoleActivityIds().split(",");
  807. List<SysRoleActivity> roleActivities = new ArrayList<>();
  808. for (String roleActivityId:ids){
  809. roleActivities.add(new SysRoleActivity(roleActivityId));
  810. }
  811. role.setRoleActivityList(roleActivities);
  812. roleDao.deleteRoleActivity(role);
  813. return true;
  814. }
  815. @Transactional(readOnly = false)
  816. public Boolean deleteRoleActivityOne(String roleId,String roleActivityId) {
  817. roleDao.deleteRoleActivityOne(roleId,roleActivityId);
  818. return true;
  819. }
  820. @Transactional(readOnly = false)
  821. public Boolean deleteRole(Role role) {
  822. Boolean b = false;
  823. try {
  824. int userCount = roleDao.findUserRoleCount(role);
  825. if (userCount==0){
  826. roleDao.delete(role);
  827. roleDao.deleteRoleMenu(role);
  828. roleDao.deleteRoleActivity(role);
  829. // 清除用户岗位缓存
  830. UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
  831. // // 清除权限缓存
  832. // systemRealm.clearAllCachedAuthorizationInfo();
  833. b = true;
  834. }
  835. }catch (Exception e){
  836. logger.info("deleteRole:Exception e:"+e);
  837. }
  838. return b;
  839. }
  840. @Transactional(readOnly = false)
  841. public Boolean outUserInRole(Role role, User user) {
  842. try {
  843. List<Role> roles = user.getRoleList();
  844. for (Role e : roles) {
  845. if (e.getId().equals(role.getId())) {
  846. roles.remove(e);
  847. if (StringUtils.isNotBlank(user.getId())) {
  848. // 更新用户与岗位关联
  849. userDao.deleteUserRole(user);
  850. if (user.getRoleList() != null && user.getRoleList().size() > 0) {
  851. userDao.insertUserRole(user);
  852. } else {
  853. List<Role> roleList = new ArrayList<>();
  854. Role role1 = new Role();
  855. role1.setId("1");
  856. role1.setOffice(new Office("5"));
  857. roleList.add(role1);
  858. user.setRoleList(roleList);
  859. userDao.insertUserRole(user);
  860. }
  861. delMenuCache(user);
  862. UserUtils.clearCache(user);
  863. removeSession(user);
  864. // // 清除权限缓存
  865. // systemRealm.clearAllCachedAuthorizationInfo();
  866. }
  867. return true;
  868. }
  869. }
  870. }catch (Exception e){
  871. logger.info("outUserInRole:Exception e:"+e);
  872. }
  873. return false;
  874. }
  875. public void delMenuCache(User user) {
  876. Jedis jedis = null;
  877. try {
  878. jedis = JedisUtils.getResource();
  879. jedis.del("menu1_"+user.getComId()+"_" + user.getId());
  880. jedis.del("menu2_"+user.getComId()+"_" + user.getId());
  881. }catch (Exception e){
  882. logger.error("清除用户权限缓存异常!userId:{}",user.getId(),e);
  883. }finally {
  884. JedisUtils.returnResource(jedis);
  885. }
  886. }
  887. @Transactional(readOnly = false)
  888. public User addUserToRole(User user) {
  889. try {
  890. if (user == null) {
  891. return null;
  892. }
  893. User user1 = new User();
  894. Role role = new Role();
  895. role.setOffice(new Office("5"));
  896. role.setId("1");
  897. user1.setId(user.getId());
  898. user1.getRoleList().add(role);
  899. user1.setRole(role);
  900. user1.setId(user.getId());
  901. userDao.insertUserRole(user1);
  902. user.setRemove("");
  903. // 清除用户缓存
  904. delMenuCache(user);
  905. UserUtils.clearCache(user);
  906. removeSession(user);
  907. return user;
  908. }catch (Exception e){
  909. logger.info("assignUserToRole:Exception e:"+e);
  910. }
  911. return null;
  912. }
  913. @Transactional(readOnly = false)
  914. public User assignUserToRole(Role role, User user,int type) {
  915. try {
  916. if (user == null) {
  917. return null;
  918. }
  919. if (role.getDataScope().equals("1")){
  920. return null;
  921. }
  922. List<Role> roleList = new ArrayList<>();
  923. roleList.addAll(user.getRoleList());
  924. int count = 0;
  925. for (Role role1:roleList){
  926. if (role1.getId().equals(role.getId())) {
  927. return new User("1");
  928. }else if (role1.getId().equals("1")){
  929. count++;
  930. User user1 = new User();
  931. Role role2 = new Role();
  932. role2.setOffice(new Office("5"));
  933. role2.setId("1");
  934. user1.setRole(role2);
  935. user1.setId(user.getId());
  936. user.getRoleList().remove(role1);
  937. userDao.deleteUserRole(user1);
  938. }
  939. }
  940. user.getRoleList().add(role);
  941. User user1 = new User();
  942. user1.setId(user.getId());
  943. user1.getRoleList().add(role);
  944. Role role2 = new Role();
  945. role2.setOffice(role.getOffice());
  946. role2.setId(role.getId());
  947. user1.setRole(role2);
  948. userDao.insertUserRole(user1);
  949. user.setRemove("");
  950. if (count != 0){
  951. user.setRemove("remove");
  952. }
  953. delMenuCache(user);
  954. if (type == 0) {
  955. // 清除用户缓存
  956. UserUtils.clearCache(user);
  957. removeSession(user);
  958. }
  959. return user;
  960. }catch (Exception e){
  961. logger.error("assignUserToRole:Exception e:",e);
  962. }
  963. return null;
  964. }
  965. @Transactional(readOnly = false)
  966. public int assignUserToRoleList(List<Role> roleLists, User user) {
  967. int count = 0;
  968. try {
  969. if (user == null) {
  970. return 0;
  971. }
  972. List<Role> roleList = user.getRoleList();
  973. for (Role role1 : roleList) {
  974. for (Role role : roleLists) {
  975. if (role1.getId().equals(role.getId())) {
  976. return 0;
  977. } else if (role1.getId().equals("1")) {
  978. User user1 = new User();
  979. Role role2 = new Role();
  980. user1.setId(user.getId());
  981. role2.setOffice(new Office("5"));
  982. role2.setId("1");
  983. user1.setRole(role2);
  984. user.getRoleList().remove(role1);
  985. userDao.deleteUserRole(user1);
  986. }
  987. }
  988. }
  989. for (Role role : roleLists) {
  990. try {
  991. user.getRoleList().add(role);
  992. User user1 = new User();
  993. Role role2 = new Role();
  994. user1.setId(user.getId());
  995. user1.getRoleList().add(role);
  996. role2.setOffice(role.getOffice());
  997. role2.setId(role.getId());
  998. user1.setRole(role2);
  999. userDao.insertUserRole(user);
  1000. count ++;
  1001. }catch (Exception e){
  1002. logger.error("数据重复");
  1003. }
  1004. }
  1005. user.setRemove("");
  1006. if (count != 0) {
  1007. user.setRemove("remove");
  1008. // 清除用户缓存
  1009. UserUtils.clearCache(user);
  1010. removeSession(user);
  1011. }
  1012. }catch (Exception e){
  1013. logger.info("assignUserToRoleList:Exception e:"+e);
  1014. count = -1;
  1015. }
  1016. return count;
  1017. }
  1018. @Transactional(readOnly = false)
  1019. public User updateUserToRole(Role role, User user) {
  1020. try {
  1021. if (user == null) {
  1022. return null;
  1023. }
  1024. List<Role> roleList = user.getRoleList();
  1025. for (Role role1:roleList){
  1026. if (role1.getOffice().equals(role.getOffice()) && role1.getId().equals(role.getId())) {
  1027. User user1 = new User();
  1028. user1.setId(user.getId());
  1029. Role role2 = new Role();
  1030. role2.setOffice(role.getOffice());
  1031. role2.setId(role.getId());
  1032. user1.setRole(role2);
  1033. user.getRoleList().remove(user1);
  1034. userDao.deleteUserRole(user1);
  1035. }else {
  1036. return null;
  1037. }
  1038. }
  1039. // 清除用户缓存
  1040. UserUtils.clearCache(user);
  1041. removeSession(user);
  1042. return user;
  1043. }catch (Exception e){
  1044. logger.info("updateUserToRole:Exception e:"+e);
  1045. }
  1046. return null;
  1047. }
  1048. public Office getSelectCompany() {
  1049. Object companyId = UserUtils.getCache("selectCompany");
  1050. if (companyId != null) {
  1051. Office company = officeDao.get(companyId.toString());
  1052. return company;
  1053. }
  1054. return null;
  1055. }
  1056. @Transactional(readOnly = true)
  1057. public List<Role> getRoleList(String userId,String companyId) {
  1058. Role roles = new Role();
  1059. if (StringUtils.isNotBlank(userId)){
  1060. User user = UserUtils.get(userId);
  1061. roles.setUser(user);
  1062. }
  1063. List<Role> roleList = new ArrayList<>();
  1064. if (StringUtils.isNotBlank(companyId)){
  1065. if (companyId.equals("1")){
  1066. roles.setCompany(new Office("1"));
  1067. }else {
  1068. roles.setCompany(new Office(companyId));
  1069. roleList = roleDao.findList(roles);
  1070. }
  1071. }else {
  1072. roleList = roleDao.findList(roles);
  1073. }
  1074. return roleList;
  1075. }
  1076. @Transactional(readOnly = false)
  1077. public List<Map<String,Object>> getCurrentCompany(String userId) {
  1078. User user =UserUtils.get(userId);
  1079. List<Office> companyList = UserUtils.getAllCompany(user);
  1080. HashSet<Office> hs = new HashSet<Office>(companyList);
  1081. List<Map<String,Object>> mapList = Lists.newArrayList();
  1082. if (hs!=null &&hs.size()!=0) {
  1083. for (Office off : hs) {
  1084. if (!off.getId().equals("1")) {
  1085. Map<String, Object> map = Maps.newHashMap();
  1086. map.put("companyName", off.getName());
  1087. map.put("companyLogo", off.getLogo() == null ? "" : off.getLogo());
  1088. map.put("companyQrcode", off.getCompanyQrcode() == null ? "" : off.getCompanyQrcode());
  1089. map.put("companyId", off.getId());
  1090. map.put("branchOfficeId", off.getBranchOffice());
  1091. if (StringUtils.isNotBlank(off.getBranchOffice())){
  1092. Office branchOffice = officeDao.get(off.getBranchOffice());
  1093. map.put("branchOfficeName", branchOffice.getName());
  1094. }else {
  1095. map.put("branchOfficeName", off.getName());
  1096. }
  1097. map.put("master", off.getMaster() == null || UserUtils.get(off.getMaster()) == null ? "" : UserUtils.get(off.getMaster()).getName());
  1098. map.put("masterId", off.getMaster() == null ? "" : off.getMaster());
  1099. Useroffice useroffice = new Useroffice();
  1100. useroffice.setCompanyId(off.getId());
  1101. useroffice.setStatus("3");
  1102. List<Useroffice> userofficeList = userofficeService.findList(useroffice);
  1103. map.put("count", userofficeList.size());
  1104. for (Useroffice useroffice1:userofficeList){
  1105. if (useroffice1.getUserId().equals(user.getId())){
  1106. map.put("officeId", useroffice1.getOfficeId());
  1107. if (StringUtils.isNotBlank(useroffice1.getOfficeId())){
  1108. map.put("officeName", officeService.get(useroffice1.getOfficeId()).getName());
  1109. }else {
  1110. map.put("officeName","");
  1111. }
  1112. }
  1113. }
  1114. Role role2 = new Role();
  1115. role2.setCompany(off);
  1116. role2.setUser(UserUtils.get(userId));
  1117. List<Role> roleList2 = roleDao.findList(role2);
  1118. for (Role role : roleList2) {
  1119. /*map.put("officeId", role.getOffice().getId());
  1120. map.put("officeName", role.getOffice().getName());*/
  1121. if(user!=null && user.getComId()!=null && user.getComId().equals(off.getId())){
  1122. map.put("isCurrent", 1);
  1123. if(UserUtils.getUser().isAdmin() || (StringUtils.isNotBlank(role.getEnname()) && (role.getEnname().endsWith("gly")||role.getEnname().equals("system")))){
  1124. map.put("isManager","1");
  1125. if (role.getCompany() == null || StringUtils.isBlank(role.getCompany().getId())){
  1126. role.setCompany(UserUtils.getSelectCompany());
  1127. }
  1128. List<User> userList2 = userDao.findUserByRole(role);
  1129. for (int k = 0; k < userList2.size(); k++) {
  1130. if (userList2.get(k) != null || userList2.get(k).equals("null")) {
  1131. map.put("glyName", userList2.get(k).getName());
  1132. }
  1133. }
  1134. break;
  1135. }else{
  1136. map.put("isManager","0");
  1137. map.put("glyName", "");
  1138. }
  1139. }else {
  1140. map.put("isCurrent", 0);
  1141. }
  1142. }
  1143. mapList.add(map);
  1144. }
  1145. }
  1146. }
  1147. return mapList;
  1148. }
  1149. @Transactional(readOnly = false)
  1150. public List<Map<String,Object>> saveCurrentCompany(String userId,String companyId) {
  1151. UserUtils.saveSelectCompany();
  1152. return getCurrentCompany(userId);
  1153. }
  1154. @Transactional(readOnly = false)
  1155. public List<Map<String,Object>> saveCurrentCompanyById(String userId,String companyId,String officeId) {
  1156. User user=UserUtils.get(userId);
  1157. if (companyId != null) {
  1158. user.setComId(companyId);
  1159. user.setCompany(new Office(companyId));
  1160. }
  1161. if (officeId != null){
  1162. user.setOffice(new Office(officeId));
  1163. }
  1164. // 清除原用户机构用户缓存
  1165. User oldUser = userDao.get(userId);
  1166. if (oldUser.getOffice() != null
  1167. && oldUser.getOffice().getId() != null) {
  1168. CacheUtils.remove(UserUtils.USER_CACHE,
  1169. UserUtils.USER_CACHE_LIST_BY_OFFICE_ID_
  1170. + oldUser.getOffice().getId());
  1171. CacheUtils.remove(UserUtils.USER_CACHE,
  1172. UserUtils.CACHE_COMPANY
  1173. + oldUser.getComId());
  1174. }
  1175. user.preUpdate();
  1176. userDao.updateUserInfo(user);
  1177. if (UserUtils.getUser().getId().equals(userId)){
  1178. UserUtils.clearCache();
  1179. UserUtils.removeCache("selectCompany");
  1180. UserUtils.removeCache("selectOffice");
  1181. UserUtils.removeCache("selectRole");
  1182. UserUtils.putCache("selectCompany", companyId);
  1183. UserUtils.putCache("selectOffice", officeId);
  1184. Role role = new Role();
  1185. role.setCompany(getSelectCompany());
  1186. role.setUser(user);
  1187. List<Role> roleList = roleDao.findList(role);
  1188. if(roleList!=null && roleList.size()!=0){
  1189. UserUtils.putCache("selectRole", roleList);
  1190. }
  1191. }
  1192. return getCurrentCompany(userId);
  1193. }
  1194. // -- Menu Service --//
  1195. public Menu getMenu(String id) {
  1196. return menuDao.get(id);
  1197. }
  1198. public List<Menu> findAllMenu() {
  1199. return UserUtils.getMenuList();
  1200. }
  1201. public List<Menu> findAllMenu(Role role) {
  1202. return UserUtils.getMenuList(role);
  1203. }
  1204. @Transactional(readOnly = false)
  1205. public void saveMenu(Menu menu) {
  1206. // 获取父节点实体
  1207. menu.setParent(this.getMenu(menu.getParent().getId()));
  1208. // 获取修改前的parentIds,用于更新子节点的parentIds
  1209. String oldParentIds = menu.getParentIds();
  1210. // 设置新的父节点串
  1211. menu.setParentIds(menu.getParent().getParentIds()
  1212. + menu.getParent().getId() + ",");
  1213. // 保存或更新实体
  1214. if (StringUtils.isBlank(menu.getId())) {
  1215. menu.preInsert();
  1216. menuDao.insert(menu);
  1217. } else {
  1218. menu.preUpdate();
  1219. menuDao.update(menu);
  1220. }
  1221. // 更新子节点 parentIds
  1222. Menu m = new Menu();
  1223. m.setParentIds("%," + menu.getId() + ",%");
  1224. List<Menu> list = menuDao.findByParentIdsLike(m);
  1225. for (Menu e : list) {
  1226. e.setParentIds(e.getParentIds().replace(oldParentIds,
  1227. menu.getParentIds()));
  1228. menuDao.updateParentIds(e);
  1229. }
  1230. // 清除用户菜单缓存
  1231. UserUtils.removeCache(UserUtils.CACHE_MENU_LIST);
  1232. // // 清除权限缓存
  1233. // systemRealm.clearAllCachedAuthorizationInfo();
  1234. // 清除日志相关缓存
  1235. CacheUtils.remove(LogUtils.CACHE_MENU_NAME_PATH_MAP);
  1236. }
  1237. @Transactional(readOnly = false)
  1238. public void updateMenuSort(Menu menu) {
  1239. menuDao.updateSort(menu);
  1240. // 清除用户菜单缓存
  1241. UserUtils.removeCache(UserUtils.CACHE_MENU_LIST);
  1242. // // 清除权限缓存
  1243. // systemRealm.clearAllCachedAuthorizationInfo();
  1244. // 清除日志相关缓存
  1245. CacheUtils.remove(LogUtils.CACHE_MENU_NAME_PATH_MAP);
  1246. }
  1247. @Transactional(readOnly = false)
  1248. public void deleteMenu(Menu menu) {
  1249. menuDao.delete(menu);
  1250. // 清除用户菜单缓存
  1251. UserUtils.removeCache(UserUtils.CACHE_MENU_LIST);
  1252. // // 清除权限缓存
  1253. // systemRealm.clearAllCachedAuthorizationInfo();
  1254. // 清除日志相关缓存
  1255. CacheUtils.remove(LogUtils.CACHE_MENU_NAME_PATH_MAP);
  1256. }
  1257. /**
  1258. * 获取Key加载信息
  1259. */
  1260. public static boolean printKeyLoadMessage() {
  1261. StringBuilder sb = new StringBuilder();
  1262. sb.append("\r\n======================================================================\r\n");
  1263. sb.append("\r\n 欢迎使用 " + Global.getConfig("productName")
  1264. + " - Powered By http://www.jeeplus.org\r\n");
  1265. sb.append("\r\n======================================================================\r\n");
  1266. System.out.println(sb.toString());
  1267. return true;
  1268. }
  1269. @Override
  1270. public void afterPropertiesSet() throws Exception {
  1271. // TODO Auto-generated method stub
  1272. }
  1273. @Autowired
  1274. private JedisSessionDAO jedisSessionDAO;
  1275. public JedisSessionDAO getJedisSessionDao() {
  1276. return jedisSessionDAO;
  1277. }
  1278. public Collection<Session> getSessionList() {
  1279. return getJedisSessionDao().getActiveSessions(false);
  1280. }
  1281. public String removeSession(List<User> users) {
  1282. String str = "";
  1283. if (users != null && users.size() != 0) {
  1284. Jedis jedis = null;
  1285. try {
  1286. jedis = JedisUtils.getResource();
  1287. Long delCount = 0L;
  1288. /*Long delCount = 0L;
  1289. String sessionPrefix = Global.getConfig("redis.keyPrefix") + "_session_";
  1290. Map<String, String> map = jedis.hgetAll(sessionPrefix);
  1291. for (Map.Entry<String, String> e : map.entrySet()) {
  1292. if (StringUtils.isNotBlank(e.getKey()) && StringUtils.isNotBlank(e.getValue())) {
  1293. String[] ss = StringUtils.split(e.getValue(), "|");
  1294. if (ss != null && ss.length == 3) {
  1295. String principalId = ss[0];
  1296. for (User user:users){
  1297. if (StringUtils.isNotBlank(principalId) && principalId.equals(user.getId())) {
  1298. String jsessionId = sessionPrefix + e.getKey();
  1299. if (StringUtils.isNotBlank(e.getKey())) {
  1300. delCount += jedis.del(jsessionId);
  1301. jedis.hdel(sessionPrefix, e.getKey());
  1302. }
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }*/
  1308. Collection<Session> sessions = getSessionList();
  1309. logger.info("sessionKeyPrefix:"+sessionKeyPrefix+","+ com.alibaba.fastjson.JSON.toJSONString(jedis.hgetAll(sessionKeyPrefix)));
  1310. for (Session session :sessions){
  1311. String pid = jedis.hget(sessionKeyPrefix,session.getId().toString());
  1312. for (User user :users) {
  1313. if (pid != null && pid.contains(user.getId())) {
  1314. getJedisSessionDao().delete(session);
  1315. clearCache(user);
  1316. delCount++;
  1317. }
  1318. }
  1319. }
  1320. logger.info("sessions:"+sessions);
  1321. if (delCount < 1) {
  1322. System.err.println("删除用户的session失败");
  1323. str = "删除用户的session失败";
  1324. } else {
  1325. System.err.println("删除用户的session成功");
  1326. str = "删除用户的session成功";
  1327. }
  1328. } catch (Exception e) {
  1329. e.printStackTrace();
  1330. } finally {
  1331. JedisUtils.returnResource(jedis);
  1332. }
  1333. return str;
  1334. } else {
  1335. return "用户信息为空!";
  1336. }
  1337. }
  1338. public String removeSession(User user){
  1339. String str = "";
  1340. if(StringUtils.isNotBlank(user.getId())){
  1341. Jedis jedis = null;
  1342. try {
  1343. jedis = JedisUtils.getResource();
  1344. Long delCount = 0L;
  1345. Collection<Session> sessions = getSessionList();
  1346. logger.info("sessionKeyPrefix:"+sessionKeyPrefix+","+ com.alibaba.fastjson.JSON.toJSONString(jedis.hgetAll(sessionKeyPrefix)));
  1347. for (Session session :sessions){
  1348. String pid = jedis.hget(sessionKeyPrefix,session.getId().toString());
  1349. if (pid!=null && pid.contains(user.getId())){
  1350. clearCache(user);
  1351. getJedisSessionDao().delete(session);
  1352. delCount++;
  1353. }
  1354. }
  1355. /*String sessionPrefix = Global.getConfig("redis.keyPrefix")+"_session_";
  1356. Map<String, String> map = jedis.hgetAll(sessionPrefix);
  1357. for (Map.Entry<String, String> e : map.entrySet()){
  1358. if (StringUtils.isNotBlank(e.getKey()) && StringUtils.isNotBlank(e.getValue())){
  1359. String[] ss = StringUtils.split(e.getValue(), "|");
  1360. if (ss != null && ss.length == 3){
  1361. String principalId = ss[0];
  1362. if(StringUtils.isNotBlank(principalId) && principalId.equals(user.getId())){
  1363. String jsessionId = sessionPrefix+e.getKey();
  1364. if (StringUtils.isNotBlank(e.getKey())){
  1365. jedis.hdel(sessionPrefix, e.getKey());
  1366. delCount += jedis.del(jsessionId);
  1367. }
  1368. }
  1369. }
  1370. }
  1371. }*/
  1372. if (delCount<1){
  1373. System.err.println("删除"+user.getName()+"的session失败");
  1374. str = "删除"+user.getName()+"的session失败";
  1375. }else{
  1376. System.err.println("删除"+user.getName()+"的session成功");
  1377. str = "删除"+user.getName()+"的session成功";
  1378. }
  1379. }catch (Exception e){
  1380. e.printStackTrace();
  1381. } finally {
  1382. JedisUtils.returnResource(jedis);
  1383. }
  1384. return str;
  1385. }else{
  1386. return "用户信息为空!";
  1387. }
  1388. }
  1389. public void clearCache(User user) {
  1390. DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
  1391. Collection<Realm> realms = securityManager.getRealms();
  1392. for (Realm realm : realms) {
  1393. SystemAuthorizingRealm realm1 = (SystemAuthorizingRealm)realm;
  1394. realm1.clearCachedAuthorizationInfo(new SystemAuthorizingRealm.Principal(user,false));
  1395. }
  1396. }
  1397. public User findUserByNo(String no) {
  1398. Office company = UserUtils.getSelectCompany();
  1399. return userDao.findUserByNo(no,company.getId());
  1400. }
  1401. public int countByOffice(Office office) {
  1402. User user = new User();
  1403. user.setOffice(office);
  1404. return userDao.countByOffice(user);
  1405. }
  1406. }