001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.jaas;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.model.Company;
021    import com.liferay.portal.model.CompanyConstants;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.service.CompanyLocalServiceUtil;
024    import com.liferay.portal.service.UserLocalServiceUtil;
025    import com.liferay.portal.util.PropsValues;
026    
027    /**
028     * @author Raymond Augé
029     */
030    public class JAASHelper {
031    
032            public static JAASHelper getInstance() {
033                    return _instance;
034            }
035    
036            public static long getJaasUserId(long companyId, String name)
037                    throws PortalException, SystemException {
038    
039                    return _instance.doGetJaasUserId(companyId, name);
040            }
041    
042            public static void setInstance(JAASHelper instance) {
043                    _instance = instance;
044            }
045    
046            protected long doGetJaasUserId(long companyId, String name)
047                    throws PortalException, SystemException {
048    
049                    String jaasAuthType = PropsValues.PORTAL_JAAS_AUTH_TYPE;
050    
051                    if (jaasAuthType.equals("login")) {
052                            Company company = CompanyLocalServiceUtil.getCompany(companyId);
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    }