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.MustHavePermission(
037                                    permissionChecker, SocialRequest.class.getName(), requestId,
038                                    actionId);
039                    }
040            }
041    
042            @Override
043            public boolean contains(
044                            PermissionChecker permissionChecker, long requestId,
045                            String actionId)
046                    throws PortalException {
047    
048                    if (permissionChecker.isOmniadmin()) {
049                            return true;
050                    }
051    
052                    if (actionId.equals(ActionKeys.UPDATE)) {
053                            SocialRequest request =
054                                    SocialRequestLocalServiceUtil.getSocialRequest(requestId);
055    
056                            if (permissionChecker.getUserId() == request.getReceiverUserId()) {
057                                    return true;
058                            }
059                    }
060    
061                    return false;
062            }
063    
064    }