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