PageRenderTime 9467ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/jieshuhuiyou/interceptor/PermissionInterceptor.java

https://bitbucket.org/psjay/ants-bookbase
Java | 206 lines | 153 code | 37 blank | 16 comment | 37 complexity | 40889244c349e6355e5dfd1bae4a9357 MD5 | raw file
  1. package com.jieshuhuiyou.interceptor;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.jieshuhuiyou.Config;
  10. import com.jieshuhuiyou.entity.User;
  11. import com.jieshuhuiyou.service.permission.PermissionValidatableAction;
  12. import com.jieshuhuiyou.service.permission.Role;
  13. import com.jieshuhuiyou.service.permission.RoleParser;
  14. import com.jieshuhuiyou.service.permission.Validator;
  15. import com.jieshuhuiyou.util.UserUtil;
  16. import com.opensymphony.xwork2.Action;
  17. import com.opensymphony.xwork2.ActionInvocation;
  18. import com.opensymphony.xwork2.ActionSupport;
  19. import com.opensymphony.xwork2.ModelDriven;
  20. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  21. /**
  22. * ???????
  23. * @author psjay
  24. *
  25. */
  26. public class PermissionInterceptor extends AbstractInterceptor {
  27. private static final long serialVersionUID = -3136125501446582857L;
  28. private Set<Role> allowRoles = new HashSet<Role>();
  29. private Set<Role> disallowRoles = new HashSet<Role>();
  30. private Map<Role, MessageAndResult> messageAndResult;
  31. private String user;
  32. private String subject;
  33. @Override
  34. public String intercept(ActionInvocation ai) throws Exception {
  35. Action action = (Action) ai.getAction();
  36. User validatedUser = null;
  37. Object validatedSubject = null;
  38. Set<Role> roles = null;
  39. // get validated user and subject
  40. if(user != null && !user.equals("")) {
  41. validatedUser = (User) ai.getStack().findValue(user, User.class);
  42. }
  43. if(subject != null && !subject.equals("")) {
  44. validatedSubject = ai.getStack().findValue(subject);
  45. }
  46. // get current user by default
  47. if(validatedUser == null) {
  48. validatedUser = UserUtil.getCurrentUser();
  49. }
  50. // get model by default if action is model driven
  51. if(validatedSubject == null && ai.getAction() instanceof ModelDriven<?>) {
  52. validatedSubject = ((ModelDriven<?>)ai.getAction()).getModel();
  53. }
  54. if(action instanceof PermissionValidatableAction) {
  55. PermissionValidatableAction pva = (PermissionValidatableAction)action;
  56. // check the validation is skipped or not
  57. if(pva.isSkipValidation()) {
  58. return ai.invoke();
  59. }
  60. // set validated user and subject
  61. pva.populateValidatedUser(validatedUser);
  62. pva.populateValidatedSubject(validatedSubject);
  63. roles = pva.getRoles();
  64. } else {
  65. roles = Validator.fetchRoles(validatedUser, validatedSubject);
  66. }
  67. boolean pass = Validator.validate(roles, allowRoles, disallowRoles);
  68. if(pass) {
  69. return ai.invoke();
  70. } else {
  71. if(messageAndResult == null || messageAndResult.isEmpty()) {
  72. return Config.PERMISSION_FORBIDDEN;
  73. }
  74. for(Role r : roles) {
  75. for(Role dr : disallowRoles) {
  76. if(r.equals(dr)) {
  77. MessageAndResult mar = messageAndResult.get(r);
  78. if(mar != null) {
  79. String msg = mar.getMessage();
  80. String result = mar.getResult();
  81. if(msg != null) {
  82. if(ai.getAction() instanceof ActionSupport) {
  83. ActionSupport as = (ActionSupport) ai.getAction();
  84. as.addActionError(msg); // add action error.
  85. }
  86. }
  87. if(result != null) {
  88. return result;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return Config.PERMISSION_FORBIDDEN; // forbidden as the default result
  95. }
  96. }
  97. // setters and getters
  98. public Set<Role> getAllowRoles() {
  99. return allowRoles;
  100. }
  101. public void setAllowRoles(String allowRolesStr) {
  102. String[] temp = allowRolesStr.split(",");
  103. for(String s: temp) {
  104. this.allowRoles.add(RoleParser.parseString(s.trim()));
  105. }
  106. }
  107. public Set<Role> getDisallowRoles() {
  108. return disallowRoles;
  109. }
  110. public void setDisallowRoles(String disallowRolesStr) {
  111. String[] temp = disallowRolesStr.split(",");
  112. if(messageAndResult == null) {
  113. messageAndResult = new HashMap<Role, MessageAndResult>();
  114. }
  115. for(String s: temp) {
  116. this.disallowRoles.add(RoleParser.parseString(s.trim()));
  117. }
  118. }
  119. public Map<Role, MessageAndResult> getMessageAndResult() {
  120. return messageAndResult;
  121. }
  122. /**
  123. * Convert JSON string to messageAndResult map
  124. * @param messageAndResultStr is something like : [{role: "Book.Provider", msg: "you've shared this book", result: "input"}]
  125. */
  126. public void setMessageAndResult(String messageAndResultStr) {
  127. JSONArray array = JSONArray.parseArray(messageAndResultStr);
  128. Iterator<?> i = array.iterator();
  129. JSONObject obj = null;
  130. while(i.hasNext()) {
  131. obj = (JSONObject) i.next();
  132. this.messageAndResult.put(RoleParser.parseString(obj.getString("role"))
  133. , new MessageAndResult(obj.getString("msg"), obj.getString("result")));
  134. }
  135. }
  136. private static class MessageAndResult {
  137. private String message;
  138. private String result;
  139. public MessageAndResult(String msg, String result) {
  140. this.message = msg;
  141. this.result = result;
  142. }
  143. // setters and getters
  144. public String getMessage() {
  145. return message;
  146. }
  147. public String getResult() {
  148. return result;
  149. }
  150. }
  151. public String getUser() {
  152. return user;
  153. }
  154. public void setUser(String user) {
  155. this.user = user;
  156. if(this.user != null) {
  157. this.user = this.user.trim();
  158. }
  159. }
  160. public String getSubject() {
  161. return subject;
  162. }
  163. public void setSubject(String subject) {
  164. this.subject = subject;
  165. if(this.subject != null) {
  166. this.subject = this.subject.trim();
  167. }
  168. }
  169. }