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.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 (StringUtil.equalsIgnoreCase(screenName, 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                    for (int i = 1;; i++) {
098                            String tempScreenName = screenName + StringPool.PERIOD + i;
099    
100                            if (UserLocalServiceUtil.fetchUserByScreenName(
101                                            companyId, tempScreenName) != null) {
102    
103                                    continue;
104                            }
105    
106                            if (GroupLocalServiceUtil.fetchFriendlyURLGroup(
107                                            companyId, StringPool.SLASH + tempScreenName) == null) {
108    
109                                    return tempScreenName;
110                            }
111                    }
112            }
113    
114            private static final String[] _ADMIN_RESERVED_SCREEN_NAMES =
115                    StringUtil.splitLines(
116                            PropsUtil.get(PropsKeys.ADMIN_RESERVED_SCREEN_NAMES));
117    
118            private static final String _NON_NUMERICAL_PREFIX = "user.";
119    
120            private static final boolean _USERS_SCREEN_NAME_ALLOW_NUMERIC =
121                    GetterUtil.getBoolean(
122                            PropsUtil.get(PropsKeys.USERS_SCREEN_NAME_ALLOW_NUMERIC));
123    
124    }