/modules/core/portal-security-pacl/src/main/java/com/liferay/portal/security/pacl/checker/PortalServiceChecker.java

http://github.com/liferay/liferay-portal · Java · 184 lines · 118 code · 49 blank · 17 comment · 19 complexity · 103e4ff49b6dbef7590dd3c7c3cddd0d MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.security.pacl.checker;
  15. import com.liferay.portal.kernel.security.pacl.permission.PortalServicePermission;
  16. import com.liferay.portal.kernel.util.SetUtil;
  17. import com.liferay.portal.kernel.util.StringBundler;
  18. import com.liferay.portal.kernel.util.StringPool;
  19. import com.liferay.portal.kernel.util.StringUtil;
  20. import com.liferay.portal.kernel.util.Validator;
  21. import com.liferay.portal.security.pacl.Reflection;
  22. import java.security.Permission;
  23. import java.util.Collections;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Properties;
  27. import java.util.Set;
  28. /**
  29. * @author Brian Wing Shun Chan
  30. * @author Raymond AugĂŠ
  31. */
  32. public class PortalServiceChecker extends BaseChecker {
  33. @Override
  34. public void afterPropertiesSet() {
  35. initServices();
  36. }
  37. @Override
  38. public AuthorizationProperty generateAuthorizationProperty(
  39. Object... arguments) {
  40. if ((arguments == null) || (arguments.length != 1) ||
  41. !(arguments[0] instanceof Permission)) {
  42. return null;
  43. }
  44. AuthorizationProperty authorizationProperty =
  45. new AuthorizationProperty();
  46. StringBundler sb = new StringBundler(4);
  47. sb.append("security-manager-services");
  48. sb.append(StringPool.OPEN_BRACKET);
  49. PortalServicePermission portalServicePermission =
  50. (PortalServicePermission)arguments[0];
  51. sb.append(portalServicePermission.getServletContextName());
  52. sb.append(StringPool.CLOSE_BRACKET);
  53. authorizationProperty.setKey(sb.toString());
  54. authorizationProperty.setValue(
  55. portalServicePermission.getClassName() + StringPool.POUND +
  56. portalServicePermission.getMethodName());
  57. return authorizationProperty;
  58. }
  59. @Override
  60. public boolean implies(Permission permission) {
  61. PortalServicePermission portalServicePermission =
  62. (PortalServicePermission)permission;
  63. String name = portalServicePermission.getShortName();
  64. if (name.equals(PORTAL_SERVICE_PERMISSION_SERVICE)) {
  65. if (!hasService(
  66. portalServicePermission.getServletContextName(),
  67. portalServicePermission.getClassName(),
  68. portalServicePermission.getMethodName(), permission)) {
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. protected Set<String> getServices(String servletContextName) {
  75. Set<String> services = null;
  76. if (servletContextName.equals("portal")) {
  77. services = _portalServices;
  78. }
  79. else {
  80. services = _pluginServices.get(servletContextName);
  81. if (services == null) {
  82. return Collections.emptySet();
  83. }
  84. }
  85. return services;
  86. }
  87. protected boolean hasService(
  88. String servletContextName, String className, String methodName,
  89. Permission permission) {
  90. int stackIndex = Reflection.getStackIndex(8, 7);
  91. Class<?> callerClass = Reflection.getCallerClass(stackIndex);
  92. if (isTrustedCaller(callerClass, permission)) {
  93. callerClass = Reflection.getCallerClass(stackIndex + 1);
  94. if (isTrustedCaller(callerClass, permission)) {
  95. return true;
  96. }
  97. }
  98. Set<String> services = getServices(servletContextName);
  99. if (services.contains(className)) {
  100. return true;
  101. }
  102. if (Validator.isNull(methodName)) {
  103. return false;
  104. }
  105. if (services.contains(
  106. className.concat(StringPool.POUND).concat(methodName))) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. protected void initServices() {
  112. Properties properties = getProperties();
  113. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  114. String key = (String)entry.getKey();
  115. String value = (String)entry.getValue();
  116. if (!key.startsWith("security-manager-services[")) {
  117. continue;
  118. }
  119. int x = key.indexOf("[");
  120. int y = key.indexOf("]", x);
  121. String servicesServletContextName = key.substring(x + 1, y);
  122. Set<String> services = SetUtil.fromArray(StringUtil.split(value));
  123. if (servicesServletContextName.equals(
  124. _PORTAL_SERVLET_CONTEXT_NAME)) {
  125. _portalServices = services;
  126. }
  127. else {
  128. _pluginServices.put(servicesServletContextName, services);
  129. }
  130. }
  131. }
  132. private static final String _PORTAL_SERVLET_CONTEXT_NAME = "portal";
  133. private final Map<String, Set<String>> _pluginServices = new HashMap<>();
  134. private Set<String> _portalServices = Collections.emptySet();
  135. }