001    /**
002     * Copyright (c) 2000-2012 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.portal.util.PropsValues;
025    import com.liferay.portlet.admin.util.OmniadminUtil;
026    
027    import java.util.Collections;
028    import java.util.List;
029    
030    import javax.portlet.PortletRequest;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public abstract class BasePermissionChecker implements PermissionChecker {
036    
037            @Override
038            public abstract PermissionChecker clone();
039    
040            public long getCompanyId() {
041                    return user.getCompanyId();
042            }
043    
044            public List<Long> getGuestResourceBlockIds(
045                    long companyId, long groupId, String name, String actionId) {
046    
047                    return Collections.emptyList();
048            }
049    
050            public List<Long> getOwnerResourceBlockIds(
051                    long companyId, long groupId, String name, String actionId) {
052    
053                    return Collections.emptyList();
054            }
055    
056            public long getOwnerRoleId() {
057                    return ownerRole.getRoleId();
058            }
059    
060            public List<Long> getResourceBlockIds(
061                    long companyId, long groupId, long userId, String name,
062                    String actionId) {
063    
064                    return Collections.emptyList();
065            }
066    
067            public long[] getRoleIds(long userId, long groupId) {
068                    return PermissionChecker.DEFAULT_ROLE_IDS;
069            }
070    
071            public long getUserId() {
072                    return user.getUserId();
073            }
074    
075            public boolean hasOwnerPermission(
076                    long companyId, String name, long primKey, long ownerId,
077                    String actionId) {
078    
079                    return hasOwnerPermission(
080                            companyId, name, String.valueOf(primKey), ownerId, actionId);
081            }
082    
083            public boolean hasPermission(
084                    long groupId, String name, long primKey, String actionId) {
085    
086                    return hasPermission(groupId, name, String.valueOf(primKey), actionId);
087            }
088    
089            public void init(User user) {
090                    this.user = user;
091    
092                    if (user.isDefaultUser()) {
093                            this.defaultUserId = user.getUserId();
094                            this.signedIn = false;
095                    }
096                    else {
097                            try {
098                                    this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
099                                            user.getCompanyId());
100                            }
101                            catch (Exception e) {
102                                    _log.error(e, e);
103                            }
104    
105                            this.signedIn = true;
106                    }
107    
108                    try {
109                            this.ownerRole = RoleLocalServiceUtil.getRole(
110                                    user.getCompanyId(), RoleConstants.OWNER);
111                    }
112                    catch (Exception e) {
113                            _log.error(e, e);
114                    }
115            }
116    
117            public boolean isCheckGuest() {
118                    return checkGuest;
119            }
120    
121            /**
122             * @deprecated As of 6.1, renamed to {@link #isGroupAdmin(long)}
123             */
124            public boolean isCommunityAdmin(long groupId) {
125                    return isGroupAdmin(groupId);
126            }
127    
128            /**
129             * @deprecated As of 6.1, renamed to {@link #isGroupOwner(long)}
130             */
131            public boolean isCommunityOwner(long groupId) {
132                    return isGroupOwner(groupId);
133            }
134    
135            public boolean isOmniadmin() {
136                    if (omniadmin == null) {
137                            omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUserId()));
138                    }
139    
140                    return omniadmin.booleanValue();
141            }
142    
143            public boolean isSignedIn() {
144                    return signedIn;
145            }
146    
147            public void resetValues() {
148            }
149    
150            public void setValues(PortletRequest portletRequest) {
151            }
152    
153            protected boolean checkGuest = PropsValues.PERMISSIONS_CHECK_GUEST_ENABLED;
154            protected long defaultUserId;
155            protected Boolean omniadmin;
156            protected Role ownerRole;
157            protected boolean signedIn;
158            protected User user;
159    
160            private static Log _log = LogFactoryUtil.getLog(
161                    BasePermissionChecker.class);
162    
163    }