001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.access.control;
016    
017    import com.liferay.portal.kernel.security.access.control.AccessControl;
018    import com.liferay.portal.kernel.security.access.control.AccessControlUtil;
019    import com.liferay.portal.kernel.security.access.control.AccessControlled;
020    import com.liferay.portal.security.auth.AccessControlContext;
021    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
022    
023    import java.util.Map;
024    
025    import org.aopalliance.intercept.MethodInvocation;
026    
027    /**
028     * @author Tomas Polesovsky
029     * @author Igor Spasic
030     * @author Michael C. Han
031     * @author Raymond Aug??
032     * @author Shuyang Zhou
033     */
034    public class AccessControlAdvice
035            extends AnnotationChainableMethodAdvice<AccessControlled> {
036    
037            @Override
038            public Object before(MethodInvocation methodInvocation) throws Throwable {
039                    incrementServiceDepth();
040    
041                    AccessControlled accessControlled = findAnnotation(methodInvocation);
042    
043                    if (accessControlled == AccessControl.NULL_ACCESS_CONTROLLED) {
044                            return null;
045                    }
046    
047                    _accessControlAdvisor.accept(methodInvocation, accessControlled);
048    
049                    return null;
050            }
051    
052            @Override
053            public void duringFinally(MethodInvocation methodInvocation) {
054                    decrementServiceDepth();
055            }
056    
057            @Override
058            public AccessControlled getNullAnnotation() {
059                    return AccessControl.NULL_ACCESS_CONTROLLED;
060            }
061    
062            public void setAccessControlAdvisor(
063                    AccessControlAdvisor accessControlAdvisor) {
064    
065                    _accessControlAdvisor = accessControlAdvisor;
066            }
067    
068            protected void decrementServiceDepth() {
069                    AccessControlContext accessControlContext =
070                            AccessControlUtil.getAccessControlContext();
071    
072                    if (accessControlContext == null) {
073                            return;
074                    }
075    
076                    Map<String, Object> settings = accessControlContext.getSettings();
077    
078                    Integer serviceDepth = (Integer)settings.get(
079                            AccessControlContext.Settings.SERVICE_DEPTH.toString());
080    
081                    if (serviceDepth == null) {
082                            return;
083                    }
084    
085                    serviceDepth--;
086    
087                    settings.put(
088                            AccessControlContext.Settings.SERVICE_DEPTH.toString(),
089                            serviceDepth);
090            }
091    
092            protected void incrementServiceDepth() {
093                    AccessControlContext accessControlContext =
094                            AccessControlUtil.getAccessControlContext();
095    
096                    if (accessControlContext == null) {
097                            return;
098                    }
099    
100                    Map<String, Object> settings = accessControlContext.getSettings();
101    
102                    Integer serviceDepth = (Integer)settings.get(
103                            AccessControlContext.Settings.SERVICE_DEPTH.toString());
104    
105                    if (serviceDepth == null) {
106                            serviceDepth = Integer.valueOf(1);
107                    }
108                    else {
109                            serviceDepth++;
110                    }
111    
112                    settings.put(
113                            AccessControlContext.Settings.SERVICE_DEPTH.toString(),
114                            serviceDepth);
115            }
116    
117            private AccessControlAdvisor _accessControlAdvisor;
118    
119    }