001    /**
002     * Copyright (c) 2000-2013 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.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    }