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

http://github.com/liferay/liferay-portal · Java · 99 lines · 59 code · 24 blank · 16 comment · 9 complexity · 76ca0ce35b40f7ae5a35effb69073b8a 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.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.security.pacl.Reflection;
  18. import java.security.Permission;
  19. /**
  20. * @author Brian Wing Shun Chan
  21. */
  22. public class SecurityChecker extends BaseChecker {
  23. @Override
  24. public void afterPropertiesSet() {
  25. }
  26. @Override
  27. public boolean implies(Permission permission) {
  28. String name = permission.getName();
  29. if (name.equals(SECURITY_PERMISSION_GET_POLICY)) {
  30. if (!hasGetPolicy(permission)) {
  31. logSecurityException(_log, "Attempted to get the policy");
  32. return false;
  33. }
  34. }
  35. else if (name.equals(SECURITY_PERMISSION_SET_POLICY)) {
  36. if (!hasSetPolicy(permission)) {
  37. logSecurityException(_log, "Attempted to set the policy");
  38. return false;
  39. }
  40. }
  41. else {
  42. if (_log.isDebugEnabled()) {
  43. Thread.dumpStack();
  44. }
  45. logSecurityException(
  46. _log,
  47. "Attempted to " + permission.getName() + " on " +
  48. permission.getActions());
  49. return false;
  50. }
  51. return true;
  52. }
  53. protected boolean hasGetPolicy(Permission permission) {
  54. int stackIndex = Reflection.getStackIndex(
  55. new int[] {4, 4}, new int[] {4, 3});
  56. Class<?> callerClass = Reflection.getCallerClass(stackIndex);
  57. if (isTrustedCaller(callerClass, permission)) {
  58. return true;
  59. }
  60. logSecurityException(_log, "Attempted to get the policy");
  61. return false;
  62. }
  63. protected boolean hasSetPolicy(Permission permission) {
  64. int stackIndex = Reflection.getStackIndex(
  65. new int[] {4, 4}, new int[] {4, 3});
  66. Class<?> callerClass = Reflection.getCallerClass(stackIndex);
  67. if (isTrustedCaller(callerClass, permission)) {
  68. return true;
  69. }
  70. logSecurityException(_log, "Attempted to set the policy");
  71. return false;
  72. }
  73. private static final Log _log = LogFactoryUtil.getLog(
  74. SecurityChecker.class);
  75. }