001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.workflow;
016    
017    import com.liferay.portal.security.auth.PrincipalException;
018    import com.liferay.portal.security.permission.PermissionChecker;
019    import com.liferay.portal.security.permission.PermissionThreadLocal;
020    
021    import org.aspectj.lang.ProceedingJoinPoint;
022    import org.aspectj.lang.Signature;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class WorkflowPermissionAdvice {
028    
029            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
030                    throws Throwable {
031    
032                    Signature signature = proceedingJoinPoint.getSignature();
033    
034                    String methodName = signature.getName();
035    
036                    Object[] arguments = proceedingJoinPoint.getArgs();
037    
038                    if (methodName.equals(_ASSIGN_WORKFLOW_TASK_TO_USER_METHOD_NAME)) {
039                            long userId = (Long)arguments[1];
040    
041                            PermissionChecker permissionChecker =
042                                    PermissionThreadLocal.getPermissionChecker();
043    
044                            if (permissionChecker.getUserId() != userId) {
045                                    throw new PrincipalException();
046                            }
047                    }
048    
049                    return proceedingJoinPoint.proceed();
050            }
051    
052            private static final String _ASSIGN_WORKFLOW_TASK_TO_USER_METHOD_NAME =
053                    "assignWorkflowTaskToUser";
054    
055    }