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