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