/connect-web/src/main/java/org/osforce/connect/web/security/tag/freemarker/SecurityTag.java

http://focus-sns.googlecode.com/ · Java · 69 lines · 52 code · 9 blank · 8 comment · 7 complexity · c2a68f3d2c1a931e43a81a808a29061d MD5 · raw file

  1. package org.osforce.connect.web.security.tag.freemarker;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.osforce.connect.entity.system.Project;
  6. import org.osforce.connect.entity.system.User;
  7. import org.osforce.connect.service.system.PermissionService;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.util.Assert;
  10. import freemarker.core.Environment;
  11. import freemarker.ext.beans.BeanModel;
  12. import freemarker.template.TemplateDirectiveBody;
  13. import freemarker.template.TemplateDirectiveModel;
  14. import freemarker.template.TemplateException;
  15. import freemarker.template.TemplateModel;
  16. /**
  17. *
  18. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  19. * @since 1.1.0
  20. * @create May 20, 2011 - 10:29:34 AM
  21. * <a href="http://www.opensourceforce.org">????</a>
  22. */
  23. public class SecurityTag implements TemplateDirectiveModel {
  24. private static final String PROJECT_KEY = "_" + Project.class.getName();
  25. private static final String USER_KEY = "_" + User.class.getName();
  26. private PermissionService permissionService;
  27. public SecurityTag() {
  28. }
  29. @Autowired
  30. public void setPermissionService(PermissionService permissionService) {
  31. this.permissionService = permissionService;
  32. }
  33. @SuppressWarnings("unchecked")
  34. public void execute(Environment env, Map params, TemplateModel[] loopVars,
  35. TemplateDirectiveBody body) throws TemplateException, IOException {
  36. String code = params.get("code").toString();
  37. Assert.notNull(code, "Parameter code can not be null");
  38. String[] resources = StringUtils.split(code, "|");
  39. BeanModel projectModel = (BeanModel) env.getDataModel().get(PROJECT_KEY);
  40. BeanModel userModel = (BeanModel) env.getDataModel().get(USER_KEY);
  41. Project project = (Project) projectModel.getWrappedObject();
  42. User user = userModel!=null ? (User) userModel.getWrappedObject() : null;
  43. //
  44. if(params.containsKey("userRequired")) {
  45. String userRequired = params.get("userRequired").toString();
  46. if(StringUtils.equals("true", userRequired) && user==null) {
  47. return ;
  48. }
  49. }
  50. if(params.containsKey("projectRequired")) {
  51. String projectRequired = params.get("projectRequired").toString();
  52. if(StringUtils.equals("true", projectRequired) && project==null) {
  53. return ;
  54. }
  55. }
  56. if(permissionService.hasPermission(project, user, resources)) {
  57. body.render(env.getOut());
  58. }
  59. }
  60. }