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.util.GetterUtil;
019    import com.liferay.portal.model.Company;
020    import com.liferay.portal.model.CompanyConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.CompanyLocalServiceUtil;
023    import com.liferay.portal.service.UserLocalServiceUtil;
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                            String authType = company.getAuthType();
053    
054                            if (authType.equals(CompanyConstants.AUTH_TYPE_EA)) {
055                                    jaasAuthType = "emailAddress";
056                            }
057                            else if (authType.equals(CompanyConstants.AUTH_TYPE_SN)) {
058                                    jaasAuthType = "screenName";
059                            }
060                            else {
061                                    jaasAuthType = "userId";
062                            }
063                    }
064    
065                    long userId = 0;
066    
067                    if (jaasAuthType.equals("emailAddress")) {
068                            User user = UserLocalServiceUtil.fetchUserByEmailAddress(
069                                    companyId, name);
070    
071                            if (user != null) {
072                                    userId = user.getUserId();
073                            }
074                    }
075                    else if (jaasAuthType.equals("screenName")) {
076                            User user = UserLocalServiceUtil.fetchUserByScreenName(
077                                    companyId, name);
078    
079                            if (user != null) {
080                                    userId = user.getUserId();
081                            }
082                    }
083                    else {
084                            userId = GetterUtil.getLong(name);
085                    }
086    
087                    return userId;
088            }
089    
090            private static JAASHelper _instance = new JAASHelper();
091    
092    }