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.portlet.social.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portlet.social.model.SocialRequest;
023    import com.liferay.portlet.social.service.SocialRequestLocalServiceUtil;
024    
025    /**
026     * @author Shinn Lok
027     */
028    public class SocialRequestPermission {
029    
030            public static void check(
031                            PermissionChecker permissionChecker, long requestId,
032                            String actionId)
033                    throws PortalException, SystemException {
034    
035                    if (!contains(permissionChecker, requestId, actionId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public static boolean contains(
041                            PermissionChecker permissionChecker, long requestId,
042                            String actionId)
043                    throws PortalException, SystemException {
044    
045                    if (permissionChecker.isOmniadmin()) {
046                            return true;
047                    }
048    
049                    if (actionId.equals(ActionKeys.UPDATE)) {
050                            SocialRequest request =
051                                    SocialRequestLocalServiceUtil.getSocialRequest(requestId);
052    
053                            if (permissionChecker.getUserId() == request.getReceiverUserId()) {
054                                    return true;
055                            }
056                    }
057    
058                    return false;
059            }
060    
061    }