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     * @author Michael C. Han
029     */
030    public class OmniadminImpl implements Omniadmin {
031    
032            @Override
033            public boolean isOmniadmin(long userId) {
034                    try {
035                            User user = UserLocalServiceUtil.fetchUser(userId);
036    
037                            if (user == null) {
038                                    return false;
039                            }
040    
041                            return isOmniadmin(user);
042                    }
043                    catch (SystemException se) {
044                            return false;
045                    }
046            }
047    
048            @Override
049            public boolean isOmniadmin(User user) {
050                    long userId = user.getUserId();
051    
052                    if (userId <= 0) {
053                            return false;
054                    }
055    
056                    try {
057                            if (PropsValues.OMNIADMIN_USERS.length > 0) {
058                                    for (int i = 0; i < PropsValues.OMNIADMIN_USERS.length; i++) {
059                                            if (PropsValues.OMNIADMIN_USERS[i] == userId) {
060                                                    if (user.getCompanyId() !=
061                                                                    PortalInstances.getDefaultCompanyId()) {
062    
063                                                            return false;
064                                                    }
065    
066                                                    return true;
067                                            }
068                                    }
069    
070                                    return false;
071                            }
072    
073                            if (user.getCompanyId() != PortalInstances.getDefaultCompanyId()) {
074                                    return false;
075                            }
076    
077                            return RoleLocalServiceUtil.hasUserRole(
078                                    userId, user.getCompanyId(), RoleConstants.ADMINISTRATOR, true);
079                    }
080                    catch (Exception e) {
081                            _log.error(e);
082    
083                            return false;
084                    }
085            }
086    
087            private static final Log _log = LogFactoryUtil.getLog(OmniadminImpl.class);
088    
089    }