role.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package restcontroller.webmanage.role;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import common.Controller;
  5. import common.YosException;
  6. import common.annotation.API;
  7. import common.annotation.CACHEING;
  8. import common.annotation.CACHEING_CLEAN;
  9. import common.annotation.cm;
  10. import common.data.Row;
  11. import common.data.Rows;
  12. import common.data.RowsMap;
  13. import common.data.SQLFactory;
  14. import restcontroller.R;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. @API(title = "管理端-权限管理")
  18. public class role extends Controller {
  19. public role(JSONObject content) throws YosException {
  20. super(content);
  21. }
  22. @API(title = "角色列表查询", apiversion = R.ID20221101131902.v1.class)
  23. @CACHEING
  24. public String query_roleList() throws YosException {
  25. /*
  26. 过滤条件设置
  27. */
  28. StringBuffer where = new StringBuffer(" 1=1 ");
  29. if (content.containsKey("where")) {
  30. JSONObject whereObject = content.getJSONObject("where");
  31. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  32. where.append(" and(");
  33. where.append("t1.rolename like'%").append(whereObject.getString("condition")).append("%' ");
  34. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  35. where.append(")");
  36. }
  37. if (whereObject.containsKey("usertype") && !"".equals(whereObject.getString("usertype"))) {
  38. where.append(" and(");
  39. where.append("t1.usertype like'%").append(whereObject.getString("usertype")).append("%' ");
  40. where.append(")");
  41. }
  42. if (whereObject.containsKey("issystem") && !"".equals(whereObject.getString("issystem"))) {
  43. where.append(" and(");
  44. where.append("t1.issystem ='").append(whereObject.getString("issystem")).append("'");
  45. where.append(")");
  46. }
  47. }
  48. SQLFactory sqlFactory = new SQLFactory(this, "角色列表查询", pageSize, pageNumber, pageSorting);
  49. sqlFactory.addParameter("siteid", siteid);
  50. sqlFactory.addParameter_SQL("where", where);
  51. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  52. HashMap<String, String> usertypemap = getOptionType("usertype");
  53. for (Row row : rows) {
  54. row.put("usertypename", usertypemap.get(row.getString("usertype")));
  55. }
  56. return getSucReturnObject().setData(rows).toString();
  57. }
  58. @API(title = "角色详情查询")
  59. @CACHEING
  60. public String query_roleMain() throws YosException {
  61. long roleid = content.getLong("roleid");
  62. SQLFactory rolesql = new SQLFactory(this, "角色详情查询");
  63. rolesql.addParameter("roleid", roleid);
  64. rolesql.addParameter("siteid", siteid);
  65. Rows roleRows = dbConnect.runSqlQuery(rolesql);
  66. for (Row roleRow : roleRows) {
  67. roleRow.put("usertypename", getOptionType("usertype").get(roleRow.getString("usertype")));
  68. SQLFactory appsql = new SQLFactory(this, "角色详情查询_授权应用");
  69. StringBuffer where = new StringBuffer(" 1=1 ");
  70. if (content.containsKey("where")) {
  71. JSONObject whereObject = content.getJSONObject("where");
  72. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  73. where.append(" and(");
  74. where.append("t4.systemappname like'%").append(whereObject.getString("condition")).append("%' ");
  75. where.append("or t3.systemmodulename like'%").append(whereObject.getString("condition")).append("%' ");
  76. where.append("or t1.systemname like'%").append(whereObject.getString("condition")).append("%' ");
  77. where.append(")");
  78. }
  79. }
  80. appsql.addParameter_SQL("where", where);
  81. appsql.addParameter("roleid", roleid);
  82. Rows appRows = dbConnect.runSqlQuery(appsql);
  83. SQLFactory optionsql = new SQLFactory(this, "角色详情查询_授权功能");
  84. optionsql.addParameter("roleid", roleid);
  85. RowsMap optionRowsMap = dbConnect.runSqlQuery(optionsql).toRowsMap("systemappid");
  86. SQLFactory fieldlimitsql = new SQLFactory(this, "角色详情查询_隐藏栏位限制");
  87. fieldlimitsql.addParameter("roleid", roleid);
  88. RowsMap fieldlimitRowsMap = dbConnect.runSqlQuery(fieldlimitsql).toRowsMap("systemappid");
  89. for (Row appRow : appRows) {
  90. appRow.put("options", optionRowsMap.get(appRow.getString("systemappid")));
  91. appRow.put("hiddenfields", fieldlimitRowsMap.get(appRow.getString("systemappid")));
  92. }
  93. roleRow.put("apps", appRows);
  94. }
  95. return getSucReturnObject().setData(roleRows.get(0)).toString();
  96. }
  97. @API(title = "角色新增修改")
  98. @CACHEING_CLEAN(cms = {@cm(clazz = role.class, method = {"query_roleList", "query_roleMain"})})
  99. public String insertormodify_role() throws YosException {
  100. long roleid = content.getLongValue("roleid");
  101. String rolename = content.getString("rolename", "sys_role");
  102. String remarks = content.getString("remarks", "sys_role");
  103. String usertype = content.getString("usertype", "sys_role");
  104. SQLFactory sqlFactory = null;
  105. if (roleid <= 0 || dbConnect.runSqlQuery("select roleid from sys_role where roleid=" + roleid).isEmpty()) {
  106. roleid = createTableID("sys_role");
  107. sqlFactory = new SQLFactory(this, "角色新增");
  108. sqlFactory.addParameter("roleid", roleid);
  109. sqlFactory.addParameter("isshieldinfo", content.getLongValue("isshieldinfo"));
  110. sqlFactory.addParameter("siteid", siteid);
  111. sqlFactory.addParameter("rolename", rolename);
  112. sqlFactory.addParameter("remarks", remarks);
  113. sqlFactory.addParameter("usertype", usertype);
  114. sqlFactory.addParameter("createby", username);
  115. content.put("roleid", roleid);
  116. } else {
  117. if (dbConnect.runSqlQuery("select * from sys_role where issystem=1 and roleid=" + roleid).isNotEmpty()) {
  118. //return getErrReturnObject().setErrMsg("系统预设权限组不可修改").toString();
  119. }
  120. sqlFactory = new SQLFactory(this, "角色修改");
  121. sqlFactory.addParameter("roleid", roleid);
  122. sqlFactory.addParameter("rolename", rolename);
  123. sqlFactory.addParameter("isshieldinfo", content.getLongValue("isshieldinfo"));
  124. sqlFactory.addParameter("remarks", remarks);
  125. sqlFactory.addParameter("usertype", usertype);
  126. sqlFactory.addParameter("changeby", username);
  127. }
  128. dbConnect.runSqlUpdate(sqlFactory);
  129. return query_roleMain();
  130. }
  131. @API(title = "角色删除", apiversion = R.ID20221101132002.v1.class)
  132. @CACHEING_CLEAN(cms = {@cm(clazz = role.class, method = {"query_roleList", "query_roleMain"})})
  133. public String delete_role() throws YosException {
  134. JSONArray roleids = content.getJSONArray("roleids");
  135. String sql = "select * from sys_role where issystem=1 and roleid in " + roleids.toJavaList(Long.class);
  136. sql = sql.replace("[", "(").replace("]", ")");
  137. if (dbConnect.runSqlQuery(sql).isNotEmpty()) {
  138. return getErrReturnObject().setErrMsg("系统预设权限组不可删除").toString();
  139. }
  140. ArrayList<String> sqlilist = new ArrayList<>();
  141. for (Object id : roleids) {
  142. Long roleid = Long.parseLong(id.toString());
  143. sqlilist.add("delete from sys_role where roleid=" + roleid);
  144. sqlilist.add("delete from sys_roleappoptionauth where roleid=" + roleid);
  145. sqlilist.add("delete from sys_roleapphiddenfieldlimit where roleid=" + roleid);
  146. sqlilist.add("delete from sys_userrole where roleid=" + roleid);
  147. sqlilist.add("delete from sys_role_datalimit where roleid=" + roleid);
  148. sqlilist.add("delete from sys_rolereportauth where roleid=" + roleid);
  149. }
  150. dbConnect.runSqlUpdate(sqlilist);
  151. for (Object id : roleids) {
  152. Long roleid = Long.parseLong(id.toString());
  153. logroleout(roleid);
  154. }
  155. return getSucReturnObject().toString();
  156. }
  157. /**
  158. * 系统应用选择
  159. *
  160. * @return
  161. */
  162. @API(title = "角色授权系统应用选择")
  163. @CACHEING
  164. public String query_appselect() throws YosException {
  165. /*
  166. 过滤条件设置
  167. */
  168. StringBuffer where = new StringBuffer(" 1=1 ");
  169. if (content.containsKey("where")) {
  170. JSONObject whereObject = content.getJSONObject("where");
  171. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  172. where.append(" and(");
  173. where.append("t1.systemname like'%").append(whereObject.getString("condition")).append("%' ");
  174. where.append("or t2.systemclientname like'%").append(whereObject.getString("condition")).append("%' ");
  175. where.append("or t3.systemmodulename like'%").append(whereObject.getString("condition")).append("%' ");
  176. where.append("or t4.systemappname like'%").append(whereObject.getString("condition")).append("%' ");
  177. where.append(")");
  178. }
  179. if (whereObject.containsKey("systemname") && !"".equals(whereObject.getString("systemname"))) {
  180. where.append(" and(");
  181. where.append("t1.systemname like'%").append(whereObject.getString("systemname")).append("%' ");
  182. where.append(")");
  183. }
  184. if (whereObject.containsKey("systemclientname") && !"".equals(whereObject.getString("systemclientname"))) {
  185. where.append(" and(");
  186. where.append("t2.systemclientname like'%").append(whereObject.getString("systemclientname")).append("%' ");
  187. where.append(")");
  188. }
  189. if (whereObject.containsKey("systemmodulename") && !"".equals(whereObject.getString("systemmodulename"))) {
  190. where.append(" and(");
  191. where.append("t3.systemmodulename like'%").append(whereObject.getString("systemmodulename")).append("%' ");
  192. where.append(")");
  193. }
  194. if (whereObject.containsKey("systemappname") && !"".equals(whereObject.getString("systemappname"))) {
  195. where.append(" and(");
  196. where.append("t4.systemappname like'%").append(whereObject.getString("systemappname")).append("%' ");
  197. where.append(")");
  198. }
  199. }
  200. SQLFactory appselectsql = new SQLFactory(this, "应用授权选择", pageSize, pageNumber, pageSorting);
  201. appselectsql.addParameter_SQL("where", where);
  202. Rows approws = dbConnect.runSqlQuery(appselectsql);
  203. return getSucReturnObject().setData(approws).toString();
  204. }
  205. /**
  206. * 系统应用功能选择
  207. *
  208. * @return
  209. */
  210. @API(title = "角色授权系统功能选择")
  211. @CACHEING
  212. public String query_appoptionselect() throws YosException {
  213. long roleid = content.getLongValue("roleid");
  214. long systemappid = content.getLong("systemappid");
  215. SQLFactory optionselectsql = new SQLFactory(this, "应用功能授权选择");
  216. optionselectsql.addParameter_in("systemappid", systemappid);
  217. optionselectsql.addParameter("roleid", roleid);
  218. Rows optionRows = dbConnect.runSqlQuery(optionselectsql.getSQL());
  219. return getSucReturnObject().setData(optionRows).toString();
  220. }
  221. /**
  222. * 系统应用隐藏栏位选择
  223. *
  224. * @return
  225. */
  226. @API(title = "角色授权系统隐藏栏位选择")
  227. @CACHEING
  228. public String query_apphiddenfieldselect() throws YosException {
  229. long roleid = content.getLongValue("roleid");
  230. long systemappid = content.getLong("systemappid");
  231. SQLFactory hiddenfieldselectsql = new SQLFactory(this, "应用隐藏栏位选择");
  232. hiddenfieldselectsql.addParameter_in("systemappid", systemappid);
  233. hiddenfieldselectsql.addParameter("roleid", roleid);
  234. Rows hiddenfieldsRows = dbConnect.runSqlQuery(hiddenfieldselectsql.getSQL());
  235. return getSucReturnObject().setData(hiddenfieldsRows).toString();
  236. }
  237. /**
  238. * 角色应用功能授权
  239. *
  240. * @return
  241. */
  242. @API(title = "角色应用功能授权")
  243. @CACHEING_CLEAN(cms = {@cm(clazz = role.class, method = {"query_roleMain", "query_appoptionselect"})})
  244. public String add_appauth() throws YosException {
  245. JSONArray apparrays = content.getJSONArray("systemapps");
  246. long roleid = content.getLong("roleid");
  247. ArrayList<String> sqllist = new ArrayList<>();
  248. for (Object o : apparrays) {
  249. JSONObject appObject = (JSONObject) o;
  250. long systemappid = appObject.getLong("systemappid");
  251. JSONArray optionids = appObject.getJSONArray("optionids");
  252. for (Object optionid : optionids) {
  253. SQLFactory sqlFactory = new SQLFactory(this, "角色功能授权");
  254. sqlFactory.addParameter("roleid", roleid);
  255. sqlFactory.addParameter("optionauthid", createTableID("sys_roleappoptionauth"));
  256. sqlFactory.addParameter("systemappid", systemappid);
  257. sqlFactory.addParameter("optionid", optionid.toString());
  258. sqllist.add(sqlFactory.getSQL());
  259. }
  260. JSONArray hiddenfields = appObject.getJSONArray("hiddenfields");
  261. for (Object hiddenfieldid : hiddenfields) {
  262. SQLFactory sqlFactory = new SQLFactory(this, "角色隐藏栏位授权");
  263. sqlFactory.addParameter("roleid", roleid);
  264. sqlFactory.addParameter("hiddenfieldlimitid", createTableID("sys_roleapphiddenfieldlimit"));
  265. sqlFactory.addParameter("systemappid", systemappid);
  266. sqlFactory.addParameter("hiddenfieldid", hiddenfieldid.toString());
  267. sqllist.add(sqlFactory.getSQL());
  268. }
  269. }
  270. dbConnect.runSqlUpdate(sqllist);
  271. logroleout(roleid);
  272. return getSucReturnObject().toString();
  273. }
  274. /**
  275. * 角色应用功能授权取消
  276. *
  277. * @return
  278. */
  279. @API(title = "角色应用功能取消授权")
  280. @CACHEING_CLEAN(cms = {@cm(clazz = role.class, method = {"query_roleMain"})})
  281. public String delete_appauth() throws YosException {
  282. JSONArray apparrays = content.getJSONArray("systemapps");
  283. long roleid = content.getLong("roleid");
  284. ArrayList<String> sqllist = new ArrayList<>();
  285. for (Object o : apparrays) {
  286. JSONObject appObject = (JSONObject) o;
  287. long systemappid = appObject.getLong("systemappid");
  288. JSONArray optionids = appObject.getJSONArray("optionids");
  289. for (Object optionid : optionids) {
  290. sqllist.add("delete from sys_roleappoptionauth where roleid=" + roleid + " and systemappid=" + systemappid + " and optionid=" + optionid);
  291. }
  292. JSONArray hiddenfields = appObject.getJSONArray("hiddenfields");
  293. for (Object hiddenfieldid : hiddenfields) {
  294. sqllist.add("delete from sys_roleapphiddenfieldlimit where roleid=" + roleid + " and systemappid=" + systemappid + " and hiddenfieldid=" + hiddenfieldid);
  295. }
  296. }
  297. dbConnect.runSqlUpdate(sqllist);
  298. logroleout(roleid);
  299. return getSucReturnObject().toString();
  300. }
  301. @API(title = "查询角色是否已使用", apiversion = R.ID20221121112102.v1.class)
  302. public String isRoleUsed() throws YosException {
  303. JSONArray roleids = content.getJSONArray("roleids");
  304. String sql = "SELECT * from sys_userrole WHERE roleid in " + roleids + " and siteid = '" + siteid + "'";
  305. sql = sql.replace("[", "(").replace("]", ")");
  306. Rows rows = dbConnect.runSqlQuery(sql);
  307. if (rows.isEmpty()) {
  308. //角色没有在使用
  309. return getSucReturnObject().setData(1).toString();
  310. } else {
  311. //角色在使用
  312. return getSucReturnObject().setData(2).toString();
  313. }
  314. }
  315. @API(title = "角色授权,报表选择查询", apiversion = R.ID20221213141501.v1.class)
  316. public String roleAuthReportSelect() throws YosException {
  317. /*
  318. 过滤条件设置
  319. */
  320. StringBuffer where = new StringBuffer(" 1=1 ");
  321. if (content.containsKey("where")) {
  322. JSONObject whereObject = content.getJSONObject("where");
  323. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  324. where.append(" and(");
  325. where.append("t2.name like'%").append(whereObject.getString("condition")).append("%' ");
  326. where.append(")");
  327. }
  328. }
  329. long roleid = content.getLong("roleid");
  330. SQLFactory sqlFactory = new SQLFactory(this, "角色授权报表选择查询");
  331. sqlFactory.addParameter("siteid", siteid);
  332. sqlFactory.addParameter("roleid", roleid);
  333. sqlFactory.addParameter_SQL("where", where);
  334. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  335. return getSucReturnObject().setData(rows).toString();
  336. }
  337. @API(title = "角色报表查询", apiversion = R.ID20221213141601.v1.class)
  338. public String roleAuthReportQuery() throws YosException {
  339. long roleid = content.getLong("roleid");
  340. SQLFactory sqlFactory = new SQLFactory(this, "角色授权报表查询");
  341. sqlFactory.addParameter("siteid", siteid);
  342. sqlFactory.addParameter("roleid", roleid);
  343. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  344. return getSucReturnObject().setData(rows).toString();
  345. }
  346. @API(title = "角色报表删除", apiversion = R.ID20221213141701.v1.class)
  347. public String roleAuthReportDelete() throws YosException {
  348. long roleid = content.getLong("roleid");
  349. JSONArray sys_reportids = content.getJSONArray("sys_reportids");
  350. ArrayList<String> sqlist = new ArrayList<>();
  351. for (Object o : sys_reportids) {
  352. sqlist.add("delete from sys_rolereportauth where roleid=" + roleid + " and sys_reportid=" + o);
  353. }
  354. dbConnect.runSqlUpdate(sqlist);
  355. return getSucReturnObject().toString();
  356. }
  357. @API(title = "角色报表添加", apiversion = R.ID20221213141801.v1.class)
  358. public String roleAuthReportadd() throws YosException {
  359. long roleid = content.getLong("roleid");
  360. JSONArray sys_reportids = content.getJSONArray("sys_reportids");
  361. ArrayList<String> sqlist = new ArrayList<>();
  362. for (Object o : sys_reportids) {
  363. SQLFactory sqlFactory = new SQLFactory(this, "角色授权报表新增");
  364. sqlFactory.addParameter("sys_rolereportauthid", createTableID("sys_rolereportauth"));
  365. sqlFactory.addParameter("roleid", roleid);
  366. sqlFactory.addParameter("sys_reportid", String.valueOf(o));
  367. sqlist.add(sqlFactory.getSQL());
  368. }
  369. dbConnect.runSqlUpdate(sqlist);
  370. return getSucReturnObject().toString();
  371. }
  372. @API(title = "角色数据限制查询", apiversion = R.ID20230216181301.v1.class)
  373. public String roleAuthDataLimitQuery() throws YosException {
  374. long roleid = content.getLong("roleid");
  375. SQLFactory sqlFactory = new SQLFactory(this, "角色数据限制查询");
  376. sqlFactory.addParameter("siteid", siteid);
  377. sqlFactory.addParameter("roleid", roleid);
  378. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  379. return getSucReturnObject().setData(rows).toString();
  380. }
  381. @API(title = "角色数据限制删除", apiversion = R.ID20230216181401.v1.class)
  382. public String roleAuthDataLimitDelete() throws YosException {
  383. long roleid = content.getLong("roleid");
  384. JSONArray sys_role_datalimitids = content.getJSONArray("sys_role_datalimitids");
  385. ArrayList<String> sqlist = new ArrayList<>();
  386. for (Object o : sys_role_datalimitids) {
  387. sqlist.add("delete from sys_role_datalimit where roleid=" + roleid + " and sys_role_datalimitid=" + o);
  388. logroleout(roleid);
  389. }
  390. dbConnect.runSqlUpdate(sqlist);
  391. return getSucReturnObject().toString();
  392. }
  393. @API(title = "角色数据限制添加修改", apiversion = R.ID20230216181501.v1.class)
  394. public String roleAuthDataLimitadd() throws YosException {
  395. long roleid = content.getLong("roleid");
  396. long sys_role_datalimitid = content.getLong("sys_role_datalimitid");
  397. String table_name = content.getString("table_name");
  398. String sqlstr = content.getString("sqlstr", true);
  399. if (dbConnect.runSqlQuery("select *from sys_role_datalimit where roleid='" + roleid + "' and table_name='" + table_name + "' and sys_role_datalimitid!=" + sys_role_datalimitid).isNotEmpty()) {
  400. return getErrReturnObject().setErrMsg("当前角色已存在" + table_name + "表的限制").toString();
  401. }
  402. if (sys_role_datalimitid <= 0 || dbConnect.runSqlQuery("select * from sys_role_datalimit where sys_role_datalimitid=" + sys_role_datalimitid).isEmpty()) {
  403. SQLFactory sqlFactory = new SQLFactory(this, "角色数据限制新增");
  404. sqlFactory.addParameter("sys_role_datalimitid", createTableID("sys_role_datalimit"));
  405. sqlFactory.addParameter("roleid", roleid);
  406. sqlFactory.addParameter("table_name", table_name);
  407. sqlFactory.addParameter("sqlstr", sqlstr);
  408. sqlFactory.addParameter("username", username);
  409. sqlFactory.addParameter("userid", userid);
  410. dbConnect.runSqlUpdate(sqlFactory.getSQL(false));
  411. } else {
  412. dbConnect.runSqlUpdate("update sys_role_datalimit set table_name='" + table_name + "',sqlstr='" + sqlstr + "',changeby='" + username + "',changedate=now(),changeuserid=" + userid + " where roleid=" + roleid + " and sys_role_datalimitid=" + sys_role_datalimitid);
  413. }
  414. logroleout(roleid);
  415. return getSucReturnObject().toString();
  416. }
  417. @API(title = "角色授权用户查询", apiversion = R.ID20230302135404.v1.class)
  418. public String queryRoles_Users() throws YosException {
  419. Long roleid = content.getLong("roleid");
  420. SQLFactory sqlFactory = new SQLFactory(this, "角色授权用户查询", pageSize, pageNumber, pageSorting);
  421. sqlFactory.addParameter("siteid", siteid);
  422. sqlFactory.addParameter("roleid", roleid);
  423. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  424. return getSucReturnObject().setData(rows).toString();
  425. }
  426. }