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 User getUser() {
072                    return user;
073            }
074    
075            public long getUserId() {
076                    return user.getUserId();
077            }
078    
079            public boolean hasOwnerPermission(
080                    long companyId, String name, long primKey, long ownerId,
081                    String actionId) {
082    
083                    return hasOwnerPermission(
084                            companyId, name, String.valueOf(primKey), ownerId, actionId);
085            }
086    
087            public boolean hasPermission(
088                    long groupId, String name, long primKey, String actionId) {
089    
090                    return hasPermission(groupId, name, String.valueOf(primKey), actionId);
091            }
092    
093            public void init(User user) {
094                    this.user = user;
095    
096                    if (user.isDefaultUser()) {
097                            this.defaultUserId = user.getUserId();
098                            this.signedIn = false;
099                    }
100                    else {
101                            try {
102                                    this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
103                                            user.getCompanyId());
104                            }
105                            catch (Exception e) {
106                                    _log.error(e, e);
107                            }
108    
109                            this.signedIn = true;
110                    }
111    
112                    try {
113                            this.ownerRole = RoleLocalServiceUtil.getRole(
114                                    user.getCompanyId(), RoleConstants.OWNER);
115                    }
116                    catch (Exception e) {
117                            _log.error(e, e);
118                    }
119            }
120    
121            public boolean isCheckGuest() {
122                    return checkGuest;
123            }
124    
125            /**
126             * @deprecated As of 6.1, renamed to {@link #isGroupAdmin(long)}
127             */
128            public boolean isCommunityAdmin(long groupId) {
129                    return isGroupAdmin(groupId);
130            }
131    
132            /**
133             * @deprecated As of 6.1, renamed to {@link #isGroupOwner(long)}
134             */
135            public boolean isCommunityOwner(long groupId) {
136                    return isGroupOwner(groupId);
137            }
138    
139            public boolean isOmniadmin() {
140                    if (omniadmin == null) {
141                            omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUser()));
142                    }
143    
144                    return omniadmin.booleanValue();
145            }
146    
147            public boolean isSignedIn() {
148                    return signedIn;
149            }
150    
151            public void resetValues() {
152            }
153    
154            public void setValues(PortletRequest portletRequest) {
155            }
156    
157            protected boolean checkGuest = PropsValues.PERMISSIONS_CHECK_GUEST_ENABLED;
158            protected long defaultUserId;
159            protected Boolean omniadmin;
160            protected Role ownerRole;
161            protected boolean signedIn;
162            protected User user;
163    
164            private static Log _log = LogFactoryUtil.getLog(
165                    BasePermissionChecker.class);
166    
167    }