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.portal.security.jaas;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.model.Company;
019    import com.liferay.portal.kernel.model.CompanyConstants;
020    import com.liferay.portal.kernel.model.User;
021    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
022    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.util.PropsValues;
025    
026    /**
027     * @author Raymond Aug??
028     */
029    public class JAASHelper {
030    
031            public static JAASHelper getInstance() {
032                    return _instance;
033            }
034    
035            public static long getJaasUserId(long companyId, String name)
036                    throws PortalException {
037    
038                    return _instance.doGetJaasUserId(companyId, name);
039            }
040    
041            public static void setInstance(JAASHelper instance) {
042                    _instance = instance;
043            }
044    
045            protected long doGetJaasUserId(long companyId, String name)
046                    throws PortalException {
047    
048                    String jaasAuthType = PropsValues.PORTAL_JAAS_AUTH_TYPE;
049    
050                    if (jaasAuthType.equals("login")) {
051                            Company company = CompanyLocalServiceUtil.getCompany(companyId);
052    
053                            String authType = company.getAuthType();
054    
055                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
056                                    jaasAuthType = "emailAddress";
057                            }
058                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
059                                    jaasAuthType = "screenName";
060                            }
061                            else {
062                                    jaasAuthType = "userId";
063                            }
064                    }
065    
066                    long userId = 0;
067    
068                    if (jaasAuthType.equals("emailAddress")) {
069                            User user = UserLocalServiceUtil.fetchUserByEmailAddress(
070                                    companyId, name);
071    
072                            if (user != null) {
073                                    userId = user.getUserId();
074                            }
075                    }
076                    else if (jaasAuthType.equals("screenName")) {
077                            User user = UserLocalServiceUtil.fetchUserByScreenName(
078                                    companyId, name);
079    
080                            if (user != null) {
081                                    userId = user.getUserId();
082                            }
083                    }
084                    else {
085                            userId = GetterUtil.getLong(name);
086                    }
087    
088                    return userId;
089            }
090    
091            private static JAASHelper _instance = new JAASHelper();
092    
093    }