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.portlet.admin.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.RoleConstants;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.security.auth.CompanyThreadLocal;
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                    if (CompanyThreadLocal.getCompanyId() !=
045                                    PortalInstances.getDefaultCompanyId()) {
046    
047                            return false;
048                    }
049    
050                    if (userId <= 0) {
051                            return false;
052                    }
053    
054                    try {
055                            if (PropsValues.OMNIADMIN_USERS.length > 0) {
056                                    for (int i = 0; i < PropsValues.OMNIADMIN_USERS.length; i++) {
057                                            if (PropsValues.OMNIADMIN_USERS[i] == userId) {
058                                                    User user = UserLocalServiceUtil.getUserById(userId);
059    
060                                                    if (user.getCompanyId() !=
061                                                                    PortalInstances.getDefaultCompanyId()) {
062    
063                                                            return false;
064                                                    }
065    
066                                                    return true;
067                                            }
068                                    }
069    
070                                    return false;
071                            }
072                            else {
073                                    User user = UserLocalServiceUtil.getUserById(userId);
074    
075                                    if (user.getCompanyId() !=
076                                                    PortalInstances.getDefaultCompanyId()) {
077    
078                                            return false;
079                                    }
080    
081                                    return RoleLocalServiceUtil.hasUserRole(
082                                            userId, user.getCompanyId(), RoleConstants.ADMINISTRATOR,
083                                            true);
084                            }
085                    }
086                    catch (Exception e) {
087                            _log.error(e);
088    
089                            return false;
090                    }
091            }
092    
093            private static Log _log = LogFactoryUtil.getLog(OmniadminUtil.class);
094    
095    }