001    /**
002     * Copyright (c) 2000-2010 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.auth;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.NoSuchUserException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.util.PrefsPropsUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Alexander Chow
033     * @author Juan Fernández
034     */
035    public class DefaultScreenNameGenerator implements ScreenNameGenerator {
036    
037            public String generate(long companyId, long userId, String emailAddress)
038                    throws Exception {
039    
040                    String screenName = null;
041    
042                    if (Validator.isNotNull(emailAddress)) {
043                            screenName = StringUtil.extractFirst(
044                                    emailAddress, StringPool.AT).toLowerCase();
045    
046                            screenName = StringUtil.replace(
047                                    screenName,
048                                    new String[] {StringPool.SLASH, StringPool.UNDERLINE},
049                                    new String[] {StringPool.PERIOD, StringPool.PERIOD});
050    
051                            if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
052                                    screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
053    
054                                    screenName += StringPool.PERIOD + userId;
055                            }
056                    }
057                    else {
058                            screenName = String.valueOf(userId);
059                    }
060    
061                    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
062                            companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
063                            StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);
064    
065                    for (String reservedScreenName : reservedScreenNames) {
066                            if (screenName.equalsIgnoreCase(reservedScreenName)) {
067                                    return getUnusedScreenName(companyId, screenName);
068                            }
069                    }
070    
071                    try {
072                            UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
073                    }
074                    catch (NoSuchUserException nsue) {
075                            try {
076                                    GroupLocalServiceUtil.getFriendlyURLGroup(
077                                            companyId, StringPool.SLASH + screenName);
078                            }
079                            catch (NoSuchGroupException nsge) {
080                                    return screenName;
081                            }
082                    }
083    
084                    return getUnusedScreenName(companyId, screenName);
085            }
086    
087            protected String getUnusedScreenName(long companyId, String screenName)
088                    throws PortalException, SystemException {
089    
090                    for (int i = 1;; i++) {
091                            String tempScreenName = screenName + StringPool.PERIOD + i;
092    
093                            try {
094                                    UserLocalServiceUtil.getUserByScreenName(
095                                            companyId, tempScreenName);
096                            }
097                            catch (NoSuchUserException nsue) {
098                                    try {
099                                            GroupLocalServiceUtil.getFriendlyURLGroup(
100                                                    companyId, StringPool.SLASH + tempScreenName);
101                                    }
102                                    catch (NoSuchGroupException nsge) {
103                                            screenName = tempScreenName;
104    
105                                            break;
106                                    }
107                            }
108                    }
109    
110                    return screenName;
111            }
112    
113            private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
114                    StringUtil.split(
115                            PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES),
116                            StringPool.NEW_LINE);
117    
118    }