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