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 SocialRequestPermissionImpl implements SocialRequestPermission {
029    
030            @Override
031            public void check(
032                            PermissionChecker permissionChecker, long requestId,
033                            String actionId)
034                    throws PortalException, SystemException {
035    
036                    if (!contains(permissionChecker, requestId, actionId)) {
037                            throw new PrincipalException();
038                    }
039            }
040    
041            @Override
042            public boolean contains(
043                            PermissionChecker permissionChecker, long requestId,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (permissionChecker.isOmniadmin()) {
048                            return true;
049                    }
050    
051                    if (actionId.equals(ActionKeys.UPDATE)) {
052                            SocialRequest request =
053                                    SocialRequestLocalServiceUtil.getSocialRequest(requestId);
054    
055                            if (permissionChecker.getUserId() == request.getReceiverUserId()) {
056                                    return true;
057                            }
058                    }
059    
060                    return false;
061            }
062    
063    }