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.portlet.admin.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
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.PortalInstances;
025    import com.liferay.portal.util.PropsValues;
026    
027    /**
028     * Provides utility methods for determining if a user is a universal
029     * administrator. Universal administrators have administrator permissions in
030     * every company.
031     *
032     * <p>
033     * A user can be made a universal administrator by adding their primary key to
034     * the list in <code>portal.properties</code> under the key
035     * <code>omniadmin.users</key>. If this property is left blank, administrators
036     * of the default company will automatically be universal administrators.
037     * </p>
038     *
039     * @author Brian Wing Shun Chan
040     */
041    public class OmniadminUtil {
042    
043            public static boolean isOmniadmin(long userId) {
044                    try {
045                            User user = UserLocalServiceUtil.fetchUser(userId);
046    
047                            if (user == null) {
048                                    return false;
049                            }
050    
051                            return isOmniadmin(user);
052                    }
053                    catch (SystemException se) {
054                            return false;
055                    }
056            }
057    
058            public static boolean isOmniadmin(User user) {
059                    long userId = user.getUserId();
060    
061                    if (userId <= 0) {
062                            return false;
063                    }
064    
065                    try {
066                            if (PropsValues.OMNIADMIN_USERS.length > 0) {
067                                    for (int i = 0; i < PropsValues.OMNIADMIN_USERS.length; i++) {
068                                            if (PropsValues.OMNIADMIN_USERS[i] == userId) {
069                                                    if (user.getCompanyId() !=
070                                                                    PortalInstances.getDefaultCompanyId()) {
071    
072                                                            return false;
073                                                    }
074    
075                                                    return true;
076                                            }
077                                    }
078    
079                                    return false;
080                            }
081    
082                            if (user.getCompanyId() != PortalInstances.getDefaultCompanyId()) {
083                                    return false;
084                            }
085    
086                            return RoleLocalServiceUtil.hasUserRole(
087                                    userId, user.getCompanyId(), RoleConstants.ADMINISTRATOR, true);
088                    }
089                    catch (Exception e) {
090                            _log.error(e);
091    
092                            return false;
093                    }
094            }
095    
096            private static Log _log = LogFactoryUtil.getLog(OmniadminUtil.class);
097    
098    }