/parkingos_cloud_web/src/com/zld/struts/auth/AuthRoleAction.java

https://github.com/ParkingOS/ParkingOS_cloud · Java · 104 lines · 87 code · 9 blank · 8 comment · 8 complexity · 6c2ce4a8ceb5c7b2a4e4d1fd966c41ad MD5 · raw file

  1. package com.zld.struts.auth;
  2. import com.zld.AjaxUtil;
  3. import com.zld.impl.MongoDbUtils;
  4. import com.zld.service.DataBaseService;
  5. import com.zld.utils.RequestUtil;
  6. import com.zld.utils.StringUtils;
  7. import org.apache.log4j.Logger;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * 权限管理---设置角色权限
  21. * @author Gecko
  22. *
  23. */
  24. public class AuthRoleAction extends Action {
  25. @Autowired
  26. private DataBaseService daService;
  27. @Autowired
  28. private MongoDbUtils mongoDbUtils;
  29. private Logger logger = Logger.getLogger(AuthRoleAction.class);
  30. @Override
  31. public ActionForward execute(ActionMapping mapping, ActionForm form,
  32. HttpServletRequest request, HttpServletResponse response)
  33. throws Exception {
  34. String action = RequestUtil.getString(request, "action");
  35. String target = null;
  36. if(action.equals("editauthrole")){
  37. Long roleId = RequestUtil.getLong(request, "roleid", -1L);
  38. Long oid = RequestUtil.getLong(request, "oid", -1L);
  39. //查询所权限
  40. List<Map<String, Object>> allAuthsList = daService.getAll("select id,pid,nname as name,sub_auth from auth_tb " +
  41. "where state =? and oid=? order by id ",new Object[]{0,oid});
  42. //查自己权限
  43. List<Map<String, Object>> ownAuthsList = daService.getAll("select auth_id,ar.sub_auth,pid from auth_role_tb ar" +
  44. " left join auth_tb at on ar.auth_id= at.id where role_id =? ",new Object[]{roleId});
  45. //查角色名称
  46. Map userRoleMap = daService.getMap("select role_name from user_role_tb where id=? ", new Object[]{roleId});
  47. String own = StringUtils.createJson(ownAuthsList);
  48. request.setAttribute("rolename",userRoleMap.get("role_name"));
  49. request.setAttribute("ownauths",own.replace("null", ""));
  50. request.setAttribute("allauths", StringUtils.createJson(allAuthsList));
  51. request.setAttribute("roleid", roleId);
  52. target = "editauthrole";
  53. }else if(action.equals("edit")){
  54. Long roleid = RequestUtil.getLong(request, "roleid", -1L);
  55. String auths = AjaxUtil.decodeUTF8(RequestUtil.getString(request, "auths"));
  56. String as[] = auths.split("\\|");
  57. Map<Long, String> aMap = new HashMap<Long, String>();
  58. for(int i=0;i<as.length;i++){
  59. String a = as[i];
  60. if(a.indexOf(",")!=-1){
  61. String subs[] = a.split(",");
  62. Long aid = Long.valueOf(subs[0]);
  63. if(aMap.containsKey(aid)){
  64. String v = aMap.get(aid);
  65. if(v.length()>0)
  66. v +=",";
  67. v +=subs[1];
  68. aMap.put(aid, v);
  69. }else {
  70. aMap.put(aid, subs[1]);
  71. }
  72. }else {
  73. aMap.put(Long.valueOf(a), "");
  74. }
  75. }
  76. int ret = daService.update("delete from auth_role_tb where role_id=?", new Object[]{roleid});
  77. logger.error("-----> authrole,roleid:"+roleid+":原权限删除,ret:"+ret);
  78. String sql = "insert into auth_role_tb (role_id,auth_id,sub_auth) values(?,?,?)";
  79. List<Object[]>lists = new ArrayList<Object[]>();
  80. for(Long key :aMap.keySet()){
  81. Object[] values = new Object[]{roleid,key,aMap.get(key)};
  82. lists.add(values);
  83. }
  84. ret = daService.bathInsert(sql, lists, new int []{4,4,12});
  85. logger.error("-----> authrole,roleid:"+roleid+":更新权限,ret:"+ret);
  86. if(ret>0){
  87. ret = 1;
  88. mongoDbUtils.saveLogs(request, 0, 3, "修改了角色权限:"+roleid);
  89. }
  90. AjaxUtil.ajaxOutput(response, ret+"");
  91. return null;
  92. }
  93. return mapping.findForward(target);
  94. }
  95. }