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