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.auth;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.PrefsPropsUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Alexander Chow
032     * @author Juan Fern??ndez
033     */
034    public class DefaultScreenNameGenerator implements ScreenNameGenerator {
035    
036            @Override
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, CharPool.AT).toLowerCase();
045    
046                            for (char c : screenName.toCharArray()) {
047                                    if (!Validator.isChar(c) && !Validator.isDigit(c) &&
048                                            (c != CharPool.DASH) && (c != CharPool.PERIOD)) {
049    
050                                            screenName = StringUtil.replace(
051                                                    screenName, c, CharPool.PERIOD);
052                                    }
053                            }
054    
055                            if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
056                                    screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
057    
058                                    screenName += StringPool.PERIOD + userId;
059                            }
060                    }
061                    else {
062                            screenName = String.valueOf(userId);
063                    }
064    
065                    if (!_USERS_SCREEN_NAME_ALLOW_NUMERIC &&
066                            Validator.isNumber(screenName)) {
067    
068                            screenName = _NON_NUMERICAL_PREFIX + screenName;
069                    }
070    
071                    String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
072                            companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
073                            StringPool.NEW_LINE, _ADMIN_RESERVED_SCREEN_NAMES);
074    
075                    for (String reservedScreenName : reservedScreenNames) {
076                            if (screenName.equalsIgnoreCase(reservedScreenName)) {
077                                    return getUnusedScreenName(companyId, screenName);
078                            }
079                    }
080    
081                    if (UserLocalServiceUtil.fetchUserByScreenName(
082                                    companyId, screenName) != null) {
083    
084                            return getUnusedScreenName(companyId, screenName);
085                    }
086    
087                    if (GroupLocalServiceUtil.fetchFriendlyURLGroup(
088                                    companyId, StringPool.SLASH + screenName) == null) {
089    
090                            return screenName;
091                    }
092    
093                    return getUnusedScreenName(companyId, screenName);
094            }
095    
096            protected String getUnusedScreenName(long companyId, String screenName)
097                    throws SystemException {
098    
099                    for (int i = 1;; i++) {
100                            String tempScreenName = screenName + StringPool.PERIOD + i;
101    
102                            if (UserLocalServiceUtil.fetchUserByScreenName(
103                                            companyId, tempScreenName) != null) {
104    
105                                    continue;
106                            }
107    
108                            if (GroupLocalServiceUtil.fetchFriendlyURLGroup(
109                                            companyId, StringPool.SLASH + tempScreenName) == null) {
110    
111                                    return tempScreenName;
112                            }
113                    }
114            }
115    
116            private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
117                    StringUtil.splitLines(
118                            PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES));
119    
120            private static final String _NON_NUMERICAL_PREFIX = "user.";
121    
122            private static final boolean _USERS_SCREEN_NAME_ALLOW_NUMERIC =
123                    GetterUtil.getBoolean(
124                            PropsUtil.get(PropsKeys.USERS_SCREEN_NAME_ALLOW_NUMERIC));
125    
126    }