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