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.calendar.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.PermissionChecker;
020    import com.liferay.portlet.calendar.model.CalEvent;
021    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class CalEventPermission {
027    
028            public static void check(
029                            PermissionChecker permissionChecker, CalEvent event,
030                            String actionId)
031                    throws PortalException {
032    
033                    if (!contains(permissionChecker, event, actionId)) {
034                            throw new PrincipalException();
035                    }
036            }
037    
038            public static void check(
039                            PermissionChecker permissionChecker, long eventId, String actionId)
040                    throws PortalException {
041    
042                    if (!contains(permissionChecker, eventId, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static boolean contains(
048                    PermissionChecker permissionChecker, CalEvent event, String actionId) {
049    
050                    if (permissionChecker.hasOwnerPermission(
051                                    event.getCompanyId(), CalEvent.class.getName(),
052                                    event.getEventId(), event.getUserId(), actionId)) {
053    
054                            return true;
055                    }
056    
057                    return permissionChecker.hasPermission(
058                            event.getGroupId(), CalEvent.class.getName(), event.getEventId(),
059                            actionId);
060            }
061    
062            public static boolean contains(
063                            PermissionChecker permissionChecker, long eventId, String actionId)
064                    throws PortalException {
065    
066                    CalEvent event = CalEventLocalServiceUtil.getEvent(eventId);
067    
068                    return contains(permissionChecker, event, actionId);
069            }
070    
071    }