001    /**
002     * Copyright (c) 2000-2010 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     * @author Brian Wing Shun Chan
029     */
030    public class OmniadminUtil {
031    
032            public static boolean isOmniadmin(long userId) {
033                    if (CompanyThreadLocal.getCompanyId() !=
034                                    PortalInstances.getDefaultCompanyId()) {
035    
036                            return false;
037                    }
038    
039                    if (userId <= 0) {
040                            return false;
041                    }
042    
043                    try {
044                            if (PropsValues.OMNIADMIN_USERS.length > 0) {
045                                    for (int i = 0; i < PropsValues.OMNIADMIN_USERS.length; i++) {
046                                            if (PropsValues.OMNIADMIN_USERS[i] == userId) {
047                                                    User user = UserLocalServiceUtil.getUserById(userId);
048    
049                                                    if (user.getCompanyId() !=
050                                                                    PortalInstances.getDefaultCompanyId()) {
051    
052                                                            return false;
053                                                    }
054    
055                                                    return true;
056                                            }
057                                    }
058    
059                                    return false;
060                            }
061                            else {
062                                    User user = UserLocalServiceUtil.getUserById(userId);
063    
064                                    if (user.getCompanyId() !=
065                                                    PortalInstances.getDefaultCompanyId()) {
066    
067                                            return false;
068                                    }
069    
070                                    return RoleLocalServiceUtil.hasUserRole(
071                                            userId, user.getCompanyId(), RoleConstants.ADMINISTRATOR,
072                                            true);
073                            }
074                    }
075                    catch (Exception e) {
076                            _log.error(e);
077    
078                            return false;
079                    }
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(OmniadminUtil.class);
083    
084    }