001    /**
002     * Copyright (c) 2000-2013 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.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    }