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