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    /**
032     * @author Brian Wing Shun Chan
033     */
034    public abstract class BasePermissionChecker implements PermissionChecker {
035    
036            @Override
037            public abstract PermissionChecker clone();
038    
039            @Override
040            public long getCompanyId() {
041                    return user.getCompanyId();
042            }
043    
044            @Override
045            public List<Long> getOwnerResourceBlockIds(
046                    long companyId, long groupId, String name, String actionId) {
047    
048                    return Collections.emptyList();
049            }
050    
051            @Override
052            public long getOwnerRoleId() {
053                    return ownerRole.getRoleId();
054            }
055    
056            @Override
057            public List<Long> getResourceBlockIds(
058                    long companyId, long groupId, long userId, String name,
059                    String actionId) {
060    
061                    return Collections.emptyList();
062            }
063    
064            @Override
065            public long[] getRoleIds(long userId, long groupId) {
066                    return PermissionChecker.DEFAULT_ROLE_IDS;
067            }
068    
069            @Override
070            public User getUser() {
071                    return user;
072            }
073    
074            @Override
075            public long getUserId() {
076                    return user.getUserId();
077            }
078    
079            @Override
080            public boolean hasOwnerPermission(
081                    long companyId, String name, long primKey, long ownerId,
082                    String actionId) {
083    
084                    return hasOwnerPermission(
085                            companyId, name, String.valueOf(primKey), ownerId, actionId);
086            }
087    
088            @Override
089            public boolean hasPermission(
090                    long groupId, String name, long primKey, String actionId) {
091    
092                    return hasPermission(groupId, name, String.valueOf(primKey), actionId);
093            }
094    
095            @Override
096            public void init(User user) {
097                    this.user = user;
098    
099                    if (user.isDefaultUser()) {
100                            this.defaultUserId = user.getUserId();
101                            this.signedIn = false;
102                    }
103                    else {
104                            try {
105                                    this.defaultUserId = UserLocalServiceUtil.getDefaultUserId(
106                                            user.getCompanyId());
107                            }
108                            catch (Exception e) {
109                                    _log.error(e, e);
110                            }
111    
112                            this.signedIn = true;
113                    }
114    
115                    try {
116                            this.ownerRole = RoleLocalServiceUtil.getRole(
117                                    user.getCompanyId(), RoleConstants.OWNER);
118                    }
119                    catch (Exception e) {
120                            _log.error(e, e);
121                    }
122            }
123    
124            @Override
125            public boolean isCheckGuest() {
126                    return checkGuest;
127            }
128    
129            @Override
130            public boolean isOmniadmin() {
131                    if (omniadmin == null) {
132                            omniadmin = Boolean.valueOf(OmniadminUtil.isOmniadmin(getUser()));
133                    }
134    
135                    return omniadmin.booleanValue();
136            }
137    
138            @Override
139            public boolean isSignedIn() {
140                    return signedIn;
141            }
142    
143            protected boolean checkGuest = PropsValues.PERMISSIONS_CHECK_GUEST_ENABLED;
144            protected long defaultUserId;
145            protected Boolean omniadmin;
146            protected Role ownerRole;
147            protected boolean signedIn;
148            protected User user;
149    
150            private static final Log _log = LogFactoryUtil.getLog(
151                    BasePermissionChecker.class);
152    
153    }