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