RoleManageAction.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. package com.minpay.huicai.system.action;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import com.minpay.common.bean.User;
  13. import com.minpay.common.constant.Constant;
  14. import com.minpay.common.constant.IConstant;
  15. import com.minpay.common.exception.BusinessCodeException;
  16. import com.minpay.common.format.IFormatService;
  17. import com.minpay.common.service.ILogService;
  18. import com.minpay.common.service.IPublicService;
  19. import com.minpay.db.table.mapper.ImBranchMapper;
  20. import com.minpay.db.table.mapper.ImItemMapper;
  21. import com.minpay.db.table.mapper.ImRoleAuthMapper;
  22. import com.minpay.db.table.mapper.ImRoleItemRelMapper;
  23. import com.minpay.db.table.mapper.ImRoleMapper;
  24. import com.minpay.db.table.mapper.ImUserRoleRelMapper;
  25. import com.minpay.db.table.model.ImBranch;
  26. import com.minpay.db.table.model.ImBranchExample;
  27. import com.minpay.db.table.model.ImItem;
  28. import com.minpay.db.table.model.ImItemExample;
  29. import com.minpay.db.table.model.ImRole;
  30. import com.minpay.db.table.model.ImRoleAuthExample;
  31. import com.minpay.db.table.model.ImRoleAuthKey;
  32. import com.minpay.db.table.model.ImRoleExample;
  33. import com.minpay.db.table.model.ImRoleItemRel;
  34. import com.minpay.db.table.model.ImRoleItemRelExample;
  35. import com.minpay.db.table.model.ImRoleItemRelKey;
  36. import com.minpay.db.table.model.ImUserRoleRelExample;
  37. import com.minpay.db.table.model.ImUserRoleRelKey;
  38. import com.minpay.db.table.own.mapper.RoleMapper;
  39. import com.startup.minpay.frame.business.IMINAction;
  40. import com.startup.minpay.frame.business.res.MINActionResult;
  41. import com.startup.minpay.frame.constant.IMINBusinessConstant;
  42. import com.startup.minpay.frame.constant.IMINTransactionEnum;
  43. import com.startup.minpay.frame.data.format.MINCopyFormat;
  44. import com.startup.minpay.frame.exception.MINBusinessException;
  45. import com.startup.minpay.frame.jdbc.MINRowBounds;
  46. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  47. import com.startup.minpay.frame.service.base.Service;
  48. import com.startup.minpay.frame.session.MINSession;
  49. import com.startup.minpay.frame.target.MINAction;
  50. import com.startup.minpay.frame.target.MINComponent;
  51. import com.startup.minpay.frame.target.MINParam;
  52. import com.startup.minpay.util.DateTime;
  53. import com.startup.minpay.util.Str;
  54. /**
  55. * 角色管理
  56. *
  57. * @author sunbz
  58. *
  59. */
  60. @MINComponent
  61. public class RoleManageAction implements IMINAction {
  62. public final static String ROLE_QUERY = "roleQuery";
  63. public final static String ROLE_QUERY_ALL = "roleQueryAll";
  64. public final static String ROLE_MODIFY = "roleModify";
  65. public final static String ROLE_DELETE = "roleDelete";
  66. public final static String ROLE_ADD = "roleAdd";
  67. public final static String ROLE_AUTH_QUERY = "roleAuthQuery";
  68. public final static String ROLE_AUTH_QUERY_WITHOUT_PWD = "roleAuthQueryWithoutPwd";
  69. public final static String AUTH_MODIFY = "authModify";
  70. public final static String HAVE_AUTH_SHAPE = "isHaveAuthShape";
  71. /** 查询当前机构下的角色信息 */
  72. public final static String ROLE_QUERY_BY_BRANCHID = "roleQueryByBranchId";
  73. /** 查询当前机构及下一级机构下的角色 信息 */
  74. public final static String ROLE_QUERY_BY_PARENTID = "roleQueryByParentId";
  75. /** 查询角色菜单已经设置的权限 */
  76. public final static String ROLE_AUTH_QUERY_BY_ROLEID = "roleAuthQueryByRoleId";
  77. /** 查询角色需要设置权限的菜单和权限列表 */
  78. public final static String ROLE_AUTHSET_QUERY = "roleAuthSetQuery";
  79. /** 给角色设置菜单权限 */
  80. public final static String SET_ITEM_AUTH = "setItemAuth";
  81. /** 查询角色对应的模块权限 */
  82. public final static String GET_ROLE_MODE = "getRoleModule";
  83. /** 修改角色菜单权限 */
  84. public final static String MODIFY_ITEM_AUTH = "modifyItemAuth";
  85. /** 查询所有部门 */
  86. public final static String ROLE_QUERY_BRANCH = "roleQueryBranch";
  87. @MINAction(value = ROLE_QUERY)
  88. public MINActionResult roleQuery(MINSession session, @MINParam(key = "page", defaultValue = "1") int page,
  89. @MINParam(key = "limit", defaultValue = "3") int limit, @MINParam(key = "rolename") String rolename,
  90. @MINParam(key = "roleId") String roleId) throws MINBusinessException {
  91. MINActionResult res = new MINActionResult();
  92. // 创建查询条件
  93. ImRoleExample s = new ImRoleExample();
  94. ImRoleExample.Criteria c = s.createCriteria();
  95. User u = session.getUser();
  96. String branchId = u.getBranchId();
  97. if (branchId == null || "".equals(branchId)) {
  98. throw new MINBusinessException("获取客户开户机构号失败!");
  99. }
  100. Map<String, String> m = new HashMap<String, String>();
  101. m.put("rolename", rolename);
  102. m.put("roleId", roleId);
  103. m.put("branchId", branchId);
  104. if (!Str.isEmptyMaskNull(rolename)) {
  105. c.andNameEqualTo(rolename);
  106. }
  107. MINRowBounds rows = new MINRowBounds(page, limit);
  108. // 查询
  109. // List<ImRole> ls =
  110. // Service.lookup(IMINDataBaseService.class).selectByExample(ImRoleMapper.class,
  111. // s, rows);
  112. List<Map<String, String>> ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  113. .roleManageQuery(m, rows);
  114. // 格式化
  115. ls = Service.lookup(IFormatService.class).formatDateTime(ls, "addtime");
  116. // 设置返回值
  117. res.set(IMINBusinessConstant.F_PAGING_LAY, ls);
  118. res.set(IMINBusinessConstant.F_PAGING_COUNT, rows.getMaxRows());
  119. res.set("sessionId", u.getId());
  120. return res;
  121. }
  122. /**
  123. *
  124. * @param id 角色编号
  125. * @param name 角色名称
  126. * @param desc 描述
  127. * @param itemidStr 菜单
  128. * @param authInputs 权限
  129. * @param moduleType
  130. * @param session
  131. * @return
  132. * @throws MINBusinessException
  133. */
  134. @MINAction(value = ROLE_MODIFY, transaction = IMINTransactionEnum.CMT)
  135. public MINActionResult roleModify(@MINParam(key = "id") String id, @MINParam(key = "name") String name,
  136. @MINParam(key = "desc") String desc, @MINParam(key = "itemidStr") String itemidStr,
  137. @MINParam(key = "authInputs") String authInputs, @MINParam(key = "moduleType") String[] moduleType,
  138. @MINParam(key = "roleMainPage") String roleMainPage, MINSession session) throws MINBusinessException {
  139. if ("".equals(name.trim()) || name == null) {
  140. throw new MINBusinessException("请输入角色名称!");
  141. }
  142. if ("".equals(desc) || desc == null) {
  143. throw new MINBusinessException("请输入角色描述!");
  144. }
  145. String[] itemid = itemidStr.split(",");
  146. if (itemid.length == 0) {
  147. throw new MINBusinessException("至少选择一个菜单!");
  148. }
  149. User u = session.getUser();
  150. if (u.getRoleId().equals(id)) {
  151. throw new MINBusinessException("不能修改当前登录操作员的相关信息");
  152. }
  153. // 当新增用户为客户经理时 校验所选角色有客户经理中心菜单
  154. ImRoleItemRelExample irir = new ImRoleItemRelExample();
  155. irir.createCriteria().andRoleidEqualTo(id).andItemidEqualTo(Constant.CUSTOMER_MANAGER_MENU);
  156. List<ImRoleItemRel> irirNum = Service.lookup(IMINDataBaseService.class)
  157. .selectByExample(ImRoleItemRelMapper.class, irir);
  158. // 之前未选择客户经理中心菜单 修改也不能选择
  159. /*
  160. * if (irirNum.size() == 0){
  161. * if(itemidStr.indexOf(Constant.CUSTOMER_MANAGER_MENU) != -1) { throw new
  162. * BusinessCodeException("JINM3154"); } }else { //之前已选择客户经理中心 修改时不能不选
  163. * if(itemidStr.indexOf(Constant.CUSTOMER_MANAGER_MENU) == -1) { throw new
  164. * BusinessCodeException("JINM3153"); } }
  165. */
  166. MINActionResult res = new MINActionResult();
  167. ImRole role = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImRoleMapper.class, id);
  168. role.setName(name);
  169. role.setDesc(desc);
  170. //role.setIndexpage(roleMainPage);
  171. role.setIndexpage("main.html");
  172. Service.lookup(IMINDataBaseService.class).updateByPrimaryKey(ImRoleMapper.class, role);
  173. Set<String> set = new HashSet<String>(Arrays.asList(itemid));
  174. set.remove(null);
  175. set.remove("");
  176. List<String> sls = new ArrayList<String>(set);
  177. // 删除不存在的菜单
  178. ImRoleItemRelExample dex = new ImRoleItemRelExample();
  179. ImRoleItemRelExample.Criteria c = dex.createCriteria();
  180. c.andRoleidEqualTo(id);
  181. if (sls.size() > 0) {
  182. c.andItemidNotIn(sls);
  183. }
  184. Service.lookup(IMINDataBaseService.class).deleteByExample(ImRoleItemRelMapper.class, dex);
  185. // 过滤已存在的权限
  186. ImRoleItemRelExample ex = new ImRoleItemRelExample();
  187. ex.createCriteria().andRoleidEqualTo(id);
  188. List<ImRoleItemRel> rils = Service.lookup(IMINDataBaseService.class).selectByExample(ImRoleItemRelMapper.class,
  189. ex);
  190. Set<String> existRoleItem = new HashSet<String>();
  191. for (ImRoleItemRel ri : rils) {
  192. existRoleItem.add(ri.getItemid());
  193. }
  194. if (sls.size() > 0) {
  195. ImItemExample iex = new ImItemExample();
  196. iex.createCriteria().andIdIn(sls);
  197. List<ImItem> ils = Service.lookup(IMINDataBaseService.class).selectByExample(ImItemMapper.class, iex);
  198. List<ImRoleItemRel> ls = new ArrayList<ImRoleItemRel>();
  199. for (ImItem i : ils) {
  200. if (existRoleItem.contains(i.getId()))
  201. continue;
  202. ImRoleItemRel ri = new ImRoleItemRel();
  203. ri.setRoleid(id);
  204. ri.setItemid(i.getId());
  205. ri.setAuthtype(i.getAuthtype());
  206. ri.setAuthshape(i.getAuthshape());
  207. ls.add(ri);
  208. }
  209. ImRoleItemRelMapper im = Service.lookup(IMINDataBaseService.class)
  210. .getMybatisMapper(ImRoleItemRelMapper.class);
  211. for (ImRoleItemRel i : ls) {
  212. im.insertSelective(i);
  213. }
  214. }
  215. // 删除所有权限
  216. ImRoleAuthExample roleAuthEx = new ImRoleAuthExample();
  217. roleAuthEx.createCriteria().andRoleidEqualTo(id);
  218. Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleAuthMapper.class).deleteByExample(roleAuthEx);
  219. // 添加所有权限
  220. ImRoleAuthKey roleAuth = new ImRoleAuthKey();
  221. roleAuth.setRoleid(id);
  222. for (int i = 0; i < authInputs.split(",").length; i++) {
  223. String auth = authInputs.split(",")[i];
  224. if (auth != null && !"".equals(auth) && !"undefined".equals(auth)) {
  225. roleAuth.setAuthNo(auth);
  226. Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleAuthMapper.class).insert(roleAuth);
  227. }
  228. }
  229. // 更新缓存
  230. Service.lookup(IPublicService.class).init();
  231. String logInfo = "操作员" + u.getName() + "修改角色,编号:" + id + ",名称:" + name + ",描述:" + desc;
  232. // Service.lookup(ILogService.class).logging(session, logInfo);
  233. res.set("roleId", id);
  234. res.set("roleName", name);
  235. return res;
  236. }
  237. /**
  238. * 角色删除
  239. *
  240. * @param id
  241. * @param session
  242. * @return
  243. * @throws MINBusinessException
  244. */
  245. @MINAction(value = ROLE_DELETE, transaction = IMINTransactionEnum.CMT)
  246. public MINActionResult roleDelete(@MINParam(key = "id") String id, MINSession session) throws MINBusinessException {
  247. User u = session.getUser();
  248. if (u.getRoleId().equals(id)) {
  249. throw new MINBusinessException("不能修改当前登录操作员的相关信息");
  250. }
  251. MINActionResult res = new MINActionResult();
  252. ImRole role = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImRoleMapper.class, id);
  253. if (role == null) {
  254. throw new MINBusinessException("角色不存在");
  255. }
  256. // 判断角色下是否存在操作员
  257. ImUserRoleRelExample ire = new ImUserRoleRelExample();
  258. ire.createCriteria().andRoleidEqualTo(id);
  259. List<ImUserRoleRelKey> list = Service.lookup(IMINDataBaseService.class)
  260. .getMybatisMapper(ImUserRoleRelMapper.class).selectByExample(ire);
  261. if (list.size() > 0) {
  262. throw new MINBusinessException("该角色下存在操作员,不能删除");
  263. }
  264. Service.lookup(IMINDataBaseService.class).deleteByPrimaryKey(ImRoleMapper.class, id);
  265. ImRoleItemRelExample ex = new ImRoleItemRelExample();
  266. ex.createCriteria().andRoleidEqualTo(id);
  267. Service.lookup(IMINDataBaseService.class).deleteByExample(ImRoleItemRelMapper.class, ex);
  268. String logInfo = "删除角色id:" + id;
  269. Service.lookup(ILogService.class).logging(session, logInfo);
  270. return res;
  271. }
  272. @MINAction(value = ROLE_ADD, transaction = IMINTransactionEnum.CMT, session = true)
  273. public MINActionResult roleAdd(
  274. // @MINParam(value = "id") String id,
  275. @MINParam(key = "name") String name, @MINParam(key = "desc") String desc,
  276. @MINParam(key = "itemidStr") String itemidStr, @MINParam(key = "authInputs") String authInputs,
  277. @MINParam(key = "moduleType") String[] moduleType, @MINParam(key = "roleMainPage") String roleMainPage,
  278. MINSession session) throws MINBusinessException {
  279. if ("".equals(name.trim()) || name == null) {
  280. throw new MINBusinessException("请输入角色名称!");
  281. }
  282. if ("".equals(desc) || desc == null) {
  283. throw new MINBusinessException("请输入角色描述!");
  284. }
  285. /*if ("".equals(roleMainPage) || roleMainPage == null) {
  286. throw new MINBusinessException("请输入角色主页!");
  287. }*/
  288. String[] itemid = itemidStr.split(",");
  289. if (itemid.length == 0) {
  290. throw new MINBusinessException("至少选择一个菜单!");
  291. }
  292. // 校验全先生数据是否选择
  293. String noAuthItem = "";// TODO 添加无权限菜单数据 用,拼接
  294. for (int i = 0; i < itemid.length; i++) {
  295. String item_id = itemid[i];
  296. if (authInputs.indexOf(item_id) == -1 && noAuthItem.indexOf(item_id) != -1) {// TODO 存在不无权限的菜单,需要单独列出来
  297. throw new MINBusinessException("存在菜单未选择权限情况,请检查!");
  298. }
  299. }
  300. String[] auths = authInputs.split(",");
  301. User u = session.getUser();
  302. MINActionResult res = new MINActionResult();
  303. String roleId = Service.lookup(IPublicService.class).getSequence("IM_ROLE_NO");
  304. roleId = getRoleId(Integer.valueOf(roleId));
  305. ImRoleExample na = new ImRoleExample();
  306. na.createCriteria().andNameEqualTo(name).andBranchidEqualTo(u.getBranchId());
  307. List<ImRole> list = Service.lookup(IMINDataBaseService.class).selectByExample(ImRoleMapper.class, na);
  308. if (list != null && list.size() > 0) {
  309. throw new BusinessCodeException("JINM0109");
  310. }
  311. ImRole role = new ImRole();
  312. role.setId(roleId);
  313. role.setName(name);
  314. role.setDesc(desc);
  315. role.setBranchid(u.getBranchId());
  316. role.setAddbranch(u.getBranchId());
  317. role.setAddtime(new DateTime().toDateTimeString());
  318. //role.setIndexpage(roleMainPage);
  319. role.setIndexpage("main.html");
  320. Service.lookup(IMINDataBaseService.class).insert(ImRoleMapper.class, role);
  321. String[] itemIds = new String[itemid.length + 1];
  322. System.arraycopy(itemid, 0, itemIds, 0, itemid.length);
  323. itemIds[itemid.length] = "240001";
  324. if (itemIds.length > 0) {
  325. ImItemExample iex = new ImItemExample();
  326. iex.createCriteria().andIdIn(Arrays.asList(itemIds));
  327. List<ImItem> ils = Service.lookup(IMINDataBaseService.class).selectByExample(ImItemMapper.class, iex);
  328. List<ImRoleItemRel> ls = new ArrayList<ImRoleItemRel>();
  329. for (ImItem i : ils) {
  330. if (!"".equals(i.getId())) {
  331. ImRoleItemRel ri = new ImRoleItemRel();
  332. ri.setRoleid(roleId);
  333. ri.setItemid(i.getId());
  334. ri.setAuthtype(i.getAuthtype());
  335. ri.setAuthshape(i.getAuthshape());
  336. ls.add(ri);
  337. }
  338. }
  339. ImRoleItemRelMapper im = Service.lookup(IMINDataBaseService.class)
  340. .getMybatisMapper(ImRoleItemRelMapper.class);
  341. for (ImRoleItemRel i : ls)
  342. im.insert(i);
  343. }
  344. // 添加角色菜单权限数据
  345. ImRoleAuthKey roleAuth = new ImRoleAuthKey();
  346. roleAuth.setRoleid(roleId);
  347. for (int j = 0; j < auths.length; j++) {
  348. String authNo = auths[j];
  349. roleAuth.setAuthNo(authNo);
  350. Service.lookup(IMINDataBaseService.class).insert(ImRoleAuthMapper.class, roleAuth);
  351. }
  352. res.set("roleId", roleId);
  353. res.set("roleName", name);
  354. String logInfo = "新增角色:" + roleId;
  355. // Service.lookup(ILogService.class).logging(session, logInfo);
  356. return res;
  357. }
  358. private String getRoleId(int roleId) throws BusinessCodeException, MINBusinessException {
  359. ImRole rx = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImRoleMapper.class,
  360. String.valueOf(roleId));
  361. if (rx == null) {
  362. return String.valueOf(roleId);
  363. } else {
  364. return getRoleId(roleId + 1);
  365. }
  366. }
  367. @MINAction(value = ROLE_QUERY_ALL)
  368. public MINActionResult roleQueryAll() throws MINBusinessException {
  369. MINActionResult res = new MINActionResult();
  370. ImRoleExample s = new ImRoleExample();
  371. List<ImRole> ls = Service.lookup(IMINDataBaseService.class).selectByExample(ImRoleMapper.class, s);
  372. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  373. return res;
  374. }
  375. @MINAction(value = HAVE_AUTH_SHAPE)
  376. public MINActionResult isHaveAuthShape(@MINParam(key = "itemId") String itemId,
  377. @MINParam(key = "roleId") String roleId, @MINParam(key = "dataTypeStr") String dataTypeStr,
  378. @MINParam(key = "dataShapeStr") String dataShapeStr, MINSession session) throws MINBusinessException {
  379. MINActionResult res = new MINActionResult();
  380. Map m = new HashMap();
  381. m.put("itemId", itemId);
  382. m.put("roleId", roleId);
  383. m.put("dataTypeStr", dataTypeStr);
  384. m.put("dataShapeStr", dataShapeStr);
  385. List<Map<String, String>> ls = new ArrayList();
  386. ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class).queryHaveAuthShape(m);
  387. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  388. return res;
  389. }
  390. @MINAction(value = AUTH_MODIFY, transaction = IMINTransactionEnum.CMT)
  391. public MINActionResult authModify(@MINParam(key = "roleid") String roleid, @MINParam(key = "itemid") String itemid,
  392. @MINParam(key = "authtype") String authtype, // 要变成的类型
  393. @MINParam(key = "authshape") String authshape, // 要变成的类型
  394. @MINParam(key = "authrole") String[] authrole, @MINParam(key = "autIds") String autIds,
  395. @MINParam(key = "nowShape") String nowShape, // 没有修改前的类型
  396. @MINParam(key = "nowType") String nowType, // 没有修改前的类型
  397. MINSession session) throws MINBusinessException {
  398. User u = session.getUser();
  399. if (u.getRoleId().equals(roleid))
  400. throw new MINBusinessException("不能修改当前登录操作员的相关信息");
  401. ImItem item = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImItemMapper.class, itemid);
  402. if (item == null)
  403. throw new MINBusinessException("授权资源不存在");
  404. String auth = item.getAuth();
  405. if (IConstant.AUTH_CANDIDATE_NONE.equals(auth)) {
  406. throw new MINBusinessException("该资源不需要授权");
  407. } else if (IConstant.AUTH_CANDIDATE_INTIME.equals(auth)) {
  408. if (IConstant.AUTH_TYPE_SPECIFY.equals(authtype) || IConstant.AUTH_SHAPE_AUTH.equals(authshape))
  409. throw new MINBusinessException("授权类型支持即时生效和互为授权,授权类型只支持临柜");
  410. }
  411. MINActionResult res = new MINActionResult();
  412. ImRoleItemRelKey key = new ImRoleItemRelKey();
  413. key.setItemid(itemid);
  414. key.setRoleid(roleid);
  415. ImRoleItemRel ri = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImRoleItemRelMapper.class, key);
  416. if (ri == null)
  417. throw new MINBusinessException("权限信息不存在");
  418. ri.setAuthtype(authtype);
  419. ri.setAuthshape(authshape);
  420. if ("2".equals(authtype)) {
  421. StringBuffer sb = new StringBuffer(",");
  422. if (authrole != null) {
  423. for (int i = 0; i < authrole.length; i++) {
  424. if (i > 0)
  425. sb.append(",");
  426. sb.append(authrole[i]);
  427. }
  428. }
  429. ri.setAuthrole(sb.append(",").toString());
  430. }
  431. boolean statusType = "1".equals(nowType) || "2".equals(nowType);
  432. if (!"".equals(nowType) && !"".equals(nowShape) && (statusType && "0".equals(authtype)
  433. || (statusType && "1".equals(authshape) && "0".equals(nowShape)))) {
  434. String[] autAry = autIds.split(",");
  435. List list = new ArrayList();
  436. for (String string : autAry) {
  437. list.add(string);
  438. }
  439. Map m = new HashMap();
  440. m.put("name", u.getId());
  441. m.put("list", list);
  442. if (list.size() > 0) {
  443. Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class).updateAuthStatusToFive(m);
  444. }
  445. }
  446. Service.lookup(IMINDataBaseService.class).updateByPrimaryKey(ImRoleItemRelMapper.class, ri);
  447. String logInfo = "修改授权规则:" + roleid;
  448. // Service.lookup(ILogService.class).logging(session, logInfo);
  449. return res;
  450. }
  451. @MINAction(value = ROLE_AUTH_QUERY_WITHOUT_PWD)
  452. public MINActionResult roleAuthQueryWithoutPwd(@MINParam(key = "id") String id, MINSession session)
  453. throws MINBusinessException {
  454. MINActionResult res = new MINActionResult();
  455. Map<String, String> map = new HashMap<String, String>();
  456. map.put("roleid", id);
  457. List<Map<String, String>> ls = null;
  458. if (id == null || "".equals(id)) {// 新增角色时使用
  459. // ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class).roleMenuWithOutPwd(id);
  460. ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  461. .roleMenuWithAuthForAdd(map);
  462. } else {// 查看角色使用
  463. // ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class).roleMenuWithOutPwdByTemp(map);
  464. ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  465. .roleMenuWithAuthForDetail(map);
  466. }
  467. Map<String, Map<String, String>> menu = new HashMap<String, Map<String, String>>();
  468. for (Map<String, String> m : ls) {
  469. menu.put(m.get("menuid"), m);
  470. String s = m.get("authrole");
  471. if (s != null) {
  472. ((Map) m).put("authrole", s.split(","));
  473. }
  474. }
  475. for (Map<String, String> s : menu.values()) {
  476. Map<String, String> m = new HashMap<String, String>();
  477. m.put("menuid", s.get("menuid"));
  478. m.put("lv1", s.get("lv1"));
  479. m.put("menserialno", s.get("menserialno"));
  480. m.put("menuparentid", s.get("menuparentid"));
  481. ls.add(m);
  482. }
  483. Collections.sort(ls, new Comparator<Map<String, String>>() {
  484. public int compare(Map<String, String> o1, Map<String, String> o2) {
  485. String x1 = Str.nvl(o1.get("menserialno"));
  486. String x2 = Str.nvl(o2.get("menserialno"));
  487. String s1 = Str.nvl(o1.get("itmserialno"));
  488. String s2 = Str.nvl(o2.get("itmserialno"));
  489. String m1 = Str.nvl(o1.get("menuparentid"));
  490. String m2 = Str.nvl(o2.get("menuparentid"));
  491. if (m1.equals(m2)) {
  492. return x1.equals(x2) ? s1.compareTo(s2) : x1.compareTo(x2);
  493. }
  494. return m1.compareTo(m2);
  495. }
  496. });
  497. ls = new MINCopyFormat("{authtype:'authtypeDesc',authshape:'authshapeDesc'}").format(ls);
  498. // ls = Service.lookup(IFormatService.class).formatEnum(ls, "{authtypeDesc:'IM_AUTH_TYPE',authshapeDesc:'IM_AUTH_SHAPE'}");
  499. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  500. return res;
  501. }
  502. @MINAction(value = ROLE_AUTH_QUERY)
  503. public MINActionResult roleAuthQuery(@MINParam(key = "id") String id,
  504. @MINParam(key = "filterbyrole") boolean filterbyrole, MINSession session) throws MINBusinessException {
  505. MINActionResult res = new MINActionResult();
  506. ImRole role = Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleMapper.class)
  507. .selectByPrimaryKey(id);
  508. Map<String, String> map = new HashMap<String, String>();
  509. map.put("roleid", id);
  510. User user = session.getUser();
  511. ImBranch branch = Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImBranchMapper.class)
  512. .selectByPrimaryKey(role.getBranchid());
  513. if (!Constant.BRANCH_LEVEL_SYS.equals(branch.getLevel().toString())) {// 非平台用户使用模板菜单数据
  514. // map.put("tempid", branch.getType() );
  515. }
  516. // List<Map<String, String>> ls =
  517. // Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class).roleMenu(id);
  518. List<Map<String, String>> ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  519. .roleMenuByTemp(map);
  520. if (filterbyrole) {
  521. for (Iterator<Map<String, String>> it = ls.iterator(); it.hasNext();)
  522. if (it.next().get("roleid") == null)
  523. it.remove();
  524. }
  525. Map<String, Map<String, String>> menu = new HashMap<String, Map<String, String>>();
  526. for (Map<String, String> m : ls) {
  527. menu.put(m.get("menuid"), m);
  528. String s = m.get("authrole");
  529. if (s != null)
  530. ((Map) m).put("authrole", s.split(","));
  531. }
  532. for (Map<String, String> s : menu.values()) {
  533. Map<String, String> m = new HashMap<String, String>();
  534. m.put("menuid", s.get("menuid"));
  535. m.put("lv1", s.get("lv1"));
  536. m.put("menserialno", s.get("menserialno"));
  537. ls.add(m);
  538. }
  539. Collections.sort(ls, new Comparator<Map<String, String>>() {
  540. public int compare(Map<String, String> o1, Map<String, String> o2) {
  541. String x1 = Str.nvl(o1.get("menserialno"));
  542. String x2 = Str.nvl(o2.get("menserialno"));
  543. String s1 = Str.nvl(o1.get("itmserialno"));
  544. String s2 = Str.nvl(o2.get("itmserialno"));
  545. return x1.equals(x2) ? s1.compareTo(s2) : x1.compareTo(x2);
  546. }
  547. });
  548. ls = new MINCopyFormat("{authtype:'authtypeDesc',authshape:'authshapeDesc'}").format(ls);
  549. // ls = Service.lookup(IFormatService.class).formatEnum(ls, "{authtypeDesc:'IM_AUTH_TYPE',authshapeDesc:'IM_AUTH_SHAPE'}");
  550. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  551. // res.set(IMINBusinessConstant.F_QUERY_RESULT, authList);
  552. return res;
  553. }
  554. /**
  555. * 查询当前机构及下一级机构下的角色 信息
  556. *
  557. * @param session
  558. * @return
  559. * @throws MINBusinessException
  560. */
  561. @MINAction(value = ROLE_QUERY_BY_PARENTID)
  562. public MINActionResult roleQueryByParentId(MINSession session) throws MINBusinessException {
  563. MINActionResult res = new MINActionResult();
  564. User user = session.getUser();
  565. String branchId = user.getBranchId();
  566. if (branchId == null || "".equals(branchId)) {
  567. // throw new BusinessCodeException("EBIM0009");
  568. }
  569. Map<String, String> m = new HashMap<String, String>();
  570. m.put("branchId", branchId);
  571. List<Map<String, String>> ls = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  572. .roleQueryByParentId(m);
  573. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  574. return res;
  575. }
  576. /**
  577. * 查询当前机构下的角色信息
  578. *
  579. * @param session
  580. * @return
  581. * @throws MINBusinessException
  582. */
  583. @MINAction(value = ROLE_QUERY_BY_BRANCHID)
  584. public MINActionResult roleQueryByBranchId(@MINParam(key = "branchId") String branchId, MINSession session)
  585. throws MINBusinessException {
  586. MINActionResult res = new MINActionResult();
  587. ImRoleExample example = new ImRoleExample();
  588. ImRoleExample.Criteria criteria = example.createCriteria();
  589. User user = session.getUser();
  590. criteria.andBranchidEqualTo(user.getBranchId());
  591. List<ImRole> ls = Service.lookup(IMINDataBaseService.class).selectByExample(ImRoleMapper.class, example);
  592. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  593. return res;
  594. }
  595. /**
  596. * 查询角色菜单已经设置的权限
  597. *
  598. * @param session
  599. * @return
  600. * @throws MINBusinessException
  601. */
  602. @MINAction(value = ROLE_AUTH_QUERY_BY_ROLEID)
  603. public MINActionResult roleAuthQueryByRoleId(@MINParam(key = "roleId") String roleId,
  604. @MINParam(key = "itemNo") String itemNo, MINSession session) throws MINBusinessException {
  605. MINActionResult res = new MINActionResult();
  606. /*
  607. * ImRoleAuthExample ae = new ImRoleAuthExample();
  608. * ae.createCriteria().andRoleidEqualTo(roleId).andCodeEqualTo(Constant.
  609. * ARTICLE_MODULE_CODE).andItemidEqualTo(itemNo); List<ImRoleAuth> authList =
  610. * Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleAuthMapper.
  611. * class).selectByExample(ae); String[] authArr = new String[authList.size()];
  612. * String str = ""; Map mm = new HashMap<String,String>(); for(int
  613. * i=0;i<authList.size();i++){ authArr[i] = authList.get(i).getValue(); str =
  614. * str+authList.get(i).getValue()+","; // } mm.put("authMod", str); //
  615. * ls.add(mm); res.set(IMINBusinessConstant.F_QUERY_RESULT, authList);
  616. */
  617. return res;
  618. }
  619. /**
  620. * 查询角色需要设置权限的菜单和权限列表
  621. *
  622. * @param roleId
  623. * @param session
  624. * @return
  625. * @throws MINBusinessException
  626. */
  627. @MINAction(value = ROLE_AUTHSET_QUERY)
  628. public MINActionResult roleAuthSetQuery(@MINParam(key = "roleId") String roleId, MINSession session)
  629. throws MINBusinessException {
  630. MINActionResult res = new MINActionResult();
  631. User u = session.getUser();
  632. String branchId = u.getBranchId();
  633. ImBranch br = Service.lookup(IMINDataBaseService.class).selectByPrimaryKey(ImBranchMapper.class, branchId);
  634. Map<String, String> mm = new HashMap<String, String>();
  635. mm.put("roleId", roleId);
  636. // mm.put("btype", br.getType());
  637. List<Map<String, String>> list = Service.lookup(IMINDataBaseService.class).getMybatisMapper(RoleMapper.class)
  638. .roleAuthSetQuery(mm);
  639. List<Map<String, String>> authList = new ArrayList<Map<String, String>>();
  640. String itemId = "";// 缓存菜单Id
  641. String codes = "";
  642. String vals = "";
  643. String itemName = "";
  644. for (Map<String, String> m : list) {
  645. String id = m.get("itemId");
  646. String code = m.get("code");
  647. String val = m.get("val");
  648. String name = m.get("name");
  649. if (itemId.equals(id)) {// 相等则itemId已存在
  650. vals += val + ",";
  651. } else {
  652. if ("".equals(itemId)) {// 第一次
  653. itemId = id;
  654. codes = code;
  655. itemName = name;
  656. vals += val + ",";
  657. } else {
  658. Map<String, String> ms = new HashMap<String, String>();
  659. ms.put("itemNo", itemId);
  660. ms.put("codes", codes);
  661. ms.put("itemName", itemName);
  662. ms.put("vals", vals.substring(0, vals.length() - 1));
  663. authList.add(ms);
  664. itemId = id;
  665. codes = code;
  666. itemName = name;
  667. vals = val + ",";
  668. }
  669. }
  670. }
  671. Map<String, String> ms = new HashMap<String, String>();
  672. ms.put("itemNo", itemId);
  673. ms.put("codes", codes);
  674. ms.put("itemName", itemName);
  675. ms.put("vals", vals.substring(0, vals.length() - 1));
  676. authList.add(ms);
  677. res.set("roleId", roleId);
  678. res.set(IMINBusinessConstant.F_QUERY_RESULT, authList);
  679. return res;
  680. }
  681. /**
  682. * 给角色设置菜单权限
  683. *
  684. * @param roleId
  685. * @param session
  686. * @return
  687. * @throws MINBusinessException
  688. */
  689. @MINAction(value = SET_ITEM_AUTH)
  690. public MINActionResult setItemAuth(@MINParam(key = "roleId") String roleId,
  691. @MINParam(key = "parameStr") String parameStr, MINSession session) throws MINBusinessException {
  692. MINActionResult res = new MINActionResult();
  693. String[] parameArr = parameStr.split("\\|");
  694. for (int i = 0; i < parameArr.length; i++) {
  695. String str = parameArr[i];
  696. String itemId = str.substring(0, str.indexOf(":"));// 菜单编号
  697. String code = str.substring(str.indexOf(":") + 1, str.indexOf("="));// 模块编码
  698. String[] types = str.substring(str.indexOf("=") + 1).split(",");// 模块值
  699. // 添加模块控制
  700. /*
  701. * List<ImRoleAuth> authList = new ArrayList<ImRoleAuth>(); for(int
  702. * j=0;j<types.length;j++){ String authNo =
  703. * Service.lookup(IPublicService.class).getSequence("IM_ROLE_AUTH_NO");
  704. * ImRoleAuth ai = new ImRoleAuth(); ai.setNo(authNo); ai.setCode(code);
  705. * ai.setRoleid(roleId); ai.setType(Constant.MODULE_AUTH_CODE);
  706. * ai.setValue(types[j]); ai.setItemid(itemId); authList.add(ai); }
  707. * ImRoleAuthMapper am =
  708. * Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleAuthMapper.
  709. * class); for(ImRoleAuth roleAuth:authList){ am.insert(roleAuth); }
  710. */
  711. }
  712. String logInfo = "设置角色菜单权限,角色编号" + roleId;
  713. // Service.lookup(ILogService.class).logging(session, logInfo);
  714. return res;
  715. }
  716. /**
  717. * 修改角色菜单权限 菜单模块设置
  718. *
  719. * @param roleId
  720. * @param session
  721. * @return
  722. * @throws MINBusinessException
  723. */
  724. @MINAction(value = MODIFY_ITEM_AUTH)
  725. public MINActionResult modifyItemAuth(@MINParam(key = "roleId") String roleId,
  726. @MINParam(key = "parameStr") String parameStr, MINSession session) throws MINBusinessException {
  727. MINActionResult res = new MINActionResult();
  728. /*
  729. * String[] parameArr = parameStr.split("\\|"); for(int
  730. * i=0;i<parameArr.length;i++){ String str = parameArr[i]; String itemId =
  731. * str.substring(0, str.indexOf(":"));//菜单编号 String code =
  732. * str.substring(str.indexOf(":")+1, str.indexOf("="));//模块编码 String[] types =
  733. * str.substring(str.indexOf("=")+1).split(",");//模块值 //删除原来的菜单权限 List<String>
  734. * sls = Arrays.asList(types); ImRoleAuthExample ae = new ImRoleAuthExample();
  735. * ImRoleAuthExample.Criteria aec = ae.createCriteria();
  736. * aec.andRoleidEqualTo(roleId).andCodeEqualTo(code).andItemidEqualTo(itemId).
  737. * andTypeEqualTo(Constant.MODULE_AUTH_CODE);
  738. *
  739. * Service.lookup(IMINDataBaseService.class).deleteByExample(ImRoleAuthMapper.
  740. * class, ae); //添加模块控制 List<ImRoleAuth> authList = new ArrayList<ImRoleAuth>();
  741. * for(int j=0;j<types.length;j++){ String authNo =
  742. * Service.lookup(IPublicService.class).getSequence("IM_ROLE_AUTH_NO");
  743. * ImRoleAuth ai = new ImRoleAuth(); ai.setNo(authNo); ai.setCode(code);
  744. * ai.setRoleid(roleId); ai.setType(Constant.MODULE_AUTH_CODE);
  745. * ai.setValue(types[j]); ai.setItemid(itemId); authList.add(ai); }
  746. * ImRoleAuthMapper am =
  747. * Service.lookup(IMINDataBaseService.class).getMybatisMapper(ImRoleAuthMapper.
  748. * class); for(ImRoleAuth roleAuth:authList){ am.insert(roleAuth); } }
  749. */
  750. String logInfo = "修改角色菜单权限:" + roleId;
  751. // Service.lookup(ILogService.class).logging(session, logInfo);
  752. return res;
  753. }
  754. /**
  755. * 查询角色对应的模块权限
  756. *
  757. * @param roleId
  758. * @param session
  759. * @return
  760. * @throws MINBusinessException
  761. */
  762. @MINAction(value = GET_ROLE_MODE)
  763. public MINActionResult getRoleModule(@MINParam(key = "roleId") String roleId,
  764. @MINParam(key = "MIN_context_itemId") String itemId, @MINParam(key = "businessCode") String businessCode,
  765. MINSession session) throws MINBusinessException {
  766. MINActionResult res = new MINActionResult();
  767. /*
  768. * User user = session.getUser(); roleId = user.getRoleId();
  769. *
  770. * String sessionItemId = (String)session.get("sessionItemId");
  771. *
  772. * ImRoleAuthExample example = new ImRoleAuthExample();
  773. *
  774. * ImRoleAuthExample.Criteria criteria = example.createCriteria();
  775. * criteria.andRoleidEqualTo(roleId);
  776. * criteria.andCodeEqualTo(Constant.ARTICLE_MODULE_CODE);
  777. * criteria.andItemidEqualTo(sessionItemId);
  778. * example.setOrderByClause("IRA_VALUE");
  779. *
  780. * List<ImRoleAuth> list = Service.lookup(IMINDataBaseService.class)
  781. * .selectByExample(ImRoleAuthMapper.class, example); list = new
  782. * MINCopyFormat("{value:'moduleDesc'}").format(list); list =
  783. * Service.lookup(IFormatService.class).formatEnum(list,
  784. * "{moduleDesc:'ARTICLE_MODULE'}");//状态
  785. * res.set(IMINBusinessConstant.F_QUERY_RESULT, list);
  786. */
  787. return res;
  788. }
  789. /**
  790. * 查询所有部门
  791. *
  792. * @param session
  793. * @return
  794. * @throws MINBusinessException
  795. */
  796. @MINAction(value = "roleQueryBranch")
  797. public MINActionResult roleQueryBranch(MINSession session) throws MINBusinessException {
  798. MINActionResult res = new MINActionResult();
  799. ImBranchExample example = new ImBranchExample();
  800. ImBranchExample.Criteria criteria = example.createCriteria();
  801. User user = session.getUser();
  802. criteria.andSttNotEqualTo(Constant.DEPT_STT_DEL);
  803. if(!user.getBranchId().equals(Constant.DEFAULT_INSTITUTIONS)){
  804. criteria.andIdEqualTo(user.getBranchId());
  805. }
  806. List<ImBranch> ls = Service.lookup(IMINDataBaseService.class).selectByExample(ImBranchMapper.class, example);
  807. res.set(IMINBusinessConstant.F_QUERY_RESULT, ls);
  808. return res;
  809. }
  810. }