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