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