001    /**
002     * Copyright (c) 2000-2010 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.portal.security.permission;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Role;
020    import com.liferay.portal.model.RoleConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.RoleLocalServiceUtil;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    import com.liferay.portlet.admin.util.OmniadminUtil;
025    
026    import javax.portlet.PortletRequest;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public abstract class BasePermissionChecker implements PermissionChecker {
032    
033            public long getCompanyId() {
034                    return user.getCompanyId();
035            }
036    
037            public long getOwnerRoleId() {
038                    return ownerRole.getRoleId();
039            }
040    
041            public long[] getRoleIds(long userId, long groupId) {
042                    return PermissionChecker.DEFAULT_ROLE_IDS;
043            }
044    
045            public long getUserId() {
046                    return user.getUserId();
047            }
048    
049            public boolean hasOwnerPermission(
050                    long companyId, String name, long primKey, long ownerId,
051                    String actionId) {
052    
053                    return hasOwnerPermission(
054                            companyId, name, String.valueOf(primKey), ownerId, actionId);
055            }
056    
057            public boolean hasPermission(
058                    long groupId, String name, long primKey, String actionId) {
059    
060                    return hasPermission(groupId, name, String.valueOf(primKey), actionId);
061            }
062    
063            public void init(User user, boolean checkGuest) {
064                    this.user = user;
065    
066                    if (user.isDefaultUser()) {
067                            this.defaultUserId = user.getUserId();
068                            this.signedIn = false;
069                    }
070                    else {
071                            try {
072                                    this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
073                                            user.getCompanyId());
074                            }
075                            catch (Exception e) {
076                                    _log.error(e, e);
077                            }
078    
079                            this.signedIn = true;
080                    }
081    
082                    this.checkGuest = checkGuest;
083    
084                    try {
085                            this.ownerRole = RoleLocalServiceUtil.getRole(
086                                    user.getCompanyId(), RoleConstants.OWNER);
087                    }
088                    catch (Exception e) {
089                            _log.error(e, e);
090                    }
091            }
092    
093            public boolean isOmniadmin() {
094                    if (omniadmin == null) {
095                            omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
096                    }
097    
098                    return omniadmin.booleanValue();
099            }
100    
101            public void resetValues() {
102            }
103    
104            public void setCheckGuest(boolean checkGuest) {
105                    this.checkGuest = checkGuest;
106            }
107    
108            public void setValues(PortletRequest portletRequest) {
109    
110                    // This method is called in com.liferay.portlet.StrutsPortlet to allow
111                    // developers to hook in additional parameters from the portlet request.
112                    // Don't overwrite this method unless you're using Liferay in a 2 tier
113                    // environment and don't expect to make remote calls. Remote calls to
114                    // service beans will not have any values set from the portlet request.
115    
116            }
117    
118            protected User user;
119            protected long defaultUserId;
120            protected boolean signedIn;
121            protected boolean checkGuest;
122            protected Boolean omniadmin;
123            protected Role ownerRole;
124    
125            private static Log _log = LogFactoryUtil.getLog(
126                    BasePermissionChecker.class);
127    
128    }