/portal-pacl/src/com/liferay/portal/security/pacl/PortalPermissionCollection.java

https://github.com/edgonzales/liferay-portal · Java · 97 lines · 58 code · 23 blank · 16 comment · 5 complexity · f1aa740587416de169e8ba64869367eb MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2013 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;
  15. import com.liferay.portal.kernel.util.StringPool;
  16. import java.security.Permission;
  17. import java.security.PermissionCollection;
  18. import java.security.Policy;
  19. import java.security.ProtectionDomain;
  20. import java.util.Collections;
  21. import java.util.Enumeration;
  22. /**
  23. * @author Raymond Augé
  24. */
  25. public class PortalPermissionCollection extends PermissionCollection {
  26. public PortalPermissionCollection(
  27. PACLPolicy paclPolicy, PermissionCollection permissionCollection) {
  28. _paclPolicy = paclPolicy;
  29. _permissionCollection = permissionCollection;
  30. }
  31. @Override
  32. public void add(Permission permission) {
  33. throw new SecurityException();
  34. }
  35. @Override
  36. public Enumeration<Permission> elements() {
  37. return Collections.enumeration(Collections.<Permission>emptyList());
  38. }
  39. public ClassLoader getClassLoader() {
  40. return _paclPolicy.getClassLoader();
  41. }
  42. public PACLPolicy getPACLPolicy() {
  43. return _paclPolicy;
  44. }
  45. public Policy getPolicy() {
  46. return _paclPolicy.getPolicy();
  47. }
  48. @Override
  49. public boolean implies(Permission permission) {
  50. if (Reflection.getCallerClass(1) == ProtectionDomain.class) {
  51. return false;
  52. }
  53. if (!_paclPolicy.isActive()) {
  54. return true;
  55. }
  56. if (permission instanceof PACLUtil.Permission) {
  57. throw new PACLUtil.Exception(_paclPolicy);
  58. }
  59. if (_permissionCollection.implies(permission) ||
  60. _paclPolicy.implies(permission)) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. @Override
  66. public String toString() {
  67. Class<?> clazz = getClass();
  68. String className = clazz.getSimpleName();
  69. return className.concat(StringPool.POUND).concat(
  70. _paclPolicy.toString());
  71. }
  72. private PACLPolicy _paclPolicy;
  73. private PermissionCollection _permissionCollection;
  74. }